main.cc 10 KB

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