Browse Source

通过depth buffer得到阴影图

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

BIN
.vs/shader3/v14/.suo

15
FBO.cpp

@ -13,18 +13,19 @@ 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);
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0);
mDrawBuffers.push_back(attachment); mDrawBuffers.push_back(attachment);
mBuffers.insert(std::pair<std::string,GLuint>(bufferName,colorBuffer));
mBuffers.insert(std::pair<std::string, GLuint>(bufferName, colorBuffer));
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
@ -52,7 +53,7 @@ void FBO::Finish()
{ {
GLenum *buffers = new GLenum[nCount]; GLenum *buffers = new GLenum[nCount];
int i = 0; int i = 0;
while (i < nCount)
while (i<nCount)
{ {
buffers[i++] = mDrawBuffers[i]; buffers[i++] = mDrawBuffers[i];
} }
@ -75,7 +76,7 @@ void FBO::Unbind()
GLuint FBO::GetBuffer(const char*bufferName) GLuint FBO::GetBuffer(const char*bufferName)
{ {
auto iter = mBuffers.find(bufferName); auto iter = mBuffers.find(bufferName);
if (iter!=mBuffers.end())
if (iter != mBuffers.end())
{ {
return iter->second; return iter->second;
} }

2
FBO.h

@ -12,7 +12,7 @@ public:
public: public:
FBO(); FBO();
//hdr/normal color //hdr/normal color
void AttachColorBuffer(const char*bufferName,GLenum attachment,GLenum dataType,int width,int height);
void AttachColorBuffer(const char*bufferName, GLenum attachment, GLenum dataType, int width, int height);
void AttachDepthBuffer(const char*bufferName, int width, int height); void AttachDepthBuffer(const char*bufferName, int width, int height);
//complete fbo settings //complete fbo settings
void Finish(); void Finish();

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