Hello Guest

Texture problem

  • 9 Replies
  • 9357 Views
Texture problem
« on: December 08, 2005, 21:17:43 »
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?

*

Offline hvor

  • *
  • 37
    • http://hvor.madpage.com
Texture problem
« Reply #1 on: December 09, 2005, 09:09:13 »
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:
Code: [Select]

        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 ...

Texture problem
« Reply #2 on: December 09, 2005, 14:33:41 »
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:
Code: [Select]

        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:
Code: [Select]

        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

*

Offline hvor

  • *
  • 37
    • http://hvor.madpage.com
Texture problem
« Reply #3 on: December 09, 2005, 15:00:13 »
hmm.. try to do the following before render process:
Code: [Select]
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.

*

Offline hvor

  • *
  • 37
    • http://hvor.madpage.com
Texture problem
« Reply #4 on: December 09, 2005, 15:01:40 »
And yes, do you use
Code: [Select]
IL_RGBA in your texture loader?

Texture problem
« Reply #5 on: December 09, 2005, 15:35:39 »
Still I see the images with corners.
My init code:
Code: [Select]

        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:
Code: [Select]

        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!

*

Offline hvor

  • *
  • 37
    • http://hvor.madpage.com
Texture problem
« Reply #6 on: December 09, 2005, 15:40:56 »
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.

Texture problem
« Reply #7 on: December 09, 2005, 15:43:38 »
Ahhhhhh.... Thanks! Didn't notice it!  :D

Texture problem
« Reply #8 on: December 09, 2005, 15:45:29 »
Ok here's my code:
Code: [Select]

        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...)

Texture problem
« Reply #9 on: December 09, 2005, 16:51:24 »
Ok now it works. Just had to change the background to the other colour...