Jump to content
  • Advertisement
Sign in to follow this  
HatiEth

Problem/Question regarding FBOs and Render to Texture

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

Hi, I have a rather odd problem regarding my code I've implemented FBO, but have got a rather odd problem with my code.
I've implemented it twice, where both implementations should work (atleast from my point of view , but I'm properly just code-blind)

The not working code is:

void CParticleSystemMouse::generate( void )
{
glGenFramebuffers(1, &m_frontBuffer);
glGenFramebuffers(1, &m_backBuffer);
glBindFramebuffer(1, m_frontBuffer);

glGenTextures(1, &m_frontParticles);
glBindTexture(GL_TEXTURE_2D,m_frontParticles);
int AM = m_width*m_height*4; //amount of data
srand(0);
int r =0;
char* data = new char[AM];
for(int i = 0; i < AM; i+=4) {
r = rand()%10;
//std::cout << r << std::endl;
data[i+0] = 0; //x velo
data[i+1] = 0; //y velo
data[i+2] = 255; //~
data[i+3] = (rand()%10)<5?255:0; // is particle
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,m_width,m_height,0,GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)data);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,m_frontParticles,0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
std::cout << "FBO("<<m_frontBuffer <<") incomplete!" << std::endl;
glBindTexture(GL_TEXTURE_2D,0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
glBindFramebuffer(GL_FRAMEBUFFER,m_backBuffer);
glGenTextures(1, &m_backParticles);
glBindTexture(GL_TEXTURE_2D,m_backParticles);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,m_width,m_height,0,GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)0);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,m_backParticles,0);
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status!= GL_FRAMEBUFFER_COMPLETE)
std::cout << "FBO("<<m_frontBuffer <<") incomplete!" << std::endl;
glBindTexture(GL_TEXTURE_2D,0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
delete [] data;
m_calcShader = new CShader(NULL,NULL,"particlesystemmouse.frag");
swapBuffers();
}

It is generating the FBOs successfully - I can render to them but the texture is not actually saved after rendering to it.

the swapBuffers function is:

void CParticleSystemMouse::swapBuffers() {

GLuint tmp = m_frontParticles;
m_frontParticles = m_backParticles;
m_backParticles = tmp;

tmp = m_frontBuffer;
m_frontBuffer = m_backBuffer;
m_backBuffer = tmp;
}


I then copied the generate code from another class where it was working correctly, but I have no clue why it does, and the problem is localized in the generate function that I can confirm so far.

void CParticleSystemMouse::generate( void )
{
//Gen firstbuffer
glGenFramebuffers(1,&m_frontBuffer);
glGenFramebuffers(1,&m_backBuffer);
//Bind firstbuffer
glBindFramebuffer(GL_FRAMEBUFFER,m_frontBuffer);

GLuint depthBuffer;
glGenRenderbuffers(1,&depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,depthBuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width,m_height);

glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthBuffer);

int AM = m_width*m_height*4;

srand(0);
//srand(time(NULL));

char* data = new char[AM];
int r = 0;
for(int i = 0;i<AM;i+=4){
r = rand() % 10;
if(r<5) {
data[i+0] = 0;
data[i+1] = 0;
data[i+2] = 255;
data[i+3] = 255;
}
else {
data[i+0] = 0;
data[i+1] = 0;
data[i+2] = 0;
data[i+3] = 0;
}
}
/*bool swapSet=false;
for(int i=0;i<AM;i+=4) {
data[i+0] = 0;
data[i+1] = swapSet?255:0;
data[i+2] = 0;
data[i+3] = 255;
swapSet=!swapSet;
}*/

// Texture0
glGenTextures(1,&m_frontParticles);
glBindTexture(GL_TEXTURE_2D,m_frontParticles);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,m_width,m_height,0,GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)data);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,m_frontParticles,0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if(status != GL_FRAMEBUFFER_COMPLETE)
std::cout << "[Error] FBO " << m_frontBuffer << " incomplete!" << std::endl;


glBindTexture(GL_TEXTURE_2D,0);
glBindRenderbuffer(GL_RENDERBUFFER,0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
//Gen backBuffer

glBindFramebuffer(GL_FRAMEBUFFER,m_backBuffer);
depthBuffer = 0;
glGenRenderbuffers(1,&depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,depthBuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width,m_height);

glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthBuffer);

// backTexture 0
glGenTextures(1,&m_backParticles);
glBindTexture(GL_TEXTURE_2D,m_backParticles);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,m_width,m_height,0,GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)0);

//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,m_backParticles,0);

status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if(status != GL_FRAMEBUFFER_COMPLETE)
std::cout << "[Error] FBO " << m_backBuffer << " incomplete!" << std::endl;

m_calcShader = new CShader(NULL, NULL, "particlesystemmouse.frag");

delete [] data;

glBindFramebuffer(GL_FRAMEBUFFER,0);
glBindTexture(GL_TEXTURE_2D,0);
glBindRenderbuffer(GL_RENDERBUFFER,0);

swapBuffers();
}



So, can someone explain me, what is going on, is there something I'm ignoring ?
(Just in case someone points to the render buffer, I've already commented them out in the working generate code - ain't no change to the working state)

Share this post


Link to post
Share on other sites
Advertisement
In your non-working generate function, you don't set the GL_DEPTH_ATTACHMENT for either of the FBOs. Try setting those and see if that works.

Also, this:

glBindFramebuffer(1, m_frontBuffer);


should be this:

glBindFramebuffer(GL_FRAMEBUFFER, m_frontBuffer);

Share this post


Link to post
Share on other sites

In your non-working generate function, you don't set the GL_DEPTH_ATTACHMENT for either of the FBOs. Try setting those and see if that works.


RenderBuffers (including Depth-Buffers are not actually necessary for what I try to do - like i said in the last lines of my first post - i already tried if it was them by commenting them out in the working code which changed nothing on the working status)


Also, this:

glBindFramebuffer(1, m_frontBuffer);


should be this:

glBindFramebuffer(GL_FRAMEBUFFER, m_frontBuffer);


Thanks, that was the issue, code-blindness :) - didn't saw it.

Share this post


Link to post
Share on other sites
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!