Heightmap
Uses keyframe animation with smoothing
#include <windows.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <GL/glut.h> float distance = 8.0, angle = 0.0; float lightPos = 0.0; bool fullscreenMode = false, rotateGrid = true; int windowWidth = 800, windowHeight = 600; void dumpCubePos(void); struct cube { float r, g, b, a; float x, y, z; }; cube cubeArray[10 * 10 * 10]; void centerWindow(void) { int screenWidth = glutGet(GLUT_SCREEN_WIDTH); int screenHeight = glutGet(GLUT_SCREEN_HEIGHT); glutPositionWindow((screenWidth / 2) - (windowWidth / 2), (screenHeight / 2) - (windowHeight / 2)); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: case 'q': case 'Q': exit(EXIT_SUCCESS); break; case 'f': case 'F': fullscreenMode = !fullscreenMode; if (fullscreenMode) glutFullScreen(); else { glutReshapeWindow(800, 600); centerWindow(); } break; case 't': case 'T': dumpCubePos(); break; case 'w': case 'W': distance-=0.5; break; case 's': case 'S': distance+=0.5; break; } } float xc, zc; void idle(void) { //float xc, zc; angle = angle + 0.05; if (angle > 360.0) angle = 0.0; xc = distance * cos(angle / 180.0 * M_PI); zc = distance * sin(angle / 180.0 * M_PI); glLoadIdentity(); gluLookAt(xc, 0.0, zc, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutPostRedisplay(); } int count = 0, miniCount = 0; void initCubes(void) { int count = 0; for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { for (int z = 0; z < 10; z++) { cubeArray[count].r = (float) x / 10; cubeArray[count].g = (float) y / 10; cubeArray[count].b = (float) z / 10; cubeArray[count].x = x; cubeArray[count].y = y; cubeArray[count].z = z; count++; } } } } void dumpCubePos(void) { for (int k = 0; k < 1000; k++) { std::cout << cubeArray[k].x << " " << cubeArray[k].y << " " << cubeArray[k].z << std::endl; } } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); float scale = 3.0; float prevX = 5.0f, prevY = 5.0f, prevZ = 5.0f; glPushMatrix(); for (int k = 0; k < 1000; k++) { glTranslatef((prevX - cubeArray[k].x) / scale, (prevY - cubeArray[k].y) / scale, (prevZ - cubeArray[k].z) / scale); prevX = cubeArray[k].x; prevY = cubeArray[k].y; prevZ = cubeArray[k].z; glColor3f(cubeArray[k].r, cubeArray[k].g, cubeArray[k].b); glutSolidCube(0.2f); glColor3f(1.0, 1.0, 1.0); //glColor3f(0.0, 0.0, 0.0); glutWireCube(0.22f); } glPopMatrix(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.0, (GLfloat) w / (GLfloat) h, 3.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow(argv[0]); glMatrixMode(GL_PROJECTION); glEnable(GL_DEPTH_TEST); glClearColor(0.0, 0.0, 0.0, 0.0); //glClearColor(1.0, 1.0, 1.0, 0.0); glutReshapeWindow(windowWidth, windowHeight); glLoadIdentity(); gluPerspective(50.0, 1.0, 3.0, 10.0); glMatrixMode(GL_MODELVIEW); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutIdleFunc(idle); glutReshapeFunc(reshape); centerWindow(); //Center window initCubes(); glutMainLoop(); return EXIT_SUCCESS; }