At last I made the DevIL work, but I have a new problem. I have a round texture on a quad. How can I make the quad invisible (0% opaque) when the texture is transparent, so the user can't see the quad's corners?
First, make an gif texture (or png texture) that have some invisible parts. Then enable blending before rendering process (and bind your texture of course):
for example:
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures.getTexture_grass1());
... polygon ...
Strange, it's not working though my gif and png images are (I think so) transparent. The images just get a bit darker.
Here's my init GL code:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(-1.0f, 1.0f, -(float)displayMode.getHeight()/displayMode.getWidth(), (float)displayMode.getHeight()/displayMode.getWidth());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
And here's the rendered code:
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2f(getX() - (size / 2), getY() - (size / 2));
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2f(getX() + (size / 2), getY() - (size / 2));
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2f(getX() + (size / 2), getY() + (size / 2));
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2f(getX() - (size / 2), getY() + (size / 2));
GL11.glEnd();
My gif image:
http://img287.imageshack.us/img287/3130/player1hc.gif
My png image:
http://img287.imageshack.us/img287/3728/player5kk.png
hmm.. try to do the following before render process:
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.1f);
That should prevent to draw any pixels that have alpha value less then 0.1
If that doesn't work, then maybe is problem with your texture loading.
And yes, do you use IL_RGBA
in your texture loader?
Still I see the images with corners.
My init code:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.1f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(-1.0f, 1.0f, -(float)displayMode.getHeight()/displayMode.getWidth(), (float)displayMode.getHeight()/displayMode.getWidth());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
My loading image code:
IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
IL.ilGenImages(image);
IL.ilBindImage(image.get(0));
IL.ilLoadImage(path);
ILU.iluFlipImage();
IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
ByteBuffer scratch = ByteBuffer.allocateDirect(IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 3);
IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGB, 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);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, IL.ilGetInteger(IL.IL_IMAGE_WIDTH),
IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
return buf.get(0);
Thanks for the reply!
I think that's it! in your texturing code replace IL_RGB with IL_RGBA (you have 2 lines with IL_RGB)... hope it helps.
Ahhhhhh.... Thanks! Didn't notice it! :D
Ok here's my code:
IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
IL.ilGenImages(image);
IL.ilBindImage(image.get(0));
IL.ilLoadImage(path);
ILU.iluFlipImage();
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);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 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 buf.get(0);
But still I see only a white circle (hmmm very close to the circle in the "Ring" film :) strange...)
Ok now it works. Just had to change the background to the other colour...