File: gejimpl.c

package info (click to toggle)
vrwave 0.9-4
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 5,032 kB
  • ctags: 7,153
  • sloc: java: 15,050; ansic: 8,219; sh: 458; makefile: 181
file content (514 lines) | stat: -rw-r--r-- 16,413 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * gejimpl.c - implementation of the GE3D Java interface
 * Copyright (c) 1996,97 IICM - all rights reserved
 *
 * created: mpichler, 19960924
 *
 * changed: mpopetz, 19970121
 * changed: mpichler, 19970709
 * changed: kwagen, 19970801
 *
 * $Id: gejimpl.c,v 1.21 1997/09/15 09:37:21 apesen Exp $
 */


#ifdef __cplusplus
extern "C" {
#endif
#include "GE3D.h"
#ifdef __cplusplus
} // C++
#endif


#include "jutils.h"

#include <math.h>  /* M_PI */
#ifndef M_PI
# include <values.h>
#endif
#include <stdio.h>  /* debugging */

#ifdef macintosh
# include <vectors.h>
# include <ge3d.h>
# define M_PI _PI
# define PACKAGE iicm_ge3d_
#else
# include <ge3d/vectors.h>
# include <ge3d/ge3d.h>
#endif

/* conversion radians to degrees */
#define DEGREES(R)  ( (R) * (180 / M_PI) )

/* note: also static native functions get a "this" handle in Java */

static int texturealpha = 0;
static materialsGE3D* gejmaterial = NULL; 
static materialsGE3D* gejvertmaterial = NULL; 


/* initGE3D */
void name2(PACKAGE,GE3D_initGE3D) (struct name3(H,PACKAGE,GE3D)* handle)
{
  /* static colorRGB white = { 1, 1, 1 }; */

  ge3d_init_ ();
  gejmaterial = malloc (sizeof (materialsGE3D));
  initmtl3D (gejmaterial);
  gejvertmaterial = malloc (sizeof (materialsGE3D));
  initmtl3D (gejvertmaterial);
  /* ge3dHint (hint_lighting, 0);  / * need light sources and normal vectors for lighting */
  /* caller responsible for light sources; e.g. setHeadLight */
}

/* setDrawMode */
void name2(PACKAGE,GE3D_setDrawMode) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 mode)
{
  ge3d_setmode ((int) mode);
}

/* setBackgroundColor */
void name2(PACKAGE,GE3D_setBackgroundColor) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 rgb)
{
  /* fprintf (stderr, "background: 0x%06x\n", rgb); */
  ge3d_setbackgcolor (((rgb >> 16) & 0xff) / 255.0f, ((rgb >> 8) & 0xff) / 255.0f, (rgb & 0xff) / 255.0f);
}

/* clearScreen */
void name2(PACKAGE,GE3D_clearScreen) (struct name3(H,PACKAGE,GE3D)* handle)
{ /* why is the function to clear the window still named clearScreen here and in ge3d? ;-) */
  ge3d_clearscreen ();
}

/* hint */
void name2(PACKAGE,GE3D_hint) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 flag, jn_int32 value)
{
  ge3dHint (flag, value);
}

/* antialiasingSupport */
jn_int32 name2(PACKAGE,GE3D_antialiasingSupport) (struct name3(H,PACKAGE,GE3D)* handle)
{
  return ge3dAntialiasingSupport ();
}

/* antialiasing */
void name2(PACKAGE,GE3D_antialiasing) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 flags)
{
  ge3dAntialiasing (flags);
}


/*** transformations ***/

/* rotatef3f */
void name2(PACKAGE,GE3D_rotatef3f) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* haxis, float angle)
{
  ge3dRotate ((const vector3D*) unhand (haxis)->body, angle);  /* angle in radians */
}

/* translatefff */
void name2(PACKAGE,GE3D_translatefff) (struct name3(H,PACKAGE,GE3D)* handle, float x, float y, float z)
{
  /* fprintf (stderr, "translation [%g, %g, %g]\n", x, y, z); */
  ge3d_translate (x, y, z);
}

/* ge3dTransformMcWc */
void name2(PACKAGE,GE3D_ge3dTransformMcWc) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* hin, HArrayOfFloat* hout)
{
  ge3dTransformMcWc ((const point3D*) unhand (hin)->body, (point3D*) unhand (hout)->body);  
}


/*** matrix operations ***/

/* loadIdentity */
void name2(PACKAGE,GE3D_loadIdentity) (struct name3(H,PACKAGE,GE3D)* handle)
{
  ge3dLoadIdentity ();
}

/* pushMatrix */
void name2(PACKAGE,GE3D_pushMatrix) (struct name3(H,PACKAGE,GE3D)* handle)
{
  ge3d_push_matrix ();
}

/* pushThisMatrix */
void name2(PACKAGE,GE3D_pushThisMatrix) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* math)
{
  ge3d_push_this_matrix ((const float (*)[4]) unhand (math)->body);
}

/* popMatrix */
void name2(PACKAGE,GE3D_popMatrix) (struct name3(H,PACKAGE,GE3D)* handle)
{
  ge3d_pop_matrix ();
}

/* getMatrix */
void name2(PACKAGE,GE3D_getMatrix) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* math)
{
  ge3d_get_matrix ((float (*)[4]) unhand (math)->body);
}


/*** camera setting ***/

/* setPerspectiveCamera */
void name2(PACKAGE,GE3D_setPerspectiveCamera) (struct name3(H,PACKAGE,GE3D)* handle,
  HArrayOfFloat* position, HArrayOfFloat* axisangle, float fovy, float aspect, float hither, float yon)
{
  const point3D* pos = (const point3D*) unhand (position)->body;
  const vector3D* rotaxis = (const vector3D*) unhand (axisangle)->body;
  float rotangle = DEGREES (((const float*) rotaxis)[3]);
  fovy = DEGREES (fovy);

/*fprintf (stderr, "%% position (%g, %g, %g), rotaxis (%g, %g, %g), angle (degrees) %g\n",*/
/*pos->x, pos->y, pos->z, rotaxis->x, rotaxis->y, rotaxis->z, rotangle);*/
/*fprintf (stderr, "view angle (degrees): %g, aspect ratio: %g, hither: %g, yon: %g\n", fovy, aspect, hither, yon);*/

  ge3dCamera (cam_perspective | cam_relative, pos, rotangle, rotaxis, fovy, aspect, hither, yon);
}

/* simpleOrthoCamera */
void name2(PACKAGE,GE3D_simpleOrthoCamera) (struct name3(H,PACKAGE,GE3D)* handle, float width, float height)
{
  point3D eyepos = { 0, 0, 1 };
  point3D lookat = { 0, 0, 0 };
  /* view centered at window center */
  ge3d_ortho_cam (&eyepos, &lookat, NULL, width, height, 0, 2);
}


/*** color ***/

/* fillColor3f (fillcolor) */
void name2(PACKAGE,GE3D_fillColor3f) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* colh)
{
  const colorRGB* color = (const colorRGB*) unhand (colh)->body;
  /* fprintf (stderr, "fillcolor: (%g, %g, %g)\n", color->R, color->G, color->B); */
  ge3dFillColor (color);  /* macro */
}

/* lineColorRGBi (fillcolor) */
void name2(PACKAGE,GE3D_lineColorRGBi) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 color)
{
  /* color: 0xrrggbb */
  float r = ((color >> 16) & 0xff) / 255.0;
  float g = ((color >> 8) & 0xff) / 255.0;
  float b = (color & 0xff) / 255.0;
  ge3d_setlinecolor (r, g, b);
}

/* lineColor3f */
void name2(PACKAGE,GE3D_lineColor3f) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* colh)
{
  const colorRGB* color = (const colorRGB*) unhand (colh)->body;
  ge3dLineColor (color);
}

/* material */
void name2(PACKAGE,GE3D_material) (struct name3(H,PACKAGE,GE3D)* handle, float ambient,
  HArrayOfFloat* diffuseh, HArrayOfFloat* emissiveh, float shininess, HArrayOfFloat* specularh,
  float transparency
)
{
  colorRGB* diffuse = (colorRGB*) unhand (diffuseh)->body;
  colorRGB* specular = (colorRGB*) unhand (specularh)->body;
  colorRGB* emissive = (colorRGB*) unhand (emissiveh)->body;
  static colorRGB ambientrgb;
  static float shiny, transp;

  initRGB (ambientrgb, ambient * diffuse->R, ambient * diffuse->G, ambient * diffuse->B);
  shiny = shininess;
  transp = transparency;

  gejmaterial->num_ambient = gejmaterial->num_diffuse = gejmaterial->num_specular = 1;
  gejmaterial->num_emissive = gejmaterial->num_shininess = gejmaterial->num_transparency = 1;
  gejmaterial->num_base = 1;
  gejmaterial->rgb_base = diffuse;
  gejmaterial->rgb_ambient = &ambientrgb;
  gejmaterial->rgb_diffuse = diffuse;
  gejmaterial->rgb_specular = specular;
  gejmaterial->rgb_emissive = emissive;
  gejmaterial->val_shininess = &shiny;
  gejmaterial->val_transparency = &transp;

  ge3dMaterial (mat_front_and_back, gejmaterial);
}

/* defaultMaterial */
void name2(PACKAGE,GE3D_defaultMaterial) (struct name3(H,PACKAGE,GE3D)* handle)
{
  ge3dDefaultMaterial ();
  initmtl3D (gejmaterial);
}

/* lineStyle */
void name2(PACKAGE,GE3D_lineStyle) (struct name3(H,PACKAGE,GE3D)* handle, jn_int16 pattern)
{
  ge3d_setlinestyle (pattern);
}


/*** lighting ***/

/* activateLightSource */
void name2(PACKAGE,GE3D_activateLightSource) (struct name3(H,PACKAGE,GE3D)* handle, 
  jn_int32 index, HArrayOfFloat* colh, float intensity, HArrayOfFloat* posh, float positional
)
{ /* point/directional light source */
  const point3D* position = (const point3D*) unhand (posh)->body;
  colorRGB color = *(colorRGB*) unhand (colh)->body;
  color.R *= intensity;
  color.G *= intensity;
  color.B *= intensity;

  if (positional)
    ge3dSetLightSource ((int) index, &color, position, positional, 0);
  else  /* reverse direction to point towards light source */
  {
    point3D direction = *position;
    neg3D (direction);
    ge3dSetLightSource ((int) index, &color, &direction, positional, 0);
  }
  ge3d_switchlight ((int) index, 1);
}

/* setHeadLight */
void name2(PACKAGE,GE3D_setHeadLight) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* colh)
{
  static const vector3D vwlgtdir = { 0, 0, 1 };  /* towards camera */
  const colorRGB* color = (const colorRGB*) unhand (colh)->body;

  ge3dSetLightSource (0, color, &vwlgtdir, 0.0, 1);
  ge3d_switchlight (0, 1);  /* headlight has index 0 by convention */
}

/* deactivateLights */
void name2(PACKAGE,GE3D_deactivateLights) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 from, jn_int32 to)
{
  while (from <= to)
  {
    ge3d_switchlight (from++, 0);
  }
}


/*** texturing ***/

/* createPixelTexture */
jn_int32 name2(PACKAGE,GE3D_createPixelTexture) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfInt* datah)
{
  const int* data = (const int*) unhand (datah)->body;
  int width = data[0];
  int height = data[1];
  int format = data[2] + ge3d_int_I_BT - 1;  /* + 4 */
  int colored = (data[2] > 2);
  int index = ge3dCreateTexture (width, height, data + 3, format, 0);
  texturealpha = (data[2] == 2 || data[2] == 4);
  return colored ? -index : index;
}

/* createImageTexture */
jn_int32 name2(PACKAGE,GE3D_createImageTexture) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 width, jn_int32 height, HArrayOfInt* datah)
{
  const int* data = (const int*) unhand (datah)->body;
  int flag = 0;
  int index = ge3dCreateTexture (width, height, data, ge3d_int_ARGB_TB, &flag);
  int colored = flag & 2;
  texturealpha = flag & 1;
  return colored ? -index : index;
}

/* doTexturing */
void name2(PACKAGE,GE3D_doTexturing) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 on)
{
  ge3dDoTexturing (on);
}

/* applyTexture */
void name2(PACKAGE,GE3D_applyTexture) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 index)
{
  ge3dApplyTexture (index);
}

