Skip to main content
GameDev.net gamedev.net
🔒 Locked

Depth Texture without gradient and in red color

Started by Hector San Roman Lanza Mar 19, 2013 at 5:04 PM 0 replies 3.4k views
Original Post
Hector San Roman Lanza
Hector San Roman Lanza

Hi!

I render the depth texture to generate the shadow mapping. I am following this tutorial:

http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

In this tutorial, the author obtain a greyscale depthmap, but I obtain a red depth map without gradient.

My code:


// After initialize the meshes and load the textures, generate the textures in OpenGL
void Engine::GenerateTextures()
{
    // Generate the textures
    textureCount = texturelist.size()+1;
texturesID.reserve(textureCount);   // +1 to reserve space for depth texture
glGenTextures(textureCount, &texturesID[0]);
    int c = 0;
    foreach(std::list<Texture*>, texturelist)
    {
        glActiveTexture(GL_TEXTURE0+c);
        glBindTexture(GL_TEXTURE_2D, texturesID[c]);
        ExitOnGLError("Error: Could not bind the texture");
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, (*it)->width, (*it)->height, 0, GL_BGRA,GL_UNSIGNED_BYTE,(*it)->pixels );
        ExitOnGLError("Error: Could not create the texture with glTexImage2D");
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
        glGenerateMipmap(GL_TEXTURE_2D);
        c++;
    }


    // Initialize and binds the framebuffer.
    ShadowFramebuffer = 0;
    glGenFramebuffers(1, &ShadowFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, ShadowFramebuffer);
    // Generate the depth texture
    glActiveTexture(GL_TEXTURE0+c);
    glBindTexture(GL_TEXTURE_2D, texturesID[c]);
    ExitOnGLError("Error: Could not bind the shadow texture");
    glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
    ExitOnGLError("Error: Could not create the shadow texture with glTexImage2D");
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


    // Attach the texture to the framebuffer
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  texturesID[c], 0);
    ExitOnGLError("Error: Could not attach the texture to the frame buffer");
    glDrawBuffer(GL_NONE); // No color buffer is drawn to.
    ExitOnGLError("Error: Could not draw the buffer");
    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        ExitOnGLError("Error: FrameBuffer isn´t OK");
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
 

void Engine::Draw(void)
{
    drawDepthMap = true;
    if(drawDepthMap)
    {
       // draw the objects using the depthRTT shader
        foreach(std::list<GameScreen*>, gamescreens)
        {
            (*it)->Draw();
        }
    }
}
 

depthRTT.glsl


#ifdef COMPILING_VS


layout(location=0) in vec4 in_Position;


uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;


void main(void)
{
gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * in_Position;
}
#endif


#ifdef COMPILING_FS


out float fragmentdepth;


void main(void)
{
fragmentdepth = gl_FragCoord.z;
}


#endif
 

http://imageshack.us/photo/my-images/259/sinttulovbu.png/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.