Browse Source

使用fullscreenquad,渲染rbg cube

master
blobt 4 years ago
parent
commit
529b52e089
  1. 9
      Res/fullscreenquad.fs
  2. 10
      Res/fullscreenquad.vs
  3. 11
      Res/gray.fs
  4. 2
      framebufferobject.cpp
  5. 3
      renderFramework.vcxproj
  6. 9
      renderFramework.vcxproj.filters
  7. 19
      scene.cpp

9
Res/fullscreenquad.fs

@ -0,0 +1,9 @@
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 V_Texcoord;
uniform sampler2D U_Texture;
void main(){
gl_FragColor = texture2D(U_Texture, V_Texcoord);
}

10
Res/fullscreenquad.vs

@ -0,0 +1,10 @@
#ifdef GL_ES
precision mediump float;
#endif
attribute vec4 position;
attribute vec4 texcoord;
varying vec2 V_Texcoord;
void main(){
V_Texcoord = texcoord.xy;
gl_Position = position;
}

11
Res/gray.fs

@ -0,0 +1,11 @@
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 V_Texcoord;
uniform sampler2D U_Texture;
void main()
{
vec4 color=texture2D(U_Texture,V_Texcoord);
float gray=(color.r+color.g+color.b)/3.0;
gl_FragColor=vec4(gray,gray,gray,1.0);
}

2
framebufferobject.cpp

@ -31,7 +31,7 @@ void FrameBufferObject::AttachDepthBuffer(const char * bufferName, int width, in
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, depthMap, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
mBuffers.insert(std::pair<std::string, GLuint>(bufferName, depthMap));

3
renderFramework.vcxproj

@ -149,6 +149,9 @@
<ClInclude Include="vertexbuffer.h" />
</ItemGroup>
<ItemGroup>
<None Include="Res\fullscreenquad.fs" />
<None Include="Res\fullscreenquad.vs" />
<None Include="Res\gray.fs" />
<None Include="Res\rgbcube.fs" />
<None Include="Res\rgbcube.vs" />
</ItemGroup>

9
renderFramework.vcxproj.filters

@ -68,5 +68,14 @@
<None Include="Res\rgbcube.vs">
<Filter>源文件</Filter>
</None>
<None Include="Res\fullscreenquad.fs">
<Filter>源文件</Filter>
</None>
<None Include="Res\fullscreenquad.vs">
<Filter>源文件</Filter>
</None>
<None Include="Res\gray.fs">
<Filter>源文件</Filter>
</None>
</ItemGroup>
</Project>

19
scene.cpp

@ -1,26 +1,43 @@
#include "scene.h"
#include "utils.h"
#include "model.h"
#include "framebufferobject.h"
#include "fullscreenquad.h"
glm::mat4 viewMatrix, projectionMatrix;
glm::vec3 cameraPos(4.0f, 3.0f, 4.0f);
Model model;
FrameBufferObject *fbo;
FullScreenQuad *fsq;
void Init()
{
model.Init("Res/Cube.obj");
model.mShader->Init("Res/rgbcube.vs", "Res/rgbcube.fs");
model.SetPosition(0.0f, 0.0f, 0.0f);
viewMatrix = glm::lookAt(cameraPos, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
fsq = new FullScreenQuad();
fsq->Init();
fsq->mShader->Init("Res/fullscreenquad.vs", "Res/gray.fs");
}
void SetViewPortSize(float width, float height)
{
projectionMatrix = glm::perspective(50.0f, width/height, 0.1f, 1000.0f);
fbo = new FrameBufferObject();
fbo->AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, (int)width, (int)height);
fbo->AttachDepthBuffer("depth", (int)width, (int)height);
fbo->Finish();
fbo->Bind();
model.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
fbo->Unbind();
}
void Draw()
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
model.Draw(viewMatrix, projectionMatrix, cameraPos.x, cameraPos.y, cameraPos.z);
fsq->mShader->SetTexture("U_Texture", fbo->GetBuffer("color"));
fsq->Draw();
}
Loading…
Cancel
Save