So I've got a major issue in my program. I'm working on building something in OpenGL (using glut), the problem is that I changed my TexureLoader from being just a function, that takes the storagevariable as reference and the name, to a class, which just takes the name (and got the storagevariable as private var).

The thing that tricks me, is that if I call my LoadBMP(char *Address) inside my constructor it wont return the texture, but if I'm calling my LoadBMP inside a function (for an example; the one returning the texture), then it works perfectly...

Any one got a clue why?

Code:

GLTexture::GLTexture(char *Filename)
{
    	TextureName = strlwr(strdup(Filename));
    	
    	if(strstr(TextureName, ".bmp"))
    		LoadBMP(TextureName);
}

^^Wont load texture, when calling:

GLuint GLTexture::GetTexture()
{
    return TextureStorage;
}

However:

GLuint GLTexture::GetTexture()
{
if(TextureNotLoadedYet){LoadBmp(TextureName);TextureNotLoadedYet=false;}
    return TextureStorage;
}

Will load the texture on first run, and then return it perfectly, why?
Shouldn't those 2 examples yield same result?

Recommended Answers

All 4 Replies

If you already haven't, then try debugging the constructor, that is, place a breakpoint at the following line and take it from there ..

TextureName = strlwr(strdup(Filename));

If you already haven't, then try debugging the constructor, that is, place a breakpoint at the following line and take it from there ..

TextureName = strlwr(strdup(Filename));

I've check it, the and nothing is really wrong with it; except from the fact it's not working?

I've check it, the and nothing is really wrong with it; except from the fact it's not working?

So you are saying that you used the debugger and stepped through the code, including the LoadBMP() function and everything worked as expected?

Perhaps you could post the code that exhibits this behaviour.

So you are saying that you used the debugger and stepped through the code, including the LoadBMP() function and everything worked as expected?

Perhaps you could post the code that exhibits this behaviour.

well yea, it just seems like it wont use the texture if it's loaded in the constructor, everything is alike, the function called, the parameters, everything, expect where it's called!

LoadGLTexture.hpp

#ifndef __LOAD_GL_TEXTURE_HPP
#define __LOAD_GL_TEXTURE_HPP

#include <gl\freeglut.h>

class GLTexture
{
    /* Functions */
    public:
        GLTexture(char *Filename);
        ~GLTexture();
        GLuint GetTexture();
    private:
        void LoadBMP(char *PathOfTexture);
        // void LoadTGA
        // void LoadJPG
        // void LoadSomething
        void BuildTexture();
    protected:
    /* Variables */
    public:
    private:
        GLuint TextureStorage;
        char *TextureName;
    protected:
};

#endif

LoadGLTexture.cpp

/* TODO (Skeen#1#): Make a TGA/Universal loader instead of just bmp (avoid glaux.h!) */

/******************/
/* Texture Loader */
/***********************************************/
/* Based upon NeHe - Lesson: 06 (Jeff Molofee) */
/* 2001 Thanks To Maxwell Sayles & Peter Puck  */
/* http://nehe.gamedev.net                     */
/***********************************************/
/* Will in future, be able dectect filetype,   */
/* and use filetype specific loading technique */
/***********************************************/

#include <gl\glaux.h>
#include <gl\freeglut.h>

#include "LoadGLTexture.hpp"

GLTexture::GLTexture(char *Filename)
{
    if(Filename=='\0') // Check if no texture is to load
    {
        BuildTexture();
    }
    else
    {
    	// make the texture name all lower case
    	TextureName = strlwr(strdup(Filename));
    	// check the file extension to see what type of texture
    	if(strstr(TextureName, ".bmp"))
    		LoadBMP(TextureName);
    
    	//if(strstr(TextureName, ".tga"))	
    	//	LoadTGA(Filename);
        // add support for .jpg ect.
    }
}

GLTexture::~GLTexture()
{
}

/********************/
/* Texture Returner */
/********************/
GLuint GLTexture::GetTexture()
{
    return TextureStorage;
}

void GLTexture::BuildTexture()
{
	unsigned char data[12];	// a 2x2 texture at 24 bits

	// Store the data
	for(int i = 0; i < 12; i += 3)
	{
		data[i] = 255;
		data[i+1] = 0;
		data[i+2] = 0;
	}

	// Generate the OpenGL texture id
	glGenTextures(1, &TextureStorage);

	// Bind this texture to its id
	glBindTexture(GL_TEXTURE_2D, TextureStorage);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Use mipmapping filter
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	// Generate the texture
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 2, 2, GL_RGB, GL_UNSIGNED_BYTE, data);
}

void GLTexture::LoadBMP(char *PathOfTexture)
{
        AUX_RGBImageRec *TextureImage;	

        if (TextureImage=auxDIBImageLoad(PathOfTexture));
        {
			glGenTextures(1, &TextureStorage);

			glBindTexture(GL_TEXTURE_2D, TextureStorage);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
        }

        if (TextureImage)				
		{
			if (TextureImage->data)				
			{
				free(TextureImage->data);		
			}
			free(TextureImage);				
		}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.