Browse Source

普通混合

master
blobt 5 years ago
parent
commit
ed4e0f81c1
  1. BIN
      .vs/shader3/v14/.suo
  2. 195
      main.cpp
  3. BIN
      res/image/grass.png
  4. BIN
      res/image/head.png
  5. BIN
      res/image/test.bmp
  6. 11
      res/shader/normal_blend.fs
  7. 10
      shader3.vcxproj
  8. 26
      shader3.vcxproj.filters
  9. 2
      utils.cpp

BIN
.vs/shader3/v14/.suo

195
main.cpp

@ -44,8 +44,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
RECT rect; RECT rect;
rect.left = 0; rect.left = 0;
rect.top = 0; rect.top = 0;
rect.right = 800;
rect.bottom = 600;
rect.right = 256;
rect.bottom = 256;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE); 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); HWND hwnd = CreateWindowEx(NULL, "OpenGL", "RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
@ -81,15 +81,6 @@ 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 hdr program
GPUProgram hdrProgram;
hdrProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
hdrProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/hdrrender.fs");
hdrProgram.Link();
hdrProgram.DetectAttribute("pos");
hdrProgram.DetectAttribute("texcoord");
hdrProgram.DetectUniform("U_MainTexture");
//高斯模糊 //高斯模糊
GPUProgram gaussianProgram; GPUProgram gaussianProgram;
gaussianProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); gaussianProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
@ -102,55 +93,19 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
//combine program //combine program
GPUProgram combineProgram; GPUProgram combineProgram;
combineProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs"); combineProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
combineProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/combine.fs");
combineProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/normal_blend.fs");
combineProgram.Link(); combineProgram.Link();
combineProgram.DetectAttribute("pos"); combineProgram.DetectAttribute("pos");
combineProgram.DetectAttribute("texcoord"); combineProgram.DetectAttribute("texcoord");
combineProgram.DetectUniform("U_MainTexture");
combineProgram.DetectUniform("U_HDRTexture");
//init light source program
GPUProgram lightSourceProgram;
lightSourceProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/lightSource.vs");
lightSourceProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/lightSource.fs");
lightSourceProgram.Link();
lightSourceProgram.DetectAttribute("pos");
lightSourceProgram.DetectUniform("M");
lightSourceProgram.DetectUniform("V");
lightSourceProgram.DetectUniform("P");
//init gpu program
GPUProgram gpuProgram;
gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/hdr.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");
combineProgram.DetectUniform("U_BaseTexture");
combineProgram.DetectUniform("U_BlendTexture");
//init 3d model //init 3d model
ObjModel obj,quad,sphere;
obj.Init("res/model/Cube.obj");
ObjModel quad;
quad.Init("res/model/Quad.obj"); quad.Init("res/model/Quad.obj");
sphere.Init("res/model/Sphere.obj");
float identity[] = { float identity[] = {
1.0f,0,0,0, 1.0f,0,0,0,
@ -159,32 +114,11 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
0,0,0,1.0f 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 = 3.0f;
float spotLightDirection[] = { 0.0f, -1.0f, 0.0f, 128.0f };//把最后一位改成......
float spotLightCutoff = 0.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.0f) * glm::rotate(-60.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(model);
glm::mat4 quadModel = glm::translate<float>(0.0f, 0.0f, -2.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(8.0f, 8.0f, 8.0f); glm::mat4 quadModel = glm::translate<float>(0.0f, 0.0f, -2.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(8.0f, 8.0f, 8.0f);
glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel); glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel);
glm::mat4 sphereModel = glm::translate<float>(0.1f, 3.0f, -2.2f) * glm::scale(0.2f, 0.2f, 0.2f);
glm::mat4 sphereNormalMatrix = glm::inverseTranspose(sphereModel);
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);
@ -201,11 +135,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
fbo[1].AttachDepthBuffer("depth", viewportWidth, viewportHeight); fbo[1].AttachDepthBuffer("depth", viewportWidth, viewportHeight);
FBO HDRfbo;
HDRfbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
HDRfbo.AttachColorBuffer("hdrcolor", GL_COLOR_ATTACHMENT1, GL_RGBA16F, viewportWidth, viewportHeight);
HDRfbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
HDRfbo.Finish();
GLuint head = CreateTextureFromFile("res/image/head.png");
GLuint grass = CreateTextureFromFile("res/image/grass.png");
ShowWindow(hwnd, SW_SHOW); ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd); UpdateWindow(hwnd);
@ -225,110 +156,32 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
} }
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
HDRfbo.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();
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(quadModel));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(quadNormalMatrix));
quad.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
quad.Draw();
glUseProgram(lightSourceProgram.mProgram);
glUniformMatrix4fv(lightSourceProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(sphereModel));
glUniformMatrix4fv(lightSourceProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(lightSourceProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
sphere.Bind(lightSourceProgram.GetLocation("pos"), lightSourceProgram.GetLocation("texcoord"), lightSourceProgram.GetLocation("normal"));
sphere.Draw();
glUseProgram(0);
HDRfbo.Unbind();
/* blur start */
fbo[0].Bind();
glUseProgram(gaussianProgram.mProgram);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("hdrcolor"));
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
fbo[0].Unbind();
fbo[1].Bind();
glUseProgram(gaussianProgram.mProgram);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo[0].GetBuffer("color"));
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
fbo[1].Unbind();
fbo[0].Bind();
glUseProgram(gaussianProgram.mProgram);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
fbo[0].Unbind();
fbo[1].Bind();
glUseProgram(gaussianProgram.mProgram);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo[0].GetBuffer("color"));
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
fsq.Draw(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
fbo[1].Unbind();
/* blur end */
glFlush();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glUseProgram(originalProgram.mProgram); glUseProgram(originalProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
glBindTexture(GL_TEXTURE_2D, head);
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(gaussianProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftBottom(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
glUseProgram(originalProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, grass);
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
glUseProgram(combineProgram.mProgram); glUseProgram(combineProgram.mProgram);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, HDRfbo.GetBuffer("color"));
glUniform1i(combineProgram.GetLocation("U_MainTexture"), 0);
glBindTexture(GL_TEXTURE_2D, head);
glUniform1i(combineProgram.GetLocation("U_BaseTexture"), 0);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fbo[1].GetBuffer("color"));
glUniform1i(combineProgram.GetLocation("U_HDRTexture"), 1);
glBindTexture(GL_TEXTURE_2D, grass);
glUniform1i(combineProgram.GetLocation("U_BlendTexture"), 1);
fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord")); fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord"));
glDisable(GL_BLEND);
glFlush();
SwapBuffers(dc); SwapBuffers(dc);
} }
return 0; return 0;

BIN
res/image/grass.png

After

Width: 129  |  Height: 111  |  Size: 24 KiB

BIN
res/image/head.png

After

Width: 108  |  Height: 108  |  Size: 28 KiB

BIN
res/image/test.bmp

11
res/shader/normal_blend.fs

@ -0,0 +1,11 @@
varying vec2 V_Texcoord;
uniform sampler2D U_BaseTexture;
uniform sampler2D U_BlendTexture;
void main()
{
vec4 baseColor = texture2D(U_BaseTexture, V_Texcoord);
vec4 blendColor = texture2D(U_BlendTexture, V_Texcoord);
gl_FragColor = blendColor*blendColor.a + baseColor*(1-blendColor.a);
}

10
shader3.vcxproj

@ -82,15 +82,7 @@
<ClInclude Include="utils.h" /> <ClInclude Include="utils.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="res\shader\combine.fs" />
<None Include="res\shader\fullscreenquad.vs" />
<None Include="res\shader\fullscreenquad_gaussian.fs" />
<None Include="res\shader\fullscreenquad_gaussian_horizontal.fs" />
<None Include="res\shader\fullscreenquad_gaussian_vertical.fs" />
<None Include="res\shader\hdr.fs" />
<None Include="res\shader\hdrrender.fs" />
<None Include="res\shader\lightSource.fs" />
<None Include="res\shader\lightSource.vs" />
<None Include="res\shader\normal_blend.fs" />
<None Include="res\shader\test.fs" /> <None Include="res\shader\test.fs" />
<None Include="res\shader\test.vs" /> <None Include="res\shader\test.vs" />
</ItemGroup> </ItemGroup>

26
shader3.vcxproj.filters

@ -52,31 +52,7 @@
<None Include="res\shader\test.vs"> <None Include="res\shader\test.vs">
<Filter>src</Filter> <Filter>src</Filter>
</None> </None>
<None Include="res\shader\fullscreenquad_gaussian.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\fullscreenquad_gaussian_horizontal.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\fullscreenquad_gaussian_vertical.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\hdr.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\hdrrender.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\fullscreenquad.vs">
<Filter>src</Filter>
</None>
<None Include="res\shader\combine.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\lightSource.fs">
<Filter>src</Filter>
</None>
<None Include="res\shader\lightSource.vs">
<None Include="res\shader\normal_blend.fs">
<Filter>src</Filter> <Filter>src</Filter>
</None> </None>
</ItemGroup> </ItemGroup>

2
utils.cpp

@ -60,7 +60,7 @@ unsigned char* LoadBMP(const char*path, int &width, int &height)
GLuint CreateTextureFromFile(const char*filePath) GLuint CreateTextureFromFile(const char*filePath)
{ {
GLuint texture = SOIL_load_OGL_texture(filePath, 0, 0, SOIL_FLAG_POWER_OF_TWO);
GLuint texture = SOIL_load_OGL_texture(filePath, 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y);
return texture; return texture;
} }

Loading…
Cancel
Save