/* textureRepeat */
void name2(PACKAGE,GE3D_textureRepeat) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 s, jn_int32 t)
{
  ge3dTextureRepeat (s, t);
}

/* freeTexture */
void name2(PACKAGE,GE3D_freeTexture) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 index)
{
  ge3dFreeTexture (index);
}

/* textureMipmapping */
void name2(PACKAGE,GE3D_setTextureMipmapping) (struct name3(H,PACKAGE,GE3D)* handle, jn_int32 quality)
{
  ge3dTextureMipmapping (quality);
}

/* loadTextureIdentity */
void name2(PACKAGE,GE3D_loadTextureIdentity) (struct name3(H,PACKAGE,GE3D)* handle)
{
  ge3dLoadTextureIdentity ();
}

/* loadTextureMatrix */
void name2(PACKAGE,GE3D_loadTextureMatrix) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* math)
{
  ge3dLoadTextureMatrix ((const float (*)[4]) unhand (math)->body);
}

/* alphaTest */
void name2(PACKAGE,GE3D_alphaTest) (struct name3(H,PACKAGE,GE3D)* handle, float threshold)
{
  ge3dAlphaTest (threshold);
}

/* getTextureAlpha */
jn_int32 name2(PACKAGE,GE3D_getTextureAlpha) (struct name3(H,PACKAGE,GE3D)* handle)
{
  return texturealpha;
}


/*** drawing geometry ***/

/* drawCube */
void name2(PACKAGE,GE3D_drawCube) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* hmin, HArrayOfFloat* hmax)
{
  const point3D* min = (const point3D*) unhand (hmin)->body;
  const point3D* max = (const point3D*) unhand (hmax)->body;

/*fprintf (stderr, "cube ((%g, %g, %g), (%g, %g, %g))\n", min->x, min->y, min->z, max->x, max->y, max->z);*/

  ge3dCube (min, max);
}

/* drawWireCube */
void name2(PACKAGE,GE3D_drawWireCube) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* hmin, HArrayOfFloat* hmax)
{
  const point3D* min = (const point3D*) unhand (hmin)->body;
  const point3D* max = (const point3D*) unhand (hmax)->body;

  ge3dWireCube (min, max);
}

/* drawCylinder */
void name2(PACKAGE,GE3D_drawCylinder) (struct name3(H,PACKAGE,GE3D)* handle,
  float botrad, float toprad, float bottom, float height, jn_int32 parts)
{
  ge3dCylinder (botrad, toprad, bottom, height, parts);
}

/* drawFaceSet */
void name2(PACKAGE,GE3D_drawFaceSet) (struct name3(H,PACKAGE,GE3D)* handle,
  HArrayOfFloat* hverts, jn_int32 numcoordinds, HArrayOfInt* hcoordinds,
  HArrayOfFloat* hfnormals,
  HArrayOfFloat* htexcoords, jn_int32 numtexinds, HArrayOfInt* htexcoordinds,
  jn_int32 numcolor, HArrayOfFloat* hcolor, jn_int32 numcolorinds, HArrayOfInt* hcolorinds,
  jn_int32 matbinding,
  HArrayOfFloat* hnormallist, jn_int32 numnormalinds, HArrayOfInt* hnormalinds
)
{
  const point3D* verts = (const point3D*) unhand (hverts)->body;
  /* not using array sizes, because they may be larger than the used part numverts (FloatArray) */
  const int* coordinds = (const int*) unhand (hcoordinds)->body;
  const vector3D* fnormals = hfnormals ? (const vector3D*) unhand (hfnormals)->body : 0;
  const point2D* texcoords = htexcoords ? (const point2D*) unhand (htexcoords)->body : 0;
  const int* texcoordinds = htexcoordinds ? (const int*) unhand (htexcoordinds)->body : 0;
  colorRGB* color = hcolor ? (colorRGB*) unhand (hcolor)->body : 0;
  const int* colorinds = hcolorinds ? (const int*) unhand (hcolorinds)->body : 0;
  const vector3D * normallist = hnormallist ? (const vector3D*) unhand (hnormallist)->body : 0;
  const int* normalinds = hnormalinds ? (const int*) unhand (hnormalinds)->body : 0;
  /* assert: C's int is 32 bit (jn_int32 is) */

/* fprintf (stderr, "drawing faceset with %d vertex indices (%d, %d, %d, ...)\n", */
/* (int) numcoordinds, (int) coordinds[0], (int) coordinds[1], (int) coordinds[2]); */

  gejvertmaterial->num_diffuse = numcolor;
  gejvertmaterial->rgb_diffuse = color;

/* fprintf (stderr, "calling ge3dFaceSet, %d colors, %p\n", numcolor, (void*) color); */
/*if (numcolor == 5)*/
/*  fprintf (stderr, "(%g, %g, %g), (%g, %g, %g), (%g, %g, %g)\n", color[0].R, color[0].G, color[0].B,*/
/*color[1].R, color[1].G, color[1].B, color[2].R, color[2].G, color[2].B);*/

  ge3dFaceSet (
    verts, numcoordinds, coordinds,             /* vertex data */
    gejvertmaterial, matbinding, numcolorinds, colorinds,  /* materials */
    normallist, numnormalinds, normalinds,      /* vertex normals */
    fnormals,                                   /* facenormals */
    texcoords, numtexinds, texcoordinds         /* texturing */
  );

/* fprintf (stderr, "after calling ge3dFaceSet\n"); */

  if (gejmaterial->rgb_diffuse)  /* restore diffuse color */
    ge3dFillColor (gejmaterial->rgb_diffuse);
}

