render_opengl.cpp
上传用户:gzqinmao
上传日期:2022-07-13
资源大小:472k
文件大小:1k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <GL/gl.h>
  3. #include "Gut.h"
  4. #include "render_data.h"
  5. static Matrix4x4 g_view_matrix;
  6. bool InitResourceOpenGL(void)
  7. {
  8. // `计算出一个可以转换到镜头坐标系的矩阵`
  9. g_view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
  10. // `投影矩阵`
  11. Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_OpenGL(60.0f, 1.0f, 1.0f, 100.0f);
  12. // `设置视角转换矩阵`
  13. glMatrixMode(GL_PROJECTION);
  14. glLoadMatrixf( (float *) &projection_matrix);
  15. return true;
  16. }
  17. bool ReleaseResourceOpenGL(void)
  18. {
  19. // `没做任何事`
  20. return true;
  21. }
  22. // `使用OpenGL来绘图`
  23. void RenderFrameOpenGL(void)
  24. {
  25. // `清除画面`
  26. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  27. // `设置好GPU要去哪读取顶点数据`
  28. glEnableClientState(GL_VERTEX_ARRAY);
  29. glVertexPointer(4, GL_FLOAT, sizeof(Vector4), g_vertices);
  30. // `设置要改变GL_MODELVIEW矩阵`
  31. glMatrixMode(GL_MODELVIEW);
  32. // `4个金字塔的位置`
  33. Vector4 pos[4] = 
  34. {
  35. Vector4(-1.0f, -1.0f, 0.0f),
  36. Vector4( 1.0f, -1.0f, 0.0f),
  37. Vector4(-1.0f,  1.0f, 0.0f),
  38. Vector4( 1.0f,  1.0f, 0.0f),
  39. };
  40. Matrix4x4 world_matrix;
  41. Matrix4x4 world_view_matrix;
  42. for ( int i=0; i<4; i++ )
  43. {
  44. // `得到位移矩阵`
  45. world_matrix.Translate_Replace(pos[i]); 
  46. world_view_matrix = world_matrix * g_view_matrix;
  47. // `设置转换矩阵'
  48. glLoadMatrixf( (float *) &world_view_matrix);
  49. // `画出金字塔的8条边线`
  50. glDrawArrays(GL_LINES, 0, 16);
  51. }
  52. // `把背景backbuffer显示出来`
  53. GutSwapBuffersOpenGL();
  54. }