Browse Source

渲染fbo到多个纹理

master
blobt 4 years ago
parent
commit
74b6493f0e
  1. BIN
      .vs/shader2/v14/.suo
  2. 28
      main.cpp
  3. 17
      misc.cpp
  4. 2
      misc.h
  5. 35
      res/shader/MixLightMT.fs
  6. 19
      res/shader/MixLightMT.vs
  7. 4
      shader2.vcxproj
  8. 4
      shader2.vcxproj.filters

BIN
.vs/shader2/v14/.suo

28
main.cpp

@ -91,7 +91,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glewInit(); glewInit();
/*´´½¨program*/ /*´´½¨program*/
GLuint program = CreateGPUProgram("res/shader/test.vs", "res/shader/test.fs");
GLuint program = CreateGPUProgram("res/shader/MixLightMT.vs", "res/shader/MixLightMT.fs");
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation; GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation;
posLocation = glGetAttribLocation(program, "pos"); posLocation = glGetAttribLocation(program, "pos");
@ -134,8 +134,8 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp"); GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp");
/*´´½¨FBO*/ /*´´½¨FBO*/
GLuint colorBuffer, depthBuffer;
GLuint fbo = CreateFramebufferObject(colorBuffer, depthBuffer, windowWidth, windowHeight);
GLuint colorBuffer, depthBuffer, colorBuffer2;
GLuint fbo = CreateFramebufferObject(colorBuffer, depthBuffer, windowWidth, windowHeight, &colorBuffer2);
glViewport(0, 0, windowWidth, windowHeight); glViewport(0, 0, windowWidth, windowHeight);
@ -208,15 +208,27 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
GL_CALL(glClearColor(0.5f, 0.4f, 0.7f, 1.0f)); GL_CALL(glClearColor(0.5f, 0.4f, 0.7f, 1.0f));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glEnable(GL_SCISSOR_TEST);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glScissor(0, 0, windowWidth, windowHeight/2);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -4.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -4.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -4.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -4.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -1.0f);
glEnd(); glEnd();
glBindTexture(GL_TEXTURE_2D, colorBuffer2);
glScissor(0, windowHeight / 2, windowWidth, windowHeight / 2);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -1.0f);
glEnd();
glDisable(GL_SCISSOR_TEST);
SwapBuffers(dc); SwapBuffers(dc);
} }

17
misc.cpp

@ -26,7 +26,7 @@ GLuint CreateVAOWithVBOSettings(std::function<void()> settings)
return vao; return vao;
} }
GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int width, int height)
GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int width, int height, GLuint *colorBuffer2)
{ {
GLuint fbo; GLuint fbo;
glGenFramebuffers(1, &fbo); glGenFramebuffers(1, &fbo);
@ -43,6 +43,21 @@ GLuint CreateFramebufferObject(GLuint & colorBuffer, GLuint & depthBuffer, int w
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);//绑定刚才生成的texture到GL_COLOR_ATTACHMENT0 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);//绑定刚才生成的texture到GL_COLOR_ATTACHMENT0
//colorbuffer2
if (colorBuffer2 != nullptr) {
glGenTextures(1, colorBuffer2);
glBindTexture(GL_TEXTURE_2D, *colorBuffer2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, *colorBuffer2, 0);//绑定刚才生成的texture到GL_COLOR_ATTACHMENT0
GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, buffers);
}
//depthbuffer //depthbuffer
glGenTextures(1, &depthBuffer); glGenTextures(1, &depthBuffer);
glBindTexture(GL_TEXTURE_2D, depthBuffer); glBindTexture(GL_TEXTURE_2D, depthBuffer);

2
misc.h

@ -4,7 +4,7 @@
GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr); GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr);
GLuint CreateVAOWithVBOSettings(std::function<void()>settings); GLuint CreateVAOWithVBOSettings(std::function<void()>settings);
GLuint CreateFramebufferObject(GLuint &colorBuffer, GLuint &depthBuffer, int width, int height);
GLuint CreateFramebufferObject(GLuint &colorBuffer, GLuint &depthBuffer, int width, int height, GLuint *colorBuffer2 = nullptr);
char* LoadFileContent(const char *path); char* LoadFileContent(const char *path);
GLuint CompileShader(GLenum shaderType, const char* shaderPath); GLuint CompileShader(GLenum shaderType, const char* shaderPath);

35
res/shader/MixLightMT.fs

@ -0,0 +1,35 @@
uniform sampler2D U_MainTexture;
varying vec3 V_Normal;
varying vec4 V_WorldPos;//
varying vec2 V_Texcoord;
void main() {
vec3 lightPos = vec3(10.0, 10.0, 0.0);
vec3 L=lightPos;
L = normalize(L);
vec3 n = normalize(V_Normal);
//ambient
vec4 AmbientLightColor = vec4(0.2, 0.2, 0.2, 1.0);
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0);
vec4 AmbientColor = AmbientLightColor*AmbientMaterial;
//diffuse
vec4 DiffuseLightColor = vec4(1.0,1.0,1.0,1.0);
vec4 DiffuseMaterial = vec4(0.8, 0.8, 0.8, 1.0);
vec4 diffuseColor = DiffuseLightColor*DiffuseMaterial*max(0.0, dot(L,n));
//specular
vec4 SpecularLightColor = vec4(1.0,1.0,1.0,1.0);
vec4 SpecularMaterial = vec4(0.9,0.9,0.9,1.0);
vec3 reflectDir = normalize(reflect(-L,n));//-L线线
vec3 viewDir = normalize(vec3(0.0) - V_WorldPos.xyz);//vec3(0.0)线
vec4 specularColor = SpecularLightColor*SpecularMaterial*pow(max(0.0,dot(viewDir,reflectDir)),128.0);
//gl_FragColor = texture2D(U_MainTexture, V_Texcoord) * (AmbientColor + diffuseColor + specularColor);
gl_FragData[0] = AmbientColor + texture2D(U_MainTexture, V_Texcoord) * diffuseColor + specularColor;
gl_FragData[1] = vec4(1.0);
}

19
res/shader/MixLightMT.vs

@ -0,0 +1,19 @@
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform mat4 NM;
varying vec3 V_Normal;
varying vec4 V_WorldPos;//µ±Ç°µÄλÖÃ
varying vec2 V_Texcoord;
void main() {
V_Normal = mat3(NM)*normal;
V_WorldPos = M * vec4(pos, 1.0);
V_Texcoord = texcoord;
gl_Position = P*V*M*vec4(pos,1.0);
}

4
shader2.vcxproj

@ -159,8 +159,8 @@
<ClInclude Include="timer.h" /> <ClInclude Include="timer.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="res\shader\MixLight.fs" />
<None Include="res\shader\MixLight.vs" />
<None Include="res\shader\MixLightMT.fs" />
<None Include="res\shader\MixLightMT.vs" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

4
shader2.vcxproj.filters

@ -46,10 +46,10 @@
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="res\shader\MixLight.fs">
<None Include="res\shader\MixLightMT.fs">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</None> </None>
<None Include="res\shader\MixLight.vs">
<None Include="res\shader\MixLightMT.vs">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</None> </None>
</ItemGroup> </ItemGroup>
Loading…
Cancel
Save