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.
- */
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif // __APPLE__
- #include <stdlib.h>
- #include <stdio.h>
- float counter = 0;
- void initGL() //Setbackgroundcolortoblackandopaque
- {
- glClearColor(0.0f,0.0f,0.0f,1.0f); //Setbackgrounddepthtofarthest
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);//Enabledepthtestingforz-culling
- glDepthFunc(GL_LEQUAL);//Setthetypeofdepth-test
- glShadeModel(GL_SMOOTH);//Enablesmoothshading //Niceperspectivecorrections
- glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
- }
- void reshape(int width,int height)
- {
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(120.0,(GLfloat)height/(GLfloat)width,1.0,128.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0,1.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0);
- }
- void keyboard(unsigned char key,int x,int y)
- {
- switch(key)
- {
- case'h':
- printf("help\n\n");
- printf("c-Toggleculling\n");
- printf("q/escape-Quit\n\n");
- break;
- case'c':
- if(glIsEnabled(GL_CULL_FACE))
- glDisable(GL_CULL_FACE);
- else
- glEnable(GL_CULL_FACE);
- break;
- case(1):
- glRotatef(1.0,1.,0.,0.);
- break;
- case'2':
- glRotatef(1.0,0.,1.,0.);
- break;
- case'q':
- case 27:
- exit(0);
- break;
- }
- glutPostRedisplay();
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.3f,0.0f,-9.0f);
- glRotatef(counter,0.0,1.0,0.0);
- counter+=0.01;
- glBegin(GL_TRIANGLES);
- glColor3f(0.0f,0.5f,1.0f);
- glVertex3f(0.0f,1.0f,0.0f);
- glColor3f(1.0f,0.5f,0.0f);
- glVertex3f(-1.0f,-1.0f,1.0f);
- glColor3f(0.0f,0.5f,1.0f);
- glVertex3f(1.0f,-1.0f,1.0f);
- glColor3f(0.75f,0.68f,1.0f);
- glVertex3f(0.0f,1.0f,0.0f);
- glColor3f(0.4f,1.0f,0.79f);
- glVertex3f(1.0f,-1.0f,1.0f);
- glColor3f(0.0f,1.0f,0.0f);
- glVertex3f(1.0f,-1.0f,-1.0f);
- glColor3f(1.0f,0.0f,0.0f);
- glVertex3f(0.0f,1.0f,0.0f);
- glColor3f(0.0f,1.0f,0.0f);
- glVertex3f(1.0f,-1.0f,-1.0f);
- glColor3f(0.0f,0.0f,1.0f);
- glVertex3f(-1.0f,-1.0f,-1.0f);
- glColor3f(-1.0f,0.0f,1.0f); //Red
- glVertex3f(0.0f,1.0f,0.0f);
- glColor3f(0.3f,0.25f,1.0f); //Blue
- glVertex3f(-1.0f,-1.0f,-1.0f);
- glColor3f(0.1f,1.0f,0.55f); //Green
- glVertex3f(-1.0f,-1.0f,1.0f);
- glEnd();
- glutSwapBuffers();
- }
- int main(int argc,char**argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
- glutInitWindowSize(512,512);
- glutInitWindowPosition(20,20);
- glutCreateWindow("Torus");
- glutDisplayFunc(display);
- glutIdleFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- initGL();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement