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.
262 lines
9.0 KiB
262 lines
9.0 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 = 800;
|
|
rect.bottom = 600;
|
|
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 vertical program
|
|
GPUProgram verticalProgram;
|
|
verticalProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
|
|
verticalProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_gaussian_vertical.fs");
|
|
verticalProgram.Link();
|
|
verticalProgram.DetectAttribute("pos");
|
|
verticalProgram.DetectAttribute("texcoord");
|
|
verticalProgram.DetectUniform("U_MainTexture");
|
|
|
|
//init horizontal program
|
|
GPUProgram horizontalProgram;
|
|
horizontalProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
|
|
horizontalProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_gaussian_horizontal.fs");
|
|
horizontalProgram.Link();
|
|
horizontalProgram.DetectAttribute("pos");
|
|
horizontalProgram.DetectAttribute("texcoord");
|
|
horizontalProgram.DetectUniform("U_MainTexture");
|
|
|
|
//init dilation program
|
|
GPUProgram gaussianProgram;
|
|
gaussianProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
|
|
gaussianProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_gaussian.fs");
|
|
gaussianProgram.Link();
|
|
gaussianProgram.DetectAttribute("pos");
|
|
gaussianProgram.DetectAttribute("texcoord");
|
|
gaussianProgram.DetectUniform("U_MainTexture");
|
|
|
|
//init gpu program
|
|
GPUProgram gpuProgram;
|
|
gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
|
|
gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/test.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_DiffuseIntensity");
|
|
gpuProgram.DetectUniform("U_LightPos");
|
|
gpuProgram.DetectUniform("U_LightDirection");
|
|
gpuProgram.DetectUniform("U_Cutoff");
|
|
gpuProgram.DetectUniform("U_SpecularLightColor");
|
|
gpuProgram.DetectUniform("U_SpecularMaterial");
|
|
gpuProgram.DetectUniform("U_EyePos");
|
|
|
|
//init 3d model
|
|
ObjModel obj;
|
|
obj.Init("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.8f, 0.8f, 0.8f, 1.0f };
|
|
float lightPos[] = { 0.1f,3.0f, -2.2f,1.0f };
|
|
float diffuseIntensity = 4.0f;
|
|
|
|
float spotLightDirection[] = { 0.0f, -1.0f, 0.0f, 128.0f };//把最后一位改成......
|
|
float spotLightCutoff = 15.0f;
|
|
|
|
|
|
float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
|
|
float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
|
|
float eyePos[] = { 0.0f,0.0f,0.0f };
|
|
|
|
|
|
glm::mat4 model = glm::translate<float>(0.0f, 0.0f, -2.5f) * glm::rotate(-60.0f, 1.0f, 1.0f, 1.0f);
|
|
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
|
|
glm::mat4 normalMatrix = glm::inverseTranspose(model);
|
|
|
|
//初始化fsq
|
|
FullScreenQuad fsq;
|
|
fsq.Init();
|
|
|
|
//初始化FBO
|
|
FBO fbo;
|
|
fbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
|
|
fbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
|
|
fbo.Finish();
|
|
|
|
FBO fbo2;
|
|
fbo2.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
|
|
fbo2.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
|
|
fbo2.Finish();
|
|
|
|
ShowWindow(hwnd, SW_SHOW);
|
|
UpdateWindow(hwnd);
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
MSG msg;
|
|
while (true)
|
|
{
|
|
if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
|
|
{
|
|
if (msg.message==WM_QUIT)
|
|
{
|
|
break;
|
|
}
|
|
TranslateMessage(&msg);
|
|
DispatchMessage(&msg);
|
|
}
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
fbo.Bind();
|
|
glUseProgram(gpuProgram.mProgram);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
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);
|
|
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
|
|
glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
|
|
glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
|
|
glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
|
|
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
|
|
glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
|
|
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
|
|
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
|
|
obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
|
|
obj.Draw();
|
|
glUseProgram(0);
|
|
fbo.Unbind();
|
|
|
|
//blur * 2
|
|
fbo2.Bind();
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glUseProgram(gaussianProgram.mProgram);
|
|
glBindTexture(GL_TEXTURE_2D, fbo.GetBuffer("color"));
|
|
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
|
|
fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
|
|
fbo2.Unbind();
|
|
|
|
glFlush();
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, fbo.GetBuffer("color"));
|
|
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
|
|
|
|
glUseProgram(originalProgram.mProgram);
|
|
fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
|
|
|
|
|
|
glUseProgram(verticalProgram.mProgram);
|
|
fsq.DrawToRightTop(verticalProgram.GetLocation("pos"), verticalProgram.GetLocation("texcoord"));
|
|
|
|
glUseProgram(horizontalProgram.mProgram);
|
|
fsq.DrawToLeftBottom(horizontalProgram.GetLocation("pos"), horizontalProgram.GetLocation("texcoord"));
|
|
|
|
glUseProgram(gaussianProgram.mProgram);
|
|
glBindTexture(GL_TEXTURE_2D, fbo2.GetBuffer("color"));
|
|
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
|
|
fsq.DrawToRightBottom(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
|
|
|
|
SwapBuffers(dc);
|
|
}
|
|
return 0;
|
|
}
|