Jump to content
  • Advertisement
Sign in to follow this  
whiterook6

Rendering to Texture, Drawing that Texture

This topic is 2775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm trying to test my framebuffer/render to texture code, and I'm stuck. Specifically, when I try to render all blue to a texture and then draw that texture, instead of the blue quad I have floating in space, all I get is a white quad. I believe I've done the framebuffer creation/binding and texture creation/attaching thing correctly, since I've been up and down the framebuffers 101 tutorial, and since I've gotten code like this to work before.

(Of course, I might have just been staring at the problem for so long that I can't see it any more.)

I suspect, though, that I just don't get something about rendering. To render to the texture, after binding it, I set the viewing matrices to the identity then used glOrtho(), then set the viewport to the texture size, not window size, then draw a full screen quad. THEN I unbind, detach, whatever, and reset the view and the matrices, and draw a quad, texture mapped, binded the texture to GL_TEXTURE2D, etc. But the texture doesn't show up, just a white quad. Can someone lend me a spare pair of eyes and say, "ah, you've just missed this?"

[source lang="c++"]
GLuint workingFBO;
GLuint testTexture;
int dataWidth=1024, dataHeight=1024, screenwidth=1680, screenHeight=1050;


// First, rendering to the texture
// framebuffer gen
glGenFramebuffersEXT(1, &workingFBO);

// build the texture
glGenTextures(1, &testTexture);
glBindTexture(GL_TEXTURE_2D, testTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, dataWidth, dataHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);

// prepare to render to texture: bind fbo, attach texture, check status
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, workingFBO);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, testTexture, 0);
GLenum status=glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
assert(status==GL_FRAMEBUFFER_COMPLETE_EXT);

// Viewport and viewing matrices
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0,0,dataWidth,dataHeight);
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// Color and depth
glDisable(GL_DEPTH_TEST);
glColor4f(0.0f, 0.0f,1.0f, 1.0f);


// fill the texture blue
glBegin(GL_QUADS);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glVertex2f(1.0f, 0.0f);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();


// clean up
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

// Later...
// Rendering the texture on a quad:
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, screenWidth, screenHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Matrices
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, static_cast<float>(screenWidth)/screenHeight, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-10, -10, 10, 0,0,2, 0,0,1);

// Bind
glBindTexture(GL_TEXTURE_2D, testTexture);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Set the color to white to avoid tinting the texture

// Draw the white quad texture mapped with the above texture -- should be blue
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D, 0);
glPopAttrib();[/source]

If you're still reading, as you might have guessed, the quad floats in space completely white, blue texture free. Am I missing something? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!