Jump to content
  • Advertisement
Sign in to follow this  
_sim

Problems Texturing a Sphere

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

Hey everyone, I'm trying to work on a basic billiards game and I am having problems texturing the balls. Just to give a brief description of what is happening, the ball appears fine, but then when I start to rotate it using a call to glRotatef, the texture messes up at a certain point and ball starts rotating in the opposite direction (i.e. if it's rotating in a clockwise direction, half way through the texture becomes strange and it rotates in the counter clockwise direction around the specified axis). I can't figure out what the problem is. My best guess is that there is something wrong with the way I am texturing the Sphere even though the rotation screws up. I'll post a screen shot here of what it looks like when the texture messes up: strange texturing effect So here is the code for my sphere class:
#include "gameobjects/sphere.h"
#include "input/inputmanager.h"

cSphere::cSphere()
:
	quadric(gluNewQuadric()),
	zLoc(0.0f),
	rot(0.0f)
{
	gluQuadricDrawstyle(quadric, GLU_FILL);
	gluQuadricNormals(quadric, GLU_SMOOTH);
	gluQuadricTexture(quadric, GL_TRUE);
	mTextureID = gTextureManager->LoadTextureIntoMemory("data\\art\\b3.bmp");
}

cSphere::~cSphere()
{
	gluDeleteQuadric(quadric);

}

void cSphere::Render()
{
	glLoadIdentity();

	glEnable(GL_TEXTURE_2D);
		gTextureManager->LoadTextureFromMemory(mTextureID);
		glTranslatef(0,0,zLoc);
		glRotatef(rot,1.0f,0.0f,0.0f);
		gluSphere(quadric,1.0f,64,64);
	glDisable(GL_TEXTURE_2D);
}

void cSphere::Update()
{
	if (gInputManager->IsKeyPressed(SDLK_UP))
	{
		zLoc += 0.25f;

		if (zLoc > -3.0f)
		{
			zLoc = -3.0f;
		}
	}
	else if (gInputManager->IsKeyPressed(SDLK_DOWN))
	{
		zLoc -= 0.25f;

		if (zLoc < - 90.0f)
		{
			zLoc = -90.0f;
		}
	}

	if (gInputManager->IsKeyPressed(SDLK_SPACE))
	{
		rot += 2.0f;

		//if (rot > 360.0f)
		//{
		//	rot = 0.0f;
		//}
	}
}

here's the call to my texture manager, it loads the texture fine since the texture can be seen.
void cTextureManager::LoadTextureFromMemory(int textureID)
{
	glBindTexture(GL_TEXTURE_2D, mTextureIDs[textureID]);

	// turn off basic texture mapping and use mip-mapping
	glTexImage2D(GL_TEXTURE_2D, 0, 3, mTextureImages[textureID]->sizeX, mTextureImages[textureID]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, mTextureImages[textureID]->data);

	// mip-mapping!!
	//gluBuild2DMipmaps(GL_TEXTURE_2D, 3, mTextureImages[textureID]->sizeX, mTextureImages[textureID]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, mTextureImages[textureID]->data);

	//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	//
	//// not sure what this is for...
	//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

	glTexParameterf (GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameterf (GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexParameterf (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameterf (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
}

The texture is a 16 bit bmp with a size of 512x256. Anyone have any idea what the problem is?

Share this post


Link to post
Share on other sites
Advertisement
It's not texture related, it looks like there is no depth testing or backface culling, and without this sometimes the backside is rendered on top of the front side.

Share this post


Link to post
Share on other sites
That fixed it! I was missing the following:

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

Thanks a lot! I'm still a bit confused about certain things, but I'll start a different thread for that since it's unrelated.

Thanks!!!

Share this post


Link to post
Share on other sites
Quote:
Original post by dpadam450
It works with glDepthMask? I believe if depthmask is true, no depth data is written, only tested.
Close [wink]
Quote:
From the glDepthMask man page
glDepthMask specifies whether the depth buffer is enabled for writing. If flag is GL_FALSE, depth buffer writing is disabled. Otherwise, it is enabled. Initially, depth buffer writing is enabled.

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.

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!