Jump to content
  • Advertisement
Sign in to follow this  
BlimeyOReilly

texture mapping

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

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Are you displaying the proper face? (front/back)

Share this post


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

Share this post


Link to post
Share on other sites
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
// position
void 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 ship
void 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);

}

Share this post


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

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!