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

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

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

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