Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------
- // File: sample1.cpp
- // Description: A simple OpenGL sample program
- // Programmer: Pooja Nath
- // For: CMSC 427 - Computer Graphics
- // Date: Feb 2004
- //
- // This is a basic OpenGL/GLUT program, which renders a teapot.
- // Hitting the keyboard charatcers 'a' and 'l' rotate the teapot
- // about the Y (vertical) axis. Hitting 'q' quits the program.
- //-----------------------------------------------------------------------
- #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
- float r = 1, g = 0, b = 0, m = 2;
- //-----------------------------------------------------------------------
- // init
- // Sets up some default OpenGL values.
- //-----------------------------------------------------------------------
- void init()
- {
- glClearColor(0, 0, 0, 0); // background color
- glClearDepth(1.0); // background depth value
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60, 1, 1, 1000); // setup a perspective projection
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt( // set up the camera
- 0.0, 0.0, 5.0, // eye position
- 0.0, 0.0, 0.0, // lookat position
- 0.0, 1.0, 0.0); // up direction
- glEnable(GL_DEPTH_TEST); // enable hidden surface removal
- glEnable(GL_LIGHTING); // enable lighting
- glEnable(GL_LIGHT0); // enable
- float lpos[] = { 5, 5, 5, 0 };
- glLightfv(GL_LIGHT0, GL_POSITION, lpos);
- // glShadeModel(GL_FLAT); // flat shading
- glShadeModel(GL_SMOOTH); // smooth shading
- }
- //-----------------------------------------------------------------------
- // display callback function
- // This is called each time application needs to redraw itself.
- // Most of the opengl work is done through this function.
- //-----------------------------------------------------------------------
- void display()
- {
- glClear(
- GL_COLOR_BUFFER_BIT | // clear the frame buffer (color)
- GL_DEPTH_BUFFER_BIT); // clear the depth buffer (depths)
- glPushMatrix(); // save the current camera transform
- glRotated(0, 1, 0, 0); // rotate by rotAngle about y-axis
- glEnable(GL_COLOR_MATERIAL); // specify object color
- glColor3f(r,g,b); // redish
- glutSolidTeapot(m); // draw the teapot
- glPopMatrix(); // restore the modelview matrix
- glFlush(); // force OpenGL to render now
- glutSwapBuffers(); // make the image visible
- }
- //-----------------------------------------------------------------------
- // 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;
- }
- else if(k == 'g')
- {
- r = 0;
- g = 1;
- b = 0;
- }
- else if(k == 'b')
- {
- r = 0;
- g = 0;
- b = 1;
- }
- else if((k == 'W') && (glutGetModifiers() == GLUT_ACTIVE_SHIFT))
- {
- r = 1;
- g = 1;
- b = 1;
- }
- else if(k == 'c')
- {
- glRotatef(90,0,0,1);
- }
- else if(k == 'a')
- {
- glRotatef(-90,0,0,1);
- }
- else exit(0);
- 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_LEFT)
- {
- glTranslatef(-1, 0, 0);
- }
- else if(key == GLUT_KEY_RIGHT)
- {
- glTranslatef(1, 0, 0);
- }
- if(key == GLUT_KEY_UP)
- {
- glTranslatef(0, 1, 0);
- }
- else if(key == GLUT_KEY_DOWN)
- {
- glTranslatef(0, -1, 0);
- }*/
- if((key == GLUT_KEY_UP) && (glutGetModifiers() == GLUT_ACTIVE_CTRL))
- {
- m++;
- }
- else if((key == GLUT_KEY_DOWN) && (glutGetModifiers() == GLUT_ACTIVE_CTRL))
- {
- m--;
- }
- if((glutGetModifiers() == GLUT_ACTIVE_SHIFT) && (key == GLUT_KEY_UP)) //color brightness incress
- {
- r += 0.1;
- }
- else if((glutGetModifiers() == GLUT_ACTIVE_SHIFT) && (key == GLUT_KEY_DOWN)) //color brightness incress
- {
- r -= 0.1;
- }
- glutPostRedisplay();
- }
- void mouseClicks(int button, int state, int x, int y)
- {
- }
- //-----------------------------------------------------------------------
- // main program
- // Where everything begins.
- //-----------------------------------------------------------------------
- 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
- printf("Press r for red, g for green and b for blue, SHIFT+W to white else any key for exit. Press Shift+Up Arrow for zooming in and Shift+Down for zooming out");
- glutCreateWindow("GLUT Example"); // create the window
- glutDisplayFunc(display); // call display() to redraw window
- glutKeyboardFunc(keyboard); // call keyboard() when key is hit
- glutSpecialFunc(processSpecialKeys);
- glutMouseFunc(mouseClicks);
- init(); // our own initializations
- glutMainLoop(); // let GLUT take care of everything
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment