skiselkov/librain

please add a new example code

Opened this issue · 4 comments

Hi together,

please mr. kiselkov update the example from your librain plugin.
Its not working with the newest source code you provide on github!

if possible please make a new release of compiled .xpl version from your code - so everybody can use the new version including "vulkan".

PLEASE HELP HERE!

Greets
Robert

Okay ... i got now the librain 0.12 version to run ...

I will do a librain generator now - so everybody can use librain in his own aircraft :)

Greets
Robert

Okay - this should do the job :)

#include "librain.h"
#include "obj8.h"
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include "XPLMUtilities.h"
#include <string.h>
#include <math.h>

#if IBM
# include <windows.h>
#endif
#if LIN
# include <GL/gl.h>
#elif __GNUC__
# include <OpenGL/gl.h>
#else
# include <GL/gl.h>
#endif
#include <XPLMProcessing.h>

#ifndef XPLM300
# error This is made to be compiled against the XPLM300 SDK
#endif

static const char* windShieldObjPath = "path/to/windshield.obj";
static const vect3_t pos_offset = { 0, 0, 0 };

static const char* shaderDir = "path/to/shaders_dir";

static const vect2_t wiper1_pivot = { 0.4, 0.2 };
static const vect2_t wiper2_pivot = { 0.6, 0.2 };

static const vect2_t tp = { 0.5, -0.3 };
static const vect2_t gp = { 0.5, 1.3 };
static const vect2_t wp = { 1.5, 0.5 };

static librain_glass_t windShield = {
	.slant_factor = 1.0,
	.thrust_point = tp,
	.gravity_point = gp,
	.gravity_factor = 0.5,
	.wind_point = wp,
	.wind_factor = 1.0,
	.wind_normal = 1.0,
	.max_tas = 100.0,
	.therm_inertia = 20.0,
	.cabin_temp = 22.0,
	.wiper_pivot = { wiper1_pivot, wiper2_pivot },
	.wiper_radius_outer = { 0.4, 0.4 },
	.wiper_radius_inner = { 0.05, 0.05 },
};

static librain_glass_t glassElementsArray[1] = { windShield };

static double wiper_angle[2] = { 0, 0 };

static double wiper_ang_vel[2] = { -M_PI * 0.8, M_PI * 0.4 };

/*
 * Wiper edge positions. Wiper [0] goes _| and wiper [1] goes |_
 */
static const double wiper_edge[2][2] = {
	{	-M_PI / 2,	0		},
	{	0,		M_PI / 2	}
};


/* We'll be animating the wipers in this flight loop callback. */
static float wiper_floop(float delta_t, float time2, int counter, void* refcon);


static int draw_rain_effects(
	XPLMDrawingPhase inPhase,
	int              inIsBefore,
	void* inRefcon)
{
	librain_draw_prepare(FALSE);
	/* Load these OBJs using obj8_parse as necessary */
	//librain_draw_z_depth(compassObj, NULL);
	//librain_draw_z_depth(fuselageObj, NULL);
	librain_draw_exec();
	librain_draw_finish();
	return 1;
}

PLUGIN_API int XPluginStart(
	char* outName,
	char* outSig,
	char* outDesc)
{
	strcpy(outName, "HelloWorld3RainPlugin");
	strcpy(outSig, "librain.hello.world");
	strcpy(outDesc, "A Hello World plug-in for librain");

	/*
	 * Load our windshield object. You will also want to load
	 * any additional objects used for z-buffer filling.
	 */
	glassElementsArray[0].obj = obj8_parse(windShieldObjPath, pos_offset);
	if (glassElementsArray[0].obj == NULL) {
		XPLMDebugString("Oh noes, failed to load the windshield OBJ!");
		librain_fini();
		return (0);
	}

	/* Once we have loaded all OBJs, initialize the glass structures. */
	if (!librain_init(shaderDir, glassElementsArray, 1)) {
		XPLMDebugString("Oh noes, failed to initialize librain!");
		return (0);
	}
	/*
	 * Turn on debug drawing. This makes all z-buffer drawing
	 * visible to verify it's working right. When you are satisfied
	 * it is working as intended, remove this line.
	 */
	librain_set_debug_draw(TRUE);

	librain_set_wipers_visible(TRUE);
	/*
	 * Register the animation callback. Execute every flight loop.
	 */
	XPLMRegisterFlightLoopCallback(wiper_floop, -1, NULL);

	XPLMRegisterDrawCallback(draw_rain_effects, xplm_Phase_LastScene,
		0, NULL);

	return (1);
}

PLUGIN_API void XPluginStop(void)
{
	librain_fini();
	XPLMUnregisterDrawCallback(draw_rain_effects, xplm_Phase_LastScene,
		0, NULL);

	XPLMUnregisterFlightLoopCallback(wiper_floop, NULL);

}

static float wiper_floop(float delta_t, float time2, int counter, void* refcon)
{
	for (int i = 0; i < 2; i++) {
		wiper_angle[i] += wiper_ang_vel[i] * delta_t;

		/* Wiper hit edge of movement? Reverse direction. */
		if (wiper_angle[i] < wiper_edge[i][0]) {
			wiper_ang_vel[i] = -wiper_ang_vel[i];
			wiper_angle[i] = wiper_edge[i][0];
		}
		else if (wiper_angle[i] > wiper_edge[i][1]) {
			wiper_ang_vel[i] = -wiper_ang_vel[i];
				wiper_angle[i] = wiper_edge[i][1];
		}
		/*
		 * Even if the wiper is not moving, you MUST call this function
		 * with the angle unchanged and the last element set to FALSE.
		 * Otherwise the animation is not going to look right when the
		 * wipers are stopped.
		 */
		librain_set_wiper_angle(&glassElementsArray[0], i,
			wiper_angle[i], TRUE);
	}
	/* This needs to execute every flight loop. */
	return (-1.0);
}

PLUGIN_API void XPluginDisable(void)
{
}

PLUGIN_API int XPluginEnable(void)
{
	return 1;
}

PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg,
	void* inParam)
{
}

if you get compiled - just put this result and the rain.dll into the plugins folder of your aircraft in XPlane 11 :)

Greetings
Thank you very much! Please tell me where to insert this code?
Will it be interesting to work on the ixeg737 with the raindrop effect?