main.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "Angel.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <vector>
  5. #define PARAMS_DEBUG_PRINT false
  6. const int texNumTriangles = 64770;
  7. const int cowNumTriangles = 5804;
  8. const int NumTriangles = texNumTriangles + cowNumTriangles;
  9. const int NumVertices = 3 * NumTriangles;
  10. const int TextureWidth = 256;
  11. const int TextureHeight = 128;
  12. typedef Angel::vec4 point4;
  13. typedef Angel::vec4 color4;
  14. // terrain altitude array
  15. float ter_alt[128][256];
  16. // Texture objects and storage for texture image
  17. GLuint textures[1];
  18. GLubyte image[TextureWidth][TextureHeight][3];
  19. // Vertex data arrays
  20. point4 points[NumVertices];
  21. vec2 tex_coords[NumVertices];
  22. // fog params
  23. GLuint fog; // is_foggy uniform
  24. GLuint eye_pos;
  25. int foggy = 1;
  26. // Viewing transformation parameters
  27. GLfloat theta = 0.0;
  28. GLfloat phi = 0.0;
  29. GLfloat tau = 0.0;
  30. GLfloat eye_x = 0.0;
  31. GLfloat eye_y = 0.25;
  32. GLfloat eye_z = 0.1;
  33. GLfloat eye_w = 1.0;
  34. GLfloat at_x;
  35. GLfloat at_y;
  36. GLfloat at_z;
  37. GLfloat at_w;
  38. GLuint model_view; // model-view matrix uniform shader variable location
  39. // Projection transformation parameters
  40. GLfloat left = -0.01, right = 0.01;
  41. GLfloat bottom = -0.01, top = 0.01;
  42. GLfloat zNear = 0.1, zFar = 2.0;
  43. GLuint projection; // projection matrix uniform shader variable location
  44. //----------------------------------------------------------------------------
  45. bool hit_check(GLfloat &x, GLfloat &y, GLfloat &z) {
  46. int x_idx = x * 256;
  47. int y_idx = y * 256;
  48. if (x_idx > 255 || x_idx < 0 || y_idx < 0 || y_idx > 127) {
  49. x = 0.0;
  50. y = 0.25;
  51. z = 0.1;
  52. return false;
  53. }
  54. if (PARAMS_DEBUG_PRINT) {
  55. printf("%f, %f\n", ter_alt[y_idx][x_idx], z);
  56. }
  57. if (ter_alt[y_idx][x_idx] >= z) {
  58. x = 0.0;
  59. y = 0.25;
  60. z = 0.1;
  61. return false;
  62. }
  63. return true;
  64. }
  65. void load_model(const std::string &file_name, bool not_tex = false,
  66. float x_offset = 0.0f, float y_offset = 0.0f,
  67. float z_offset = 0.0f) {
  68. std::vector<point4> vertices;
  69. std::vector<vec2> tex_cods;
  70. std::fstream s(file_name, std::fstream::in);
  71. static int index = 0;
  72. if (!s.is_open()) {
  73. std::cerr << "failed to open" << file_name << std::endl;
  74. } else {
  75. std::string t;
  76. float x, y, z;
  77. int a, b, c;
  78. int num = 0;
  79. int idx = 0;
  80. while (s >> t) {
  81. if (t == "v") { // vertices
  82. s >> x >> y >> z;
  83. vertices.emplace_back(x / 50.0f + x_offset, z / 50.0f + z_offset,
  84. y / 50.0f + y_offset, 1.0);
  85. if (!not_tex) {
  86. *(reinterpret_cast<float*>(ter_alt) + idx) = y / 50.0f;
  87. idx++;
  88. }
  89. } else if (t == "vt") {
  90. s >> x >> y;
  91. tex_cods.emplace_back(x, y);
  92. } else if (t == "f") {
  93. s >> a >> b >> c;
  94. a--;
  95. b--;
  96. c--;
  97. points[index] = vertices[a];
  98. if (not_tex) {
  99. tex_coords[index] = {20.0, 0.0};
  100. } else {
  101. tex_coords[index] = tex_cods[a];
  102. }
  103. index++;
  104. points[index] = vertices[b];
  105. if (not_tex) {
  106. tex_coords[index] = {20.0, 0.0};
  107. } else {
  108. tex_coords[index] = tex_cods[b];
  109. }
  110. index++;
  111. points[index] = vertices[c];
  112. if (not_tex) {
  113. tex_coords[index] = {20.0, 0.0};
  114. } else {
  115. tex_coords[index] = tex_cods[c];
  116. }
  117. index++;
  118. num++;
  119. }
  120. }
  121. std::cout << "Load model:" << file_name << std::endl;
  122. std::cout << "There are " << vertices.size() << "vertice(s)"
  123. << ", " << tex_cods.size() << "tex cor(s)" << std::endl
  124. << "There are " << num << "fragment(s)" << std::endl;
  125. s.close();
  126. }
  127. }
  128. //----------------------------------------------------------------------------
  129. void load_tex(const std::string &file_name) {
  130. char cline[1024];
  131. char p6[10];
  132. int width;
  133. int height;
  134. int depth;
  135. FILE *fp = fopen(file_name.data(), "rb");
  136. fgets(cline, sizeof(cline), fp);
  137. sscanf(cline, "%s %d %d %d", p6, &width, &height, &depth);
  138. std::cout << p6 << "," << width << "," << height << "," << depth << std::endl;
  139. fread(image, sizeof(GLubyte), width * height * 3, fp);
  140. fclose(fp);
  141. }
  142. void init() {
  143. load_tex("gc_tex.ppm");
  144. load_model("gc_tex.obj");
  145. load_model("cow.obj", true, 0.90, 0.05, 0.25); // no tex data in cow obj
  146. // Initialize texture objects
  147. glGenTextures(1, textures);
  148. glBindTexture(GL_TEXTURE_2D, textures[0]);
  149. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureWidth, TextureHeight, 0, GL_RGB,
  150. GL_UNSIGNED_BYTE, image);
  151. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  152. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  153. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  154. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  155. glActiveTexture(GL_TEXTURE0);
  156. glBindTexture(GL_TEXTURE_2D, textures[0]);
  157. // Create a vertex array object
  158. GLuint vao;
  159. glGenVertexArrays(1, &vao);
  160. glBindVertexArray(vao);
  161. // Create and initialize a buffer object
  162. GLuint buffer;
  163. glGenBuffers(1, &buffer);
  164. glBindBuffer(GL_ARRAY_BUFFER, buffer);
  165. glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(tex_coords), NULL,
  166. GL_STATIC_DRAW);
  167. // Specify an offset to keep track of where we're placing data in our
  168. // vertex array buffer. We'll use the same technique when we
  169. // associate the offsets with vertex attribute pointers.
  170. GLintptr offset = 0;
  171. glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(points), points);
  172. offset += sizeof(points);
  173. glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(tex_coords), tex_coords);
  174. // Load shaders and use the resulting shader program
  175. GLuint program = InitShader("vshader71.glsl", "fshader71.glsl");
  176. glUseProgram(program);
  177. // set up vertex arrays
  178. offset = 0;
  179. GLuint vPosition = glGetAttribLocation(program, "vPosition");
  180. glEnableVertexAttribArray(vPosition);
  181. glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0,
  182. BUFFER_OFFSET(offset));
  183. offset += sizeof(points);
  184. GLuint vTexCoord = glGetAttribLocation(program, "vTexCoord");
  185. glEnableVertexAttribArray(vTexCoord);
  186. glVertexAttribPointer(vTexCoord, 2, GL_FLOAT, GL_FALSE, 0,
  187. BUFFER_OFFSET(offset));
  188. // Set the value of the fragment shader texture sampler variable
  189. // ("texture") to the the appropriate texture unit. In this case,
  190. // zero, for GL_TEXTURE0 which was previously set by calling
  191. // glActiveTexture().
  192. glUniform1i(glGetUniformLocation(program, "tex"), 0);
  193. model_view = glGetUniformLocation(program, "model_view");
  194. projection = glGetUniformLocation(program, "projection");
  195. fog = glGetUniformLocation(program, "foggy");
  196. eye_pos = glGetUniformLocation(program, "eye_pos");
  197. glEnable(GL_DEPTH_TEST);
  198. glClearColor(1, 1, 1, 1);
  199. }
  200. void display(void) {
  201. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  202. point4 eye(eye_x, eye_y, eye_z, 1.0);
  203. at_x = cos(theta) * cos(phi) * 100.0;
  204. at_y = sin(theta) * cos(phi) * 100.0;
  205. at_z = sin(phi) * 100.0;
  206. point4 at(at_x, at_y, at_z, 1.0);
  207. vec4 up(0.0, 100 * sin(tau), 100 * cos(tau), 0.0);
  208. mat4 mv = LookAt(eye, at, up);
  209. glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);
  210. mat4 p = Frustum(left, right, bottom, top, zNear, zFar);
  211. glUniformMatrix4fv(projection, 1, GL_TRUE, p);
  212. glUniform3f(eye_pos, eye_x, eye_y, eye_z);
  213. glUniform1i(fog, foggy);
  214. glDrawArrays(GL_TRIANGLES, 0, NumVertices);
  215. glutSwapBuffers();
  216. }
  217. //----------------------------------------------------------------------------
  218. void mouse(int button, int state, int x, int y) {}
  219. //----------------------------------------------------------------------------
  220. void idle(void) { glutPostRedisplay(); }
  221. //----------------------------------------------------------------------------
  222. void keyboard(unsigned char key, int mousex, int mousey) {
  223. switch (key) {
  224. case 033: // Escape Key
  225. exit(0);
  226. break;
  227. case 'f':
  228. if (foggy > 0) {
  229. foggy = 0;
  230. } else {
  231. foggy = 1;
  232. }
  233. break;
  234. case '1':
  235. tau += 0.01;
  236. break;
  237. case '3':
  238. tau -= 0.01;
  239. break;
  240. case 'q':
  241. theta += 0.01;
  242. break;
  243. case 'e':
  244. theta -= 0.01;
  245. break;
  246. case 'A':
  247. at_x += 0.01;
  248. break;
  249. case 'D':
  250. at_x -= 0.001;
  251. break;
  252. case 'w':
  253. eye_x += 0.01;
  254. break;
  255. case 's':
  256. eye_x -= 0.01;
  257. break;
  258. case 'd':
  259. eye_y -= 0.01;
  260. break;
  261. case 'a':
  262. eye_y += 0.01;
  263. break;
  264. case '9':
  265. eye_z -= 0.01;
  266. break;
  267. case '6':
  268. eye_z += 0.01;
  269. break;
  270. case '8':
  271. phi -= 0.01;
  272. break;
  273. case '5':
  274. phi += 0.01;
  275. break;
  276. }
  277. if (PARAMS_DEBUG_PRINT) {
  278. printf("eye_x,y,z:%f, %f, %f, %f\n", eye_x, eye_y, eye_z, eye_w);
  279. printf("at_x,y,z:%f, %f, %f, %f\n", at_x, at_y, at_z, at_w);
  280. }
  281. hit_check(eye_x, eye_y, eye_z);
  282. glutPostRedisplay();
  283. }
  284. //----------------------------------------------------------------------------
  285. int main(int argc, char **argv) {
  286. glutInit(&argc, argv);
  287. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  288. glutInitWindowSize(512, 512);
  289. glutInitContextVersion(3, 1);
  290. glutInitContextProfile(GLUT_CORE_PROFILE);
  291. glutCreateWindow("flying");
  292. if (glewInit() != GLEW_OK) {
  293. std::cerr << "Failed to initialize GLEW ... exiting" << std::endl;
  294. exit(EXIT_FAILURE);
  295. }
  296. init();
  297. glutDisplayFunc(display);
  298. glutKeyboardFunc(keyboard);
  299. glutMouseFunc(mouse);
  300. // glutIdleFunc(idle);
  301. glutMainLoop();
  302. return 0;
  303. }