texture mapping

Started by
5 comments, last by izzo 19 years, 6 months ago
I am trying to make a 2D asteroids clone, but am having problems with texture mapping. I am using the same code i used recently for a 3D tank game, which texture mapped successfully. But now it just leaves the quad i am trying to texture map white. The bitmap loads ok..so it must have something to do with the binding. i am enabling GL_TEXTURE_2D, and also generating a texture name. Is there any reason why it would work with perspective projections but not orthogonal
Advertisement
I myself had a similar problem with Direct3D - but if it's the same error then it might not matter.

Are you running the program directly from the IDE in debug mode? If so run it by double clicking on the application instead of running it inside the IDE (if you're using one).

Also - make sure the bitmap is in the appropriate directory.

EDIT: I mispelled a few words.
=============================All knowledge is good; only the way it is put to use is good or evil.
No unfortunatly that doesnt do the trick...im sure its to do with the way i am setting up Opengl...i am using the init class from game tutorials...only i have changed perspective to glOrtho. I have also taken out the depth testing as i assume you dont need this...im pretty sure that these changes have made init class not work with texture maps. Please can anyone help...OR just direct me to a website that gives code to set up opengl in 2D, like a .cpp file, as this is all i need!!
Are you displaying the proper face? (front/back)
double and triple check that the texture's dimensions are a power of 2. hell, sometimes i have to open, and re-save a texture for it to work... thats another thing, are you using the same exact texture loading code? what are you using to load in images? openGL is pretty screwy and picky with how you load in textures...
FTA, my 2D futuristic action MMORPG
no..the texture is the right size and works ok...as im using a texture i know that works from previous code. I also know that my loading function works as well...im using a class from an openGL book...ive used it before in a 3D game. Im also sure my code is correct

#include "Asteroid.h"#include <math.h>// for random function#include "stdlib.h"// defines the math function pi#define PI	(3.14159265359f)// macro to convert degrees to radians#define DEG2RAD(a)	(PI/180*(a))// To draw the spaceship the ship has to translated back to the centre so that // it can be rotated on the spot. Then it can be rotated back to its original // positionvoid CAsteroid::Draw(){	// create spaceship	glPushMatrix();		// translate the spaceship back to the original position		//glTranslated(+xPosition, +yPosition, 0);		// rotate spaceship		//glRotated(angle, 0,0,1);		// translate spaceship back to centre point		//glTranslated(-xPosition, -yPosition, 0);		// color it purple		//glColor3f(1.0f, 0.0f, 1.0f);		// create vertices of asteriod 		// enable texturing	glEnable(GL_TEXTURE_2D);    glBindTexture(GL_TEXTURE_2D, asteroidTex.texID[0]);		glBegin(GL_QUADS);				glTexCoord2f(0.0f, 1.0f); glVertex3f(xPosition - 0.2, yPosition + 0.2, -1.0);				glTexCoord2f(0.0f, 0.0f); glVertex3f(xPosition - 0.2, yPosition - 0.2, -1.0);			glTexCoord2f(1.0f, 1.0f); glVertex3f(xPosition + 0.2, yPosition - 0.2, -1.0);			glTexCoord2f(1.0f, 1.0f); glVertex3f(xPosition + 0.2, yPosition + 0.2, -1.0);		glEnd();	glPopMatrix();	glDisable(GL_TEXTURE_2D);			ReDraw();}// this method is used to do this physics calculations of the shipvoid CAsteroid::Animate(){	//angle+=incAngle;	/*// convert degrees into radians	radAngle = DEG2RAD(270);	// calculate the speeds of the velocity components	xSpeed -= 0.001 * sin(radAngle);	ySpeed += 0.001 * cos(radAngle);*/	// calculate the new position of the spaceship	//xPosition += 0.02;	//yPosition += -0.02;}void CAsteroid::ReDraw(){	// xPosition redraws	if(xPosition >= 2)		xPosition -= 4;		else if(xPosition <= -2)		xPosition += 4;		//yPosition redraws	if(yPosition >= 2)		yPosition -= 4;	else if(yPosition <= -2)		yPosition += 4;}float CAsteroid::RandomRange(float lowest_number, float highest_number){	float range = highest_number - lowest_number + 1;    return lowest_number + (range * rand()/(RAND_MAX + 1.0));}void CAsteroid::SetupTexture(int n){	 glGenTextures(1, &asteroidTex.texID[n]);     glBindTexture(GL_TEXTURE_2D, asteroidTex.texID[n]);     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);     	 glTexImage2D(GL_TEXTURE_2D, 0, 3, asteroidTex.width, asteroidTex.height,0,                GL_RGB, GL_UNSIGNED_BYTE, asteroidTex.data);}void CAsteroid::Load(){	asteroidTex.LoadTexture("camo.bmp");	SetupTexture(0);	}
Step through your SetupTexture routine to make sure width, height, and data are all valid.

Also you might want to put into your draw routine something like the following:

GLenum err;while ((err = glGetError()) != GL_NO_ERROR){   printf("%s\n", gluErrorString(err));}


to print out any error messages. The above is easier than checking every gl call.

Finally, I forget what the default is, but you might want to try adding:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);


to your SetupTexture routine.

cheers
sam

This topic is closed to new replies.

Advertisement