Sign in to follow this  

Help with Texture Mapping!!!!!!

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

Hello everyone, I need help figuring out why my Texture mapping class won't work? I'm trying to implement it with the singleton pattern, but I get the same textureID for every image that I try to load. It doesn't return any errors, it just doesn't texture the quad I'm trying to map to. I know the texture works, because I used the standard NeHe tutorial and replaced the picture with mine and it worked. Here's where I use it
// in constructor
textureID = GLTexture::getInstance()->LoadGLTextures("Images/asteroid.bmp");
// in draw routine
glEnable(GL_TEXTURE_2D);
	  glBindTexture(GL_TEXTURE_2D, textureID);
	  glPushMatrix();
		glScalef( 10.0, 10.0, 10.0 );
		glColor3f(1.0, 0.12, 0.5);
		glBegin (GL_POLYGON);								
			glTexCoord2f ( 0.0f, 0.0f);	glVertex3f(-15.0f,-15.0f, 0.0f);
			glTexCoord2f ( 1.0f, 0.0f);	glVertex3f( 15.0f,-15.0f, 0.0f);
			glTexCoord2f ( 1.0f, 1.0f);	glVertex3f( 15.0f, 15.0f, 0.0f);
			glTexCoord2f ( 0.0f, 1.0f); glVertex3f(-15.0f, 15.0f, 0.0f);
		glEnd ();
	  glPopMatrix();
	  glDisable(GL_TEXTURE_2D);


Here's the whole Texture Mapping class
#ifndef GLTEXTURE_MODULE
#define GLTEXTURE_MODULE

#include "Game.h"

class GLTexture
{
private:
	static bool instanceFlag;
    static GLTexture *glTexInstance;

	GLTexture()
	{
		instanceFlag = false;
		glTexInstance = NULL;
	}
	virtual ~GLTexture()
	{
        instanceFlag = false;
    }
	AUX_RGBImageRec * LoadBMP(char *Filename); // Loads The Bitmap Images

public:
	static GLTexture* getInstance();
	GLuint LoadGLTextures(char *texFile); // Load Bitmaps And Convert To A Texture

};

bool GLTexture::instanceFlag = false;
GLTexture* GLTexture::glTexInstance = NULL;

GLTexture* GLTexture::getInstance()
{
	if(! instanceFlag)
    {
		cout << "\nNew Instance GLTexture!";
        glTexInstance = new GLTexture();
        instanceFlag = true;
        return glTexInstance;
    }
    else
    {
        return glTexInstance;
    }
}

GLuint GLTexture::LoadGLTextures(char *texFile) /* Load Bitmaps And Convert To A Texture */
{
    GLuint texID[1];
	AUX_RGBImageRec *TextureImage[1];						// Create Storage Space For The Texture
    memset(TextureImage,0,sizeof(void *)*1);				// Set The Pointer To NULL

	if ( (TextureImage[0] = LoadBMP(texFile) ) )			// Load The Image File
	{
		glGenTextures(1, &texID[0]);								// Create The Texture
		glBindTexture(GL_TEXTURE_2D, texID[0]);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			
		if (TextureImage[0])								// If Texture Exists	
		{
			if (TextureImage[0]->data)						// If Texture Image Exists
				free(TextureImage[0]->data);				// Free The Texture Image Memory
			free(TextureImage[0]);							// Free The Image Structure
		}
	}
	else
	{	
		char error_string[40];									// Pointer to the error message
		sprintf( error_string,"Failed to load %s", texFile );
		MessageBox(NULL,error_string,"ERROR",MB_OK|MB_ICONEXCLAMATION);
	}

	return texID[0];											// Return The Status
}

AUX_RGBImageRec * GLTexture::LoadBMP(char * Filename)       
/* Loads The Bitmap Images */
{
		FILE *File=NULL;								    // File Handle
		if (!Filename)										// Make Sure A Filename Was Given
		{
			return NULL;									// If Not Return NULL
		}
		File=fopen(Filename,"r");							// Check To See If The File Exists
		if (File)											// Does The File Exist?
		{
			fclose(File);									// Close The Handle
			return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
		}
		return NULL;										// If Load Failed Return NULL
}

#endif



Also, I put print stements in where the glGenTex function call is and the textureID stays at 0.....? [Edited by - JDev on January 3, 2005 4:27:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
not sure... could be because of your local "GLuint texID[1];" variable, why not just defining an array of texture IDs as a member variable in your class?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Do you have lighting enabled?

Share this post


Link to post
Share on other sites
no lighting enabled for now.
HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Share this post


Link to post
Share on other sites
My guess: Make sure you've created the OpenGL context & selected it before you try creating textures.
win32:
wglMakeCurrent(context_render=wglCreateContext(context_draw))

Try asking gl what texture space is free before you've selected a context & gl will give an unfriendly response.
But besides that you could always try asking it ;] glGetError()

Best of luck

Share this post


Link to post
Share on other sites
Try changing the LoadGLTextures() function to


GLuint texID; // don't make this an array
AUX_RGBImageRec *TextureImage; // don't make this an array either

memset(TextureImage,0,sizeof(void *)*1);

if ( (TextureImage = LoadBMP(texFile) ) )
{
glGenTextures(1, &texID);

// Create The Texture
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

if (TextureImage)
{
if (TextureImage->data) {
free(TextureImage->data); free(TextureImage); }
}
}

else
{
char error_string[40]; // Pointer to the error message
sprintf( error_string,"Failed to load %s", texFile );
MessageBox(NULL,error_string,"ERROR",MB_OK|MB_ICONEXCLAMATION);
}

return texID;

Share this post


Link to post
Share on other sites
Ok, so I figured out it has something to do with initialization, and when I'm actually loading the texutre. I'm using GLUT, and I can get the texture to load, only it disables my key events for a couple of seconds each time I start my game up. I'm currently calling it at the end of the init routine, but I'm still not satisfied with the results.

Share this post


Link to post
Share on other sites
Sign in to follow this