Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GLUT Shapes Demo
- *
- * Written by Nigel Stewart November 2003
- *
- * This program is test harness for the sphere, cone
- * and torus shapes in GLUT.
- *
- * Spinning wireframe and smooth shaded shapes are
- * displayed until the ESC or q key is pressed. The
- * number of geometry stacks and slices can be adjusted
- * using the + and - keys.
- */
- #include<stdio.h>
- #include<math.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdlib.h>
- #include <math.h> /* cos */
- #define PI 3.14159265
- static int slices = 16;
- static int stacks = 16;
- double xx=0,yy=0,zz=0;
- double translate_x=0,translate_y=0,translate_z=0;
- double rot_x=0,rot_y=0,rot_z=0;
- double zoom_x=0,zoom_y=0,zoom_z=0;
- double eye_x = 0;
- double eye_y = 0;
- double eye_z = 25;
- double cx=0,cy=0,cz=0;
- void ownscal(double x,double y,double z)
- {
- GLfloat mat[]=
- {
- x,0,0,0,
- 0,y,0,0,
- 0,0,z,0,
- 0,0,0,1
- };
- glMatrixMode(GL_MODELVIEW);
- glMultMatrixf(mat);
- }
- /* GLUT callback Handlers */
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- double ar2=1.8;
- glFrustum(-ar2, ar2, -ar2, ar2, 2.0, 100.0);
- //glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- gluLookAt(0, 0, 25, 0, 0, 0, 0, 1, 0);
- }
- void draw_cube()
- {
- glBegin(GL_QUADS);
- //1
- glVertex3f(1,0,1);
- glVertex3f(1,1,1);
- glVertex3f(1,1,0);
- glVertex3f(1,0,0);
- //2
- glVertex3f(0,0,1);
- glVertex3f(0,1,1);
- glVertex3f(1,1,1);
- glVertex3f(1,0,1);
- //3
- glVertex3f(0,1,0);
- glVertex3f(0,1,1);
- glVertex3f(0,0,1);
- glVertex3f(0,0,0);
- //4
- glVertex3f(0,1,0);
- glVertex3f(1,1,0);
- glVertex3f(1,1,1);
- glVertex3f(0,1,1);
- //5
- glVertex3f(1,0,0);
- glVertex3f(0,0,0);
- glVertex3f(0,0,1);
- glVertex3f(1,0,1);
- //6
- glVertex3f(1,1,0);
- glVertex3f(0,1,0);
- glVertex3f(0,0,0);
- glVertex3f(1,0,0);
- glEnd();
- }
- static void cube()
- {
- glBegin(GL_QUADS);
- glColor3f(1, 0, 0);
- glVertex3f(1, 1, 1);
- glVertex3f(-1, 1, 1);
- glVertex3f(-1, -1, 1);
- glVertex3f(1, -1, 1);
- glColor3f(0, 1, 0);
- glVertex3f(1, 1, -1);
- glVertex3f(1, 1, 1);
- glVertex3f(1, -1, 1);
- glVertex3f(1, -1, -1);
- glColor3f(0, 0, 1);
- glVertex3f(-1, 1, -1);
- glVertex3f(-1, 1, 1);
- glVertex3f(1, 1, 1);
- glVertex3f(1, 1, -1);
- glColor3f(1, 1, 0);
- glVertex3f(-1, 1, 1);
- glVertex3f(-1, 1, -1);
- glVertex3f(-1, -1, -1);
- glVertex3f(-1, -1, 1);
- glColor3f(1, 0, 1);
- glVertex3f(-1, -1, 1);
- glVertex3f(-1, -1, -1);
- glVertex3f(1, -1, -1);
- glVertex3f(1, -1, 1);
- glColor3f(0, 1, 1);
- glVertex3f(-1, 1, -1);
- glVertex3f(1, 1, -1);
- glVertex3f(1, -1, -1);
- glVertex3f(-1, -1, -1);
- glEnd();
- }
- static void blade(GLdouble scaley, GLdouble rot_angle, GLdouble rot_x, GLdouble rot_y, GLdouble rot_z)
- {
- glPushMatrix();
- glRotated(rot_angle, rot_x, rot_y, rot_z);
- glRotated(20, 0, 1, 0);
- glScaled(0.75, scaley, 0.1);
- glTranslated(0, 1, 0);
- cube();
- glPopMatrix();
- }
- static void fan()
- {
- const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
- blade(-3, 0+t*250, 0,0 , 1);
- blade(-3, 120+t*250, 0, 0, 1);
- blade(-3, 240+t*250, 0, 0, 1);
- }
- void cube(double translate_x,double translate_y,double translate_z,double rotate_x,double rotate_y,double rotate_z,double scale_x,double scale_y,double scale_z)
- {
- glPushMatrix();
- glColor3d(0.325, 0.176, 0.823);
- glTranslated(translate_x,translate_y,translate_z);
- glRotated(rotate_x,1,0,0);
- glRotated(rotate_y,0,1,0);
- glRotated(rotate_z,0,0,1);
- ownscal(scale_x,scale_y,scale_z);
- draw_cube();
- glPopMatrix();
- }
- void circle()
- {
- float theta=(22.0/7.0)/180.0;
- //glTranslated(.2,0,1);
- glRotated(90,1,0,0);
- glBegin(GL_QUAD_STRIP);
- for(int i=0;i<360;i++)
- {
- //glColor3d(1,0,0);
- glVertex3f(cos(theta*i),+1,sin(theta*i));
- //glColor3d(1,0,0);
- glVertex3f(cos(theta*i),-1,sin(theta*i));
- }
- glEnd();
- float k=0;
- for(int i=1;i>=-1;i-=2)
- {
- glBegin(GL_TRIANGLE_FAN);
- //glColor3d(1,0,0);
- glVertex3f(0,i,0);
- for(k=0;k<360;k++){
- //glColor3d(1,0,0);
- glVertex3f(i*cos(k*theta),i+.01,sin(k*theta));
- glVertex3f(i*cos(k*theta),-i,sin(k*theta));
- }
- glEnd();
- }
- }
- void windmill()
- {
- cube(0,0,0,0,0,-10,.50,13.80,.5);
- cube(2.70,0,0,0,0,0,.50,13.80,.5);
- cube(5.40,0,0,0,0,10,.50,13.80,.5);
- glTranslated(2.70,13.50,1.20);
- glColor3d(1,.3,.3);
- glScaled(2.40,3,.20);
- circle();
- fan();
- }
- static void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //rod
- windmill();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- gluLookAt(eye_x, eye_y, eye_z, cx, cy, cz, 0, 1, 0);
- glutSwapBuffers();
- }
- static void key(unsigned char key, int x, int y)
- {
- double translate=.1;
- double rotation=1;
- double scale2=.1,lookat=0.5;
- switch (key)
- {
- case 't':
- translate_x+=translate;
- printf("translate_x: %.2f ",translate_x);
- break;
- case 'T':
- translate_x-=translate;
- printf("translate_x: %.2f ",xx);
- break;
- case 'y':
- translate_y+=translate;
- printf("translate_y: %.2f ",translate_y);
- break;
- case 'Y':
- translate_y-=translate;
- printf("translate_y: %.2f ",translate_y);
- break;
- case 'u':
- translate_z+=translate;
- printf("translate_z: %.2f ",translate_z);
- break;
- case 'U':
- translate_z-=translate;
- printf("translate_z: %.2f ",translate_z);
- break;
- case 'r':
- rot_x+=rotation;
- printf("rot_x: %.2f ",rot_x);
- break;
- case 'R':
- rot_x-=rotation;
- printf("rot_x: %.2f ",rot_x);
- break;
- case 'e':
- rot_y+=rotation;
- printf("rot_y: %.2f ",rot_y);
- break;
- case 'E':
- rot_y-=rotation;
- printf("rot_y: %.2f ",rot_y);
- break;
- case 'w':
- rot_z+=rotation;
- printf("rot_z: %.2f ",rot_z);
- break;
- case 'W':
- rot_z-=rotation;
- printf("rot_z: %.2f ",rot_z);
- break;
- case 's':
- zoom_x+=scale2;
- //printf("xx: %.2f ",zz);
- break;
- case 'S':
- zoom_x-=scale2;
- //printf("xx: %.2f ",zz);
- break;
- case 'd':
- zoom_y+=scale2;
- //printf("xx: %.2f ",zz);
- break;
- case 'D':
- zoom_y-=scale2;
- //printf("xx: %.2f ",zz);
- break;
- case 'f':
- zoom_z+=scale2;
- //printf("xx: %.2f ",zz);
- break;
- case 'F':
- zoom_z-=scale2;
- //printf("xx: %.2f ",zz);
- break;
- case 'j':
- eye_x+=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'J':
- eye_x-=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'k':
- eye_y+=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'K':
- eye_y-=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'l':
- eye_z+=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'L':
- eye_z-=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'b':
- cx+=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'B':
- cx-=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'n':
- cy+=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'N':
- cy-=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'm':
- cz+=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 'M':
- cz-=lookat;
- //printf("xx: %.2f ",zz);
- break;
- case 27 :
- case 'q':
- printf("\nzoom_x: %.2f zoom_y: %.2f zoom_z: %.2f",zoom_x,zoom_y,zoom_z);
- printf("\ntranslate_x: %.2f translate_y: %.2f translate_z: %.2f",translate_x,translate_y,translate_z);
- printf("\nrotate_x: %.2f rotate_y: %.2f rotate_z: %.2f",rot_x,rot_y,rot_z);
- exit(0);
- break;
- case '+':
- slices++;
- stacks++;
- break;
- case '-':
- if (slices>3 && stacks>3)
- {
- slices--;
- stacks--;
- }
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutIdleFunc(idle);
- glClearColor(1,1,1,1);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement