You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

5 years ago
  1. #include <stdio.h>
  2. #include <glew.h>
  3. #include "../common/base.h"
  4. #include "../common/CELLMath.hpp"
  5. #include "../common/GlewOpenGL.h"
  6. #include "tiny_obj_loader.h"
  7. using namespace std;
  8. using namespace CELL;
  9. class Viewer : public GlewOpenGL {
  10. public:
  11. struct Vertex {
  12. float x, y, z;
  13. float u, v;
  14. };
  15. Viewer(int width, int height, const string& title) : GlewOpenGL(width, height, title) {
  16. }
  17. void init() {
  18. _texture = createTexture("/home/blobt/Documents/dev/cpp/opengl2/models/chair/cloth.jpg");
  19. }
  20. void render() {
  21. glMatrixMode(GL_PROJECTION);
  22. glLoadIdentity();
  23. gluPerspective(100, double(_width) / double(_height), 0.1, 1000);
  24. Vertex vertex[] = {
  25. {-1, -1.5, 0, 0, 0},
  26. {1, -1.5, 0, 1, 0},
  27. {1, 1.5, 0, 1, 1},
  28. {-1, 1.5, 0, 0, 1},
  29. };
  30. glMatrixMode(GL_MODELVIEW);
  31. glLoadIdentity();
  32. glTranslatef(0, 0, -2);
  33. //开始深度测试,后面绘制的面就不会遮挡前面绘制的面
  34. glEnable(GL_DEPTH_TEST);
  35. //设置顶点
  36. glEnableClientState(GL_VERTEX_ARRAY);
  37. glVertexPointer(3, GL_FLOAT, sizeof (Vertex), &vertex[0].x);
  38. //设置贴图
  39. glBindTexture(GL_TEXTURE_2D, _texture);
  40. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  41. glTexCoordPointer(2, GL_FLOAT, sizeof (Vertex), &vertex[0].u);
  42. glDrawArrays(GL_QUADS, 0, 4);
  43. }
  44. ~Viewer() {
  45. }
  46. private:
  47. GLuint _texture;
  48. };
  49. int main(int argc, char** argv) {
  50. if (argc < 2) {
  51. }
  52. Viewer viewer(1024, 768, "Viewer");
  53. viewer.loop();
  54. return 0;
  55. }