render_opengl.cpp
资源名称:chap02.rar [点击查看]
上传用户:btjssb159
上传日期:2022-07-13
资源大小:107k
文件大小:2k
源码类别:
3D图形编程
开发平台:
Visual C++
- #include <windows.h>
- // OpenGL extensions header
- #include "glew.h"
- #include "wglew.h"
- // Standard OpenGL header
- #include <GL/gl.h>
- #include "Gut.h"
- #include "render_data.h"
- static GLuint g_VertexProgramID = 0;
- static GLuint g_FragmentProgramID = 0;
- bool InitResourceOpenGL(void)
- {
- g_VertexProgramID = GutLoadVertexProgramOpenGL_ASM
- ("../../shaders/vertex_transform.glvp");
- if ( g_VertexProgramID==0 )
- return false;
- g_FragmentProgramID = GutLoadFragmentProgramOpenGL_ASM
- ("../../shaders/vertex_transform.glpp");
- if ( g_FragmentProgramID==0 )
- return false;
- // `镜头坐标系转换矩阵`
- Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
- // `投影转换矩阵`
- Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_OpenGL(90.0f, 1.0f, 1.0f, 100.0f);
- // `前两个矩阵相乘结果`
- Matrix4x4 view_projection_matrix = view_matrix * projection_matrix;
- // `设置视角转换矩阵`
- glProgramLocalParameters4fvEXT(GL_VERTEX_PROGRAM_ARB, 0, 4, (float *) &view_projection_matrix);
- return true;
- }
- bool ReleaseResourceOpenGL(void)
- {
- if ( g_VertexProgramID )
- {
- GutReleaseVertexProgramOpenGL(g_VertexProgramID);
- g_VertexProgramID = 0;
- }
- if ( g_FragmentProgramID )
- {
- GutReleaseVertexProgramOpenGL(g_FragmentProgramID);
- g_FragmentProgramID = 0;
- }
- return true;
- }
- // `使用OpenGL来绘图`
- void RenderFrameOpenGL(void)
- {
- // `清除画面`
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // `指定要使用Shader可程序化流程`
- glEnable(GL_VERTEX_PROGRAM_ARB);
- glEnable(GL_FRAGMENT_PROGRAM_ARB);
- // `设置使用哪个Vertex/Fragment Program`
- glBindProgramARB(GL_VERTEX_PROGRAM_ARB, g_VertexProgramID);
- glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, g_FragmentProgramID);
- // `画出金字塔的8条边线`
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(4, GL_FLOAT, sizeof(Vector4), g_vertices);
- glDrawArrays(GL_LINES, 0, 16);
- // `把背景backbuffer的画面显示出来`
- GutSwapBuffersOpenGL();
- }