Jump to content
  • Advertisement
Sign in to follow this  
TurboLento

OpenGL Texture Mapping problem (render to texture)

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

My intention is to render some geometry to a texture, and apply it to a polygon on the screen. I'm using some code from Nehe's lesson 36. But the texture coordinates seem to be off: instead of mapping to the 4 corners of the polygon, there appears to be having some wierd simmetry happening through the diagonal of the polygon. Here are some pictures to explain. The output of the program, using GL_CLAMP as GL_TEXTURE_WRAP: Free Image Hosting at www.ImageShack.us and using GL_REPEAT for the same variable: Free Image Hosting at www.ImageShack.us Here's some (very unoptimized) code. I am a noob at opengl, but i've re-read my code and can't find the problem. Any help? Eternal gratitude to whoever does :).
int u=false;
GLuint textura;
unsigned int* data;

void desenha_peixes() {

		glPushMatrix();
		
		glTranslatef(0,0,-5);
		glRotatef(c,0,1,0);
		c+=1.5;
		
// creating space for texture which will be rendered unto
// this is done only once
		int tamanho_textura = 128; // texture size
		
		if(!u) {
			u=true;
			
			data = (unsigned int*)new GLuint[((tamanho_textura * tamanho_textura)* 4 * sizeof(unsigned int))];
			ZeroMemory(data,((tamanho_textura * tamanho_textura)* 4 * sizeof(unsigned int)));	// Clear Storage Memory
			
			glGenTextures(1,&textura);
			glBindTexture(GL_TEXTURE_2D, textura);
			
			// Build Texture Using Information In data
			glTexImage2D(GL_TEXTURE_2D, 0, 4, tamanho_textura, tamanho_textura, 0,GL_RGBA, GL_UNSIGNED_BYTE, data);
			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);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		}
		

// changes viewport to texture's intended size
		glViewport(0,0,tamanho_textura,tamanho_textura);		
		
// draws geometry to further render to texture, just a cube
		glDisable(GL_TEXTURE_2D);
		glutSolidCube(1);
		glEnable(GL_TEXTURE_2D);

		// Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
		glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, tamanho_textura, tamanho_textura, 0);
		
				
		glViewport( 0, 0, (GLsizei) xres, (GLsizei) yres );	
// return viewport to original size
		
		glPopMatrix();
		glPushMatrix();
		
		glTranslatef(-0,-0,-5);
		
		
// draw polygon with rendered to texture
// AM I DOING ANYTHING WRONG IN HERE? ZOMG
		glBindTexture(GL_TEXTURE_2D, textura);
		glBegin(GL_QUADS);	
		
			glNormal3f(0,0,1);
			
			glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0);
			glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0);
			glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0);
			glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0);
			
			
		glEnd();
		
		glPopMatrix();
	}


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!