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