GLFW texturing

I’m new to OpenGL and I managed to load a simple .tga image and apply it as a texture to a plain.

However I’m still just a little confused, because the way I do it is by calling the function glfwLoadTexture2D every loop to apply it, but I’m thinking that that’s not very efficient to load the texture every loop.
So I tried to put the drawing and texturing commands in a display list and simply using glCallList every loop instead, but this gives me a runtime error.
What’s the best method of applying textures with GLFW?

  1. As written in the (excellent) glfw documentation, this command replaces glTexImage2d. so, as with opengl, bind a texture name to be able to recall it later.
    http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

glGenTextures(1, &texture1);// Create The Texture name
glBindTexture(GL_TEXTURE_2D, texture1);
// glfwload texture
// gltexparameters etc

// repeat for more textures

then simply bind the correct texture before drawing, and you are set.

  1. display list should work, if not you are doing something wrong. put some glGetError() everywhere, use a debugger, read the documentation :
    http://www.opengl.org/sdk/docs/man/
    and if you still have problems, post some code.

I think your mistake is not understanding that you don’t have to have to load a texture every time. The image is then “bound” to the appropriate texture object. You can select a texture object to be current with BindTexture operation. Basically, you load the textures on a preprocess step and use the data for rendering.

Ok, I know I’m doing something stupid now, as I had it working a few days ago, but I just cannot see what I’m doing wrong all of a sudden.


GLuint tempInt, tempIntTwo;

glGenTextures(1, &tempInt);
glBindTexture(GL_TEXTURE_2D, tempInt);
glfwLoadTexture2D("crate.tga", GLFW_BUILD_MIPMAPS_BIT);
	
glGenTextures(1, &tempIntTwo);
glBindTexture(GL_TEXTURE_2D, tempIntTwo);
glfwLoadTexture2D("OGL.tga", GLFW_BUILD_MIPMAPS_BIT);

and then I do, glBindTexture(GL_TEXTURE_2D, tempInt);
To bind the “crate.tga” texture, and then I draw a quad.

However, I don’t see the crate texture instead I see the OGL.tga texture, it appears to overwrite the first one, why is this?

Probably a stupid question… but you did put the glBindTexture(GL_TEXTURE_2D, tempInt) call outside of the glBegin(GL_QUADS)/glEnd() scope, right?

N.

that’s not a stupid question because that is exactly what I was doing.

actually that wasn’t the problem…
But I know what was.
I’m making a loader, and it reads the texture filenames from a file and I’m using vectors to hold the strings, so the first texture is read in by using vectorName.push_back(char);
so then I pass the string into my texture loading function with &texture[0].

To test it I printed it to the screen and the first texture looks corretct:
“crate.tga”
the next one is all messed up:
“OGL.tga(and a bunch of junk after it)”

I’ve been trying vectorName.clear(); vectorName.resize(0); and some others but nothing is working please help.

Why don’t you use the c++ string class? Most likely the problem is that your self implemented strings aren’t null-terminated (’\0’) so the function does not know where the string ends.

N.