main.cc 8.5 KB

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