[BACK]Return to graph.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / oxmgraph

Annotation of OpenXM/src/oxmgraph/graph.c, Revision 1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>