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

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

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

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