Jump to content
  • Advertisement
Sign in to follow this  
Funkapotamus

OpenGL Pixel-based texture coords error.

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

Disclaimer: I'm using OpenGL in Java via LWJGL/DevIL. I have posted this issue at the LWJGL forums. There is a possibility that this error is LWJGL specific. However I havn't resolved the issue using their forums and it still may be an OpenGL issue. Thus, my next stop is here. It's my understanding that OpenGL uses normalized texture coordinates (0.0f to 1.0f) by default. Supposedly GL_TEXTURE_RECTANGLE_ARB changes this to use pixel-based coordinates. I have gotten this flag to work correctly, however, errors occur when multiple textures are loaded in this fashion. Here's the code. It still serves as pseudocode for non-Java people.
public static Texture loadTexture(String ref){
IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
IL.ilGenImages(image);
IL.ilBindImage(image.get(0));
IL.ilLoadImage(ref);

IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
ByteBuffer scratch = ByteBuffer.allocateDirect(IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 4);
IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGBA, IL.IL_BYTE, scratch);

IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glGenTextures(buf);

GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));

GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);     

// Generate Texture
GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL11.GL_RGBA,
 IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0,
 GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);

return new Texture(buf.get(0), IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT));
}
As you see, in glTextImage2D I'm using "ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB" in place of where I'd normally put "GL11.GL_TEXTURE_2D". If I were to load one texture and draw it, everything works fine. For example, drawing the first 128x128 pixels of a 256x256px image will work properly if I use "128.0f" instead of "0.5f" for the texture coords. The problem occurs when I load more than one image. No matter what, whenever I attempt to draw a texture after loading multiple ones, it will always draw the image loaded last. To me this seems as if something something's pointing to the wrong spot in video memory. Now, whether or not this is a flaw in LWJGL or I'm using OpenGL wrong is why I'm posting here.

Share this post


Link to post
Share on other sites
Advertisement
One thing that didn't occur to me until now is when glGenTextures(n) is called. Every time I loading an image, I generate just one texture (glGenTextures(1)). Perhaps GL_TEXTURE_RECTANGLE_ARB prefers that multiple textures be loaded at once i.e. glGenTextures(1000)? This seems a little silly though.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!