Advertisement
Guest User

Untitled

a guest
Mar 9th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #ifdef __APPLE__
  2. #include <GLUT/glut.h>
  3. #else
  4. #include <GL/glut.h>
  5. #endif
  6.  
  7. #include <math.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. #define MAP_X 10
  12. #define MAP_Z 10
  13.  
  14. int height[10][10] ={
  15. { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  16. { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  17. { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  18. { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  19. { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  20. { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  21. { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  22. { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  23. { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  24. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
  25.  
  26. float terrain[10][10][3];
  27. int x, z;
  28. float aspect = 1.0f;
  29. float lx=0.0f,lz=-1.0f;
  30. float camx=0.0f,camz=5.0f;
  31. float angle = 0.0f;
  32.  
  33. void processSpecialKeys(int key, int xx, int yy)
  34. {
  35.  
  36. float fraction = 0.1f;
  37.  
  38. switch (key) {
  39. case GLUT_KEY_LEFT :
  40. angle -= 0.01f;
  41. lx = sin(angle);
  42. lz = -cos(angle);
  43. break;
  44. case GLUT_KEY_RIGHT :
  45. angle += 0.01f;
  46. lx = sin(angle);
  47. lz = -cos(angle);
  48. break;
  49. case GLUT_KEY_UP :
  50. camx += lx * fraction;
  51. camz += lz * fraction;
  52. break;
  53. case GLUT_KEY_DOWN :
  54. camx -= lx * fraction;
  55. camz -= lz * fraction;
  56. break;
  57. }
  58. }
  59.  
  60.  
  61. void initializeTerrain()
  62. {
  63. for (z = 0; z < MAP_Z; z++)
  64. {
  65. for (x = 0; x < MAP_X; x++)
  66. {
  67. terrain[x][z][0] = (float)(x);
  68. terrain[x][z][1] = (float)height[x][z];
  69. terrain[x][z][2] = -(float)(z);
  70. }
  71. }
  72. }
  73.  
  74. void display(void){
  75.  
  76. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  77. glLoadIdentity();
  78. gluLookAt( camx, 1.0f, camz,
  79. camx+lx, 1.0f, camz+lz,
  80. 0.0f, 1.0f, 0.0f);
  81.  
  82.  
  83. glColor4f(1.0, 0.0, 0.0,0.5);
  84.  
  85. for (z = 0; z < MAP_Z-1; z++)
  86. {
  87. glBegin(GL_TRIANGLE_STRIP);
  88. for (x = 0; x < MAP_X-1; x++)
  89. {
  90. glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
  91.  
  92. glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);
  93.  
  94. glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);
  95.  
  96. glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
  97. }
  98. glEnd();
  99. }
  100.  
  101. glutSwapBuffers();
  102. }
  103.  
  104. void myInit(){
  105. initializeTerrain();
  106. glMatrixMode(GL_PROJECTION);
  107. glViewport(0, 0, 500, 500);
  108. glMatrixMode(GL_PROJECTION);
  109. glLoadIdentity();
  110. glMatrixMode(GL_MODELVIEW);
  111. glClearColor(1.0, 1.0, 1.0, 1.0);
  112.  
  113.  
  114. }
  115.  
  116. void reshape(int w, int h)
  117. {
  118. glViewport(0, 0, w, h);
  119. glMatrixMode(GL_PROJECTION);
  120. glLoadIdentity();
  121. if (w <= h)
  122. glOrtho(-10.0, 10.0, -5.0 * (GLfloat) h / (GLfloat) w,
  123. 15.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
  124. else
  125. glOrtho(-10.0 * (GLfloat) w / (GLfloat) h,
  126. 10.0 * (GLfloat) w / (GLfloat) h, -5.0, 15.0, -10.0, 10.0);
  127. glMatrixMode(GL_MODELVIEW);
  128. glLoadIdentity();
  129. }
  130.  
  131. void main(int argc, char** argv){
  132.  
  133. glutInit(&argc, argv);
  134. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  135. glutInitWindowSize(500, 500);
  136. glutCreateWindow("A3");
  137. myInit();
  138. glutReshapeFunc(reshape);
  139. glutDisplayFunc(display);
  140. glutSpecialFunc(processSpecialKeys);
  141. glEnable(GL_DEPTH_TEST);
  142. glutMainLoop();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement