Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <GLUT/glut.h> //for mac
- #include <GL/glut.h>
- #include <stdio.h>
- FILE *fpo;
- float g_angle = 0.0f;
- int g_pressed = 0;
- int g_prevX = -1;
- void mouse(int button, int state, int x, int y)
- {
- if (button == GLUT_LEFT_BUTTON)
- {
- if (state == GLUT_DOWN)
- {
- g_prevX = x;
- g_pressed = 1;
- }
- else if (state == GLUT_UP)
- {
- g_pressed = 0;
- }
- }
- }
- void motion(int x, int y)
- {
- if (g_pressed == 1)
- {
- g_angle += (float)(x - g_prevX);
- if (g_angle > 360.0f)
- {
- g_angle -= 360.0f;
- }
- g_prevX = x;
- glutPostRedisplay();
- }
- printf("x=%d,y=%d\n",x,y);
- fprintf(fpo,"%d,%d \n",x,y);
- }
- void reshape(int width, int height)
- {
- static GLfloat lightPosition[4] = {0.25f, 1.0f, 0.25f, 0.0f};
- static GLfloat lightDiffuse[3] = {1.0f, 1.0f, 1.0f};
- static GLfloat lightAmbient[3] = {0.25f, 0.25f, 0.25f};
- static GLfloat lightSpecular[3] = {1.0f, 1.0f, 1.0f};
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, (double)width / (double)height, 0.1, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.5, 1.5, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
- glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
- glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
- }
- void display()
- {
- static GLfloat vertices [8][3] =
- {
- {-0.5f, -0.5f, 0.5f},
- { 0.5f, -0.5f, 0.5f},
- { 0.5f, 0.5f, 0.5f},
- {-0.5f, 0.5f, 0.5f},
- { 0.5f, -0.5f, -0.5f},
- {-0.5f, -0.5f, -0.5f},
- {-0.5f, 0.5f, -0.5f},
- { 0.5f, 0.5f, -0.5f}
- };
- static GLfloat normals[6][3] =
- {
- { 0.0f, 0.0f, 1.0f},
- { 0.0f, 0.0f, -1.0f},
- { 1.0f, 0.0f, 0.0f},
- {-1.0f, 0.0f, 0.0f},
- { 0.0f, 1.0f, 0.0f},
- { 0.0f, -1.0f, 0.0f}
- };
- static GLfloat diffuse[3] = {1.0f, 0.0f, 0.0f};
- static GLfloat ambient[3] = {0.25f, 0.25f, 0.25f};
- static GLfloat specular[3] = {1.0f, 1.0f, 1.0f};
- static GLfloat shininess[1] = {32.0f};
- glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
- glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
- glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
- glEnable(GL_DEPTH_TEST);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotatef(g_angle, 0.0f, 1.0f, 0.0f);
- // 前
- glBegin(GL_POLYGON);
- glNormal3fv(normals[0]);
- glVertex3fv(vertices[0]);
- glVertex3fv(vertices[1]);
- glVertex3fv(vertices[2]);
- glVertex3fv(vertices[3]);
- glEnd();
- // 後
- glBegin(GL_POLYGON);
- glNormal3fv(normals[1]);
- glVertex3fv(vertices[4]);
- glVertex3fv(vertices[5]);
- glVertex3fv(vertices[6]);
- glVertex3fv(vertices[7]);
- glEnd();
- // 右
- glBegin(GL_POLYGON);
- glNormal3fv(normals[2]);
- glVertex3fv(vertices[1]);
- glVertex3fv(vertices[4]);
- glVertex3fv(vertices[7]);
- glVertex3fv(vertices[2]);
- glEnd();
- // 左
- glBegin(GL_POLYGON);
- glNormal3fv(normals[3]);
- glVertex3fv(vertices[5]);
- glVertex3fv(vertices[0]);
- glVertex3fv(vertices[3]);
- glVertex3fv(vertices[6]);
- glEnd();
- // 上
- glBegin(GL_POLYGON);
- glNormal3fv(normals[4]);
- glVertex3fv(vertices[3]);
- glVertex3fv(vertices[2]);
- glVertex3fv(vertices[7]);
- glVertex3fv(vertices[6]);
- glEnd();
- // 下
- glBegin(GL_POLYGON);
- glNormal3fv(normals[5]);
- glVertex3fv(vertices[1]);
- glVertex3fv(vertices[0]);
- glVertex3fv(vertices[5]);
- glVertex3fv(vertices[4]);
- glEnd();
- glPopMatrix();
- glutSwapBuffers();
- }
- int main(int argc, char* argv[])
- {
- float x,y;
- if(argc!=2)
- {
- fprintf(stderr,"Usage: %s (1)txtFile\n(2)VTKfile\n",argv[0]);
- return 0;
- }
- //Open VTK file for save
- if((fpo=fopen(argv[1],"w"))==NULL)
- {
- printf("The file can't be opened. The program is exit.\n");
- return 0;
- }
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(640, 480);
- glutCreateWindow("Mouse Motion Cube");
- glutReshapeFunc(reshape);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutDisplayFunc(display);
- glutMainLoop();
- fclose(fpo);
- }
Advertisement
Add Comment
Please, Sign In to add comment