分别将画面渲染到屏幕的左上,坐下,右上,右下区域 效果图:
多屏渲染效果图:
原理见代码注释
- #include "FullScreenQuad.h"
- void FullScreenQuad::Init()
- {
- glGenBuffers(1, &mVBO);
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, nullptr, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- //绘制一个铺满屏幕的四边形
- void FullScreenQuad::Draw(GLint posLoc, GLint texcoordLoc)
- {
- //draw fullscreen
- float vertices[] = {
- -0.5f,-0.5f,-1.0f,0.0f,0.0f,
- 0.5f,-0.5f,-1.0f,1.0f,0.0f,
- 0.5f,0.5f,-1.0f,1.0f,1.0f,
- -0.5f,0.5f,-1.0f,0.0f,1.0f
- };
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);//绑定到vbo
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, vertices, GL_STATIC_DRAW);//顶点数据上传至显卡
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glEnableVertexAttribArray(posLoc);//启用顶点坐标属性
- //指定顶点数据在VBO中的入口地址
- glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
- glEnableVertexAttribArray(texcoordLoc);//启用顶点纹理坐标属性
- //指定顶点纹理坐标在VBO中的入口地址
- glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float)*3));
- glDrawArrays(GL_QUADS, 0, 4);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- //绘制左上角四分之一的矩形
- void FullScreenQuad::DrawToLeftTop(GLint posLoc, GLint texcoordLoc)
- {
- float vertices[] = {
- -0.5f,0.0f,-1.0f,0.0f,0.0f,
- 0.0f,0.0f,-1.0f,1.0f,0.0f,
- 0.0f,0.5f,-1.0f,1.0f,1.0f,
- -0.5f,0.5f,-1.0f,0.0f,1.0f
- };
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glEnableVertexAttribArray(posLoc);
- glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
- glEnableVertexAttribArray(texcoordLoc);
- glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
- glDrawArrays(GL_QUADS, 0, 4);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- //绘制左下角四分之的矩形
- void FullScreenQuad::DrawToLeftBottom(GLint posLoc, GLint texcoordLoc)
- {
- float vertices[] = {
- -0.5f,-0.5f,-1.0f,0.0f,0.0f,
- 0.0f,-0.5f,-1.0f,1.0f,0.0f,
- 0.0f,0.0f,-1.0f,1.0f,1.0f,
- -0.5f,0.0f,-1.0f,0.0f,1.0f
- };
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glEnableVertexAttribArray(posLoc);
- glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
- glEnableVertexAttribArray(texcoordLoc);
- glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
- glDrawArrays(GL_QUADS, 0, 4);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- //绘制右上角四分之一的矩形
- void FullScreenQuad::DrawToRightTop(GLint posLoc, GLint texcoordLoc)
- {
- float vertices[] = {
- 0.0f,-0.0f,-1.0f,0.0f,0.0f,
- 0.5f,-0.0f,-1.0f,1.0f,0.0f,
- 0.5f,0.5f,-1.0f,1.0f,1.0f,
- 0.0f,0.5f,-1.0f,0.0f,1.0f
- };
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glEnableVertexAttribArray(posLoc);
- glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
- glEnableVertexAttribArray(texcoordLoc);
- glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
- glDrawArrays(GL_QUADS, 0, 4);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- //绘制右下角四分之的矩形
- void FullScreenQuad::DrawToRightBottom(GLint posLoc, GLint texcoordLoc)
- {
- float vertices[] = {
- 0.0f,-0.5f,-1.0f,0.0f,0.0f,
- 0.5f,-0.5f,-1.0f,1.0f,0.0f,
- 0.5f,0.0f,-1.0f,1.0f,1.0f,
- 0.0f,0.0f,-1.0f,0.0f,1.0f
- };
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 20, vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, mVBO);
- glEnableVertexAttribArray(posLoc);
- glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
- glEnableVertexAttribArray(texcoordLoc);
- glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
- glDrawArrays(GL_QUADS, 0, 4);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
渲染入口
渲染步骤
一,使用带光照效果的shader
1,首先创建一个fbo
2,在此fbo中绘制三个带光照效果的立方体(每个立方体都有各自的模型矩阵和法线矩阵)
3,分别设置三个立方体的光源并绘制
二,切换shader,使用绘制四边形的shader
4,分别在屏幕的左上,左下,右上,右下绘制四个四边形
5,将此前绘制在fbo中的内容作为纹理贴在绘制的四个四边形中
- #include <windows.h>
- #include "glew.h"
- #include <stdio.h>
- #include <math.h>
- #include "utils.h"
- #include "GPUProgram.h"
- #include "ObjModel.h"
- #include "FBO.h"
- #include "FullScreenQuad.h"
- #include "Glm/glm.hpp"
- #include "Glm/ext.hpp"
- #pragma comment(lib,"opengl32.lib")
- #pragma comment(lib,"glew32.lib")
- LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg)
- {
- case WM_CLOSE:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProc(hwnd,msg,wParam,lParam);
- }
- INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
- {
- WNDCLASSEX wndClass;
- wndClass.cbClsExtra = 0;
- wndClass.cbSize = sizeof(WNDCLASSEX);
- wndClass.cbWndExtra = 0;
- wndClass.hbrBackground = NULL;
- wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
- wndClass.hIcon = NULL;
- wndClass.hIconSm = NULL;
- wndClass.hInstance = hInstance;
- wndClass.lpfnWndProc=GLWindowProc;
- wndClass.lpszClassName = L"OpenGL";
- wndClass.lpszMenuName = NULL;
- wndClass.style = CS_VREDRAW | CS_HREDRAW;
- ATOM atom = RegisterClassEx(&wndClass);
- RECT rect;
- rect.left = 0;
- rect.top = 0;
- rect.right = 800;
- rect.bottom = 600;
- AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
- HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInstance, NULL);
- HDC dc = GetDC(hwnd);
- PIXELFORMATDESCRIPTOR pfd;
- memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
- pfd.iLayerType = PFD_MAIN_PLANE;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- pfd.cDepthBits = 24;
- pfd.cStencilBits = 8;
- int pixelFormatID = ChoosePixelFormat(dc, &pfd);
- SetPixelFormat(dc,pixelFormatID,&pfd);
- HGLRC rc = wglCreateContext(dc);
- wglMakeCurrent(dc, rc);
- GetClientRect(hwnd, &rect);
- int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top;
- glewInit();
- GPUProgram fsqProgram;
- fsqProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
- fsqProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/fullscreenquad.fs");
- fsqProgram.Link();
- fsqProgram.DetectAttribute("pos");
- fsqProgram.DetectAttribute("texcoord");
- fsqProgram.DetectUniform("U_MainTexture");
- GPUProgram gpuProgram;
- gpuProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/Light.vs");
- gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/Light.fs");
- gpuProgram.Link();
- gpuProgram.DetectAttribute("pos");
- gpuProgram.DetectAttribute("texcoord");
- gpuProgram.DetectAttribute("normal");
- gpuProgram.DetectUniform("M");
- gpuProgram.DetectUniform("V");
- gpuProgram.DetectUniform("P");
- gpuProgram.DetectUniform("NM");
- gpuProgram.DetectUniform("U_AmbientLightColor");
- gpuProgram.DetectUniform("U_AmbientMaterial");
- gpuProgram.DetectUniform("U_DiffuseLightColor");
- gpuProgram.DetectUniform("U_DiffuseMaterial");
- gpuProgram.DetectUniform("U_SpecularLightColor");
- gpuProgram.DetectUniform("U_SpecularMaterial");
- gpuProgram.DetectUniform("U_LightPos");
- gpuProgram.DetectUniform("U_EyePos");
- gpuProgram.DetectUniform("U_LightDirection");
- gpuProgram.DetectUniform("U_Cutoff");
- gpuProgram.DetectUniform("U_DiffuseIntensity");
- //init 3d model
- ObjModel cube;
- cube.Init("Debug/res/model/Cube.obj");
- //摄像机矩阵的单位矩阵
- float identity[] = {
- 1.0f,0,0,0,
- 0,1.0f,0,0,
- 0,0,1.0f,0,
- 0,0,0,1.0f
- };
- float ambientLightColor[] = { 0.4f,0.4f,0.4f,1.0f };//环境光颜色
- float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };//环境光材质
- float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };//漫反射光颜色
- float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f };//漫反射光材质
- float diffuseIntensity = 1.0f;//漫反射光强度
- float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };//环境光颜色
- float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };//环境光材质
- float lightPos[] = { 0.0f,1.5f,0.0f,0.0f };//光源位置
- float spotLightDirection[] = { 0.0f,-1.0f,0.0f,128.0f };//聚光灯中心线向量
- float spotLightCutoff = 0.0f;// 聚光灯中心线向量和入射光线最大夹角
- float eyePos[] = { 0.0f,0.0f,0.0f };//眼睛的位置
- //模型矩阵
- glm::mat4 model1 = glm::translate<float>(-2.0f, 0.0f, -6.0f)*glm::rotate(-30.0f,1.0f,1.0f,1.0f);
- glm::mat4 model2 = glm::translate<float>(0.0f, 0.0f, -6.0f)*glm::rotate(-30.0f,1.0f,1.0f,1.0f);
- glm::mat4 model3 = glm::translate<float>(2.0f, 0.0f, -6.0f)*glm::rotate(-30.0f,1.0f,1.0f,1.0f);
- //投影矩阵
- glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
- //将法线转换到世界空间的变换矩阵
- glm::mat4 normalMatrix1 = glm::inverseTranspose(model1);
- glm::mat4 normalMatrix2 = glm::inverseTranspose(model2);
- glm::mat4 normalMatrix3 = glm::inverseTranspose(model3);
- FullScreenQuad fsq;
- fsq.Init();
- FBO fbo;
- fbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
- fbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
- fbo.Finish();
- //glClearColor(41.0f/255.0f, 71.0f/255.0f, 121.0f / 255.0f, 1.0f);
- ShowWindow(hwnd, SW_SHOW);
- UpdateWindow(hwnd);
- bool isFullScreen = true;
- MSG msg;
- while (true)
- {
- if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
- {
- if (msg.message==WM_QUIT)
- {
- break;
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- //绑定fbo----将绘制的场景绑定到FBO
- fbo.Bind();
- glClearColor(0.0f,0.0f,0.0f,1.0f);
- //这步很重要
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- glUseProgram(gpuProgram.mProgram);
- //设置shader中变量的值
- glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
- glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
- glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1, ambientLightColor);
- glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
- glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
- glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
- glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
- glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
- glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
- glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
- glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
- //设置方向光光源的位置
- lightPos[0] = 0.0f;
- lightPos[1] = 1.5f;
- lightPos[2] = 0.0f;
- lightPos[3] = 0.0f;
- //漫反射光照强度
- diffuseIntensity = 1.0f;
- //聚光灯中心线和入射光线最大夹角
- spotLightCutoff = 0.0f;
- //设置shader中变量的值
- glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
- glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
- glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
- glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model1));
- glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix1));
- //绘制被方向光照射的立方体
- cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
- cube.Draw();
- //设置点光源的位置
- lightPos[0] = 0.0f;
- lightPos[1] = 3.0f;
- lightPos[2] = -6.0f;
- lightPos[3] = 1.0f;
- diffuseIntensity = 3.0f;
- //设置shader中变量的值
- glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
- glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
- glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model2));
- glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix2));
- //绘制被点光源照射的立方体
- cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
- cube.Draw();
- //设置聚光灯的位置
- lightPos[0] = 2.0f;
- lightPos[1] = 3.0f;
- lightPos[2] = -6.0f;
- lightPos[3] = 1.0f;
- diffuseIntensity = 4.0f;
- spotLightCutoff = 15.0f;//degree
- glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
- glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
- glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
- glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model3));
- glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix3));
- //绘制被聚光灯照射的立方体
- cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
- cube.Draw();
- glUseProgram(0);
- glFlush();
- fbo.Unbind();
- glClearColor(0.1f, 0.4f, 0.7f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//这步很重要
- glUseProgram(fsqProgram.mProgram);
- glActiveTexture(GL_TEXTURE0);
- //将fbo中的内容作为纹理
- glBindTexture(GL_TEXTURE_2D,fbo.GetBuffer("color"));
- glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0);
- if (isFullScreen){ //分屏渲染
- fsq.DrawToLeftTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
- fsq.DrawToLeftBottom(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
- fsq.DrawToRightTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
- fsq.DrawToRightBottom(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
- }
- else //绘制一个充满屏幕的四边形
- {
- fsq.Draw(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
- }
- glUseProgram(0);
- glFlush();
- SwapBuffers(dc);
- }
- return 0;
- }