Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Problem/Question regarding FBOs and Render to Texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Hati Eth   Members   -  Reputation: 125

Like
0Likes
Like

Posted 11 January 2012 - 07:45 AM

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)

Sponsor:

#2 shacktar   Members   -  Reputation: 809

Like
1Likes
Like

Posted 11 January 2012 - 08:55 AM

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);


#3 Hati Eth   Members   -  Reputation: 125

Like
0Likes
Like

Posted 11 January 2012 - 11:37 AM

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.

#4 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 11 January 2012 - 11:38 AM

http://www.opengl.org/wiki/Framebuffer_Object_Examples#Color_only
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS