~flypig/functy/trunk

21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
1
///////////////////////////////////////////////////////////////////
2
// Functy
3
// 3D graph drawing utility
4
//
5
// David Llewellyn-Jones
6
// http://www.flypig.co.uk
7
//
8
// Spring 2009
9
///////////////////////////////////////////////////////////////////
10
11
12
///////////////////////////////////////////////////////////////////
13
// Includes
14
131 by David Llewellyn-Jones
Added binary STL export code.
15
#include <stdint.h>
16
21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
17
#include "vis.h"
18
#include "cartesian.h"
19
#include "function.h"
20
#include "function_private.h"
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
21
22
#include <symbolic.h>
21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
23
24
///////////////////////////////////////////////////////////////////
25
// Defines
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
26
27
#define FUNCTION0 "0"
28
21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
29
///////////////////////////////////////////////////////////////////
30
// Structures and enumerations
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
31
32
struct _CartesianPersist {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
33
	// The actual function
34
	Operation * psDiffWrtX;
35
	Operation * psDiffWrtY;
36
	Variable * psVariableX;
37
	Variable * psVariableY;
38
	Variable * psVariableZ;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
39
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
40
	// Storage for the OpenGL vertex and normal data
41
	int nXVertices;
42
	int nYVertices;
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
43
44
	// Shader related variables
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
45
	GString * szShaderDiffWrtX;
46
	GString * szShaderDiffWrtY;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
47
};
48
21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
49
///////////////////////////////////////////////////////////////////
50
// Global variables
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
51
21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
52
///////////////////////////////////////////////////////////////////
53
// Function prototypes
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
54
55
void CartesianPopulateVerticesColour (FuncPersist * psFuncData);
56
void CartesianPopulateVerticesNoColour (FuncPersist * psFuncData);
55 by David Llewellyn-Jones
Fixed Functy to allow seamless transition between shader-based and non-shader-based rendering. Extended to allow different fragment (as well as vertex) shader code for different function types. Colour function moved from vertex shader to fragment shader to generate more accurate results independent of the function accuracy setting (since this only affects the number of vertices used).
57
void CartesianResetVertices (FuncPersist * psFuncData);
129 by David Llewellyn-Jones
Added ASCII STL export code.
58
void CartesianVertex (Vector3 * pvVertex, Vector3 * pvNormal, double fXFunc, double fYFunc, bool boScreenCoords, double fMultiplier, double fScale, FuncPersist const * psFuncData);
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
59
21 by David Llewellyn-Jones
Added spherical co-ordinate function rendering.
60
///////////////////////////////////////////////////////////////////
61
// Function definitions
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
62
63
CartesianPersist * NewCartesianPersist () {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
64
	CartesianPersist * psCartesianData;
65
66
	psCartesianData = g_new0 (CartesianPersist, 1);
67
68
	psCartesianData->nXVertices = 0;
69
	psCartesianData->nYVertices = 0;
70
71
	psCartesianData->psDiffWrtX = NULL;
72
	psCartesianData->psDiffWrtY = NULL;
73
	psCartesianData->psVariableX = NULL;
74
	psCartesianData->psVariableY = NULL;
75
	psCartesianData->psVariableZ = NULL;
76
77
	psCartesianData->szShaderDiffWrtX = g_string_new ("0");
78
	psCartesianData->szShaderDiffWrtY = g_string_new ("0");
79
80
	return psCartesianData;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
81
}
82
83
void DeleteCartesianPersist (CartesianPersist * psCartesianData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
84
	// Free up the function data
85
	FreeRecursive (psCartesianData->psDiffWrtX);
86
	FreeRecursive (psCartesianData->psDiffWrtY);
87
88
	if (psCartesianData->szShaderDiffWrtX) {
89
		g_string_free (psCartesianData->szShaderDiffWrtX, TRUE);
90
	}
91
	if (psCartesianData->szShaderDiffWrtY) {
92
		g_string_free (psCartesianData->szShaderDiffWrtY, TRUE);
93
	}
94
95
	g_free (psCartesianData);
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
96
}
97
98
void CartesianSetFunction (char const * const szFunction, FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
99
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
100
	Operation * psWRT;
101
	char * szShaderFunction;
102
	int nFunctionLen;
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
103
	int nUserAssigned = 0;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
104
105
	// Free up any previous function
106
	FreeRecursive (psFuncData->psFunction);
107
	FreeRecursive (psCartesianData->psDiffWrtX);
108
	FreeRecursive (psCartesianData->psDiffWrtY);
109
	psFuncData->psVariables = FreeVariables (psFuncData->psVariables);
110
111
	psFuncData->psFunction = StringToOperation (szFunction);
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
112
	nUserAssigned += AssignAllUserFuncs (psFuncData->psFunction, psFuncData->psUserFuncs);
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
113
	psFuncData->psFunction = UberSimplifyOperation (psFuncData->psFunction);
114
115
	// Differentiate with respect to x
116
	psWRT = CreateVariable ("x");
117
	psCartesianData->psDiffWrtX = DifferentiateOperation (psFuncData->psFunction, psWRT);
118
	FreeRecursive (psWRT);
119
	psCartesianData->psDiffWrtX = UberSimplifyOperation (psCartesianData->psDiffWrtX);
120
121
	// Differentiate with respect to y
122
	psWRT = CreateVariable ("y");
123
	psCartesianData->psDiffWrtY = DifferentiateOperation (psFuncData->psFunction, psWRT);
124
	FreeRecursive (psWRT);
125
	psCartesianData->psDiffWrtY = UberSimplifyOperation (psCartesianData->psDiffWrtY);
126
127
	// Set up the variables
128
	psFuncData->psVariables = CreateVariables (psFuncData->psFunction, psFuncData->psVariables);
129
	psFuncData->psVariables = CreateVariables (psCartesianData->psDiffWrtX, psFuncData->psVariables);
130
	psFuncData->psVariables = CreateVariables (psCartesianData->psDiffWrtY, psFuncData->psVariables);
131
132
	// Free up any unused variables
133
	psFuncData->psVariables = FreeVariables (psFuncData->psVariables);
134
135
	// Find the x, y, z and t variables if they exist
136
	psCartesianData->psVariableX = FindVariable (psFuncData->psVariables, "x");
137
	psCartesianData->psVariableY = FindVariable (psFuncData->psVariables, "y");
138
	psCartesianData->psVariableZ = FindVariable (psFuncData->psVariables, "z");
139
	psFuncData->psVariableT = FindVariable (psFuncData->psVariables, "t");
140
141
	// Set up the handy UI-related data
142
	g_string_assign (psFuncData->szFunction, szFunction);
143
144
	// Figure out if the functions are time dependent
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
145
	//psFuncData->boTimeDependent = (psFuncData->psVariableT != NULL) || (nUserAssigned > 0);
146
	SETBIT (psFuncData->uTimeDependent, (psFuncData->psVariableT != NULL), 0);
147
	SETBIT (psFuncData->uTimeDependent, (nUserAssigned > 0), 1);
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
148
149
	// Transfer the function details to the shader
150
	nFunctionLen = OperationToStringCLength (psFuncData->psFunction);
151
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
152
	OperationToStringC (psFuncData->psFunction, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
153
	g_string_assign (psFuncData->szShaderFunction, szShaderFunction);
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
154
	free (szShaderFunction);
155
156
	nFunctionLen = OperationToStringCLength (psCartesianData->psDiffWrtX);
157
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
158
	OperationToStringC (psCartesianData->psDiffWrtX, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
159
	g_string_assign (psCartesianData->szShaderDiffWrtX, szShaderFunction);
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
160
	free (szShaderFunction);
161
162
	nFunctionLen = OperationToStringCLength (psCartesianData->psDiffWrtY);
163
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
164
	OperationToStringC (psCartesianData->psDiffWrtY, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
165
	g_string_assign (psCartesianData->szShaderDiffWrtY, szShaderFunction);
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
166
	free (szShaderFunction);
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
167
}
168
169
void CartesianSetFunctionColours (char const * const szRed, char const * const szGreen, char const * const szBlue, char const * const szAlpha, FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
170
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
171
	double fApproximate;
172
	char * szShaderFunction;
173
	int nFunctionLen;
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
174
	int nUserAssigned = 0;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
175
176
	// Free up any previous function
177
	FreeRecursive (psFuncData->psRed);
178
	FreeRecursive (psFuncData->psGreen);
179
	FreeRecursive (psFuncData->psBlue);
180
	FreeRecursive (psFuncData->psAlpha);
181
	psFuncData->psVariables = FreeVariables (psFuncData->psVariables);
182
183
	psFuncData->psRed = StringToOperation (szRed);
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
184
	nUserAssigned += AssignAllUserFuncs (psFuncData->psRed, psFuncData->psUserFuncs);
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
185
	psFuncData->psRed = UberSimplifyOperation (psFuncData->psRed);
186
	psFuncData->psGreen = StringToOperation (szGreen);
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
187
	nUserAssigned += AssignAllUserFuncs (psFuncData->psGreen, psFuncData->psUserFuncs);
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
188
	psFuncData->psGreen = UberSimplifyOperation (psFuncData->psGreen);
189
	psFuncData->psBlue = StringToOperation (szBlue);
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
190
	nUserAssigned += AssignAllUserFuncs (psFuncData->psBlue, psFuncData->psUserFuncs);
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
191
	psFuncData->psBlue = UberSimplifyOperation (psFuncData->psBlue);
192
	psFuncData->psAlpha = StringToOperation (szAlpha);
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
193
	nUserAssigned += AssignAllUserFuncs (psFuncData->psAlpha, psFuncData->psUserFuncs);
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
194
	psFuncData->psAlpha = UberSimplifyOperation (psFuncData->psAlpha);
195
196
	// Check if we can avoid recalculating the colour for every vertex
197
	psFuncData->boColourFunction = FALSE;
198
	fApproximate = ApproximateOperation (psFuncData->psRed);
199
	psFuncData->fRed = fApproximate;
200
	if (isnan (fApproximate)) {
201
		psFuncData->boColourFunction = TRUE;
202
	}
203
	fApproximate = ApproximateOperation (psFuncData->psGreen);
204
	psFuncData->fGreen = fApproximate;
205
	if (isnan (fApproximate)) {
206
		psFuncData->boColourFunction = TRUE;
207
	}
208
	fApproximate = ApproximateOperation (psFuncData->psBlue);
209
	psFuncData->fBlue = fApproximate;
210
	if (isnan (fApproximate)) {
211
		psFuncData->boColourFunction = TRUE;
212
	}
213
	fApproximate = ApproximateOperation (psFuncData->psAlpha);
214
	psFuncData->fAlpha = fApproximate;
215
	if (isnan (fApproximate)) {
216
		psFuncData->boColourFunction = TRUE;
217
	}
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
218
	if (nUserAssigned > 0) {
219
		psFuncData->boColourFunction = TRUE;
220
	}
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
221
222
	// Set up the variables
223
	psFuncData->psVariables = CreateVariables (psFuncData->psRed, psFuncData->psVariables);
224
	psFuncData->psVariables = CreateVariables (psFuncData->psGreen, psFuncData->psVariables);
225
	psFuncData->psVariables = CreateVariables (psFuncData->psBlue, psFuncData->psVariables);
226
	psFuncData->psVariables = CreateVariables (psFuncData->psAlpha, psFuncData->psVariables);
227
228
	// Find the x, y, z and t variables if they exist
229
	psCartesianData->psVariableX = FindVariable (psFuncData->psVariables, "x");
230
	psCartesianData->psVariableY = FindVariable (psFuncData->psVariables, "y");
231
	psCartesianData->psVariableZ = FindVariable (psFuncData->psVariables, "z");
232
	psFuncData->psVariableT = FindVariable (psFuncData->psVariables, "t");
233
234
	// Free up any unused variables
235
	psFuncData->psVariables = FreeVariables (psFuncData->psVariables);
236
237
	// Set up the handy UI-related data
238
	g_string_assign (psFuncData->szRed, szRed);
239
	g_string_assign (psFuncData->szGreen, szGreen);
240
	g_string_assign (psFuncData->szBlue, szBlue);
241
	g_string_assign (psFuncData->szAlpha, szAlpha);
242
243
	// Figure out if the functions are time dependent
173 by David Llewellyn-Jones
Moved to version 0.37. Audio frequency animation extended to software as well as GPU-rendered functions.
244
	//psFuncData->boTimeDependent = (psFuncData->psVariableT != NULL) || (nUserAssigned > 0);
245
	SETBIT (psFuncData->uTimeDependent, (psFuncData->psVariableT != NULL), 0);
246
	SETBIT (psFuncData->uTimeDependent, (nUserAssigned > 0), 2);
52 by David Llewellyn-Jones
Updated Functy shader generation to include colours for Cartesian functions.
247
248
	// Transfer the colour details to the shader
249
	nFunctionLen = OperationToStringCLength (psFuncData->psRed);
250
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
251
	OperationToStringC (psFuncData->psRed, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
252
	g_string_assign (psFuncData->szShaderRed, szShaderFunction);
52 by David Llewellyn-Jones
Updated Functy shader generation to include colours for Cartesian functions.
253
	free (szShaderFunction);
254
255
	nFunctionLen = OperationToStringCLength (psFuncData->psGreen);
256
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
257
	OperationToStringC (psFuncData->psGreen, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
258
	g_string_assign (psFuncData->szShaderGreen, szShaderFunction);
52 by David Llewellyn-Jones
Updated Functy shader generation to include colours for Cartesian functions.
259
	free (szShaderFunction);
260
261
	nFunctionLen = OperationToStringCLength (psFuncData->psBlue);
262
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
263
	OperationToStringC (psFuncData->psBlue, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
264
	g_string_assign (psFuncData->szShaderBlue, szShaderFunction);
52 by David Llewellyn-Jones
Updated Functy shader generation to include colours for Cartesian functions.
265
	free (szShaderFunction);
266
267
	nFunctionLen = OperationToStringCLength (psFuncData->psAlpha);
268
	szShaderFunction = (char *)malloc (nFunctionLen + 1);
269
	OperationToStringC (psFuncData->psAlpha, szShaderFunction, nFunctionLen + 1);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
270
	g_string_assign (psFuncData->szShaderAlpha, szShaderFunction);
52 by David Llewellyn-Jones
Updated Functy shader generation to include colours for Cartesian functions.
271
	free (szShaderFunction);
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
272
}
273
274
void CartesianGetVertexDimensions (int * pnXVertices, int * pnYVertices, FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
275
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
276
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
277
	if (pnXVertices) {
278
		* pnXVertices = psCartesianData->nXVertices;
279
	}
280
	if (pnYVertices) {
281
		* pnYVertices = psCartesianData->nXVertices;
282
	}
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
283
}
284
285
void CartesianPopulateVertices (FuncPersist * psFuncData) {
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
286
	bool boUseShader;
287
288
	boUseShader = GetShaderActive (psFuncData->psShaderData);
289
55 by David Llewellyn-Jones
Fixed Functy to allow seamless transition between shader-based and non-shader-based rendering. Extended to allow different fragment (as well as vertex) shader code for different function types. Colour function moved from vertex shader to fragment shader to generate more accurate results independent of the function accuracy setting (since this only affects the number of vertices used).
290
	if (boUseShader) {
291
		// Reset the vertices for the shader
292
		// Strictly speaking this isn't necessary for the cartesian case
293
		// but it may prevent peculiar problems happening later 
294
		CartesianResetVertices (psFuncData);
295
	}
296
	else{
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
297
		if (psFuncData->boColourFunction) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
298
			CartesianPopulateVerticesColour (psFuncData);
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
299
		}
300
		else {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
301
			CartesianPopulateVerticesNoColour (psFuncData);
51 by David Llewellyn-Jones
Additional changes to Functy to allow cartesian functions to be manipulated entirely using shaders.
302
		}
303
	}
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
304
}
305
306
void CartesianPopulateVerticesNoColour (FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
307
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
308
	int nVertex;
309
	double fX;
310
	double fY;
311
	double fXFunc;
312
	double fYFunc;
313
	double fZFunc;
314
	double fZScreen;
315
	//double x;
316
	//double y;
317
	Vector3 vX;
318
	Vector3 vY;
319
	Vector3 vN;
320
	double fEnd;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
321
	double fTexXOffset = 0.0;
322
	double fTexYOffset = 0.0;
323
	double fTexXScale = 1.0;
324
	double fTexYScale = 1.0;
325
326
	sscanf (psFuncData->szTexXOffset->str, "%lf", & fTexXOffset);
327
	sscanf (psFuncData->szTexYOffset->str, "%lf", & fTexYOffset);
328
	sscanf (psFuncData->szTexXScale->str, "%lf", & fTexXScale);
329
	sscanf (psFuncData->szTexYScale->str, "%lf", & fTexYScale);
330
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
331
	fEnd = 1.0 + (psFuncData->fAccuracy / 2.0);
332
	nVertex = 0;
333
	for (fX = 0.0; fX < fEnd; fX += psFuncData->fAccuracy) {
334
335
		fXFunc = (fX * psFuncData->fXWidth) + psFuncData->fXMin;
336
337
		for (fY = 0.0; fY < fEnd; fY += psFuncData->fAccuracy) {
338
			fYFunc = (fY * psFuncData->fYWidth) + psFuncData->fYMin;
339
340
			//x = fXFunc;
341
			//y = fYFunc;
342
343
			if (psCartesianData->psVariableX) {
344
				SetVariable (psCartesianData->psVariableX, fXFunc);
345
			}
346
			if (psCartesianData->psVariableY) {
347
				SetVariable (psCartesianData->psVariableY, fYFunc);
348
			}
349
350
			fZFunc = ApproximateOperation (psFuncData->psFunction);
351
352
			vX.fX = 1.0f;
353
			vX.fY = 0.0f;
354
			vX.fZ = ApproximateOperation (psCartesianData->psDiffWrtX);
355
356
			vY.fX = 0.0f;
357
			vY.fY = 1.0f;
358
			vY.fZ = ApproximateOperation (psCartesianData->psDiffWrtY);
359
360
			vN = Normal (& vX, & vY);
361
362
			psFuncData->afNormals[(nVertex * 3)] = vN.fX;
363
			psFuncData->afNormals[(nVertex * 3) + 1] = vN.fY;
364
			psFuncData->afNormals[(nVertex * 3) + 2] = vN.fZ;
365
366
			fZScreen = (((fZFunc - psFuncData->fZMin) / psFuncData->fZWidth) * AXIS_ZSIZE) - AXIS_ZHSIZE;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
367
368
			// Calculate vertex texture coordinate
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
369
			psFuncData->afTextureCoords[(nVertex * 2)] = (fXFunc * fTexXScale) + fTexXOffset;
370
			psFuncData->afTextureCoords[(nVertex * 2) + 1] = (fYFunc * fTexYScale) + fTexYOffset;
371
372
			psFuncData->afVertices[(nVertex * 3) + 2] = fZScreen;
373
374
			nVertex++;
375
		}
376
	}
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
377
}
378
379
void CartesianPopulateVerticesColour (FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
380
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
381
	int nVertex;
382
	double fX;
383
	double fY;
384
	double fXFunc;
385
	double fYFunc;
386
	double fZFunc;
387
	double fZScreen;
388
	//double x;
389
	//double y;
390
	Vector3 vX;
391
	Vector3 vY;
392
	Vector3 vN;
393
	double fEnd;
394
	float fColour;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
395
	double fTexXOffset = 0.0;
396
	double fTexYOffset = 0.0;
397
	double fTexXScale = 1.0;
398
	double fTexYScale = 1.0;
399
400
	sscanf (psFuncData->szTexXOffset->str, "%lf", & fTexXOffset);
401
	sscanf (psFuncData->szTexYOffset->str, "%lf", & fTexYOffset);
402
	sscanf (psFuncData->szTexXScale->str, "%lf", & fTexXScale);
403
	sscanf (psFuncData->szTexYScale->str, "%lf", & fTexYScale);
404
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
405
	fEnd = 1.0 + (psFuncData->fAccuracy / 2.0);
406
	nVertex = 0;
407
	for (fX = 0.0; fX < fEnd; fX += psFuncData->fAccuracy) {
408
409
		fXFunc = (fX * psFuncData->fXWidth) + psFuncData->fXMin;
410
411
		for (fY = 0.0; fY < fEnd; fY += psFuncData->fAccuracy) {
412
			fYFunc = (fY * psFuncData->fYWidth) + psFuncData->fYMin;
413
414
			//x = fXFunc;
415
			//y = fYFunc;
416
417
			if (psCartesianData->psVariableX) {
418
				SetVariable (psCartesianData->psVariableX, fXFunc);
419
			}
420
			if (psCartesianData->psVariableY) {
421
				SetVariable (psCartesianData->psVariableY, fYFunc);
422
			}
423
424
			fZFunc = ApproximateOperation (psFuncData->psFunction);
425
426
			vX.fX = 1.0f;
427
			vX.fY = 0.0f;
428
			vX.fZ = ApproximateOperation (psCartesianData->psDiffWrtX);
429
430
			vY.fX = 0.0f;
431
			vY.fY = 1.0f;
432
			vY.fZ = ApproximateOperation (psCartesianData->psDiffWrtY);
433
434
			vN = Normal (& vX, & vY);
435
436
			psFuncData->afNormals[(nVertex * 3)] = vN.fX;
437
			psFuncData->afNormals[(nVertex * 3) + 1] = vN.fY;
438
			psFuncData->afNormals[(nVertex * 3) + 2] = vN.fZ;
439
440
			fZScreen = (((fZFunc - psFuncData->fZMin) / psFuncData->fZWidth) * AXIS_ZSIZE) - AXIS_ZHSIZE;
441
442
			if (psCartesianData->psVariableZ) {
443
				SetVariable (psCartesianData->psVariableZ, fZFunc);
444
			}
445
446
			// Calculate vertex colour
447
			fColour = ApproximateOperation (psFuncData->psRed);
448
			psFuncData->afColours[(nVertex * 4)] = fColour;
449
			fColour = ApproximateOperation (psFuncData->psGreen);
450
			psFuncData->afColours[(nVertex * 4) + 1] = fColour;
451
			fColour = ApproximateOperation (psFuncData->psBlue);
452
			psFuncData->afColours[(nVertex * 4) + 2] = fColour;
453
			fColour = ApproximateOperation (psFuncData->psAlpha);
454
			psFuncData->afColours[(nVertex * 4) + 3] = fColour;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
455
456
			// Calculate vertex texture coordinate
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
457
			psFuncData->afTextureCoords[(nVertex * 2)] = (fXFunc * fTexXScale) + fTexXOffset;
458
			psFuncData->afTextureCoords[(nVertex * 2) + 1] = (fYFunc * fTexYScale) + fTexYOffset;
459
460
			psFuncData->afVertices[(nVertex * 3) + 2] = fZScreen;
461
462
			nVertex++;
463
		}
464
	}
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
465
}
466
467
void CartesianGenerateVertices (FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
468
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
469
	int nXVertices;
470
	int nYVertices;
471
	int nIndex;
472
473
	nXVertices = floor (1.0 / psFuncData->fAccuracy) + 1;
474
	nYVertices = floor (1.0 / psFuncData->fAccuracy) + 1;
475
476
	psCartesianData->nXVertices = nXVertices;
477
	psCartesianData->nYVertices = nYVertices;
478
479
	psFuncData->afVertices = g_new0 (GLfloat, nXVertices * nYVertices * 3);
480
	psFuncData->afNormals = g_new0 (GLfloat, nXVertices * nYVertices * 3);
481
	psFuncData->auIndices = g_new (GLushort, nYVertices * 2);
482
	psFuncData->afColours = g_new0 (GLfloat, nXVertices * nYVertices * 4);
483
	psFuncData->afTextureCoords = g_new0 (GLfloat, nXVertices * nYVertices * 2);
484
485
	// Generate the index data
486
	for (nIndex = 0; nIndex < nYVertices; nIndex++) {
487
		psFuncData->auIndices[(nIndex * 2)] = nIndex;
488
		psFuncData->auIndices[(nIndex * 2) + 1] = nIndex + nYVertices;
489
	}
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
490
55 by David Llewellyn-Jones
Fixed Functy to allow seamless transition between shader-based and non-shader-based rendering. Extended to allow different fragment (as well as vertex) shader code for different function types. Colour function moved from vertex shader to fragment shader to generate more accurate results independent of the function accuracy setting (since this only affects the number of vertices used).
491
	CartesianResetVertices (psFuncData);
492
}
493
494
void CartesianResetVertices (FuncPersist * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
495
	int nVertex;
496
	double fX;
497
	double fY;
498
	double fXScreen;
499
	double fYScreen;
500
	double fEnd;
501
502
	// Generate the x,y vertex data (z values are populated dynamically)
503
	fEnd = 1.0 + (psFuncData->fAccuracy / 2.0);
504
	nVertex = 0;
505
	for (fX = 0.0; fX < fEnd; fX += psFuncData->fAccuracy) {
506
		fXScreen = (fX * AXIS_XSIZE) - AXIS_XHSIZE;
507
508
		for (fY = 0.0; fY < fEnd; fY += psFuncData->fAccuracy) {
509
			fYScreen = (fY * AXIS_YSIZE) - AXIS_YHSIZE;
510
511
			psFuncData->afNormals[(nVertex * 3)] = 0.0;
512
			psFuncData->afNormals[(nVertex * 3) + 1] = 0.0;
513
			psFuncData->afNormals[(nVertex * 3) + 2] = 1.0;
514
515
			psFuncData->afVertices[(nVertex * 3)] = fXScreen;
516
			psFuncData->afVertices[(nVertex * 3) + 1] = fYScreen;
517
			psFuncData->afVertices[(nVertex * 3) + 2] = 0.0;
518
519
			nVertex++;
520
		}
521
	}
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
522
}
523
524
void CartesianSetFunctionPosition (double fXMin, double fYMin, double fZMin, FuncPersist * psFuncData) {
54 by David Llewellyn-Jones
Added spherical shader code to Functy.
525
	Vector3 vPosition;
526
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
527
	psFuncData->fXMin = fXMin;
528
	psFuncData->fYMin = fYMin;
529
	psFuncData->fZMin = fZMin;
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
530
54 by David Llewellyn-Jones
Added spherical shader code to Functy.
531
	vPosition.fX = fXMin + ((AXIS_XHSIZE * psFuncData->fXWidth) / AXIS_XSIZE);
532
	vPosition.fY = fYMin + ((AXIS_YHSIZE * psFuncData->fYWidth) / AXIS_YSIZE);
533
	vPosition.fZ = fZMin + ((AXIS_ZHSIZE * psFuncData->fZWidth) / AXIS_ZSIZE);
534
	SetShaderPosition (& vPosition, psFuncData->psShaderData);
113 by David Llewellyn-Jones
Shadow texture calculations and transformations applied correctly for spherical functions.
535
	SetShaderPosition (& vPosition, psFuncData->psShaderShadowData);
54 by David Llewellyn-Jones
Added spherical shader code to Functy.
536
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
537
	PopulateVertices (psFuncData);
43 by David Llewellyn-Jones
Added initial code for rendering of functions using textures to Functy.
538
}
539
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
540
char * CartesianGenerateVertexShader (FuncPersist * psFuncData) {
541
	char * szShader;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
542
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
543
544
	if (psFuncData->szShaderVertexSource) {
545
		szShader = ReplaceTextCopy (psFuncData->szShaderVertexSource, "function", psFuncData->szShaderFunction->str);
546
		szShader = ReplaceTextMove (szShader, "diffX", psCartesianData->szShaderDiffWrtX->str);
547
		szShader = ReplaceTextMove (szShader, "diffY", psCartesianData->szShaderDiffWrtY->str);
548
		szShader = ReplaceTextMove (szShader, "red", psFuncData->szShaderRed->str);
549
		szShader = ReplaceTextMove (szShader, "green", psFuncData->szShaderGreen->str);
550
		szShader = ReplaceTextMove (szShader, "blue", psFuncData->szShaderBlue->str);
551
		szShader = ReplaceTextMove (szShader, "alpha", psFuncData->szShaderAlpha->str);
552
	}
553
	else {
554
		szShader = NULL;
555
	}
556
557
	return szShader;
558
}
559
55 by David Llewellyn-Jones
Fixed Functy to allow seamless transition between shader-based and non-shader-based rendering. Extended to allow different fragment (as well as vertex) shader code for different function types. Colour function moved from vertex shader to fragment shader to generate more accurate results independent of the function accuracy setting (since this only affects the number of vertices used).
560
char * CartesianGenerateFragmentShader (FuncPersist * psFuncData) {
561
	char * szShader;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
562
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
55 by David Llewellyn-Jones
Fixed Functy to allow seamless transition between shader-based and non-shader-based rendering. Extended to allow different fragment (as well as vertex) shader code for different function types. Colour function moved from vertex shader to fragment shader to generate more accurate results independent of the function accuracy setting (since this only affects the number of vertices used).
563
564
	if (psFuncData->szShaderFragmentSource) {
565
		szShader = ReplaceTextCopy (psFuncData->szShaderFragmentSource, "function", psFuncData->szShaderFunction->str);
566
		szShader = ReplaceTextMove (szShader, "diffX", psCartesianData->szShaderDiffWrtX->str);
567
		szShader = ReplaceTextMove (szShader, "diffY", psCartesianData->szShaderDiffWrtY->str);
568
		szShader = ReplaceTextMove (szShader, "red", psFuncData->szShaderRed->str);
569
		szShader = ReplaceTextMove (szShader, "green", psFuncData->szShaderGreen->str);
570
		szShader = ReplaceTextMove (szShader, "blue", psFuncData->szShaderBlue->str);
571
		szShader = ReplaceTextMove (szShader, "alpha", psFuncData->szShaderAlpha->str);
572
	}
573
	else {
574
		szShader = NULL;
575
	}
576
577
	return szShader;
578
}
579
111 by David Llewellyn-Jones
Function shaders, variables and transforms introduced to support shadow rendering.
580
char * CartesianGenerateVertexShaderShadow (FuncPersist * psFuncData) {
581
	char * szShader;
582
	//CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
583
584
	if (psFuncData->szShaderShadowVertexSource) {
585
		szShader = ReplaceTextCopy (psFuncData->szShaderShadowVertexSource, "function", psFuncData->szShaderFunction->str);
586
	}
587
	else {
588
		szShader = NULL;
589
	}
590
591
	return szShader;
592
}
593
594
char * CartesianGenerateFragmentShaderShadow (FuncPersist * psFuncData) {
595
	char * szShader;
596
	//CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
597
598
	if (psFuncData->szShaderShadowFragmentSource) {
175 by David Llewellyn-Jones
Shader discards transparent pixels
599
		//szShader = CopyText (psFuncData->szShaderShadowFragmentSource);
600
		szShader = ReplaceTextCopy (psFuncData->szShaderShadowFragmentSource, "alpha", psFuncData->szShaderAlpha->str);
111 by David Llewellyn-Jones
Function shaders, variables and transforms introduced to support shadow rendering.
601
	}
602
	else {
603
		szShader = NULL;
604
	}
605
606
	return szShader;
607
}
608
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
609
void CartesianInitShader (FuncPersist * psFuncData) {
178 by David Llewellyn-Jones
Add datadir command line option
610
	GString * szPath;
611
612
	szPath = g_string_new ("/shaders/cartesian.vs");
613
	GenerateDataPath (szPath, psFuncData->psGlobalData);
614
	LoadVertexShader (szPath->str, psFuncData);
615
616
	szPath = g_string_assign (szPath, "/shaders/cartesian.fs");
617
	GenerateDataPath (szPath, psFuncData->psGlobalData);
618
	LoadFragmentShader (szPath->str, psFuncData);
619
620
	szPath = g_string_assign (szPath, "/shaders/cartesian-shadow.vs");
621
	GenerateDataPath (szPath, psFuncData->psGlobalData);
622
	LoadVertexShaderShadow (szPath->str, psFuncData);
623
624
	szPath = g_string_assign (szPath, "/shaders/cartesian-shadow.fs");
625
	GenerateDataPath (szPath, psFuncData->psGlobalData);
626
	LoadFragmentShaderShadow (szPath->str, psFuncData);
627
628
	g_string_free (szPath, TRUE);
55 by David Llewellyn-Jones
Fixed Functy to allow seamless transition between shader-based and non-shader-based rendering. Extended to allow different fragment (as well as vertex) shader code for different function types. Colour function moved from vertex shader to fragment shader to generate more accurate results independent of the function accuracy setting (since this only affects the number of vertices used).
629
	FunctionShadersRegenerate (psFuncData);
630
}
631
632
void CartesianSetShaderActive (bool boActive, FuncPersist * psFuncData) {
633
	SetShaderActive (boActive, psFuncData->psShaderData);
634
635
	CartesianPopulateVertices (psFuncData);
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
636
}
637
111 by David Llewellyn-Jones
Function shaders, variables and transforms introduced to support shadow rendering.
638
void CartesianSetShaderShadowActive (bool boActive, FuncPersist * psFuncData) {
639
	SetShaderActive (boActive, psFuncData->psShaderShadowData);
640
641
	// TODO: Check whether we actually need to do this
642
	CartesianPopulateVertices (psFuncData);
643
}
644
63 by David Llewellyn-Jones
Tied positioning and scaling of curves into the Functy interface.
645
void CartesianUpdateCentre (FuncPersist * psFuncData) {
646
	// Do nothing
647
}
648
649
void CartesianSetFunctionCentre (char const * const szXCentre, char const * const szYCentre, char const * const szZCentre, FuncPersist * psFuncData) {
650
	// Do nothing
651
}
652
75 by David Llewellyn-Jones
Added colour and normal data to Stanford Triangle Format files exported from Functy.
653
void CartesianGetCentre (double * afCentre, FuncPersist const * psFuncData) {
63 by David Llewellyn-Jones
Tied positioning and scaling of curves into the Functy interface.
654
	afCentre[0] = 0.0f;
655
	afCentre[1] = 0.0f;
656
	afCentre[2] = 0.0f;
657
}
658
659
char const * CartesianGetXCentreString (FuncPersist * psFuncData) {
660
	return "0";
661
}
662
663
char const * CartesianGetYCentreString (FuncPersist * psFuncData) {
664
	return "0";
665
}
666
667
char const * CartesianGetZCentreString (FuncPersist * psFuncData) {
668
	return "0";
669
}
670
99 by David Llewellyn-Jones
Added option to scale model when being exported from Functy.
671
int CartesianOutputStoredVertices (Recall * hFile, bool boBinary, bool boExportAlpha, bool boScreenCoords, double fMultiplier, double fScale, FuncPersist const * psFuncData) {
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
672
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
673
	int nVertex;
674
	double fX;
675
	double fY;
676
	double fXFunc;
677
	double fYFunc;
678
	double fZFunc;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
679
	double fXScreen;
680
	double fYScreen;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
681
	double fZScreen;
682
	//double x;
683
	//double y;
684
	Vector3 vX;
685
	Vector3 vY;
686
	Vector3 vN;
687
	double fEnd;
688
	float fColour;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
689
	double fTexXOffset = 0.0;
690
	double fTexYOffset = 0.0;
691
	double fTexXScale = 1.0;
692
	double fTexYScale = 1.0;
693
	Vector3 vVertex;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
694
	unsigned char ucColour[4];
695
	double fAccuracy;
696
	
76 by David Llewellyn-Jones
Added accuracy multiplier to PLY model exporting in Functy. Added UI elements needed to export models. Added option to pause animation.
697
	if (fMultiplier > 0.0) {
698
		fAccuracy = psFuncData->fAccuracy / fMultiplier;
699
	}
700
	else {
701
		fAccuracy = 1.0;
702
	}
703
	if (fAccuracy < ACCURACY_MIN) {
704
		fAccuracy = ACCURACY_MIN;
705
	}
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
706
707
	sscanf (psFuncData->szTexXOffset->str, "%lf", & fTexXOffset);
708
	sscanf (psFuncData->szTexYOffset->str, "%lf", & fTexYOffset);
709
	sscanf (psFuncData->szTexXScale->str, "%lf", & fTexXScale);
710
	sscanf (psFuncData->szTexYScale->str, "%lf", & fTexYScale);
711
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
712
	fEnd = 1.0 + (fAccuracy / 2.0);
713
	nVertex = 0;
714
	for (fX = 0.0; fX < fEnd; fX += fAccuracy) {
715
716
		fXFunc = (fX * psFuncData->fXWidth) + psFuncData->fXMin;
717
718
		for (fY = 0.0; fY < fEnd; fY += fAccuracy) {
719
			fYFunc = (fY * psFuncData->fYWidth) + psFuncData->fYMin;
720
721
			//x = fXFunc;
722
			//y = fYFunc;
723
724
			if (psCartesianData->psVariableX) {
725
				SetVariable (psCartesianData->psVariableX, fXFunc);
726
			}
727
			if (psCartesianData->psVariableY) {
728
				SetVariable (psCartesianData->psVariableY, fYFunc);
729
			}
730
731
			fZFunc = ApproximateOperation (psFuncData->psFunction);
732
733
			vX.fX = 1.0f;
734
			vX.fY = 0.0f;
735
			vX.fZ = ApproximateOperation (psCartesianData->psDiffWrtX);
736
737
			vY.fX = 0.0f;
738
			vY.fY = 1.0f;
739
			vY.fZ = ApproximateOperation (psCartesianData->psDiffWrtY);
740
741
			vN = Normal (& vX, & vY);
742
743
			fXScreen = (fX * AXIS_XSIZE) - AXIS_XHSIZE;
744
			fYScreen = (fY * AXIS_YSIZE) - AXIS_YHSIZE;
745
			fZScreen = (((fZFunc - psFuncData->fZMin) / psFuncData->fZWidth) * AXIS_ZSIZE) - AXIS_ZHSIZE;
746
747
			if (psCartesianData->psVariableZ) {
748
				SetVariable (psCartesianData->psVariableZ, fZFunc);
749
			}
750
751
			// Calculate vertex colour
752
			fColour = ApproximateOperation (psFuncData->psRed);
753
			ucColour[0] = (unsigned char)(fColour * 255.0);
754
			fColour = ApproximateOperation (psFuncData->psGreen);
755
			ucColour[1] = (unsigned char)(fColour * 255.0);
756
			fColour = ApproximateOperation (psFuncData->psBlue);
757
			ucColour[2] = (unsigned char)(fColour * 255.0);
758
			fColour = ApproximateOperation (psFuncData->psAlpha);
759
			ucColour[3] = (unsigned char)(fColour * 255.0);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
760
761
			// Calculate vertex texture coordinate
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
762
			//psFuncData->afTextureCoords[(nVertex * 2)] = (fXFunc * fTexXScale) + fTexXOffset;
763
			//psFuncData->afTextureCoords[(nVertex * 2) + 1] = (fYFunc * fTexYScale) + fTexYOffset;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
764
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
765
			//psFuncData->afVertices[(nVertex * 3) + 2] = fZScreen;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
766
767
			// Output the vertex to file
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
768
			if (boScreenCoords) {
769
				SetVector3 (vVertex, fXScreen, fYScreen, fZScreen);
770
			}
771
			else {
772
				SetVector3 (vVertex, fXFunc, fYFunc, fZFunc);
773
			}
99 by David Llewellyn-Jones
Added option to scale model when being exported from Functy.
774
			ScaleVectorDirect (& vVertex, fScale);
775
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
776
			if (boBinary) {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
777
				recwrite (& vVertex, sizeof (float), 3, hFile);
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
778
				if (boExportAlpha) {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
779
					recwrite (ucColour, sizeof (unsigned char), 4, hFile);
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
780
				}
781
				else {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
782
					recwrite (ucColour, sizeof (unsigned char), 3, hFile);
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
783
				}
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
784
				recwrite (& vN, sizeof (float), 3, hFile);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
785
			}
786
			else {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
787
				recprintf (hFile, "%f %f %f ", vVertex.fX, vVertex.fY, vVertex.fZ);
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
788
				if (boExportAlpha) {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
789
					recprintf (hFile, "%u %u %u %u ", ucColour[0], ucColour[1], ucColour[2], ucColour[3]);
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
790
				}
791
				else {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
792
					recprintf (hFile, "%u %u %u ", ucColour[0], ucColour[1], ucColour[2]);
78 by David Llewellyn-Jones
Added various new options to the PLY export from Functy, including choice of whether to use screen or function coordinates, and whether to export alpha values (to allow colour models to be uploaded to Shapeways). Updated the configuration load/save to include these values.
793
				}
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
794
				recprintf (hFile, "%f %f %f\n", vN.fX, vN.fY, vN.fZ);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
795
			}
796
			nVertex++;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
797
		}
798
	}
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
799
800
	return nVertex;
801
}
802
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
803
int CartesianOutputStoredIndices (Recall * hFile, bool boBinary, double fMultiplier, int nOffset, FuncPersist const * psFuncData) {
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
804
	int nIndices;
805
	int anIndex[3];
806
	unsigned char uVertices;
807
	int nXVertex;
808
	int nYVertex;
809
	int nXVertices;
810
	int nYVertices;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
811
	double fAccuracy;
812
	
76 by David Llewellyn-Jones
Added accuracy multiplier to PLY model exporting in Functy. Added UI elements needed to export models. Added option to pause animation.
813
	if (fMultiplier > 0.0) {
814
		fAccuracy = psFuncData->fAccuracy / fMultiplier;
815
	}
816
	else {
817
		fAccuracy = 1.0;
818
	}
819
	if (fAccuracy < ACCURACY_MIN) {
820
		fAccuracy = ACCURACY_MIN;
821
	}
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
822
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
823
	nXVertices = floor (1.0 / fAccuracy) + 1;
824
	nYVertices = floor (1.0 / fAccuracy) + 1;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
825
826
	// Output index buffer identifiers
827
	uVertices = 3;
828
	nIndices = 0;
829
	for (nXVertex = 0; nXVertex < (nXVertices - 1); nXVertex++) {
830
		for (nYVertex = 0; nYVertex < (nYVertices - 1); nYVertex++) {
831
			anIndex[0] = ((nXVertex + 0) * nYVertices) + ((nYVertex + 0) % nYVertices) + nOffset;
832
			anIndex[1] = ((nXVertex + 1) * nYVertices) + ((nYVertex + 0) % nYVertices) + nOffset;
833
			anIndex[2] = ((nXVertex + 0) * nYVertices) + ((nYVertex + 1) % nYVertices) + nOffset;
834
835
			if (boBinary) {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
836
				recwrite (& uVertices, sizeof (unsigned char), 1, hFile);
837
				recwrite (& anIndex, sizeof (int), 3, hFile);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
838
			}
839
			else {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
840
				recprintf (hFile, "3 %d %d %d\n", anIndex[0], anIndex[1], anIndex[2]);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
841
			}
842
			nIndices++;
843
844
			anIndex[0] = ((nXVertex + 0) * nYVertices) + ((nYVertex + 1) % nYVertices) + nOffset;
845
			anIndex[1] = ((nXVertex + 1) * nYVertices) + ((nYVertex + 0) % nYVertices) + nOffset;
846
			anIndex[2] = ((nXVertex + 1) * nYVertices) + ((nYVertex + 1) % nYVertices) + nOffset;
847
848
			if (boBinary) {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
849
				recwrite (& uVertices, sizeof (unsigned char), 1, hFile);
850
				recwrite (& anIndex, sizeof (int), 3, hFile);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
851
			}
852
			else {
94 by David Llewellyn-Jones
Added code to Functy for stream output to memory rather than a file (to be used for animation export). Added interface elements for animation export. Simplified configure and makefiles to perform improved library checks.
853
				recprintf (hFile, "3 %d %d %d\n", anIndex[0], anIndex[1], anIndex[2]);
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
854
			}
855
			nIndices++;
856
		}
857
	}
858
859
	return nIndices;
860
}
861
76 by David Llewellyn-Jones
Added accuracy multiplier to PLY model exporting in Functy. Added UI elements needed to export models. Added option to pause animation.
862
int CartesianCountStoredVertices (double fMultiplier, FuncPersist const * psFuncData) {
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
863
	int nVertices;
864
	int nXVertices;
865
	int nYVertices;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
866
	double fAccuracy;
867
	
76 by David Llewellyn-Jones
Added accuracy multiplier to PLY model exporting in Functy. Added UI elements needed to export models. Added option to pause animation.
868
	if (fMultiplier > 0.0) {
869
		fAccuracy = psFuncData->fAccuracy / fMultiplier;
870
	}
871
	else {
872
		fAccuracy = 1.0;
873
	}
874
	if (fAccuracy < ACCURACY_MIN) {
875
		fAccuracy = ACCURACY_MIN;
876
	}
877
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
878
	nXVertices = floor (1.0 / fAccuracy) + 1;
879
	nYVertices = floor (1.0 / fAccuracy) + 1;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
880
	
881
	nVertices = nXVertices * nYVertices;
882
	
883
	return nVertices;
884
}
885
76 by David Llewellyn-Jones
Added accuracy multiplier to PLY model exporting in Functy. Added UI elements needed to export models. Added option to pause animation.
886
int CartesianCountStoredFaces (double fMultiplier, FuncPersist const * psFuncData) {
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
887
	int nFaces;
888
	int nXVertices;
889
	int nYVertices;
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
890
	double fAccuracy;
891
	
76 by David Llewellyn-Jones
Added accuracy multiplier to PLY model exporting in Functy. Added UI elements needed to export models. Added option to pause animation.
892
	if (fMultiplier > 0.0) {
893
		fAccuracy = psFuncData->fAccuracy / fMultiplier;
894
	}
895
	else {
896
		fAccuracy = 1.0;
897
	}
898
	if (fAccuracy < ACCURACY_MIN) {
899
		fAccuracy = ACCURACY_MIN;
900
	}
901
97 by David Llewellyn-Jones
Fixed lots of dubious indentation in the Functy code (basically, converted all the spaced indentations into tabs).
902
	nXVertices = floor (1.0 / fAccuracy) + 1;
903
	nYVertices = floor (1.0 / fAccuracy) + 1;
74 by David Llewellyn-Jones
Added code to export PLY (stanford triangle format) format graphs from Functy.
904
	
905
	nFaces = (nXVertices - 1) * (nYVertices - 1) * 2;
906
907
	return nFaces;
908
}
909
129 by David Llewellyn-Jones
Added ASCII STL export code.
910
void CartesianVertex (Vector3 * pvVertex, Vector3 * pvNormal, double fXFunc, double fYFunc, bool boScreenCoords, double fMultiplier, double fScale, FuncPersist const * psFuncData) {
911
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
912
	double fZFunc;
913
	double fXScreen;
914
	double fYScreen;
915
	double fZScreen;
916
	Vector3 vX;
917
	Vector3 vY;
918
	Vector3 vN;
919
	Vector3 vVertex;
920
921
	if ((pvVertex != NULL) || (pvNormal != NULL)) {
922
		if (psCartesianData->psVariableX) {
923
			SetVariable (psCartesianData->psVariableX, fXFunc);
924
		}
925
		if (psCartesianData->psVariableY) {
926
			SetVariable (psCartesianData->psVariableY, fYFunc);
927
		}
928
929
		fZFunc = ApproximateOperation (psFuncData->psFunction);
930
931
		vX.fX = 1.0f;
932
		vX.fY = 0.0f;
933
		vX.fZ = ApproximateOperation (psCartesianData->psDiffWrtX);
934
935
		vY.fX = 0.0f;
936
		vY.fY = 1.0f;
937
		vY.fZ = ApproximateOperation (psCartesianData->psDiffWrtY);
938
939
		vN = Normal (& vX, & vY);
940
941
		fXScreen = (((fXFunc - psFuncData->fXMin) / psFuncData->fXWidth) * AXIS_XSIZE) - AXIS_XHSIZE;
942
		fYScreen = (((fYFunc - psFuncData->fYMin) / psFuncData->fYWidth) * AXIS_YSIZE) - AXIS_YHSIZE;
943
		fZScreen = (((fZFunc - psFuncData->fZMin) / psFuncData->fZWidth) * AXIS_ZSIZE) - AXIS_ZHSIZE;
944
945
		if (psCartesianData->psVariableZ) {
946
			SetVariable (psCartesianData->psVariableZ, fZFunc);
947
		}
948
949
		// Output the vertex to file
950
		if (boScreenCoords) {
951
			SetVector3 (vVertex, fXScreen, fYScreen, fZScreen);
952
		}
953
		else {
954
			SetVector3 (vVertex, fXFunc, fYFunc, fZFunc);
955
		}
956
		ScaleVectorDirect (& vVertex, fScale);
957
958
		if (pvVertex) {
959
			*pvVertex = vVertex;
960
		}
961
		if (pvNormal) {
962
			*pvNormal = vN;
963
		}
964
	}
965
}
966
131 by David Llewellyn-Jones
Added binary STL export code.
967
int CartesianOutputStoredTrianglesSTL (Recall * hFile, bool boBinary, bool boScreenCoords, double fMultiplier, double fScale, FuncPersist const * psFuncData) {
129 by David Llewellyn-Jones
Added ASCII STL export code.
968
	//CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
969
	int nX;
970
	int nY;
971
	double fXFunc;
972
	double fYFunc;
973
	int nXVertices;
974
	int nYVertices;
975
	Vector3 avPos[4];
976
	int nVertex;
977
	Vector3 vX;
978
	Vector3 vY;
979
	Vector3 vN;
980
	double fAccuracy;
131 by David Llewellyn-Jones
Added binary STL export code.
981
	float afVector[3];
982
	uint16_t uAttribute = 0;
983
	int nTriangles = 0;
129 by David Llewellyn-Jones
Added ASCII STL export code.
984
	
985
	if (fMultiplier > 0.0) {
986
		fAccuracy = psFuncData->fAccuracy / fMultiplier;
987
	}
988
	else {
989
		fAccuracy = 0.5;
990
	}
991
	if (fAccuracy < ACCURACY_MIN) {
992
		fAccuracy = ACCURACY_MIN;
993
	}
994
995
	nXVertices = floor (1.0 / fAccuracy) + 1;
996
	nYVertices = floor (1.0 / fAccuracy) + 1;
997
998
	for (nX = 0; nX < (nXVertices - 1); nX++) {
999
		for (nY = 0; nY < (nYVertices - 1); nY++) {
1000
			for (nVertex = 0; nVertex < 4; nVertex++) {
1001
				fXFunc = ((((double)((nX + (int)(nVertex / 2)) % nXVertices)) * fAccuracy) * psFuncData->fXWidth) + psFuncData->fXMin;
1002
				fYFunc = ((((double)((nY + (nVertex % 2)) % nYVertices)) * fAccuracy) * psFuncData->fYWidth) + psFuncData->fYMin;
1003
				CartesianVertex (& avPos[nVertex], NULL, fXFunc, fYFunc, boScreenCoords, fMultiplier, fScale, psFuncData);
1004
			}
1005
131 by David Llewellyn-Jones
Added binary STL export code.
1006
			if (boBinary) {
1007
				// Calculate the normals
1008
				// Based on the triangles rather than the curves to avoid messy results
1009
				vX = SubtractVectors (& avPos[1], & avPos[0]);
1010
				vY = SubtractVectors (& avPos[2], & avPos[0]);
132 by David Llewellyn-Jones
Added checks for invalid normals when exporting STL files.
1011
				vN = NormalOrUp (& vX, & vY);
131 by David Llewellyn-Jones
Added binary STL export code.
1012
1013
				afVector[0] = vN.fX;
1014
				afVector[1] = vN.fY;
1015
				afVector[2] = vN.fZ;
1016
				recwrite (afVector, sizeof(float), 3, hFile);
1017
1018
				afVector[0] = avPos[0].fX;
1019
				afVector[1] = avPos[0].fY;
1020
				afVector[2] = avPos[0].fZ;
1021
				recwrite (afVector, sizeof(float), 3, hFile);
1022
1023
				afVector[0] = avPos[2].fX;
1024
				afVector[1] = avPos[2].fY;
1025
				afVector[2] = avPos[2].fZ;
1026
				recwrite (afVector, sizeof(float), 3, hFile);
1027
1028
				afVector[0] = avPos[1].fX;
1029
				afVector[1] = avPos[1].fY;
1030
				afVector[2] = avPos[1].fZ;
1031
				recwrite (afVector, sizeof(float), 3, hFile);
1032
1033
				recwrite (& uAttribute, sizeof(uint16_t), 1, hFile);
1034
1035
				// Calculate the normals
1036
				// Based on the triangles rather than the curves to avoid messy results
1037
				vX = SubtractVectors (& avPos[3], & avPos[1]);
1038
				vY = SubtractVectors (& avPos[2], & avPos[1]);
132 by David Llewellyn-Jones
Added checks for invalid normals when exporting STL files.
1039
				vN = NormalOrUp (& vX, & vY);
131 by David Llewellyn-Jones
Added binary STL export code.
1040
1041
				afVector[0] = vN.fX;
1042
				afVector[1] = vN.fY;
1043
				afVector[2] = vN.fZ;
1044
				recwrite (afVector, sizeof(float), 3, hFile);
1045
1046
				afVector[0] = avPos[1].fX;
1047
				afVector[1] = avPos[1].fY;
1048
				afVector[2] = avPos[1].fZ;
1049
				recwrite (afVector, sizeof(float), 3, hFile);
1050
1051
				afVector[0] = avPos[2].fX;
1052
				afVector[1] = avPos[2].fY;
1053
				afVector[2] = avPos[2].fZ;
1054
				recwrite (afVector, sizeof(float), 3, hFile);
1055
1056
				afVector[0] = avPos[3].fX;
1057
				afVector[1] = avPos[3].fY;
1058
				afVector[2] = avPos[3].fZ;
1059
				recwrite (afVector, sizeof(float), 3, hFile);
1060
1061
				recwrite (& uAttribute, sizeof(uint16_t), 1, hFile);
1062
				
1063
				nTriangles += 2;
1064
			}
1065
			else {
1066
				// Calculate the normals
1067
				// Based on the triangles rather than the curves to avoid messy results
1068
				vX = SubtractVectors (& avPos[1], & avPos[0]);
1069
				vY = SubtractVectors (& avPos[2], & avPos[0]);
132 by David Llewellyn-Jones
Added checks for invalid normals when exporting STL files.
1070
				vN = NormalOrUp (& vX, & vY);
131 by David Llewellyn-Jones
Added binary STL export code.
1071
1072
				// Output the triangles
1073
				recprintf (hFile, "facet normal %e %e %e\n", vN.fX, vN.fY, vN.fZ);
1074
				recprintf (hFile, "outer loop\n");
1075
				recprintf (hFile, "\tvertex %e %e %e\n", avPos[0].fX, avPos[0].fY, avPos[0].fZ);
1076
				recprintf (hFile, "\tvertex %e %e %e\n", avPos[2].fX, avPos[2].fY, avPos[2].fZ);
1077
				recprintf (hFile, "\tvertex %e %e %e\n", avPos[1].fX, avPos[1].fY, avPos[1].fZ);
1078
				recprintf (hFile, "endloop\n");
1079
				recprintf (hFile, "endfacet\n");
1080
1081
				// Calculate the normals
1082
				// Based on the triangles rather than the curves to avoid messy results
1083
				vX = SubtractVectors (& avPos[3], & avPos[1]);
1084
				vY = SubtractVectors (& avPos[2], & avPos[1]);
132 by David Llewellyn-Jones
Added checks for invalid normals when exporting STL files.
1085
				vN = NormalOrUp (& vX, & vY);
131 by David Llewellyn-Jones
Added binary STL export code.
1086
1087
				recprintf (hFile, "facet normal %e %e %e\n", vN.fX, vN.fY, vN.fZ);
1088
				recprintf (hFile, "outer loop\n");
1089
				recprintf (hFile, "\tvertex %e %e %e\n", avPos[1].fX, avPos[1].fY, avPos[1].fZ);
1090
				recprintf (hFile, "\tvertex %e %e %e\n", avPos[2].fX, avPos[2].fY, avPos[2].fZ);
1091
				recprintf (hFile, "\tvertex %e %e %e\n", avPos[3].fX, avPos[3].fY, avPos[3].fZ);
1092
				recprintf (hFile, "endloop\n");
1093
				recprintf (hFile, "endfacet\n");
1094
			}
129 by David Llewellyn-Jones
Added ASCII STL export code.
1095
		}
1096
	}
131 by David Llewellyn-Jones
Added binary STL export code.
1097
	
1098
	return nTriangles;
129 by David Llewellyn-Jones
Added ASCII STL export code.
1099
}
1100
106 by David Llewellyn-Jones
Control variables set to apply to centre coordinate functions as well as structure functions in Functy.
1101
bool CartesianAssignControlVarsToFunction (FnControlPersist * psFnControlData, FuncPersist * psFuncData) {
1102
	// Do nothing	
1103
	return FALSE;
1104
}
53 by David Llewellyn-Jones
Rearranged Functy source so that different shader code can be generated depending on whether a function is cartesian or spherical.
1105
142 by David Llewellyn-Jones
First attempt voxel rendering to support SVX export.
1106
void CartesianOutputVoxelSlice (unsigned char * pcData, int nResolution, int nChannels, int nSlice, FuncPersist * psFuncData) {
1107
	CartesianPersist * psCartesianData = psFuncData->Func.psCartesianData;
1108
	int nX;
1109
	int nY;
1110
	int nChannel;
1111
	double fXFunc;
1112
	double fYFunc;
1113
	double fZFunc;
1114
	double fXStep;
1115
	double fYStep;
1116
	double fZStep;
1117
	double fZSlice;
1118
	unsigned char ucFill;
1119
1120
	fXStep = psFuncData->fXWidth / ((double)nResolution);
1121
	fYStep = psFuncData->fYWidth / ((double)nResolution);
1122
	fZStep = psFuncData->fZWidth / ((double)nResolution);
1123
1124
	fZSlice = psFuncData->fZMin + (nSlice * fZStep);
1125
1126
	for (nX = 0; nX < nResolution; nX++) {
1127
		fXFunc = psFuncData->fXMin + (nX * fXStep);
1128
		for (nY = 0; nY < nResolution; nY++) {
1129
			fYFunc = psFuncData->fYMin + (nY * fYStep);
1130
1131
			if (psCartesianData->psVariableX) {
1132
				SetVariable (psCartesianData->psVariableX, fXFunc);
1133
			}
1134
			if (psCartesianData->psVariableY) {
1135
				SetVariable (psCartesianData->psVariableY, fYFunc);
1136
			}
1137
1138
			fZFunc = ApproximateOperation (psFuncData->psFunction);
1139
157 by David Llewellyn-Jones
OpenVDB export enabled. Control variables correctly appled during voxel export.
1140
			if ((fZSlice < fZFunc) && ((psFuncData->boMaterialFill == TRUE) || (fZSlice > (fZFunc - psFuncData->fMaterialThickness)))) {
142 by David Llewellyn-Jones
First attempt voxel rendering to support SVX export.
1141
				ucFill = 255u;
1142
			}
1143
			else {
1144
				ucFill = 0u;
1145
			}
1146
1147
			for (nChannel = 0; nChannel < nChannels; nChannel++) {
1148
				if (ucFill != 0) {
1149
					pcData[(((nX + ((nResolution - nY - 1) * nResolution)) * nChannels) + nChannel)] = ucFill;
1150
				}
1151
			}
1152
		}
1153
	}
1154
}
1155
1156
1157