ait-survey

mouse position output

Sep 7th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.34 KB | None | 0 0
  1. //#include  <GLUT/glut.h>  //for mac
  2. #include <GL/glut.h>
  3. #include <stdio.h>
  4.  
  5. FILE *fpo;
  6.  
  7. float g_angle = 0.0f;
  8. int g_pressed = 0;
  9. int g_prevX = -1;
  10.  
  11.  
  12. void mouse(int button, int state, int x, int y)
  13. {
  14.     if (button == GLUT_LEFT_BUTTON)
  15.     {
  16.        
  17.         if (state == GLUT_DOWN)
  18.         {
  19.             g_prevX = x;
  20.             g_pressed = 1;
  21.            
  22.         }
  23.        
  24.         else if (state == GLUT_UP)
  25.         {
  26.             g_pressed = 0;
  27.         }
  28.     }
  29. }
  30.  
  31.  
  32.  
  33. void motion(int x, int y)
  34. {
  35.     if (g_pressed == 1)
  36.         {
  37.        
  38.         g_angle += (float)(x - g_prevX);
  39.        
  40.         if (g_angle > 360.0f)
  41.         {
  42.            
  43.             g_angle -= 360.0f;
  44.            
  45.         }
  46.        
  47.         g_prevX = x;
  48.         glutPostRedisplay();
  49.        
  50.     }
  51.      printf("x=%d,y=%d\n",x,y);  
  52.      fprintf(fpo,"%d,%d \n",x,y);
  53.    
  54. }
  55.  
  56.  
  57.  
  58. void reshape(int width, int height)
  59.  
  60. {
  61.    
  62.     static GLfloat lightPosition[4] = {0.25f, 1.0f, 0.25f, 0.0f};
  63.    
  64.     static GLfloat lightDiffuse[3] = {1.0f, 1.0f, 1.0f};
  65.    
  66.     static GLfloat lightAmbient[3] = {0.25f, 0.25f, 0.25f};
  67.    
  68.     static GLfloat lightSpecular[3] = {1.0f, 1.0f, 1.0f};
  69.    
  70.    
  71.    
  72.     glEnable(GL_LIGHTING);
  73.    
  74.     glEnable(GL_LIGHT0);
  75.    
  76.    
  77.    
  78.     glShadeModel(GL_SMOOTH);
  79.    
  80.    
  81.    
  82.     glViewport(0, 0, width, height);
  83.    
  84.    
  85.    
  86.     glMatrixMode(GL_PROJECTION);
  87.    
  88.     glLoadIdentity();
  89.    
  90.     gluPerspective(45.0, (double)width / (double)height, 0.1, 100.0);
  91.    
  92.    
  93.    
  94.     glMatrixMode(GL_MODELVIEW);
  95.    
  96.     glLoadIdentity();
  97.    
  98.     gluLookAt(0.5, 1.5, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  99.    
  100.    
  101.    
  102.     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  103.    
  104.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
  105.    
  106.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  107.    
  108.     glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
  109.    
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. void display()
  117.  
  118. {
  119.    
  120.     static GLfloat vertices [8][3] =
  121.    
  122.     {
  123.        
  124.         {-0.5f, -0.5f,  0.5f},
  125.        
  126.         { 0.5f, -0.5f,  0.5f},
  127.        
  128.         { 0.5f,  0.5f,  0.5f},
  129.        
  130.         {-0.5f,  0.5f,  0.5f},
  131.        
  132.         { 0.5f, -0.5f, -0.5f},
  133.        
  134.         {-0.5f, -0.5f, -0.5f},
  135.        
  136.         {-0.5f,  0.5f, -0.5f},
  137.        
  138.         { 0.5f,  0.5f, -0.5f}
  139.        
  140.     };
  141.    
  142.    
  143.    
  144.     static GLfloat normals[6][3] =
  145.    
  146.     {
  147.        
  148.         { 0.0f,  0.0f,  1.0f},
  149.        
  150.         { 0.0f,  0.0f, -1.0f},
  151.        
  152.         { 1.0f,  0.0f,  0.0f},
  153.        
  154.         {-1.0f,  0.0f,  0.0f},
  155.        
  156.         { 0.0f,  1.0f,  0.0f},
  157.        
  158.         { 0.0f, -1.0f,  0.0f}
  159.        
  160.     };
  161.    
  162.    
  163.    
  164.     static GLfloat diffuse[3] = {1.0f, 0.0f, 0.0f};
  165.    
  166.     static GLfloat ambient[3] = {0.25f, 0.25f, 0.25f};
  167.    
  168.     static GLfloat specular[3] = {1.0f, 1.0f, 1.0f};
  169.    
  170.     static GLfloat shininess[1] = {32.0f};
  171.    
  172.    
  173.    
  174.     glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
  175.    
  176.     glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
  177.    
  178.     glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
  179.    
  180.     glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
  181.    
  182.    
  183.    
  184.     glEnable(GL_DEPTH_TEST);
  185.    
  186.    
  187.    
  188.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  189.    
  190.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  191.    
  192.    
  193.    
  194.     glPushMatrix();
  195.    
  196.     glRotatef(g_angle, 0.0f, 1.0f, 0.0f);
  197.    
  198.    
  199.    
  200.     // 前
  201.    
  202.     glBegin(GL_POLYGON);
  203.    
  204.     glNormal3fv(normals[0]);
  205.    
  206.     glVertex3fv(vertices[0]);
  207.    
  208.     glVertex3fv(vertices[1]);
  209.    
  210.     glVertex3fv(vertices[2]);
  211.    
  212.     glVertex3fv(vertices[3]);
  213.    
  214.     glEnd();
  215.    
  216.    
  217.    
  218.     // 後
  219.    
  220.     glBegin(GL_POLYGON);
  221.    
  222.     glNormal3fv(normals[1]);
  223.    
  224.     glVertex3fv(vertices[4]);
  225.    
  226.     glVertex3fv(vertices[5]);
  227.    
  228.     glVertex3fv(vertices[6]);
  229.    
  230.     glVertex3fv(vertices[7]);
  231.    
  232.     glEnd();
  233.    
  234.    
  235.    
  236.     // 右
  237.    
  238.     glBegin(GL_POLYGON);
  239.    
  240.     glNormal3fv(normals[2]);
  241.    
  242.     glVertex3fv(vertices[1]);
  243.    
  244.     glVertex3fv(vertices[4]);
  245.    
  246.     glVertex3fv(vertices[7]);
  247.    
  248.     glVertex3fv(vertices[2]);
  249.    
  250.     glEnd();
  251.    
  252.    
  253.    
  254.     // 左
  255.    
  256.     glBegin(GL_POLYGON);
  257.    
  258.     glNormal3fv(normals[3]);
  259.    
  260.     glVertex3fv(vertices[5]);
  261.    
  262.     glVertex3fv(vertices[0]);
  263.    
  264.     glVertex3fv(vertices[3]);
  265.    
  266.     glVertex3fv(vertices[6]);
  267.    
  268.     glEnd();
  269.    
  270.    
  271.    
  272.     // 上
  273.    
  274.     glBegin(GL_POLYGON);
  275.    
  276.     glNormal3fv(normals[4]);
  277.    
  278.     glVertex3fv(vertices[3]);
  279.    
  280.     glVertex3fv(vertices[2]);
  281.    
  282.     glVertex3fv(vertices[7]);
  283.    
  284.     glVertex3fv(vertices[6]);
  285.    
  286.     glEnd();
  287.    
  288.    
  289.    
  290.     // 下
  291.    
  292.     glBegin(GL_POLYGON);
  293.    
  294.     glNormal3fv(normals[5]);
  295.    
  296.     glVertex3fv(vertices[1]);
  297.    
  298.     glVertex3fv(vertices[0]);
  299.    
  300.     glVertex3fv(vertices[5]);
  301.    
  302.     glVertex3fv(vertices[4]);
  303.    
  304.     glEnd();
  305.    
  306.    
  307.    
  308.     glPopMatrix();
  309.    
  310.    
  311.    
  312.     glutSwapBuffers();
  313.    
  314. }
  315.  
  316.  
  317.  
  318.  
  319.  
  320. int main(int argc, char* argv[])
  321.  
  322. {
  323.    
  324.     float x,y;
  325.  
  326.     if(argc!=2)
  327.     {
  328.         fprintf(stderr,"Usage: %s (1)txtFile\n(2)VTKfile\n",argv[0]);
  329.    
  330.     return 0;
  331.    
  332. }
  333.     //Open VTK file for save
  334.     if((fpo=fopen(argv[1],"w"))==NULL)
  335.     {
  336.         printf("The file can't be opened. The program is exit.\n");
  337.  
  338.         return 0;
  339.     }
  340.    
  341.     glutInit(&argc, argv);
  342.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  343.     glutInitWindowPosition(100, 100);
  344.     glutInitWindowSize(640, 480);
  345.     glutCreateWindow("Mouse Motion Cube");
  346.     glutReshapeFunc(reshape);
  347.     glutMouseFunc(mouse);
  348.     glutMotionFunc(motion);
  349.     glutDisplayFunc(display);
  350.    
  351.    
  352.     glutMainLoop();
  353.    
  354.  
  355.  
  356.  
  357. fclose(fpo);
  358. }
Advertisement
Add Comment
Please, Sign In to add comment