Update your code for the previus assignment to render a grid terrain in 3D. Implement the usual translate/rotate movements on key press:
u/d: for up/down l/r: for left/right f/b: for forward/backward x/X: for rotation around X-axis y/Y: for rotation around Y-axis z/Z: for rotation around Z-axis c: cycle though the various color maps q: quitIn addition:
+/-: multiply/divide all heights in the grid by a factor of, say 1.5
>/<: increase/decrease resolutionThe idea: When the resolution is 1, you iterate through every singel row and column; when teh resolution is 2, you would consider every 2nd row and every 2nd column; and so on. The grid stays the same, just the rendering will skip some rows and columns.
glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, 1 /* aspect */, 1, 10.0); /* by default camera is at (0,0,0) looking along negative y axis; the frustrum is from z=-1 to z=-10 */
glEnable(GL_DEPTH_TEST);Make sure your display mode contains depth:
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);and when you clear in display function, make sure you clear the depth buffer as well:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertex3f(...);The display function will look like this:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //clear the matrix //viewing transformation gluLookAt(...); //MODELING //first keypress motion: translate then rotate , for e.g. glTranslatef(pos[0], pos[1], pos[2]); glRotatef(theta[0], 1,0,0); glRotatef(theta[1], 0,1,0); glRotatef(theta[2], 0,0,1); //now map the terrain to [-1,1]x[-1,1] window glScale(..); //scale [0,0]x [nrows,ncols] to [-1,1]x[-1,1] glTranslatef(-ncols/2.0, -nrows/2.0, -2); //center it //draw the grid glFlush();
For documentation on GL check the Reddbook, especially chapter 3 (Viewing) here. Below are some excerpts from the book:
As we can attest, it's all too easy to achieve the well-known black-screen effect. Although any number of things can go wrong, often you get this effect - which results in absolutely nothing being drawn in the window you open on the screen - from incorrectly aiming the "camera" and taking a picture with the model behind you. A similar problem arises if you don't choose a field of view that's wide enough to view your objects but narrow enough so they appear reasonably large.
If you find yourself exerting great programming effort only to create a black window, try these diagnostic steps.
Enjoy!