Annotation of OpenXM/src/oxmgraph/graph.c, Revision 1.5
1.1 takayama 1: /*
2: Copyright: Y.Mitou, 2009.01
3:
4: License: This software is distributed under the 2-cluases BSD license
5: (http://en.wikipedia.org/wiki/BSD_licenses)
6: */
7:
1.4 ohara 8: #if defined (_MSC_VER)
9: #include <windows.h>
10: #endif
1.5 ! ohara 11: #if defined(__APPLE__)
! 12: #include <GLUT/glut.h>
! 13: #else
1.1 takayama 14: #include<GL/glut.h>
15: #endif
16:
17: #include<stdio.h>
18: #include<math.h>
19: #include<stdlib.h>
20: #include<string.h>
21:
22: #define TEX_WIDTH 128
23: #define TEX_HEIGHT 128
24:
25: #define KAKUDO 0.05
26: #define DIV 0.3
27: #define BOXSIZE 1.0
28: #define FUNCLEN 0.3
29: #define MAG 1.1
30: #define MARGIN 0
31: #define INTERVAL 5.0
32:
33: #define LINE_MAX 2048
34: #define LIMIT 1000
35: #define outOfMemory() { fprintf(stderr,"Out of memory.\n"); return -1; }
36:
37: /* for texture */
38: static GLuint texInobuta;
39: static GLubyte image[TEX_HEIGHT][TEX_WIDTH][4];
40:
41: /* rasters */
42: GLubyte raster_5[24] = {
43: 0x3f, 0x00, 0x7f, 0x80, 0x61, 0xc0, 0x00, 0xc0, 0xc0, 0xc0,
44: 0xf1, 0xc0, 0xff, 0x80, 0xdf, 0x00, 0xc0, 0x00, 0xc0, 0x00,
45: 0xff, 0xc0, 0xff, 0xc0 };
46:
47: GLubyte raster_X[24] = {
48: 0x00, 0x00, 0x80, 0x40, 0xc0, 0xc0, 0x61, 0x80, 0x33, 0x00,
49: 0x1e, 0x00, 0x0c, 0x00, 0x1e, 0x00, 0x33, 0x00, 0x61, 0x80,
50: 0xc0, 0xc0, 0x80, 0x40 };
51:
52: GLubyte raster_Y[24] = {
53: 0x0c, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x0c, 0x00,
54: 0x0c, 0x00, 0x1e, 0x00, 0x12, 0x00, 0x33, 0x00, 0x21, 0x00,
55: 0x61, 0x80, 0xc0, 0xc0 };
56:
57: GLubyte raster_Z[24] = {
58: 0xff, 0xc0, 0xff, 0xc0, 0xc0, 0x00, 0x60, 0x00, 0x30, 0x00,
59: 0x18, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x03, 0x00, 0x01, 0x80,
60: 0xff, 0xc0, 0xff, 0xc0 };
61:
62: GLubyte raster_$[44] = {
63: 0x04, 0x00, 0x04, 0x00, 0x1f, 0x00, 0x64, 0xc0, 0xc4, 0x60,
64: 0xc4, 0x60, 0x04, 0x60, 0x04, 0x60, 0x04, 0x60, 0x04, 0xe0,
65: 0x07, 0xc0, 0x1f, 0x00, 0x7c, 0x00, 0xe4, 0x00, 0xc4, 0x00,
66: 0xc4, 0x00, 0xc4, 0x60, 0xc4, 0x60, 0x64, 0xc0, 0x1f, 0x00,
67: 0x04, 0x00 ,0x04, 0x00 };
68:
69: GLubyte raster_at[40] = {
70: 0x1f, 0x00, 0x20, 0xc0, 0x60, 0x20, 0x40, 0x00, 0xc0, 0x60,
71: 0x9d, 0xc0, 0xa7, 0x60, 0xa3, 0x20, 0xa3, 0x20, 0xa3, 0x20,
72: 0xa3, 0x20, 0xa3, 0x20, 0xa3, 0x20, 0x97, 0x20, 0x8d, 0x20,
73: 0xc0, 0x60, 0x40, 0x40, 0x60, 0xc0, 0x20, 0x80, 0x1f, 0x00 };
74:
75: /* for lighting */
76:
77: GLfloat light0_diffuse[] =
78: {0.0, 0.6, 0.2, 1.0};
79: GLfloat light0_position[] =
80: {0.0, 50.0, -50.0, 0.0};
81: GLfloat light1_diffuse[] =
82: {0.0, 0.6, 0.2, 1.0};
83: GLfloat light1_position[] =
84: {50.0, 0.0, 50.0, 0.0};
85:
86: GLfloat light2_diffuse[] =
87: {0.6, 0.6, 0.0, 1.0};
88: GLfloat light2_position[] =
89: {-50.0, -50.0, 0.0, 0.0};
90: GLfloat light3_diffuse[] =
91: {0.0, 0.0, 0.6, 1.0};
92: GLfloat light3_position[] =
93: {0.0, 3.0, 0.0, 0.0};
94: GLfloat light4_diffuse[] =
95: {0.0, 0.0, 0.0, 1.0};
96: GLfloat light4_position[] =
97: {3.0, 0.0, 0.0, 0.0};
98: GLfloat light5_diffuse[] =
99: {0.0, 1.0, 0.0, 1.0};
100: GLfloat light5_position[] =
101: {-3.0, 0.0, 0.0, 0.0};
102:
103: /*
104: GLfloat model_ambient[] = { 1.3, 1.3, 1.3, 1.2 };
105: */
106:
107: /* for Display_List name */
108: GLuint theGraph, theBox, theAxis, theWhole, theTriangle;
109:
110: /* for rotation */
111: GLfloat angle[2] = {0.0, 0.0};
112: int moving, begin[2];
113:
114: /* for normal */
115: GLfloat v1[3];
116: GLfloat v2[3];
117: GLfloat normal[3] = {0.0, 0.0, 0.0};
118:
119: /* for calc */
120: int newModel = 1;
121: int newGraph = 1;
122:
123: double x, y, z;
124:
125: int size = 12;
126: double size_z = 12.0;
127: double len = 3.0;
128: double z_interval = 5.0;
129:
130: double mag = 1.0;
131: double z_syukusyou = 1.0;
132:
133: double viewpoint[3] = {0.0, 0.0, 80.0};
134:
135: /* for TRIANGLE */
136: int triSwitch = 1;
137: double **triangles;
138: int tr_i;
139:
140: /* for Texuture of Triangles */
141: int te_i;
142: double **texturecoords;
143: static int exTex = 0;
144:
145: static int exSample = 1;
146:
147: static int exAxis = 1;
148:
149: static int exNet = 0
150: ;
151: /* control Expression */
152: GLboolean lightSwitch = GL_FALSE;
153: GLboolean textureSwitch = GL_FALSE;
154: GLboolean blendSwitch = GL_FALSE;
155:
156: void makeTexture(void)
157: {
158: FILE *fp;
1.2 takayama 159: int x, z;
160: char *oxhome;
161: char fname[1024];
1.1 takayama 162:
163: /* texture file open (file name is "inobuta.tga")*/
1.2 takayama 164: oxhome = getenv("OpenXM_HOME");
165: if (oxhome != NULL) {
166: sprintf(fname,"%s/share/oxmgraph/inobuta.tga",oxhome);
167: }else{
168: sprintf(fname,"/usr/local/share/oxmgraph/inobuta.tgz");
169: }
170: if((fp=fopen(fname, "rb"))==NULL){
171: if ((fp = fopen("inobuta.tgz","rb")) == NULL) {
172: fprintf(stderr, "texture file cannot be open\n");
173: return;
174: }
1.1 takayama 175: }
176: fseek(fp, 18, SEEK_SET);
177: for (x=0; x<TEX_HEIGHT; x++) {
178: for (z=0; z<TEX_WIDTH; z++) {
179: image[x][z][2]=fgetc(fp);/* B */
180: image[x][z][1]=fgetc(fp);/* G */
181: image[x][z][0]=fgetc(fp);/* R */
182: image[x][z][3]=255;/* alpha */
183: }
184: }
185: fclose(fp);
186: }
187:
188: double polynomial(double x, double y)
189: {
190: return ((x*x)-(y*y));
191: }
192:
193: double norm(double x, double y, double z)
194: {
195: return sqrt(x*x + y*y + z*z);
196: }
197:
198: void gaiseki(float vector1[], float vector2[], float vector3[])
199: {
200: vector3[0] = vector1[1]*vector2[2] - vector1[2]*vector2[1];
201: vector3[1] = vector1[2]*vector2[0] - vector1[0]*vector2[2];
202: vector3[2] = vector1[0]*vector2[1] - vector1[1]+vector2[0];
203: }
204:
205: void output(GLfloat x, GLfloat y, char *text)
206: {
207: char *p;
208:
209: glPushMatrix();
210: glTranslatef(x, y, 0);
211: for (p = text; *p; p++)
212: glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
213: glPopMatrix();
214: }
215:
216: static void graph(void)
217: {
218: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
219: glBindTexture(GL_TEXTURE_2D, texInobuta);
220:
221: for(x = -len; x <= len-DIV; x += DIV) {
222:
223: for(y = -len; y <= len-DIV; y += DIV) {
224: z = polynomial(x, y);
225:
226: if ( z >= -size_z && z <= size_z &&
227: z >= -size && z <= size ) {
228:
229: glColor3f((x+len)/(2*len), -(y-len)/(2*len), 1.0);
230: /* normal */
231: v1[0] = DIV;
232: v1[1] = 0.0;
233: v1[2] = polynomial(x+DIV, y) - polynomial(x, y);
234: v2[0] = DIV;
235: v2[1] = DIV;
236: v2[2] = polynomial(x+DIV, y+DIV) - polynomial(x, y);
237: gaiseki(v1, v2, normal);
238: glNormal3fv(normal);
239:
240: glBegin(GL_TRIANGLES);
241: glTexCoord2f((x+len)/(2*len-DIV), (y+len)/(2*len-DIV));
242: glVertex3f(x, y, polynomial(x,y));
243: glTexCoord2f((x+DIV+len)/(2*len-DIV), (y+len)/(2*len-DIV));
244: glVertex3f(x+DIV, y, polynomial(x+DIV, y));
245: glTexCoord2f((x+DIV+len)/(2*len-DIV), (y+DIV+len)/(2*len-DIV));
246: glVertex3f(x+DIV, y+DIV, polynomial(x+DIV, y+DIV));
247: glEnd();
248:
249: glBegin(GL_TRIANGLES);
250: glTexCoord2f((x+len)/(2*len-DIV), (y+DIV+len)/(2*len-DIV));
251: glVertex3f(x, y+DIV, polynomial(x, y+DIV));
252: glTexCoord2f((x+len)/(2*len-DIV), (y+len)/(2*len-DIV));
253: glVertex3f(x, y, polynomial(x,y));
254: glTexCoord2f((x+DIV+len)/(2*len-DIV), (y+DIV+len)/(2*len-DIV));
255: glVertex3f(x+DIV, y+DIV, polynomial(x+DIV, y+DIV));
256: glEnd();
257:
258: if ( exNet ) {
1.3 takayama 259: glColor3f(1.0,1.0,1.0);
260: if(!blendSwitch) {
261: glDisable(GL_LIGHTING);
262: glDisable(GL_TEXTURE_2D);
263: glColor3f(0.0,0.0,0.0);
264: }
1.1 takayama 265: glBegin(GL_LINE_LOOP);
266: glVertex3f(x, y, polynomial(x,y));
267: glVertex3f(x+DIV, y, polynomial(x+DIV, y));
268: glVertex3f(x+DIV, y+DIV, polynomial(x+DIV, y+DIV));
269: glEnd();
270: glBegin(GL_LINE_LOOP);
271: glVertex3f(x, y+DIV, polynomial(x,y+DIV));
272: glVertex3f(x, y, polynomial(x, y));
273: glVertex3f(x+DIV, y+DIV, polynomial(x+DIV, y+DIV));
274: glEnd();
1.3 takayama 275: if(lightSwitch)
276: glEnable(GL_LIGHTING);
277: if(textureSwitch)
278: glEnable(GL_TEXTURE_2D);
1.1 takayama 279: }
280: }
281: }
282: }
283: }
284:
285: void triangle(void)
286: {
287: int j;
288:
289: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
290: glBindTexture(GL_TEXTURE_2D, texInobuta);
291:
292: for (j=0; j<tr_i; j++) {
293: if(triangles[j][0] <= size && triangles[j][0] >= -size &&
294: triangles[j][3] <= size && triangles[j][3] >= -size &&
295: triangles[j][6] <= size && triangles[j][6] >= -size &&
296: triangles[j][1] <= size && triangles[j][1] >= -size &&
297: triangles[j][4] <= size && triangles[j][4] >= -size &&
298: triangles[j][7] <= size && triangles[j][7] >= -size &&
299: triangles[j][2] <= size && triangles[j][2] >= -size &&
300: triangles[j][5] <= size && triangles[j][5] >= -size &&
301: triangles[j][8] <= size && triangles[j][8] >= -size )
302: {
303: if (j % 2 == 0) {
304: v1[0] = triangles[j][3] - triangles[j][0];
305: v1[1] = triangles[j][4] - triangles[j][1];
306: v1[2] = triangles[j][5] - triangles[j][2];
307: v2[0] = triangles[j][6] - triangles[j][0];
308: v2[1] = triangles[j][7] - triangles[j][1];
309: v2[2] = triangles[j][8] - triangles[j][2];
310: }
311: gaiseki(v1, v2, normal);
312: glNormal3fv(normal);
313:
314: if (j < tr_i)
315: glColor3f((float)2*(tr_i-1.0-j)/(float)(tr_i-1.0), 0.6,
316: (float)2*j/(float)(tr_i-1.0)/*, 1.0*/);
317: else
318: glColor3f(0.6, (float)2*j/(float)(tr_i-1.0),
319: (float)2*(tr_i-1.0-j)/(float)(tr_i-1.0)/*, 1.0*/);
320:
321: glBegin(GL_TRIANGLES);
322: if (exTex == 1)
323: glTexCoord2f(texturecoords[j][0],texturecoords[j][1]);
324: glVertex3f(triangles[j][0],triangles[j][1],triangles[j][2]);
325: if (exTex == 1)
326: glTexCoord2f(texturecoords[j][2],texturecoords[j][3]);
327: glVertex3f(triangles[j][3],triangles[j][4],triangles[j][5]);
328: if (exTex == 1)
329: glTexCoord2f(texturecoords[j][4],texturecoords[j][5]);
330: glVertex3f(triangles[j][6],triangles[j][7],triangles[j][8]);
331: glEnd();
332:
333: if( exNet ) {
1.3 takayama 334: glColor3f(1.0,1.0,1.0);
335: if(!blendSwitch) {
336: glDisable(GL_LIGHTING);
337: glDisable(GL_TEXTURE_2D);
338: glColor3f(0.0,0.0,0.0);
339: }
340: glBegin(GL_LINE_LOOP);
341: glVertex3f(triangles[j][0],triangles[j][1],triangles[j][2]);
342: glVertex3f(triangles[j][3],triangles[j][4],triangles[j][5]);
343: glVertex3f(triangles[j][6],triangles[j][7],triangles[j][8]);
344: glEnd();
345: if(lightSwitch)
346: glEnable(GL_LIGHTING);
347: if(textureSwitch)
348: glEnable(GL_TEXTURE_2D);
1.1 takayama 349: }
350: }
351: }
352: }
353:
354: static void box(void)
355: {
356: double i;
357: int a = -1;
358: double line_length;
359:
360: glColor3f(1.0,1.0,1.0);
361: glBegin(GL_LINES);
362: glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size);
363: glVertex3f(size, -size, -size); glVertex3f(size, -size, size);
364: glVertex3f(size, -size, size); glVertex3f(-size, -size, size);
365: glVertex3f(-size, -size, size); glVertex3f(-size, -size, -size);
366:
367:
368: glVertex3f(-size, -size, -size); glVertex3f(-size, size, -size);
369: glVertex3f(size, -size, -size); glVertex3f(size, size, -size);
370: glVertex3f(size, -size, size); glVertex3f(size, size, size);
371: glVertex3f(-size, -size, size); glVertex3f(-size, size, size);
372:
373: glVertex3f(-size, size, -size); glVertex3f(size, size, -size);
374: glVertex3f(size, size, -size); glVertex3f(size, size, size);
375: glVertex3f(size, size, size); glVertex3f(-size, size, size);
376: glVertex3f(-size, size, size); glVertex3f(-size, size, -size);
377: glEnd();
378:
379: for ( a=-1; a<=1; a+=2) {
380: for (i = 0.0; i <= size; i += INTERVAL){
381: if ( i == 0.0 ) line_length = 1.5;
382: else line_length = 0.5;
383: glBegin(GL_LINES);
384: glVertex3f(a*i, size, size); glVertex3f(a*i, size-line_length, size);
385: glVertex3f(a*i, size, size); glVertex3f(a*i, size, size-line_length);
386: glVertex3f(size, a*i, size); glVertex3f(size-line_length, a*i, size);
387: glVertex3f(size, a*i, size); glVertex3f(size, a*i, size-line_length);
388: glVertex3f(size, size, a*i); glVertex3f(size-line_length, size, a*i);
389: glVertex3f(size, size, a*i); glVertex3f(size, size-line_length, a*i);
390:
391: glVertex3f(a*i, -size, size); glVertex3f(a*i, -(size-line_length), size);
392: glVertex3f(a*i, -size, size); glVertex3f(a*i, -size, size-line_length);
393: glVertex3f(size, a*i, -size); glVertex3f(size-line_length, a*i, -size);
394: glVertex3f(size, a*i, -size); glVertex3f(size, a*i, -(size-line_length));
395: glVertex3f(-size, size, a*i); glVertex3f(-size, size-line_length, a*i);
396: glVertex3f(-size, size, a*i); glVertex3f(-(size-line_length), size, a*i);
397:
398: glVertex3f(a*i, size, -size); glVertex3f(a*i, size-line_length, -size);
399: glVertex3f(a*i, size, -size); glVertex3f(a*i, size, -(size-line_length));
400: glVertex3f(-size, a*i, size); glVertex3f(-(size-line_length), a*i, size);
401: glVertex3f(-size, a*i, size); glVertex3f(-size, a*i, size-line_length);
402: glVertex3f(size, -size, a*i); glVertex3f(size, -(size-line_length), a*i);
403: glVertex3f(size, -size, a*i); glVertex3f(size-line_length, -size, a*i);
404:
405: glVertex3f(a*i, -size, -size); glVertex3f(a*i, -(size-line_length), -size);
406: glVertex3f(a*i, -size, -size); glVertex3f(a*i, -size, -(size-line_length));
407: glVertex3f(-size, a*i, -size); glVertex3f(-(size-line_length), a*i, -size);
408: glVertex3f(-size, a*i, -size); glVertex3f(-size, a*i, -(size-line_length));
409: glVertex3f(-size, -size, a*i); glVertex3f(-size, -(size-line_length), a*i);
410: glVertex3f(-size, -size, a*i); glVertex3f(-(size-line_length), -size, a*i);
411: glEnd();
412: }
413: }
414: }
415:
416: static void axis(void)
417: {
418: double i;
419: glBegin(GL_LINES);
420: glColor3f(1.0, 0.0, 0.0);
421: glVertex3f(-size, 0, 0); glVertex3f(size, 0, 0);
422:
423: glColor3f(0.0, 0.0, 1.0);
424: glVertex3f(0, -size, 0); glVertex3f(0, size, 0);
425:
426: glColor3f(1.0, 1.0, 0.0);
427: glVertex3f(0, 0, -size); glVertex3f(0, 0, size);
428: glEnd();
429: glLineWidth(1.0);
430:
431: glColor3f(1.0, 0.0, 0.0);
432: glRasterPos3i(size+1,0,0);
433: glBitmap (10, 12, 0.0, 0.0, 11.0, 0.0, raster_X);
434:
435: glColor3f(0.0, 0.0, 1.0);
436: glRasterPos3i(0,size+1,0);
437: glBitmap (10, 12, 0.0, 0.0, 11.0, 0.0, raster_Y);
438:
439: glColor3f(1.0, 1.0, 0.0);
440: glRasterPos3i(0,0,size+1);
441: glBitmap (10, 12, 0.0, 0.0, 11.0, 0.0, raster_Z);
442:
443: glColor3f(1.0, 1.0, 1.0);
444: glRasterPos3i(5,1,0);
445: glBitmap (10, 22, 0.0, 0.0, 11.0, 0.0, raster_$);
446:
447: glColor3f(1.0, 1.0, 1.0);
448: glRasterPos3i(1,0,z_interval);
449: glBitmap (10, 20, 0.0, 0.0, 11.0, 0.0, raster_at);
450:
451: for (i = 0.0; i <= size; i += INTERVAL){
452: glBegin(GL_LINES);
453: glVertex3f(i, 0, 0); glVertex3f(i, 0.5, 0);
454: glVertex3f(i, 0, 0); glVertex3f(i, 0, 0.5);
455: glVertex3f(0, i, 0); glVertex3f(0.5, i, 0);
456: glVertex3f(0, i, 0); glVertex3f(0, i, 0.5);
457:
458: glVertex3f(-i, 0, 0); glVertex3f(-i, 0.5, 0);
459: glVertex3f(-i, 0, 0); glVertex3f(-i, 0, 0.5);
460: glVertex3f(0, -i, 0); glVertex3f(0.5, -i, 0);
461: glVertex3f(0, -i, 0); glVertex3f(0, -i, 0.5);
462:
463: glEnd();
464: }
465: for (i = 0.0; i <= size; i += z_interval){
466: glBegin(GL_LINES);
467: glVertex3f(0, 0, i); glVertex3f(0.5, 0, i);
468: glVertex3f(0, 0, i); glVertex3f(0, 0.5, i);
469:
470: glVertex3f(0, 0, -i); glVertex3f(0.5, 0, -i);
471: glVertex3f(0, 0, -i); glVertex3f(0, 0.5, -i);
472: glEnd();
473: }
474: }
475:
476: static void recalcGraph(void)
477: {
478: theBox = glGenLists(1);
479: glNewList(theBox, GL_COMPILE);
480: box();
481: glEndList();
482:
483: theAxis = glGenLists(1);
484: glNewList(theAxis, GL_COMPILE);
485: axis();
486: glEndList();
487:
488: theGraph = glGenLists(1);
489: glNewList(theGraph, GL_COMPILE);
490: graph();
491: glEndList();
492:
493: theTriangle = glGenLists(1);
494: glNewList(theTriangle, GL_COMPILE);
495: triangle();
496: glEndList();
497:
1.3 takayama 498:
499: glCallList(theAxis);
500: glCallList(theBox);
501:
1.1 takayama 502: newGraph = 0;
503: }
504:
505: static void init(void)
506: {
507: static GLfloat amb[] = {0.4, 0.4, 0.4, 0.0};
508: static GLfloat dif[] = {1.0, 1.0, 1.0, 0.0};
509:
510: glClearColor(0.2, 0.2, 0.2, 0.0);
511: glShadeModel(GL_FLAT);
512: glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
1.3 takayama 513: /*
1.1 takayama 514: glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
515: */
516: glMatrixMode(GL_PROJECTION);
517: gluPerspective(40.0, 1.0, 1.0, 500.0);
518: glMatrixMode(GL_MODELVIEW);
519: gluLookAt(0.0, 0.0, 80.0,0.0, 0.0, 0.0, 0.0, 1.0, 0.);
520: glPushMatrix();
521:
522: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
523: glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif);
524:
525: glEnable(GL_DEPTH_TEST);
526: glBlendFunc(GL_ONE, GL_ONE);
527: }
528:
529: void initTexture(void)
530: {
531: makeTexture();
532: glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
533:
534: glGenTextures(1, &texInobuta);
535: glBindTexture(GL_TEXTURE_2D, texInobuta);
536:
537: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
538: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
539: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
540: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
541: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH,
542: TEX_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
543: }
544:
545: void
546: recalcModelView(void)
547: {
548: glPopMatrix();
549:
550: glPushMatrix();
551: glRotatef(angle[0], 0.0, 1.0, 0.0);
552: glRotatef(angle[1], 1.0, 0.0, 0.0);
553: glScalef(mag, mag, mag);
554: newModel = 0;
555: }
556:
557: void display(void)
558: {
559: if (newModel)
560: recalcModelView();
561: if (newGraph)
562: recalcGraph();
563:
564: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
565:
566: glPushMatrix();
567: glScalef(1.0,1.0,z_syukusyou);
568: if(triSwitch == 0)
569: glCallList(theGraph);
570: else
571: glCallList(theTriangle);
572: glPopMatrix();
573:
574: glDisable(GL_LIGHTING);
575: glDisable(GL_TEXTURE_2D);
576:
577: glCallList(theBox);
578: if(exAxis)
579: glCallList(theAxis);
580:
581: if(lightSwitch)
582: glEnable(GL_LIGHTING);
583: if(textureSwitch)
584: glEnable(GL_TEXTURE_2D);
585:
586: glutSwapBuffers();
587:
588: }
589:
590: void reshape(int w, int h)
591: {
592: glViewport(0,0, (GLsizei)w, (GLsizei)h);
593: glMatrixMode(GL_PROJECTION);
594: glLoadIdentity();
595: gluPerspective(50.0, (double)w/(double)h, 1.0, 1000.0);
596: glMatrixMode(GL_MODELVIEW);
597: }
598:
599:
600: void mouse(int button, int state, int x, int y)
601: {
602: if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
603: moving = 1;
604: begin[0] = x;
605: begin[1] = y;
606: }
607: if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
608: moving = 0;
609: }
610: }
611:
612: void motion(int x, int y)
613: {
614: if (moving) {
615: angle[0] = angle[0] + (x - begin[0]);
616: angle[1] = angle[1] + (y - begin[1]);
617: begin[0] = x;
618: begin[1] = y;
619: newModel = 1;
620: glutPostRedisplay();
621: }
622: }
623:
624: void keyboard(unsigned char key, int x, int y)
625: {
626: switch (key) {
627: case 's':
628: size += BOXSIZE;
629: size_z += BOXSIZE;
630: newGraph = 1;
631: glutPostRedisplay();
632: break;
633: case 'S':
634: size -= BOXSIZE;
635: size_z -= BOXSIZE;
636: if ( size < 0.0 )
637: size = 0.0;
638: newGraph = 1;
639: glutPostRedisplay();
640: break;
641: case 'l':
642: if (triSwitch == 0) {
643: len += FUNCLEN;
644: newGraph = 1;
645: glutPostRedisplay();
646: }
647: break;
648: case 'L':
649: if (triSwitch == 0) {
650: len -= FUNCLEN;
651: if ( len < 0.0 )
652: len = 0.0;
653: newGraph = 1;
654: glutPostRedisplay();
655: }
656: break;
657: case 'm':
658: mag *= MAG;
659: newModel = 1;
660: glutPostRedisplay();
661: break;
662: case 'M':
663: mag /= MAG;
664: newModel = 1;
665: glutPostRedisplay();
666: break;
667: case 'z':
668: z_syukusyou *= MAG;
669: z_interval *= MAG;
670: size_z /= MAG;
671: newModel = 1;
672: newGraph = 1;
673: glutPostRedisplay();
674: break;
675: case 'Z':
676: z_syukusyou /= MAG;
677: z_interval /= MAG;
678: size_z *= MAG;
679: newModel = 1;
680: newGraph = 1;
681: glutPostRedisplay();
682: break;
683: case 'i':
684: case 'I':
685: mag = 1.0;
686: z_syukusyou = 1.0;
687: z_interval = 5.0;
688: size = 12;
689: size_z = 12.0;
690: len = 3.0;
691: /*
692: angle[0] = 0.0;
693: angle[1] = 0.0;
694: */
695: newModel = 1;
696: newGraph = 1;
697: glutPostRedisplay();
698: break;
699: case 'q':
700: case 'Q':
701: exit(0);
702: break;
703: }
704: }
705:
706: GLboolean netSwitch = GL_FALSE;
707: void controlExpression(int value)
708: {
709: switch (value) {
710: case 1:
711: lightSwitch = 0;
712: textureSwitch = 0;
713: glDisable(GL_LIGHTING);
714: glDisable(GL_TEXTURE_2D);
715: newModel = 1;
1.3 takayama 716: newGraph = 1;
1.1 takayama 717: glutPostRedisplay();
718: break;
719: case 2:
720: lightSwitch = 1;
721: textureSwitch = 0;
722: glEnable(GL_LIGHTING);
723: glDisable(GL_TEXTURE_2D);
724: newModel = 1;
1.3 takayama 725: newGraph = 1;
1.1 takayama 726: glutPostRedisplay();
727: break;
728: case 3:
729: textureSwitch = 1;
730: lightSwitch = 0;
731: glEnable(GL_TEXTURE_2D);
732: glDisable(GL_LIGHTING);
733: newModel = 1;
1.3 takayama 734: newGraph = 1;
1.1 takayama 735: glutPostRedisplay();
736: break;
737: case 4:
738: blendSwitch = !blendSwitch;
739: if (blendSwitch) {
740: glEnable(GL_BLEND);
741: glDisable(GL_DEPTH_TEST);
742: }
743: else {
744: glDisable(GL_BLEND);
745: glEnable(GL_DEPTH_TEST);
746: }
1.3 takayama 747: newGraph = 1;
1.1 takayama 748: break;
749: case 5:
750: exAxis = !exAxis;
751: break;
752: case 6:
753: exNet = !exNet;
754: newGraph = 1;
755: break;
756: case 7:
757: triSwitch = !triSwitch;
758: break;
759: case 8:
760: netSwitch = !netSwitch;
761: if ( netSwitch )
762: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
763: else
764: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
765: newGraph = 1;
766: break;
767: case 9:
768: mag *= MAG;
769: newModel = 1;
770: glutPostRedisplay();
771: break;
772: case 10:
773: mag /= MAG;
774: newModel = 1;
775: glutPostRedisplay();
776: break;
777: case 11:
778: size += BOXSIZE;
779: size_z += BOXSIZE;
780: newGraph = 1;
781: glutPostRedisplay();
782: break;
783: case 12:
784: size -= BOXSIZE;
785: size_z -= BOXSIZE;
786: if ( size < 0.0 )
787: size = 0.0;
788: newGraph = 1;
789: glutPostRedisplay();
790: break;
791: case 13:
792: z_syukusyou *= MAG;
793: z_interval *= MAG;
794: size_z /= MAG;
795: newModel = 1;
796: newGraph = 1;
797: glutPostRedisplay();
798: break;
799: case 14:
800: z_syukusyou /= MAG;
801: z_interval /= MAG;
802: size_z *= MAG;
803: newModel = 1;
804: newGraph = 1;
805: glutPostRedisplay();
806: break;
807: case 16:
808: mag = 1.0;
809: z_syukusyou = 1.0;
810: z_interval = 5.0;
811: size = 12;
812: size_z = 12.0;
813: /*
814: angle[0] = 0.0;
815: angle[1] = 0.0;
816: */
817: len = 3.0;
818: newModel = 1;
819: newGraph = 1;
820: glutPostRedisplay();
821: break;
822: case 15:
823: exit(0);
824: break;
825: }
826: glutPostRedisplay();
827: }
828:
829: int main(int argc, char** argv)
830: {
831: FILE *fp;
832: char com[LINE_MAX+1];
833: double x1,y1,z1;
834: double x2,y2,z2;
835: double x3,y3,z3;
836: double tex_x1,tex_y1;
837: double tex_x2,tex_y2;
838: double tex_x3,tex_y3;
839: int limit1 = LIMIT;
840: int limit2 = LIMIT;
841: double **t1;
842: double **t2;
843: double *tr;
844: double *te;
845:
846: if (argc == 1) {
847: triSwitch = 0;
848: exTex = 1;
849: exSample = 0;
1.3 takayama 850: printf("$=5.0, @=5.0\n");
1.1 takayama 851: }
852: else if (argc != 2) {
853: fprintf(stderr,"Usage: graph filename\n");
854: }
855: else {
856: triangles = (double **) malloc(sizeof(double *)*limit1);
857: texturecoords = (double **) malloc(sizeof(double *)*limit2);
858:
859: if (triangles == NULL)
860: outOfMemory();
861: if (texturecoords == NULL)
862: outOfMemory();
863:
864: fp = fopen(argv[1],"r");
865:
866: if (fp == NULL) {
867: fprintf(stderr,"The file %s is not found.\n",argv[1]);
868: return -1;
869: }
870:
871: tr_i = 0;
872: te_i = 0;
873:
874: while (fgets(com,LINE_MAX,fp) != NULL) {
875: if (com[0] <= ' ')
876: continue;
877:
878: switch (com[0]) {
879: case '#': /* comment line */
880: break;
881:
882: case 't': /* read data for a triangle. */
883: sscanf(&(com[1]),"%lf %lf %lf %lf %lf %lf %lf %lf %lf",
884: &x1,&y1,&z1,&x2,&y2,&z2,&x3,&y3,&z3);
885: tr = (double *) malloc(sizeof(double)*9);
886: if (tr == NULL)
887: outOfMemory();
888: tr[0] = x1; tr[1] = y1; tr[2] = z1;
889: tr[3] = x2; tr[4] = y2; tr[5] = z2;
890: tr[6] = x3; tr[7] = y3; tr[8] = z3;
891:
892: triangles[tr_i] = tr;
893: tr_i++;
894:
895: if (tr_i >= limit1) {
896: t1 = (double **) malloc(sizeof(double *)*limit1*2);
897: if (t1 == NULL) outOfMemory();
898: for (tr_i=0; tr_i<limit1; tr_i++)
899: t1[tr_i] = triangles[tr_i];
900: free(triangles);
901: triangles=t1;
902: limit1 = limit1*2;
903: }
904: break;
905:
906: case 'x': /* read data for a texture coordinate. */
907: sscanf(&(com[1]),"%lf %lf %lf %lf %lf %lf",
908: &tex_x1, &tex_y1, &tex_x2, &tex_y2, &tex_x3, &tex_y3);
909: te = (double *) malloc(sizeof(double)*6);
910: exTex = 1;
911: if (te == NULL)
912: outOfMemory();
913: te[0] = tex_x1; te[1] = tex_y1;
914: te[2] = tex_x2; te[3] = tex_y2;
915: te[4] = tex_x3; te[5] = tex_y3;
916:
917: texturecoords[te_i] = te;
918: te_i++;
919:
920: if (te_i >= limit2) {
921: t2 = (double **) malloc(sizeof(double *)*limit2*2);
922: if (t2 == NULL) outOfMemory();
923: for (te_i=0; te_i<limit2; te_i++)
924: t2[te_i] = texturecoords[te_i];
925: free(texturecoords);
926: texturecoords=t2;
927: limit2 = limit2*2;
928: }
929: break;
930:
931: default:
932: fprintf(stderr,"Undefined command : %s \n",com);
933: return -1;
934: }
935: }
936: }
937: glutInit(&argc, argv);
938: glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DOUBLE |
939: GLUT_DEPTH | GLUT_MULTISAMPLE);
1.5 ! ohara 940: #if defined(__APPLE__)
1.1 takayama 941: glutInitWindowSize(512, 512);
942: #else
943: glutInitWindowSize(500, 500);
944: #endif
945: glutCreateWindow(argv[0]);
946: init();
947: initTexture();
948: glutReshapeFunc(reshape);
949: glutDisplayFunc(display);
950: glutMouseFunc(mouse);
951: glutMotionFunc(motion);
952: glutKeyboardFunc(keyboard);
953: glutCreateMenu(controlExpression);
954: glutAddMenuEntry("Default", 1);
955: glutAddMenuEntry("Add Light", 2);
956: if ( exTex )
957: glutAddMenuEntry("Add Texture", 3);
958: glutAddMenuEntry("Turn on/off Translucent Mode", 4);
959: glutAddMenuEntry("Turn on/off Wireframe Mode", 8);
960: glutAddMenuEntry("Hide/Show Axis", 5);
961: glutAddMenuEntry("Hide/Show Net", 6);
962: glutAddMenuEntry("Magnify (m)", 9);
963: glutAddMenuEntry("Reduce (M)", 10);
964: glutAddMenuEntry("Make the Box Larger (s)", 11);
965: glutAddMenuEntry("Make the Box Smaller (S)", 12);
966: glutAddMenuEntry("Magnify z Direction (z)", 13);
967: glutAddMenuEntry("Reduce z Direction (Z)", 14);
968: glutAddMenuEntry("Initialize (i)", 16);
969: if ( exSample ) {
970: glutAddMenuEntry("Change Sample/Data Graph", 7);
971: }
972: glutAddMenuEntry("Quit (q)", 15);
973: glutAttachMenu(GLUT_RIGHT_BUTTON);
974:
975: glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
976: glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
977: glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
978: glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
979: glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
980: glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
981: glLightfv(GL_LIGHT3, GL_POSITION, light3_position);
982: glLightfv(GL_LIGHT3, GL_DIFFUSE, light3_diffuse);
983: glLightfv(GL_LIGHT4, GL_POSITION, light4_position);
984: glLightfv(GL_LIGHT4, GL_DIFFUSE, light4_diffuse);
985: glLightfv(GL_LIGHT5, GL_POSITION, light5_position);
986: glLightfv(GL_LIGHT5, GL_DIFFUSE, light5_diffuse);
987:
988: glEnable(GL_LIGHT0);
989: glEnable(GL_LIGHT1);
1.3 takayama 990:
1.1 takayama 991: glutMainLoop();
992: return 0;
993: }
994:
995:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>