Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib> // standard definitions
- #include <iostream> // C++ I/O
- #include <cstdio> // C I/O (for sprintf)
- #include <cmath> // standard definitions
- #include <GL/glut.h> // GLUT
- #include <GL/glu.h> // GLU
- #include <GL/gl.h> // OpenGL
- using namespace std; // make std accessible
- int r=1,g=1,b=1;
- int m=1;
- int p=0;
- void init()
- {
- glClearColor(0, 0, 0, 0);
- glClearDepth(1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60, 1, 1, 1000);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(
- 0.0, 0.0, 5.0,
- 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- float lpos[] = { 5, 5, 5, 0 };
- glLightfv(GL_LIGHT0, GL_POSITION, lpos);
- glShadeModel(GL_SMOOTH);
- }
- void display()
- {
- glClear(
- GL_COLOR_BUFFER_BIT |
- GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotated(0, 1, 0, 0);
- glEnable(GL_COLOR_MATERIAL);
- glColor3f(r,g,b);
- glutSolidTeapot(m);
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- }
- //-----------------------------------------------------------------------
- // keyboard callback function
- // This is called whenever a keyboard key is hit.
- //-----------------------------------------------------------------------
- void keyboard(unsigned char k, int x, int y)
- {
- if(k=='r')
- {
- r=1;g=0;b=0;
- }
- glFlush();
- glutPostRedisplay();
- if(k=='g')
- {
- r=0;g=1;b=0;
- }
- glFlush();
- glutPostRedisplay();
- if(k=='b')
- {
- r=0;g=0;b=1;
- }
- glFlush();
- glutPostRedisplay();
- if(k=='l')
- {
- m++;
- }
- glFlush();
- glutPostRedisplay();
- if(k=='s')
- {
- m--;
- }
- glFlush();
- glutPostRedisplay();
- }
- void usage()
- {
- cout << "\n\
- -----------------------------------------------------------------------\n\
- CMSC 427 Sample Program.\n\
- Inputs:\n\
- a: Rotate counterclockwise\n\
- l: Rotate clockwise\n\
- q: Quit\n\
- You may need to place the cursor over the graphics window for\n\
- keyboard input to be processed.\n\
- -----------------------------------------------------------------------\n";
- cout.flush();
- }
- void processSpecialKeys(int key, int x, int y)
- {
- if(key==GLUT_KEY_UP)
- {
- glTranslatef(0,1,0);
- }
- if(key==GLUT_KEY_DOWN && glutGetModifiers()==GLUT_ACTIVE_SHIFT)
- {
- glTranslatef(0,-1,0);
- }
- if(key==GLUT_KEY_LEFT && glutGetModifiers()==GLUT_ACTIVE_SHIFT)
- {
- glTranslatef(-1,0,0);
- }
- if(key==GLUT_KEY_RIGHT && glutGetModifiers()==GLUT_ACTIVE_CTRL)
- {
- glTranslatef(1,0,0);
- }
- glutPostRedisplay();
- }
- int main()
- {
- usage(); // explain how to use
- glutInitDisplayMode( // initialize GLUT
- GLUT_DOUBLE | // use double buffering
- GLUT_DEPTH | // request memory for z-buffer
- GLUT_RGB ); // set RGB color mode
- glutCreateWindow("GLUT Example"); // create the window
- glutDisplayFunc(display); // call display() to redraw window
- //glutKeyboardFunc(keyboard); // call keyboard() when key is hit
- glutSpecialFunc(processSpecialKeys);
- init(); // our own initializations
- glutMainLoop(); // let GLUT take care of everything
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement