Browse Source

通过depth buffer得到阴影图

master
blobt 4 years ago
parent
commit
c411d87c69
  1. BIN
      .vs/shader3/v14/.suo
  2. 7
      FBO.cpp
  3. 56
      main.cpp
  4. 11
      res/shader/depthrender.fs
  5. 4
      res/shader/sample_depth_buffer.fs
  6. 12
      res/shader/sample_depth_buffer.vs
  7. 4
      shader3.vcxproj
  8. 12
      shader3.vcxproj.filters

BIN
.vs/shader3/v14/.suo

7
FBO.cpp

@ -13,11 +13,12 @@ void FBO::AttachColorBuffer(const char*bufferName, GLenum attachment, GLenum dat
glBindTexture(GL_TEXTURE_2D, colorBuffer); glBindTexture(GL_TEXTURE_2D, colorBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (dataType == GL_RGBA16F) {
if (dataType == GL_RGBA16F)
{
glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, NULL); glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, NULL);
} }
else {
else
{
glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
} }
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);

56
main.cpp

@ -81,6 +81,15 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
originalProgram.DetectAttribute("texcoord"); originalProgram.DetectAttribute("texcoord");
originalProgram.DetectUniform("U_MainTexture"); 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 hdr program //init hdr program
GPUProgram hdrProgram; GPUProgram hdrProgram;
hdrProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); hdrProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
@ -120,6 +129,16 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
lightSourceProgram.DetectUniform("V"); lightSourceProgram.DetectUniform("V");
lightSourceProgram.DetectUniform("P"); lightSourceProgram.DetectUniform("P");
//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 gpu program //init gpu program
GPUProgram gpuProgram; GPUProgram gpuProgram;
@ -188,6 +207,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(1.0f, 0.5f, -10.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 viewMatrix = glm::lookAt(glm::vec3(1.0f, 0.5f, -10.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 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glm::mat4 lightMatrix = glm::lookAt(glm::vec3(0.0f, 10.0f, 0.0f), glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 0.0f, -1.0f));
glm::mat4 lightprojectionMatrix = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 15.0f);
//³õʼ»¯fsq //³õʼ»¯fsq
FullScreenQuad fsq; FullScreenQuad fsq;
@ -208,10 +229,34 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight); HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
HDRfbo.Finish(); HDRfbo.Finish();
FBO depthfbo;
depthfbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGB, viewportWidth, viewportHeight);
depthfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
depthfbo.Finish();
ShowWindow(hwnd, SW_SHOW); ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd); UpdateWindow(hwnd);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glEnable(GL_DEPTH_TEST);
depthfbo.Bind();
glUseProgram(depthProgram.mProgram);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(depthProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(depthProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(lightMatrix));
glUniformMatrix4fv(depthProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(lightprojectionMatrix));
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();
glUseProgram(0);
depthfbo.Unbind();
MSG msg; MSG msg;
while (true) while (true)
{ {
@ -224,7 +269,6 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
} }
glEnable(GL_DEPTH_TEST);
HDRfbo.Bind(); HDRfbo.Bind();
@ -311,10 +355,10 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0); glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord")); fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
glUseProgram(hdrProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
glUniform1i(hdrProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightTop(hdrProgram.GetLocation("pos"), hdrProgram.GetLocation("texcoord"));
glUseProgram(depthRenderProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, depthfbo.GetBuffer("depth"));
glUniform1i(depthRenderProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightTop(depthRenderProgram.GetLocation("pos"), depthRenderProgram.GetLocation("texcoord"));
glUseProgram(gaussianProgram.mProgram); glUseProgram(gaussianProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color")); glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));

11
res/shader/depthrender.fs

@ -0,0 +1,11 @@
varying vec2 V_Texcoord;
uniform sampler2D U_MainTexture;
void main()
{
float depthValue=texture2D(U_MainTexture,V_Texcoord).r;
depthValue = pow(depthValue, 2);
gl_FragColor=vec4(vec3(depthValue),1.0);
}

4
res/shader/sample_depth_buffer.fs

@ -0,0 +1,4 @@
void main()
{
}

12
res/shader/sample_depth_buffer.vs

@ -0,0 +1,12 @@
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
void main()
{
gl_Position=P*V*M*vec4(pos,1.0);
}

4
shader3.vcxproj

@ -82,7 +82,11 @@
<ClInclude Include="utils.h" /> <ClInclude Include="utils.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="res\shader\depthrender.fs" />
<None Include="res\shader\fullscreenquad.vs" />
<None Include="res\shader\hdr.fs" /> <None Include="res\shader\hdr.fs" />
<None Include="res\shader\sample_depth_buffer.fs" />
<None Include="res\shader\sample_depth_buffer.vs" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

12
shader3.vcxproj.filters

@ -49,5 +49,17 @@
<None Include="res\shader\hdr.fs"> <None Include="res\shader\hdr.fs">
<Filter>src</Filter> <Filter>src</Filter>
</None> </None>
<None Include="res\shader\sample_depth_buffer.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\sample_depth_buffer.vs">
<Filter>src</Filter>
</None>
<None Include="res\shader\depthrender.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\fullscreenquad.vs">
<Filter>src</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>
Loading…
Cancel
Save