Browse Source

膨胀和腐蚀

master
blobt 4 years ago
parent
commit
4e88d11299
  1. BIN
      .vs/shader3/v14/.suo
  2. 60
      main.cpp
  3. 20
      res/shader/fullscreenquad_dilation.fs
  4. 21
      res/shader/fullscreenquad_erosion.fs
  5. 2
      shader3.vcxproj
  6. 6
      shader3.vcxproj.filters

BIN
.vs/shader3/v14/.suo

60
main.cpp

@ -72,6 +72,33 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
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 erosion program
GPUProgram erosionProgram;
erosionProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
erosionProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_erosion.fs");
erosionProgram.Link();
erosionProgram.DetectAttribute("pos");
erosionProgram.DetectAttribute("texcoord");
erosionProgram.DetectUniform("U_MainTexture");
//init dilation program
GPUProgram dilationProgram;
dilationProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
dilationProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad_dilation.fs");
dilationProgram.Link();
dilationProgram.DetectAttribute("pos");
dilationProgram.DetectAttribute("texcoord");
dilationProgram.DetectUniform("U_MainTexture");
//init gpu program
GPUProgram gpuProgram;
gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
@ -106,6 +133,17 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
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();
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
@ -128,6 +166,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
glEnable(GL_DEPTH_TEST);
glUseProgram(gpuProgram.mProgram);
fbo.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
@ -135,11 +175,29 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
obj.Draw();
fbo.Unbind();
glUseProgram(0);
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(erosionProgram.mProgram);
fsq.DrawToRightTop(erosionProgram.GetLocation("pos"), erosionProgram.GetLocation("texcoord"));
glUseProgram(dilationProgram.mProgram);
fsq.DrawToLeftBottom(dilationProgram.GetLocation("pos"), dilationProgram.GetLocation("texcoord"));
SwapBuffers(dc);
}
return 0;

20
res/shader/fullscreenquad_dilation.fs

@ -0,0 +1,20 @@
varying vec2 V_Texcoord;
uniform sampler2D U_MainTexture;
void main()
{
vec4 maxValue = vec4(0.0);
int coreSize = 2;
int halfCoreSize = coreSize / 2;
float texelOffset = 1/100.0;
for(int y=0; y<coreSize; y++){
for(int x=0; x<coreSize; x++){
vec4 color = texture2D(U_MainTexture, V_Texcoord+vec2((-halfCoreSize+x)*texelOffset, (-halfCoreSize+y)*texelOffset));
maxValue = max(maxValue,color);
}
}
gl_FragColor=maxValue;
}

21
res/shader/fullscreenquad_erosion.fs

@ -0,0 +1,21 @@
varying vec2 V_Texcoord;
uniform sampler2D U_MainTexture;
void main()
{
vec4 minValue = vec4(1.0);
int coreSize = 6;
int halfCoreSize = coreSize / 2;
float texelOffset = 1/100.0;
for(int y=0; y<coreSize; y++){
for(int x=0; x<coreSize; x++){
vec4 color = texture2D(U_MainTexture, V_Texcoord+vec2((-halfCoreSize+x)*texelOffset, (-halfCoreSize+y)*texelOffset));
minValue = min(minValue,color);
}
}
gl_FragColor=minValue;
}

2
shader3.vcxproj

@ -84,6 +84,8 @@
<ItemGroup>
<None Include="res\shader\fullscreenquad.fs" />
<None Include="res\shader\fullscreenquad.vs" />
<None Include="res\shader\fullscreenquad_dilation.fs" />
<None Include="res\shader\fullscreenquad_erosion.fs" />
<None Include="res\shader\test.fs" />
<None Include="res\shader\test.vs" />
</ItemGroup>

6
shader3.vcxproj.filters

@ -58,5 +58,11 @@
<None Include="res\shader\fullscreenquad.vs">
<Filter>src</Filter>
</None>
<None Include="res\shader\fullscreenquad_dilation.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\fullscreenquad_erosion.fs">
<Filter>src</Filter>
</None>
</ItemGroup>
</Project>
Loading…
Cancel
Save