/* drawLineSet */
void name2(PACKAGE,GE3D_drawLineSet) (struct name3(H,PACKAGE,GE3D)* handle,
  HArrayOfFloat* hverts, jn_int32 numcoordinds, HArrayOfInt* hcoordinds,
  jn_int32 numcolor, HArrayOfFloat* hcolor, jn_int32 numcolorind, HArrayOfInt* hcolorind, jn_int32 pervertex
)
{
  const point3D* verts = (const point3D*) unhand (hverts)->body;
  const int* coordinds = (const int*) unhand (hcoordinds)->body;
  const colorRGB* color = hcolor ? (const colorRGB*) unhand (hcolor)->body : 0;
  const int* colorind = hcolorind ? (const int*) unhand (hcolorind)->body : 0;

  ge3dLineSet2 (verts, numcoordinds, coordinds, numcolor, color, numcolorind, colorind, pervertex);
}

/* drawPointSet */
void name2(PACKAGE,GE3D_drawPointSet) (struct name3(H,PACKAGE,GE3D)* handle,
  HArrayOfFloat* hverts, jn_int32 numverts, HArrayOfFloat* hcolor, jn_int32 numcolor
)
{
  const point3D* verts = (const point3D*) unhand (hverts)->body;
  const colorRGB* color = hcolor ? (const colorRGB*) unhand (hcolor)->body : 0;

  ge3dPointSet2 (verts, numverts, color, numcolor);
}

/* drawSphere */
void name2(PACKAGE,GE3D_drawSphere) (struct name3(H,PACKAGE,GE3D)* handle, float radius)
{
  ge3dSphere (radius);
}

/* drawLine2D */
void name2(PACKAGE,GE3D_drawLine2D) (struct name3(H,PACKAGE,GE3D)* handle,
  float x0, float y0, float x1, float y1)
{
  ge3d_line (x0, y0, 0.0, x1, y1, 0.0);
}

/* drawRect2D */
void name2(PACKAGE,GE3D_drawRect2D) (struct name3(H,PACKAGE,GE3D)* handle,
  float x0, float y0, float x1, float y1)
{
  ge3d_rect (x0, y0, x1, y1);
}

/* drawPolyLines2D */
void name2(PACKAGE,GE3D_drawPolyLines2D) (struct name3(H,PACKAGE,GE3D)* handle, HArrayOfFloat* hlines)
{
  ge3dPolyLines2D ((float*) unhand (hlines)->body);  /* should be (const float*) in ge3d */
}

/* drawCircle */
void name2(PACKAGE,GE3D_drawCircle) (struct name3(H,PACKAGE,GE3D)* handle, float x, float y, float r)
{
  ge3d_circle (x, y, r);  /* circle outline */
}