openGL render to texture renders always black geometry

This is the only part of the code that could be buggy:

        GLuint tex_name;
        glGenTextures(1, &tex_name);
        // set id to the gl_texture_id map for later use
        gl_texture_id[t] = tex_name;
        // bind texture
        glBindTexture(GL_TEXTURE_2D, tex_name);
        // set texture filtering parameters
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glGenerateMipmap(GL_TEXTURE_2D);
        // load texture data
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,t->width(),t->height(),0,GL_BGRA,GL_UNSIGNED_BYTE,t->data());

Could you see something wrong in this code? enabling glEnable(GL_TEXTURE_2D) is not making the difference. Texture coordinates are right, fragment and vertex shader are right for sure.

SOLVED That was not the issue, i'm still using glGenerateMipmap (...) before glTexImage2D (...). The real problem is that i passed as format GL_RGBA when my image is in GL_RGB format. Additionally my t->data() array was height*width*sizeof(GL_FLOAT) long and i was passing GL_UNSIGNED_BYTE as type parameter causing data loss. Althougth this works you still have right, in fact preceding glTexImage2D with glGenerateMipmap causes weird effects on Nvidia hardware while life is beautiful (strangely) on ATI GPUs.


Why are you calling glGenerateMipmap (...) on a texture that has no data store?

You need to allocate at least image level 0 before this will work (eg call glTexImage2D (...) . You should be calling this function after you draw into your texture each frame, the way you have it right now it actually does nothing and when you finally draw into your texture you are only generating an image for 1 LOD. I would remove the mipmap texture filter if you are not going to re-compute the mipmaps everytime you give texture image level 0 data.

I also do not see what this has to do with rendering to a texture? You are passing image data to your texture from client memory. Usually when you render to a texture, this is done using either a pixel buffer (old school) or frame buffer object.

链接地址: http://www.djcxy.com/p/38540.html

上一篇: opengl 3.1纹理不加载

下一篇: openGL渲染纹理渲染总是黑色的几何