main.cc 7.8 KB

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