Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <math.h>
- #include <stdlib.h>
- #define MAP_X 10
- #define MAP_Z 10
- int height[10][10] ={
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
- float terrain[10][10][3];
- int x, z;
- float aspect = 1.0f;
- float lx=0.0f,lz=-1.0f;
- float camx=0.0f,camz=5.0f;
- float angle = 0.0f;
- void processSpecialKeys(int key, int xx, int yy)
- {
- float fraction = 0.1f;
- switch (key) {
- case GLUT_KEY_LEFT :
- angle -= 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case GLUT_KEY_RIGHT :
- angle += 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case GLUT_KEY_UP :
- camx += lx * fraction;
- camz += lz * fraction;
- break;
- case GLUT_KEY_DOWN :
- camx -= lx * fraction;
- camz -= lz * fraction;
- break;
- }
- }
- void initializeTerrain()
- {
- for (z = 0; z < MAP_Z; z++)
- {
- for (x = 0; x < MAP_X; x++)
- {
- terrain[x][z][0] = (float)(x);
- terrain[x][z][1] = (float)height[x][z];
- terrain[x][z][2] = -(float)(z);
- }
- }
- }
- void display(void){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt( camx, 1.0f, camz,
- camx+lx, 1.0f, camz+lz,
- 0.0f, 1.0f, 0.0f);
- glColor4f(1.0, 0.0, 0.0,0.5);
- for (z = 0; z < MAP_Z-1; z++)
- {
- glBegin(GL_TRIANGLE_STRIP);
- for (x = 0; x < MAP_X-1; x++)
- {
- glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
- glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);
- glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);
- glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
- }
- glEnd();
- }
- glutSwapBuffers();
- }
- void myInit(){
- initializeTerrain();
- glMatrixMode(GL_PROJECTION);
- glViewport(0, 0, 500, 500);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glClearColor(1.0, 1.0, 1.0, 1.0);
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- if (w <= h)
- glOrtho(-10.0, 10.0, -5.0 * (GLfloat) h / (GLfloat) w,
- 15.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
- else
- glOrtho(-10.0 * (GLfloat) w / (GLfloat) h,
- 10.0 * (GLfloat) w / (GLfloat) h, -5.0, 15.0, -10.0, 10.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void main(int argc, char** argv){
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(500, 500);
- glutCreateWindow("A3");
- myInit();
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutSpecialFunc(processSpecialKeys);
- glEnable(GL_DEPTH_TEST);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement