Sign in to follow this  
nattfoedd

OpenGL Rendering to texture/FBO issue

Recommended Posts

nattfoedd    266
Hi everybody,

just registered here after beeing stuck for hours. I'm fairly new to OpenGL programming, though I wrote some simple programs and shaders before.

I'm sure somebody around here can tell me, what's going wrong here.

What I'm trying to achieve here is a simple multi-pass shadowmapping rendering:
#1 Render the z-buffer of the light to a texture
#2 Render the shadows to another texture (viewport resolution)
(#3 Do something with this texture, that's not implemented at the moment)
#4 Render the scene and apply the shadows from the previously rendered texture in step #2

For reference, that's what the scene looks like without shadows:
[img]http://bildupload.sro.at/a/images/1-no_shadows.png[/img]

When I display the result of step #2 in the viewport I get, what I expect - the calculated shadows, which are correctly occluded by the geometry:
[img]http://bildupload.sro.at/a/images/shadows_viewport.png[/img]


Now when I simply display the texture rendered in step #2 in the fragment shader of step #4 I get this:
[img]http://bildupload.sro.at/a/images/shadows_texture.png[/img]


As you can see, this is basically the identical image, [b]but[/b] the first and the third cylinder are suddenly missing, eventhough I didn't change anything. So obviously there is something different rendered to the texture then, when I render the same thing to the viewport (maybe some depth/drawing order issue?).


So I guess there must be either something wrong how I setup the FBO or the texture or some settings I set before drawing the scene.

Here I generate the texture used in step #2 and bind it to the FBO:

[Code]
void CreateRawShadowFBO()
{
GLenum rawShadowsFBOstatus;

// generate and bind the rawShadowTexture
glGenTextures(1, &rawShadowTexture);
glBindTexture(GL_TEXTURE_2D, rawShadowTexture);

// set the parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

// texture size = viewport size
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, (int) observerWidth, (int) observerHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

// generate and bind the rawShadowsFBO
glGenFramebuffers(1, &rawShadowsFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, rawShadowsFBO);

// set the rawShadowTexture as render target
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rawShadowTexture, 0);

// check rawShadowsFBO status
rawShadowsFBOstatus = glCheckFramebufferStatusEXT(GL_DRAW_FRAMEBUFFER);
if(rawShadowsFBOstatus != GL_FRAMEBUFFER_COMPLETE_EXT)
printf("GL_FRAMEBUFFER_COMPLETE_EXT failed, CANNOT use rawShadowsFBO\n");

// switch back to window-system-provided framebuffer
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);

}[/Code]

Here's the shadow calculating part (step #2):

[Code]
void ProcessShadowMap()
{
//set the viewport
glViewport(0, 0, (int) observerWidth, (int)observerHeight);

glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//set the matrices
M3DMatrix44f shadowMat;
M3DMatrix44f viewMat;
M3DMatrix44f eyeToLightView;

UpdateMatrices(shadowMat, viewMat, eyeToLightView);

glColorMask(GL_TRUE,GL_TRUE, GL_TRUE, GL_FALSE);
glDepthMask(GL_TRUE);
glClear(GL_DEPTH_BUFFER_BIT);

// render the 'raw' shadows to the rawShadowsFBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, rawShadowsFBO);

// set the process shadows shader
glUseProgram(procShadowsShader);
// bind the previously rendered depthmap
glBindTexture(GL_TEXTURE_2D, shadowMap);

// set the Model View Projection Matrix for the shader
glUniformMatrix4fv(glGetUniformLocation(procShadowsShader, "mvMatrix"), 1, GL_FALSE, transformPipeline.GetModelViewMatrix());
//Projektionsmatrix
glUniformMatrix4fv(glGetUniformLocation(procShadowsShader, "projMatrix"), 1, GL_FALSE, transformPipeline.GetProjectionMatrix());

// set the ShadowMatrix for the shader
glUniformMatrix4fv(glGetUniformLocation(procShadowsShader, "eyeToLightViewTexMatrix"), 1, GL_FALSE, shadowMat);
glUniformMatrix4fv(glGetUniformLocation(procShadowsShader, "eyeToLightViewMatrix"), 1, GL_FALSE, eyeToLightView);

//calc the light's position, at the origin of the light-view matrix
M3DMatrix44f invLight;
m3dInvertMatrix44(invLight,lightView);
M3DVector4f lp;
m3dLoadVector4(lp,0,0,0,0);
m3dTransformVector4(lightPos, lp, invLight);

// set the light's parameters
glUniform4fv(glGetUniformLocation(procShadowsShader,"lightPos"),1,lightPos);
glUniform1f(glGetUniformLocation(procShadowsShader,"lightNear"),lightNearClip);
glUniform1f(glGetUniformLocation(procShadowsShader,"lightFar"),lightFarClip);
float lightSizeX = (lightSize/lightFrustumWidth)/(float)shadowMapSize;
float lightSizeY = (lightSize/lightFrustumHeight)/(float)shadowMapSize;
glUniform2f(glGetUniformLocation(procShadowsShader,"wLight"),lightSizeX,lightSizeY);
glUniform1i(glGetUniformLocation(procShadowsShader,"shadowMap"),0);

//draw the model
modelBatch.Draw();

//(de)activate stuff
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
modelViewMatrix.PopMatrix();
glColorMask(GL_TRUE,GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);

gltCheckErrors(procShadowsShader);

}[/Code]


