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.
241 lines
8.4 KiB
241 lines
8.4 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 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.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");
|
|
|
|
|
|
//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(10.0f, 10.0f, 10.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 projectorMatrix = glm::translate<float>(0.5f, 0.5f, 0.5f)*glm::scale(0.5f, 0.5f, 0.5f) * projectionMatrix * viewMatrix;
|
|
|
|
//³õʼ»¯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();
|
|
|
|
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/grass.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);
|
|
|
|
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();
|
|
|
|
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));
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
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"));
|
|
|
|
glFlush();
|
|
SwapBuffers(dc);
|
|
}
|
|
return 0;
|
|
}
|