| Home | Lua | C-Talk | Z-Script | Notes |
Tutoial
Examples
Plug-in
References

Plug-in Interface

Objects of vertex, color, and texture coordinate have the function

    <dll append="true" params="p1, p2, ...">library_file_name, function_name</DLL>

to call dynamic-link library (DLL) functions for importing or generating data. A DLL function must confirm to the C protocol of

    int (*func) (char **argv, int argc, bool(*callback)(void *data, int n))

When the object function calls the DLL function, the string "p1, p2, ..." will be parsed as argv (a pointer to argc number of strings). A DLL function exchange data with the object through the callback function.

When the optional attribute append="true" (this is the default), the object only accept data, which are expected to be in a double pointer to 3 data for vertex object, 4 data for color object, and 2 data for texture coordinate object. The callback function return false when n is differenct from the expected number.

When append="false" is used, a zero or positive n indicates a request of the DLL function for data at index n in the object; and a negative n indicates return of data to the object for the index -n-1. The callback return false when the index is out of boundary.

The following C++ code demonstrate how to add DLL functions to your own library; and Example07 shows how to call these functions.

#include <vector>
#include <math.h>

#define DEG2RAD		0.017453292519943 

using namespace std;

extern "C"
{
__declspec(dllexport)
int make_sphere_vertex(char**, int, bool(*)(void*, int));

__declspec(dllexport)
int sphere_projection(char**, int, bool(*)(void*, int));
}

typedef struct {
	double x, y, z;
} XYZ;

/////////////////////////////////////////////////////////////////////
int
make_sphere_vertex(char** argv, int argc, bool(*callback)(void*, int))
{
	if (argc < 1) return -1;
	int nv = atoi(argv[0]);
	int nu = 2 * nv - 1;

	if (nu < 5) return -2;
	
	double  u, v, xyz[3],
			v0 = -1.570796326794900,
			d  = -v0 * 2 / double(nv - 1);
	
	int i, j;
	vector xyz1, xyz2;

	double cosv, sinv;

	for (j = 0; j < nu; j++) {
		XYZ xyz = {0, 0, -1};
		xyz1.push_back(xyz);
	}

	for (i = 1; i < nv; i++) {
		v = v0 + d * i;
		cosv = cos(v);
		sinv = sin(v);
		xyz2.clear();
		for (j = 0; j < nu; j++) {
			u = d * j;
			XYZ xyz = {cos(u)*cosv, sin(u)*cosv, sinv};
			xyz2.push_back(xyz);
		}
		for (j = 1; j < nu; j++) {
			xyz[0] = xyz1[j-1].x;
			xyz[1] = xyz1[j-1].y;
			xyz[2] = xyz1[j-1].z;
			callback(xyz, 3);
			xyz[0] = xyz1[j].x;
			xyz[1] = xyz1[j].y;
			xyz[2] = xyz1[j].z;
			callback(xyz, 3);
			xyz[0] = xyz2[j].x;
			xyz[1] = xyz2[j].y;
			xyz[2] = xyz2[j].z;
			callback(xyz, 3);
			xyz[0] = xyz2[j-1].x;
			xyz[1] = xyz2[j-1].y;
			xyz[2] = xyz2[j-1].z;
			callback(xyz, 3);
		}
		xyz1 = xyz2;
	}

	return 1;
}

/////////////////////////////////////////////////////////////////////
int
sphere_projection(char** argv, int argc, bool(*callback)(void*, int))
{
	int n = 0;
	double d[3];

	while (callback(d, n)) {
		d[0] *= DEG2RAD;
		d[1] *= DEG2RAD;
		d[2] = sin(d[1]);
		double r = cos(d[1]);
		d[1] = r * sin(d[0]);
		d[0] = r * cos(d[0]);
		n++;
		if (!callback(d, -n)) return -1;
	}

	return 1;
}