Question: Modify the interaction part to allow the user to use the key board instead of the mouse 2-Rotate the camera around the model (y axis)
Modify the interaction part to allow the user to use the key board instead of the mouse
2-Rotate the camera around the model (y axis) to show all the sides of the scene, you can also rotate the camera around the x axis.
here is the code:
* * OGL01Shape3D.cpp: 3D Shapes */ #include
/* Global variables */ char title[] = "3D Shapes"; static GLfloat g_fViewDistance = 5; static BOOL g_bButton1Down = FALSE; static int g_yClick = 0;
/* Initialize OpenGL Graphics */ void initGL() { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque glClearDepth(1.0f); // Set background depth to farthest glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling }
/* Handler for window-repaint event. Called back when the window first appears and whenever the window needs to be re-painted. */
void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix glLoadIdentity(); // load the model-view matrix gluLookAt(0, 0, g_fViewDistance, 0, 0, 0, 0, 1, 0);
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads glColor3f(0.0f, 1.0f, 0.0f); // Green //Front Side glVertex3f(0.5f, 0.5f, 0.5f); glVertex3f(0.5f, -0.5f, 0.5f); glVertex3f(-0.5f, -0.5f, 0.5f); glVertex3f(-0.5f, 0.5f, 0.5f);
// top Side glColor3f(1.0f, 0.5f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f); glVertex3f(0.5f, 0.5f, 0.5f); glVertex3f(0.5f, -0.5f, -0.5f); glVertex3f(-0.5f, 0.5f, -0.5f);
//Bottom glColor3f(1.0f, 0.0f, 0.0f); // Red glVertex3f(-0.5f, -0.5f, 0.5f); glVertex3f(0.5f, -0.5f, 0.5f); glVertex3f(0.5f, -0.5f, -0.5f); glVertex3f(-0.5f, -0.5f, -0.5f);
// Left Side glColor3f(1.0f, 1.0f, 0.0f); // Yellow glVertex3f(-0.5f, -0.5f, 0.5f); glVertex3f(-0.5f, -0.5f, -0.5f); glVertex3f(-0.5f, 0.5f, -0.5f); glVertex3f(-0.5f, 0.5f, -0.5f);
// Right face glColor3f(0.0f, 0.0f, 1.0f); // Blue glVertex3f(0.5f, -0.5f, 0.5f); glVertex3f(0.5f, -0.5f, -0.5f); glVertex3f(0.5f, 0.5f, -0.5f); glVertex3f(0.5f, 0.5f, 0.5f);
// Back Side glColor3f(1.0f, 0.0f, 1.0f); // Magenta glVertex3f(-0.5f, 0.5f, -0.5f); glVertex3f(0.5f, 0.5f, -0.5f); glVertex3f(0.5f, -0.5f, -0.5f); glVertex3f(-0.5f, -0.5f, -0.5f); glEnd(); // End of drawing color-cube
//glColor3f(1.0f, 0.0f, 0.0f); //glutWireTeapot(1); //glRotatef(180, 1, 0, 0); //glutWireSphere(1, 30, 5); //glutWireCone(1, 1, 30, 23);
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering) }
/* Handler for window re-size event. Called back when the window first appears and whenever the window is re-sized with its new width and height */ void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer // Compute aspect ratio of the new window if (height == 0) height = 1; // To prevent divide by 0 GLfloat aspect = (GLfloat)width / (GLfloat)height; glViewport(0, 0, width, height); // Set the aspect ratio of the clipping volume to match the viewport glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix glLoadIdentity(); gluPerspective(45.0f, aspect, 0.01f, 100.0f); // Enable perspective projection with fovy, aspect, zNear and zFar }
void MouseButton(int button, int state, int x, int y) { // Respond to mouse button presses. // If button1 pressed, mark this state so we know in motion function. if (button == GLUT_LEFT_BUTTON) { g_bButton1Down = (state == GLUT_DOWN) ? TRUE : FALSE; g_yClick = y - g_fViewDistance; printf("g_yClick is %d ", g_yClick);
} } void MouseMotion(int x, int y) { // If button1 pressed, zoom in/out if mouse is moved up/down. if (g_bButton1Down) { g_fViewDistance = (y - g_yClick); printf("The eye location is %f and the mouse co-ordinates are (%d, %d) and ", g_fViewDistance,x,y); if (g_fViewDistance < 0) g_fViewDistance = 0; glutPostRedisplay(); } }
/* Main function: GLUT runs as a console application starting at main() */ int main(int argc, char** argv) { glutInit(&argc, argv); // Initialize GLUT glutInitWindowSize(640, 480); // Set the window's initial width & height glutCreateWindow(title); // Create window with the given title glutDisplayFunc(display); // Register callback handler for window re-paint event glutReshapeFunc(reshape); // Register callback handler for window re-size event glutMouseFunc(MouseButton); glutMotionFunc(MouseMotion); initGL(); // Our own OpenGL initialization glutMainLoop(); // Enter the infinite event-processing loop return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
