Sign in to follow this  
smani

OpenGL Shadow Mapping / z-Buffer texture on ATI card

Recommended Posts

smani    100
Hi,
I am having some problems in getting shadow mapping to work on an ATI card. I guess the problem is best described by these two images:

Scene rendered with nvidia card (GF9600GT, Linux Driver 260.19.12):
http://nas.dyndns.org/temp/nvidia.png
Scene rendered with ati card (HD3400, Linux Catalyst 10.11):
http://nas.dyndns.org/temp/ati.png

For some reason the depth buffer obtained by the ATI card is missing lots of the geometry of the scene...

Code: http://nas.dyndns.org/temp/demo.zip , relevant portions:
Initialization:
///*** CREATE DEPTH TEXTURE ***///

shadowmap.width=640;

shadowmap.height=480;

shadowmap.biasMatrix<<0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0;

LookAtf(shadowmap.modelViewMatrix, lightPosition, Vector3f::Zero(), Vector3f(0.f, 1.f, 0.f));

glUseProgram(shadowprog.program);

glUniformMatrix4fv(shadowprog.modelViewMatrixLocation, 1, GL_FALSE, shadowmap.modelViewMatrix.data());



glGenTextures(1, &shadowmap.texture);

glBindTexture(GL_TEXTURE_2D, shadowmap.texture);

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 ); // Remove artifacts when outside the domain of the shadowmap

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowmap.width, shadowmap.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);

glBindTexture(GL_TEXTURE_2D, 0);



///*** CREATE SHADOW FRAMEBUFFER OBJECT ***///

glGenFramebuffers(1, &shadowmap.fbo);

glBindFramebuffer(GL_FRAMEBUFFER, shadowmap.fbo);

glDrawBuffer(GL_NONE); // Instruct openGL that we won't bind a color texture with the currently binded FBO

glReadBuffer(GL_NONE);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowmap.texture, 0); // Attach depth texture to FBO

if(glCheckFramebufferStatus(GL_FRAMEBUFFER)!=GL_FRAMEBUFFER_COMPLETE){

std::cerr<<"GL_FRAMEBUFFER_COMPLETE failed, CANNOT use FBO\n";

}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

Drawing:
///*** RENDER FROM LIGHT POV ***///

// Bind framebuffer

glBindFramebuffer(GL_FRAMEBUFFER, shadowmap.fbo);

glClear(GL_DEPTH_BUFFER_BIT);

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

// Draw

glCullFace(GL_FRONT);

glUseProgram(shadowprog.program);

for(unsigned i=0; i<models.size(); ++i){

glBindVertexArray(models[i].vao);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, models[i].indices);

glDrawElements(GL_TRIANGLES, models[i].n_indices, GL_UNSIGNED_INT, 0);

}

glBindVertexArray(0);

glCullFace(GL_BACK);

// Store matrix

Matrix4f textureMatrix=shadowmap.biasMatrix*shadowmap.perspectiveMatrix*shadowmap.modelViewMatrix;



Would not be the first time the ATI driver shows some problems, on the other hand many people say that ATI is stricter on OpenGL while nvidia being more permissive, hence the code may be wrong and only by chance render okay on the nvidia card.

Anyone has some hints?
Thanks!
smani

Share this post


Link to post
Share on other sites
dpadam450    2357
Is the map being taken from one spot or moved because the sphere is obviously not drawn the same. If the shadow map location isn't locked in place, then do so and post the screenshot for both to get a direct comparison.

Share this post


Link to post
Share on other sites
smani    100
The shadowmap is static, it is only the observer camera which rotates around the scene (indeed I could have computed the shadow texture once and used it for all successive frames - but I am testing the framework for a more complex project with moving shadows).

Anyhow, the two shadow maps _should_ be identical.

Share this post


Link to post
Share on other sites
dpadam450    2357
Are you using a shader?

Based on your 2 images your shadow is not being rendered from the same spot because looking at the ground, the sphere's shadow tells you that in one image the light is higher up and looking more down.

Try putting the camera at the lights position on both gpu's and validate that when drawing to the screen that you are actually seeing the same 2 images.

Share this post


Link to post
Share on other sites
smani    100
Well actually the screenshots were obtained using the same identical program on both gpus. They only difference was the screenshot taken at an other viewer angle (note: the shadowmap is always the same since the light position is fix). Anyhow, taking the screenshot at the same observer angle yields http://nas.dyndns.org/temp/ati.png (updated screenshot). I am quite sure I am violating some openGL rules when creating the shadowmap, causing my program to work by chance on the nvidia gpu which tends to be more forgiving and not on the ati gpu - it's just I'm still clueless :s

Share this post


Link to post
Share on other sites
smani    100
Ok, am near to the solution.

Apparently it has nothing to do with the depth buffer, but with how I generate the VAO and it's associated shader attribute pointers:
[CODE]
glGenBuffers(1, &model.vertices);
glBindBuffer(GL_ARRAY_BUFFER, model.vertices);
glBufferData(GL_ARRAY_BUFFER, model.n_vertices*4*sizeof(float), shape.vertices, GL_STATIC_DRAW);
glUseProgram(drawprog.program);
glVertexAttribPointer(drawprog.vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(drawprog.vertexLocation);
glUseProgram(shadowprog.program);
glVertexAttribPointer(shadowprog.vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(shadowprog.vertexLocation);

glGenBuffers(1, &model.normals);
glBindBuffer(GL_ARRAY_BUFFER, model.normals);
glBufferData(GL_ARRAY_BUFFER, model.n_vertices*3*sizeof(float), shape.normals, GL_STATIC_DRAW);
glUseProgram(drawprog.program);
glVertexAttribPointer(drawprog.normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(drawprog.normalLocation);

glGenBuffers(1, &model.indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, model.n_indices*sizeof(unsigned), shape.indices, GL_STATIC_DRAW);

glBindVertexArray(0);
[/CODE]

If I comment out the
[CODE]
glUseProgram(drawprog.program);
glVertexAttribPointer(drawprog.normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(drawprog.normalLocation);
[/CODE]
block, the shadow shader works fine. It appears that the vertex attribute pointers get mixed up between the multiple programs. Is this even legal to do? I though that with vertex array, the vertex attrib pointers get automatically saved in the context of the VAO?

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