Sign in to follow this  

How can I render to a texture that's larger than the control I'm rendering to?

This topic is 3297 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

That article about resolution independence got me curious and I decided to experiment. What I'm wondering is, if the control I'm rendering to is 1024x768, how can I render to a texture of 1280x1024 without having to stretch the contents of the frame buffer? Sorry if it doesn't make sense, I'm not even quite sure what I'm asking, heh.

Share this post


Link to post
Share on other sites
Oh... cool. I'm looking that up now. Apparently the book I learned OpenGL from isn't entirely up to date as there was no mention of frame buffer objects. Thanks, that looks like exactly what I need.

Share this post


Link to post
Share on other sites
Have you some FBO code:


#ifndef FBO_H
#define FBO_H

#include <GL/glew.h>

class CFBO
{
public:
CFBO() : tex(0), fbo(0), rbo(0), width(0), height(0) {}
~CFBO() {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
if(tex) glDeleteTextures(1, &tex);
if(rbo) glDeleteRenderbuffersEXT(1, &rbo);
if(fbo) glDeleteFramebuffersEXT(1, &fbo);
}

int getWidth() { return width; }
int getHeight() { return height; }

bool setSize(int width, int height)
{
if(!GLEW_EXT_framebuffer_object) return false;

this->width = width;
this->height = height;

int max_size;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE_EXT, &max_size);
if(width > max_size) return false;
if(height > max_size) return false;

// create FBO
if(fbo) glDeleteFramebuffersEXT(1, &fbo);
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

// create and attach a new texture as the FBO's color buffer
if(tex) glDeleteTextures(1, &tex);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);

// create and attach a new depth buffer to currently bound FBO
if(rbo) glDeleteRenderbuffersEXT(1, &rbo);
glGenRenderbuffersEXT(1, &rbo);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rbo);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rbo);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // unbind fbo
return get_fbo_status();
}

void Bind() { if(fbo) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); }
void Unbind() { glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); }
void BindTexture() { if(tex) glBindTexture(GL_TEXTURE_2D, tex); }

private:
bool get_fbo_status()
{
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch(status) {
case GL_FRAMEBUFFER_COMPLETE_EXT: return true;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: return false;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: return false;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: return false;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: return false;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: return false;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: return false;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT: return false;
default: return false;
}
}

int width, height;
GLuint tex;
GLuint fbo;
GLuint rbo;
};

#endif

Share this post


Link to post
Share on other sites

This topic is 3297 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.

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