Advertisement
sunaoshibata

再現回転移動拡大縮小1/29完成☆

Jan 29th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.17 KB | None | 0 0
  1. //#include  <GLUT/glut.h>  //for mac
  2. //buffer : angle_b
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <GL/glut.h>
  8. #include <time.h>
  9. FILE *fpr;
  10.  
  11. static float g_angle = 0.0f;
  12. static float sec;
  13. static float *angle_b;
  14. static float xtrans=0.0f,ytrans=0.0f;
  15. static float sscale=1.0f;
  16. int g_pressed = 0;
  17. int i=0;
  18. int flag;
  19. int*flag_m;
  20. unsigned int timer;
  21. float timer1,timer2,timer3;
  22. int g_prevX = -1;
  23. float value;
  24. int num;
  25. clock_t passage;
  26.  
  27. void output_scan(FILE *fp,float *store,int *flag_kana);
  28.  
  29. void reshape(int width, int height)
  30. {
  31.    
  32.     static GLfloat lightPosition[4] = {0.25f, 1.0f, 0.25f, 0.0f};
  33.     static GLfloat lightDiffuse[3] = {1.0f, 1.0f, 1.0f};
  34.     static GLfloat lightAmbient[3] = {0.25f, 0.25f, 0.25f};
  35.     static GLfloat lightSpecular[3] = {1.0f, 1.0f, 1.0f};
  36.  
  37.     glEnable(GL_LIGHTING);  
  38.     glEnable(GL_LIGHT0);
  39.     glShadeModel(GL_SMOOTH);
  40.     glViewport(0, 0, width, height);
  41.     glMatrixMode(GL_PROJECTION);
  42.     glLoadIdentity();
  43.     gluPerspective(45.0, (double)width / (double)height, 0.1, 100.0);
  44.     glMatrixMode(GL_MODELVIEW);
  45.    
  46.     glLoadIdentity();
  47.     gluLookAt(0.5, 1.5, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  48.    
  49.     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  50.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
  51.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  52.     glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
  53. }
  54.  
  55. void display()
  56. {  
  57.     if(i<num)
  58.     {
  59.         i=i+1;
  60.     }
  61.     else
  62.     {
  63.         i=0;
  64.    
  65.     }
  66.  
  67.     static GLfloat vertices [8][3] =
  68.     {
  69.         {-0.5f, -0.5f,  0.5f},
  70.         { 0.5f, -0.5f,  0.5f},
  71.         { 0.5f,  0.5f,  0.5f},
  72.         {-0.5f,  0.5f,  0.5f},
  73.         { 0.5f, -0.5f, -0.5f},
  74.         {-0.5f, -0.5f, -0.5f},
  75.         {-0.5f,  0.5f, -0.5f},
  76.         { 0.5f,  0.5f, -0.5f}
  77.     };
  78.  
  79.     static GLfloat normals[6][3] =
  80.     {
  81.         { 0.0f,  0.0f,  1.0f},
  82.         { 0.0f,  0.0f, -1.0f},
  83.         { 1.0f,  0.0f,  0.0f},
  84.         {-1.0f,  0.0f,  0.0f},
  85.         { 0.0f,  1.0f,  0.0f},
  86.         { 0.0f, -1.0f,  0.0f}
  87.     };
  88.    
  89.     static GLfloat diffuse[3] = {1.0f, 0.0f, 0.0f};
  90.     static GLfloat ambient[3] = {0.25f, 0.25f, 0.25f};
  91.     static GLfloat specular[3] = {1.0f, 1.0f, 1.0f};
  92.     static GLfloat shininess[1] = {32.0f};
  93.    
  94.     glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
  95.     glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
  96.     glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
  97.     glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
  98.     glEnable(GL_DEPTH_TEST);
  99.    
  100.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  101.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  102.     glPushMatrix();
  103.     glTranslatef(xtrans, ytrans,0. );
  104.     glRotatef(g_angle, 0.0f, 1.0f, 0.0f);
  105.     glScalef(sscale,sscale,sscale);
  106.     //printf("x=%f\n",xtrans);
  107.     //printf("y=%f\n",ytrans);
  108.     //printf("z=%f\n",sscale);
  109.    
  110.     // 前
  111.    
  112.     glBegin(GL_POLYGON);
  113.         glNormal3fv(normals[0]);
  114.         glVertex3fv(vertices[0]);
  115.         glVertex3fv(vertices[1]);
  116.         glVertex3fv(vertices[2]);
  117.         glVertex3fv(vertices[3]);
  118.    
  119.     glEnd();
  120.    
  121.     // 後
  122.    
  123.     glBegin(GL_POLYGON);
  124.         glNormal3fv(normals[1]);
  125.         glVertex3fv(vertices[4]);
  126.         glVertex3fv(vertices[5]);
  127.         glVertex3fv(vertices[6]);
  128.         glVertex3fv(vertices[7]);
  129.     glEnd();
  130.    
  131.     // 右
  132.     glBegin(GL_POLYGON);
  133.         glNormal3fv(normals[2]);
  134.         glVertex3fv(vertices[1]);
  135.         glVertex3fv(vertices[4]);
  136.         glVertex3fv(vertices[7]);
  137.         glVertex3fv(vertices[2]);
  138.    
  139.     glEnd();
  140.    
  141.     // 左
  142.    
  143.     glBegin(GL_POLYGON);
  144.         glNormal3fv(normals[3]);
  145.         glVertex3fv(vertices[5]);
  146.         glVertex3fv(vertices[0]);
  147.         glVertex3fv(vertices[3]);
  148.         glVertex3fv(vertices[6]);
  149.     glEnd();
  150.  
  151.     // 上
  152.    
  153.     glBegin(GL_POLYGON);
  154.         glNormal3fv(normals[4]);
  155.         glVertex3fv(vertices[3]);
  156.         glVertex3fv(vertices[2]);
  157.         glVertex3fv(vertices[7]);
  158.         glVertex3fv(vertices[6]);
  159.    
  160.     glEnd();
  161.  
  162.     // 下
  163.    
  164.     glBegin(GL_POLYGON);
  165.         glNormal3fv(normals[5]);
  166.         glVertex3fv(vertices[1]);
  167.         glVertex3fv(vertices[0]);
  168.         glVertex3fv(vertices[5]);
  169.         glVertex3fv(vertices[4]);
  170.     glEnd();
  171.    
  172.     glPopMatrix();
  173.    
  174.    
  175.     glutSwapBuffers();
  176.    
  177. }
  178.  
  179.  
  180.  
  181. void time(int value)
  182. {
  183.     //printf("y=%f\n",timer);
  184.     //timer1=angle_b[7*i]*CLOCKS_PER_SEC;
  185.     printf("i=%d\n",i);
  186.     //timer1=angle_b[7*i];
  187.     //timer2=angle_b[7*i+1];
  188.     //timer3=angle_b[7*i+2];
  189.     timer1=angle_b[7*i]*CLOCKS_PER_SEC/100;
  190.     timer2=angle_b[7*i+1]*CLOCKS_PER_SEC/100;
  191.     timer3=angle_b[7*i+2]*CLOCKS_PER_SEC/100;
  192.     g_angle=angle_b[7*i+3];
  193.     xtrans=angle_b[7*i+4];
  194.     ytrans=angle_b[7*i+5];
  195.     sscale=angle_b[7*i+6];
  196.     flag=flag_m[i];
  197.     //printf("x=%f\n",timer1);
  198.     //printf("y=%f\n",timer2);
  199.     //printf("z=%f\n",timer3);
  200.     //printf("h=%f\n",g_angle);
  201.     //printf("u=%f\n",xtrans);
  202.     //printf("p=%f\n",ytrans);
  203.     //printf("o=%f\n",sscale);
  204.     //printf("t=%d\n",flag);
  205.     if(flag==1)
  206.     {
  207.             timer=timer1;
  208.     }
  209.     else if(flag==2)
  210.     {
  211.             timer=timer2;
  212.     }
  213.     else if(flag==3)
  214.     {
  215.             timer=timer3;
  216.     }
  217.     else
  218.     {
  219.             timer=0;
  220.     }
  221.     printf("timer=%d\n",timer);
  222.     glutPostRedisplay();
  223.     //glutTimerFunc(timer,time,value);
  224.     glutTimerFunc(timer,time,0);
  225.    
  226. }
  227.  
  228.  
  229. int main(int argc, char* argv[])
  230. {
  231.    
  232.    
  233.     float x,y;
  234.  
  235.  
  236.     //int num;
  237.  
  238.     if(argc!=3)
  239.     {
  240.         fprintf(stderr,"Usage: %s (1)txtFile\n",argv[0]);
  241.             return 0;
  242.     }
  243.  
  244.     //Open VTK file for save
  245.     if((fpr=fopen(argv[1],"r"))==NULL)
  246.     {
  247.         printf("The file can't be opened. The program is exit.\n");
  248.  
  249.         return 0;
  250.     }
  251.    
  252.     num=atoi(argv[2]);
  253.    
  254.     //Load angle buffer
  255.     angle_b=(float *)malloc(7*num*sizeof(float));
  256.     flag_m=(int *)malloc(num*sizeof(int));
  257.  
  258.     output_scan(fpr,angle_b,flag_m);
  259.    
  260.     glutInit(&argc, argv);
  261.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  262.     glutInitWindowPosition(100, 100);
  263.     glutInitWindowSize(640, 480);
  264.     glutCreateWindow("Mouse Motion Cube");
  265.     glutReshapeFunc(reshape);
  266.     //glutMotionFunc(motion);
  267.     glutDisplayFunc(display);
  268.     //glutTimerFunc(0.1,time, 0);
  269.     glutTimerFunc(timer,time, 0);
  270.     glutMainLoop();
  271.  
  272.     fclose(fpr);
  273. }
  274. void output_scan(FILE *fp,float *store,int *flag_kana)
  275. {
  276.    
  277.     float a,b,c,d,e,f,g;
  278.     int h;
  279.     int count=0;
  280.  
  281.     while(fscanf(fpr,"%f %f %f %f %f %f %f %d",&a,&b,&c,&d,&e,&f,&g,&h)!=EOF)
  282.     {
  283.  
  284.         store[count*7]=a;
  285.         store[count*7+1]=b;
  286.         store[count*7+2]=c;
  287.         store[count*7+3]=d;
  288.         store[count*7+4]=e;
  289.         store[count*7+5]=f;
  290.         store[count*7+6]=g;
  291.         printf("a=%f\n",a);
  292.         printf("count=%f\n",store[count*7]);
  293.         flag_kana[count]=h;
  294.         count=count+1;
  295.  
  296.     }
  297.    
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement