Jump to content

  • Log In with Google      Sign In   
  • Create Account


Hati Eth

Member Since 07 Jan 2012
Offline Last Active Jul 11 2012 05:56 AM
-----

Topics I've Started

Problem/Question regarding FBOs and Render to Texture

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)

PARTNERS