views:

783

answers:

4

I have an OpenGl program in which I am displaying an image using textures. I want to be able to load a new image to be displayed.

In my Init function I call:

Gl.glGenTextures(1, mTextures);

Since only one image will be displayed at time, I am using the same texture name for each image.

Each time a new image is loaded I call the following:

Gl.glBindTexture(Gl.GL_TEXTURE_2D, mTexture[0]);

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_LUMINANCE, mTexSizeX, mTexSizeY, 0, Gl.GL_LUMINANCE, Gl.GL_UNSIGNED_SHORT, mTexBuffer);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);   
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

The first image will display as expected. However, all images load after the first, display as all black.

What am I doing wrong?

A: 

When do you submit the geometry using these textures? I assume that is interleaved with the texture loading, although you don't show that. Of course, if you are going to replace the texture name with different images, you don't need to use texture names at all, do you? You could just do the glTextImage2D call when you need to set a new texture, and then use that for as long as necessary, never involving glBindTexture() at all.

An obvious thing to do is of course to insert error-checking code, OpenGL stores an error code when an operation fails and this code is not overwritten until read by the application. See glGetError() for more on this.

unwind
Thanks for you suggestions.After an image is loaded I post a redraw.I tried calling glBindTexture once, and calling just glTexImage2d when loading an image. This didn't help. I also added calls to glGetError after glTexImage2d and after my drawing code, no Errors
Dan Vogel
A: 

The function you are looking for is "glCopyTexImage2D" or "glCopyTexSubImage2D".

+1  A: 

I don't think the problem is in the code you're showing. You'll need to provide more information as to how you draw with the texture, as well as what the texture data passed to glTexImage2D looks like. Are you sure it's still valid by the time you call glTexImage2D ?

Bahbar