Hi, everyone!!!
Please help me about Deferred Rendering.
I’m implementing a simple Deferred Rendering for my study. But final output is different from i desired.
So, I’m having two problem. But i’ve not still resolve…
- Y-coordinate is reverse in gBuffer
- How to pass gBuffer content to LIghtingPass.(maybe texture coordinate or texture ID?)
Here is my minimum source code and video capture running
My env
- Mac OS 10.11
- openFrameworks 0.9.7
- Xcode 8
ofApp.cpp
class ofApp : public ofBaseApp
{
public:
ofFbo gFbo;
ofShader gBuffer;
ofShader lightingPass;
ofBoxPrimitive box;
ofEasyCam cam;
ofMesh mainMesh;
ofTexture texture, normal;
//--------------------------------------------------------------
void setup()
{
glEnable(GL_DEPTH_TEST);
ofLoadImage(texture, "texture/concrete.jpg");
ofLoadImage(normal, "texture/random.png");
box.set(300);
box.mapTexCoordsFromTexture(texture);
ofFbo::Settings settings;
settings.width = ofGetWidth();
settings.height = ofGetHeight();
settings.internalformat = GL_RGB32F;
settings.numSamples = 0;
settings.useDepth = true;
settings.useStencil = true;
settings.depthStencilAsTexture = true;
settings.textureTarget = GL_TEXTURE_2D;
settings.depthStencilInternalFormat = GL_DEPTH_COMPONENT16;
settings.minFilter = GL_NEAREST;
settings.maxFilter = GL_NEAREST;
settings.wrapModeHorizontal = GL_CLAMP_TO_BORDER;
settings.wrapModeVertical = GL_CLAMP_TO_BORDER;
gFbo.allocate(settings);
gFbo.createAndAttachTexture(GL_RGB, 1);
gFbo.createAndAttachTexture(GL_RGB, 2);
gFbo.createAndAttachRenderbuffer(GL_RGB32F, 3);
gFbo.checkStatus();
gBuffer.load("shader/gBuffer");
lightingPass.load("shader/lightingPass");
mainMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
int width = ofGetWidth();
int height = ofGetHeight();
mainMesh.addVertex(ofPoint(0, 0, 0));
mainMesh.addTexCoord(ofVec2f(0, 0));
mainMesh.addVertex(ofPoint(width, 0, 0));
mainMesh.addTexCoord(ofVec2f(1, 0));
mainMesh.addVertex(ofPoint(0, height, 0));
mainMesh.addTexCoord(ofVec2f(0, 1));
mainMesh.addVertex(ofPoint(width, height, 0));
mainMesh.addTexCoord(ofVec2f(1, 1));
}
//--------------------------------------------------------------
void update()
{
}
//--------------------------------------------------------------
void draw()
{
//GeometryPass
//-----------------------------------------
gFbo.begin();
gFbo.activateAllDrawBuffers();
glEnable(GL_DEPTH_TEST);
glClearColor(0.2, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
cam.begin();
gBuffer.begin();
gBuffer.setUniformMatrix4f("view", ofGetCurrentViewMatrix().getPtr());
gBuffer.setUniformMatrix4f("projection", cam.getProjectionMatrix().getPtr());
gBuffer.setUniformTexture("uNormalMap", normal, 0);
gBuffer.setUniformTexture("uColorMap", texture, 1);
ofMatrix4x4 model;
model.rotate(ofGetElapsedTimef(), 1, 0.5, 1);
model.translate(0, 0, 0);
gBuffer.setUniformMatrix4f("model", model.getPtr());
box.draw();
gBuffer.end();
cam.end();
gFbo.end();
//LightingPass
//-----------------------------------------
glClearColor(0.0, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightingPass.begin();
lightingPass.setUniformTexture("u_Position", gFbo.getTexture(0), 0);
lightingPass.setUniformTexture("u_Normal", gFbo.getTexture(1), 1);
lightingPass.setUniformTexture("u_Albedo", gFbo.getTexture(2), 2);
// lightingPass.setUniform3f("u_cameraPosition", cam.getPosition());
// lightingPass.setUniform3f("u_lightPosition", pointlight.getPosition());
// lightingPass.setUniform4fv("u_lightAmbient", pointlight.ambient);
// lightingPass.setUniform4fv("u_lightDiffuse", pointlight.diffuse);
// lightingPass.setUniform4fv("u_lightSpecular", pointlight.specular);
// lightingPass.setUniform3fv("u_lightAttenuation", pointlight.attenuation);
mainMesh.draw();
lightingPass.end();
glDisable(GL_DEPTH_TEST);
//Debug draw
gFbo.getTexture(0).draw(0, 0, 210, 120); //Position
gFbo.getTexture(1).draw(215, 0, 210, 120); //Nornal
gFbo.getTexture(2).draw(430, 0, 210, 120); //Albedo
}
};
//========================================================================
int main( ){
ofGLWindowSettings settings;
settings.setGLVersion(3, 3);
settings.width = 1280;
settings.height = 720;
ofCreateWindow(settings);
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
gBuffer
vertex shader
#version 330 core
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
in vec4 position;
in vec4 color;
in vec4 normal;
in vec2 texcoord;
out vec2 TexCoord;
out vec3 Normal;
out vec3 Position;
void main (void)
{
TexCoord = texcoord;
mat3 normalMatrix = transpose(inverse(mat3(model)));
Normal = normalMatrix * vec3(normal).xyz;
Position = vec3(view * model * position).xyz;
gl_Position = projection * view * model * position;
}
fragment shader
#version 330 core
uniform sampler2DRect uNormalMap;
uniform sampler2DRect uColorMap;
layout (location = 0) out vec4 gPosition;
layout (location = 1) out vec4 gNormal;
layout (location = 2) out vec4 gAlbedo;
in vec2 TexCoord;
in vec3 Position;
in vec3 Normal;
void main(void)
{
vec3 normalTexel = texture(uNormalMap, TexCoord).rgb;
vec3 colorTexel = texture(uColorMap, TexCoord).rgb;
gPosition = vec4(Position, 1.0);
gNormal = vec4(normalTexel, 1.0);
gAlbedo = vec4(colorTexel, 1.0);
}
lightingPass
vertex shader
#version 330 core
uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec2 texcoord;
out vec2 TexCoord;
void main (void)
{
TexCoord = texcoord;
gl_Position = modelViewProjectionMatrix * position;
}
fragment shader
#version 330 core
uniform sampler2DRect u_Position;
uniform sampler2DRect u_NormalMap;
uniform sampler2DRect u_Albedo;
layout (location = 0) out vec4 finalColor;
in vec2 TexCoord;
void main(void)
{
//texcel
vec3 positionTexel = texture(u_Position, TexCoord).rgb;
vec3 normalTexel = texture(u_NormalMap, TexCoord).rgb;
vec3 colorTexel = texture(u_Albedo, TexCoord).rgb;
//culculate lighting.........
finalColor = vec4(positionTexel, 1.0);
}
https://www.youtube.com/watch?v=_TrLqsfCLeQ&feature=youtu.be
thanks!!!
yuma