And here's the final step #4, at the moment the shader used here just displays the rendered texture in step #2:

[Code]
void RenderShadows()
{
//set the viewport
glViewport(0, 0, (int) observerWidth, (int)observerHeight);

glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//set the projection matrix
viewFrustum.SetPerspective(45.0f,aspect,0.1f,100);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());


//switch cameras
if(!toggleView)
modelViewMatrix.PushMatrix(observerFrame);
else
modelViewMatrix.PushMatrix(lightFrame);


//set the shader program
glUseProgram(displayShadows);
//bind the shadowtexture
glBindTexture(GL_TEXTURE_2D, rawShadowTexture);

// set the Model View Matrix
glUniformMatrix4fv(glGetUniformLocation(displayShadows, "mvMatrix"),
1, GL_FALSE, transformPipeline.GetModelViewMatrix());
//set the Projectionsmatrix
glUniformMatrix4fv(glGetUniformLocation(displayShadows, "projMatrix"),
1, GL_FALSE, transformPipeline.GetProjectionMatrix());

//set the Normalmatrix
glUniformMatrix3fv(glGetUniformLocation(displayShadows, "normalMatrix"),
1, GL_FALSE, transformPipeline.GetNormalMatrix());

//light position
M3DMatrix44f invLight;
m3dInvertMatrix44(invLight,lightView);
M3DVector4f lp;
m3dLoadVector4(lp,0,0,0,0);
m3dTransformVector4(lightPos, lp, invLight);
//light parameters
glUniform4fv(glGetUniformLocation(displayShadows,"lightPos"),1,lightPos);
glUniform4f(glGetUniformLocation(displayShadows,"lightAmbient"),1,1,1,1);
glUniform4f(glGetUniformLocation(displayShadows,"lightDiffuse"),1,1,1,1);
glUniform1f(glGetUniformLocation(displayShadows,"lightNear"),lightNearClip);
glUniform1f(glGetUniformLocation(displayShadows,"lightFar"),lightFarClip);
float lightSizeX = (lightSize/lightFrustumWidth)/(float)shadowMapSize;
float lightSizeY = (lightSize/lightFrustumHeight)/(float)shadowMapSize;
glUniform2f(glGetUniformLocation(displayShadows,"wLight"),lightSizeX,lightSizeY);
glUniform1i(glGetUniformLocation(displayShadows,"shadowMap"),0);


//draw the model
modelBatch.Draw();

glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);

modelViewMatrix.PopMatrix();
gltCheckErrors(displayShadows);
}
[/Code]


Anybode an idea what goes wrong, when rendering to the texture? I'd be really grateful for any hints [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
theagentd    990
You don't have a depth buffer in your FBO, so the geometry just appears in the order it's drawn. You need to have a depth buffer in step 2 too since you're trying to draw all the geometry again, and you obviously need the closest depth there.

I get the feeling you're trying to do a screen space blur of the shadow map to get smooth shadows, correct? This won't look very good and will be very inaccurate, especially for steep angles since it's uniformly blurred. You'll also possible get light bleeding where you're not supposed to unless you take the depth of neighboring pixels into account. Anyway, it's far from optimal. The easiest way to do soft shadows is with PCF, which is basically sampling lots of nearby depth values from the shadow map and checking how many of these that pass. This gives problems with self shadowing though so you need a high depth bias which in turn pretty much removes self shadowing. If you're feeling really adventurous look up variance shadow maps.

Share this post


Link to post
Share on other sites
nattfoedd    266
Hey agentd,

first of all, thank you a lot for your reply! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I suspected something like that, but didn't know I had to explicitely create a depth buffer FBO for it. So I tried to do my homework and checked several (very similar) tutorials about it, but after implementing the following code my texture just stays black [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img] (rest of the code didn't change)
[code]
void CreateRawShadowFBO()
{
GLenum rawShadowsFBOstatus;

// generate and bind the rawShadowTexture
glGenTextures(1, &rawShadowTexture);
glBindTexture(GL_TEXTURE_2D, rawShadowTexture);

// set the parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

// texture size = viewport size
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, (int) observerWidth, (int) observerHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);

// unbind texture
glBindTexture(GL_TEXTURE_2D, 0);


// generate and bind depth buffer
glGenRenderbuffers(1, &rawShadowsDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, rawShadowsDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, (int) observerWidth, (int) observerHeight);
//unbind depth buffer
glBindRenderbuffer(GL_RENDERBUFFER, 0);

// generate and bind the rawShadowsFBO
glGenFramebuffers(1, &rawShadowsFBO);
glBindFramebuffer(GL_FRAMEBUFFER, rawShadowsFBO);

// attach the color texture to the frame buffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rawShadowTexture, 0);
// attach depth buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rawShadowsDepthBuffer);

// check rawShadowsFBO status
rawShadowsFBOstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(rawShadowsFBOstatus != GL_FRAMEBUFFER_COMPLETE)
printf("GL_FRAMEBUFFER_COMPLETE failed, CANNOT use rawShadowsFBO\n");

// switch back to window-system-provided framebuffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
[/code]

If I don't attach the depth buffer FBO ([size=2][font=courier new,courier,monospace]glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rawShadowsDepthBuffer[/font]);[/size] ) I get the same result as before. So I'm afraid my depth buffer FBO is setup in a wrong way, though I'm unable to see what's the issue, as I can't see any difference to the 3 tutorials I checked on that matter.

That's the thing I which makes it sometimes hard to enjoy OpenGL coding for a beginner like me, as it's really hard to debug. Feels like a big blackbox. [img]http://public.gamedev.net//public/style_emoticons/default/wacko.png[/img]

Your assumption was right. I'm trying to achieve something along that matter, though I don't plan to apply a uniform blur. Had an idea to compensate the viewing angle dependent issue you mentioned. However I've no idea if this will work out as I've planned, but that doesn't really matter, as I'm having fun playing around with stuff like that.

I did implement both PCF and PCSS shadow mapping a while ago (2nd with rotated poisson disc):
[url=http://bildupload.sro.at/a/images/PCF_1.png][img]http://bildupload.sro.at/a/thumbs/PCF_1.jpg[/img][/url]
[url=http://bildupload.sro.at/a/images/PCSS_fak30_p1_rot.png][img]http://bildupload.sro.at/a/thumbs/PCSS_fak30_p1_rot.jpg[/img][/url]

So as I mentioned before, my goal here is not really about just implementing a shadow algorithm, but more playing around with my own ideas (as bad as they might turn out ;) ). I also looked into VSM when I did the other shadow mapping techniques a while back, but felt like it had too many limitations.

Share this post


Link to post
Share on other sites
theagentd    990
I think the rendering is working fine, but you're not clearing the FBO, causing the depth test to fail after the first frame. Make sure that all FBOs are cleared properly, both the shadow map and the shadow post processing buffer. I see for example that you call glViewport(), then glClear() and THEN glBindFramebuffer(). I don't think you wanted to clear a shadow map sized part of the screen, right? =S

Share this post


Link to post
Share on other sites
nattfoedd    266
Of course :facepalm: I cleared before binding the buffer. Just had to swap these in the code and now everything works fine.

Thanks again [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now