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.
 
 
 

366 lines
15 KiB

#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 = "OpenGL";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
ATOM atom = RegisterClassEx(&wndClass);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 1280;
rect.bottom = 720;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
HWND hwnd = CreateWindowEx(NULL, "OpenGL", "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();
//init fsqgpu program
GPUProgram originalProgram;
originalProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
originalProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs");
originalProgram.Link();
originalProgram.DetectAttribute("pos");
originalProgram.DetectAttribute("texcoord");
originalProgram.DetectUniform("U_MainTexture");
//init depth render program
GPUProgram depthRenderProgram;
depthRenderProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
depthRenderProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/depthrender.fs");
depthRenderProgram.Link();
depthRenderProgram.DetectAttribute("pos");
depthRenderProgram.DetectAttribute("texcoord");
depthRenderProgram.DetectUniform("U_MainTexture");
//init depth program
GPUProgram depthProgram;
depthProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample_depth_buffer.vs");
depthProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sample_depth_buffer.fs");
depthProgram.Link();
depthProgram.DetectAttribute("pos");
depthProgram.DetectUniform("M");
depthProgram.DetectUniform("V");
depthProgram.DetectUniform("P");
//init projector program
GPUProgram sampleProgram;
sampleProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/sample.vs");
sampleProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/sample.fs");
sampleProgram.Link();
sampleProgram.DetectAttribute("pos");
sampleProgram.DetectAttribute("texcoord");
sampleProgram.DetectUniform("M");
sampleProgram.DetectUniform("V");
sampleProgram.DetectUniform("P");
sampleProgram.DetectUniform("U_MainTexture");
//init projective texture program
GPUProgram projectedTextureProgram;
projectedTextureProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs");
projectedTextureProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector_shadow.fs");
projectedTextureProgram.Link();
projectedTextureProgram.DetectAttribute("pos");
projectedTextureProgram.DetectAttribute("texcoord");
projectedTextureProgram.DetectUniform("M");
projectedTextureProgram.DetectUniform("V");
projectedTextureProgram.DetectUniform("P");
projectedTextureProgram.DetectUniform("U_ShadowMap");
projectedTextureProgram.DetectUniform("U_MainTexture");
projectedTextureProgram.DetectUniform("U_ProjectiveTexture");
projectedTextureProgram.DetectUniform("U_ProjectorMatrix");
projectedTextureProgram.DetectUniform("U_ProjectorNoTransScaleMatrix");
//init projective texture program2
GPUProgram projectedTextureProgram2;
projectedTextureProgram2.AttachShader(GL_VERTEX_SHADER, "res/shader/projector.vs");
projectedTextureProgram2.AttachShader(GL_FRAGMENT_SHADER, "res/shader/projector_shadow2.fs");
projectedTextureProgram2.Link();
projectedTextureProgram2.DetectAttribute("pos");
projectedTextureProgram2.DetectAttribute("texcoord");
projectedTextureProgram2.DetectUniform("M");
projectedTextureProgram2.DetectUniform("V");
projectedTextureProgram2.DetectUniform("P");
projectedTextureProgram2.DetectUniform("U_ShadowMap");
projectedTextureProgram2.DetectUniform("U_MainTexture");
projectedTextureProgram2.DetectUniform("U_ProjectiveTexture");
projectedTextureProgram2.DetectUniform("U_ProjectorMatrix");
projectedTextureProgram2.DetectUniform("U_ProjectorNoTransScaleMatrix");
//init 3d model
ObjModel obj, quad, sphere;
obj.Init("res/model/Cube.obj");
quad.Init("res/model/Quad.obj");
sphere.Init("res/model/Sphere.obj");
float identity[] = {
1.0f,0,0,0,
0,1.0f,0,0,
0,0,1.0f,0,
0,0,0,1.0f
};
glm::mat4 model = glm::translate<float>(6.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 quadModel = glm::translate<float>(6.0f, -1.5f, -6.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(20.0f, 20.0f, 20.0f);
glm::mat4 sphereModel = glm::translate<float>(0.0f, 0.0f, 0.0f) * glm::scale(0.3f, 0.3f, 0.3f);
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(10.0f, 3.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
glm::mat4 projectorViewMatrix = glm::lookAt(glm::vec3(0.0f, 4.0f, 0.0f), glm::vec3(6.0f, -1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projectorProjectionMatrix = glm::perspective(20.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
glm::mat4 projectorMatrix = glm::translate<float>(0.5f, 0.5f, 0.5f)*glm::scale(0.5f, 0.5f, 0.5f) * projectorProjectionMatrix * projectorViewMatrix;
glm::mat4 projectorNoTransScaleMatrix = projectorProjectionMatrix * projectorViewMatrix;
//³õʼ»¯fsq
FullScreenQuad fsq;
fsq.Init();
//³õʼ»¯FBO
FBO originalFbo;
originalFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
originalFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
originalFbo.Finish();
FBO projectiveTextureFbo;
projectiveTextureFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
projectiveTextureFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
projectiveTextureFbo.Finish();
FBO projectiveTextureFbo2;
projectiveTextureFbo2.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
projectiveTextureFbo2.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
projectiveTextureFbo2.Finish();
FBO depthFbo;
depthFbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGB, viewportWidth, viewportHeight);
depthFbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
depthFbo.Finish();
GLuint mainTexture = SOIL_load_OGL_texture("res/image/stone.bmp", 0, 0, SOIL_FLAG_POWER_OF_TWO);
GLuint projectiveTexture = SOIL_load_OGL_texture("res/image/head.png", 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
depthFbo.Bind();
glViewport(0, 0, viewportWidth, viewportHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(depthProgram.mProgram);
glUniformMatrix4fv(depthProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectorProjectionMatrix));
glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(depthProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(projectorViewMatrix));
obj.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal"));
obj.Draw();
glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
quad.Bind(depthProgram.GetLocation("pos"), depthProgram.GetLocation("texcoord"), depthProgram.GetLocation("normal"));
quad.Draw();
depthFbo.Unbind();
glCullFace(GL_BACK);
/*ԭͼfbo*/
originalFbo.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(sampleProgram.mProgram);
glUniformMatrix4fv(sampleProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(sampleProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(sampleProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(sampleProgram.GetLocation("U_MainTexture"), 0);
obj.Bind(sampleProgram.GetLocation("pos"), sampleProgram.GetLocation("texcoord"), sampleProgram.GetLocation("normal"));
obj.Draw();
glUniformMatrix4fv(sampleProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
quad.Bind(sampleProgram.GetLocation("pos"), sampleProgram.GetLocation("texcoord"), sampleProgram.GetLocation("normal"));
quad.Draw();
originalFbo.Unbind();
/*ͶӰfbo*/
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
projectiveTextureFbo.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(projectedTextureProgram.mProgram);
glUniformMatrix4fv(projectedTextureProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(projectedTextureProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix));
glUniformMatrix4fv(projectedTextureProgram.GetLocation("U_ProjectorNoTransScaleMatrix"), 1, GL_FALSE, glm::value_ptr(projectorNoTransScaleMatrix));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(projectedTextureProgram.GetLocation("U_MainTexture"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, projectiveTexture);
glUniform1i(projectedTextureProgram.GetLocation("U_ProjectiveTexture"), 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth"));
glUniform1i(projectedTextureProgram.GetLocation("U_ShadowMap"), 2);
obj.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal"));
obj.Draw();
glUniformMatrix4fv(projectedTextureProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
quad.Bind(projectedTextureProgram.GetLocation("pos"), projectedTextureProgram.GetLocation("texcoord"), projectedTextureProgram.GetLocation("normal"));
quad.Draw();
projectiveTextureFbo.Unbind();
glDisable(GL_BLEND);
/*ͶӰfbo ´øÒõÓ°*/
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
projectiveTextureFbo2.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(projectedTextureProgram2.mProgram);
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("U_ProjectorMatrix"), 1, GL_FALSE, glm::value_ptr(projectorMatrix));
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("U_ProjectorNoTransScaleMatrix"), 1, GL_FALSE, glm::value_ptr(projectorNoTransScaleMatrix));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(projectedTextureProgram2.GetLocation("U_MainTexture"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, projectiveTexture);
glUniform1i(projectedTextureProgram2.GetLocation("U_ProjectiveTexture"), 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth"));
glUniform1i(projectedTextureProgram2.GetLocation("U_ShadowMap"), 2);
obj.Bind(projectedTextureProgram2.GetLocation("pos"), projectedTextureProgram2.GetLocation("texcoord"), projectedTextureProgram2.GetLocation("normal"));
obj.Draw();
glUniformMatrix4fv(projectedTextureProgram2.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
quad.Bind(projectedTextureProgram2.GetLocation("pos"), projectedTextureProgram2.GetLocation("texcoord"), projectedTextureProgram2.GetLocation("normal"));
quad.Draw();
projectiveTextureFbo2.Unbind();
glDisable(GL_BLEND);
MSG msg;
while (true)
{
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glClearColor(0.1f, 0.4f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(originalProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, originalFbo.GetBuffer("color"));
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
glUseProgram(originalProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, projectiveTextureFbo.GetBuffer("color"));
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
glUseProgram(depthRenderProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthFbo.GetBuffer("depth"));
glUniform1i(depthRenderProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightBottom(depthRenderProgram.GetLocation("pos"), depthRenderProgram.GetLocation("texcoord"));
glUseProgram(originalProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, projectiveTextureFbo2.GetBuffer("color"));
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftBottom(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
glFlush();
SwapBuffers(dc);
}
return 0;
}