Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

just give a brief explanation of how the code does this Open the demo program, Lab08a.cpp. This program that allows you to modify the viewport.

just give a brief explanation of how the code does this

Open the demo program, Lab08a.cpp. This program that allows you to modify the viewport. Create a project, build, and run the program. Use the arrow keys to make the viewport smaller and the mouse to drag the viewport to a new location. Study the code to see how this is done

lab08a.cpp:

#include  #include  #include  // The window dimensions in pixels GLint WindowHeight = 400; GLint WindowWidth = 400; // The limits of the viewport in pixels; this defines the drawable // area inside the window. GLint ViewportXmin = 0; GLint ViewportXmax = 400; GLint ViewportYmin = 0; GLint ViewportYmax = 400; // The limits of the world coordinate system GLfloat WorldXmin = -60.0; GLfloat WorldXmax = 60.0; GLfloat WorldYmin = -60.0; GLfloat WorldYmax = 60.0; // A reference point used to drag the viewport in the motion // callback function. GLint ReferenceX = 0; GLint ReferenceY = 0; //------------------------------------------------------------------- void SetViewport(void) { glViewport( ViewportXmin, ViewportYmin, // lower left corner ViewportXmax - ViewportXmin, // width ViewportYmax - ViewportYmin); // height printf("Viewport - left corner (%d,%d) width %d height %d ", ViewportXmin, ViewportYmin, ViewportXmax - ViewportXmin, ViewportYmax - ViewportYmin ); } //------------------------------------------------------------------- void myInit(void) { glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color to black glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(WorldXmin,WorldXmax,WorldYmin,WorldYmax); } //------------------------------------------------------------------- void hexagon(GLfloat size, GLfloat initialAngle) { static const GLfloat degreesToRadians = 3.141593f / 180.0f; GLfloat angle = initialAngle * degreesToRadians; const GLfloat angleIncrement = 60.0f * degreesToRadians; glBegin(GL_LINE_LOOP); for (int j=0; j<6; j++) { glVertex2f( size*cos(angle), size*sin(angle)); angle += angleIncrement; // 60 degrees (in radians) } glEnd(); } //------------------------------------------------------------------- void hexSwirl(void) { // Draw a series of hexagons, each one slighly larger than the previous // one, and each at a one degree offset from the previous one. for (int k=0; k<60; k++) hexagon( (float) k, 30.0 + k); } //------------------------------------------------------------------- void myDisplay(void) { glClear( GL_COLOR_BUFFER_BIT ); hexSwirl(); // Display a bounding box around the entire "world" glBegin(GL_LINE_LOOP); glVertex2f(WorldXmin, WorldYmin); glVertex2f(WorldXmin, WorldYmax); glVertex2f(WorldXmax, WorldYmax); glVertex2f(WorldXmax, WorldYmin); glEnd(); glFlush(); } //------------------------------------------------------------------- void myMouse(int button, int state, int x, int y) { // Remember the location of the mouse when the button was pressed if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { ReferenceX = x; ReferenceY = y; } } //------------------------------------------------------------------- void myMotion(int x, int y ) { // Translate the viewport according to the distance from the reference point int dx = x - ReferenceX; int dy = - (y - ReferenceY); // because the y axis is down ViewportXmin += dx; ViewportXmax += dx; ViewportYmin += dy; ViewportYmax += dy; SetViewport(); ReferenceX = x; ReferenceY = y; printf(" X: %d and RefX: %d, Y: %d and RefY: %d ",x, ReferenceX, y, ReferenceY); glutPostRedisplay(); // puts a "redraw" event in the event queue } //------------------------------------------------------------------- void mySpecialKey(int key, int x, int y ) { switch (key) { case GLUT_KEY_UP : ViewportYmin += 2; break; case GLUT_KEY_DOWN : ViewportYmax -= 2; break; case GLUT_KEY_RIGHT : ViewportXmin += 2; break; case GLUT_KEY_LEFT : ViewportXmax -= 2; break; case GLUT_KEY_HOME : ViewportXmin = 0; ViewportXmax = WindowWidth; ViewportYmin = 0; ViewportYmax = WindowHeight; break; } SetViewport(); glutPostRedisplay(); } //------------------------------------------------------------------- int main( int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize(WindowWidth, WindowHeight); glutInitWindowPosition(400,0); glutCreateWindow("Viewports"); glutDisplayFunc(myDisplay); glutMouseFunc(myMouse); glutMotionFunc(myMotion); glutSpecialFunc(mySpecialKey); myInit(); glutMainLoop(); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

What are the components of the operating budgets?

Answered: 1 week ago