Loading a new texture into an already defined texture name

I have an OpenGl program in which I display an image using textures. I want to be able to upload a new image to display.

In my Init function, I call:

Gl.glGenTextures(1, mTextures);

      

Since only one image will be displayed at the time of rendering, I use the same texture name for each image.

Every 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 are loaded after the first, displayed as all black.

What am I doing wrong?

0


source to share


4 answers


I don't think the problem is with the code you are showing. You will need to provide additional information about how you draw the texture, as well as how the texture data passed to glTexImage2D

. Are you sure it is still valid at the time of the call glTexImage2D

?



+1


source


Did you complete glDisable GL_TEXTURE_2D

after drawing? Put it down glColor4f(1,1,1,1);

, if 2d texture is off, you should see at least a white rectangle. This will help you find the problem here.



+1


source


When do you submit geometry with these textures? I am assuming it alternates with loading the texture, although you don't show it. Of course, if you're going to replace the texture name with different images, you don't need to use texture names at all, right? You can simply make a call glTextImage2D

whenever you need to set a new texture and then use it for as long as needed, never including glBindTexture()

at all.

It would be obvious, 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 details .

0


source


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

0


source







All Articles