Vidde's picture

Wierd texture over polygon transparency problem

Hello

I'm fairly new to OpenGL and OpenTK and have encountered a strange texture problem I'm not able to find an answer too.
I did do research in this matter but did not find anything that that worked.

I'm using the TexLib class to do my asset loading, just to ensure it's not me doing some funky loading error.
When i load and draw polygons and textures they look perfectly fine and .png files have transparency, by when I change their z-order i get some wierd results.
Sometimes the textures show the polygon behind of them and paint the background color over other textures, and sometimes the opposite occurs.

Let me show you.

The way I draw my polygons is pretty simple.
In my renderer class i do:

            // Clears the color bit buffers
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            // This is the area where all the drawing is done
            GL.Enable(EnableCap.DepthTest);
 
            for (int n = 0; n < drawPolygonList.Count(); n++)
            {
                drawPolygonList[n].Draw();
            }
 
            GL.Disable(EnableCap.DepthTest);
 
 
            // This is the end of the drawing area
            // Uppdates the picture
            glControl.SwapBuffers();

And in every polygon I do:

           // Initializes the standard ModelView mode
            GL.MatrixMode(MatrixMode.Modelview);
 
            // Loads the previously drawn matrix
            GL.LoadIdentity();
 
            if (textureID > -1 && textureVertexList != null) // If this polygon has a texture assigned
            {
                GL.Enable(EnableCap.Texture2D); // Enable texturing
                GL.BindTexture(TextureTarget.Texture2D, textureID); // Tell GL what texture to draw with this polygon
                GL.Color3(Color.Transparent);   // Set color transparent for pure texture
 
                GL.Begin(BeginMode.Triangles);
 
                // Draw every vertecies nad map out the texture
                for (int i = 0; i < vertexListTriangulated.Count(); i++)
                {
                    GL.TexCoord2(textureVertexList[i]);
                    GL.Vertex3(vertexListTriangulated[i].X, vertexListTriangulated[i].Y, zIndex);
                }
 
                GL.End();
                GL.Disable(EnableCap.Texture2D);    // Disable texturing
            }
            else // If no texture, draw solid color
            {
                GL.Disable(EnableCap.Texture2D);    // Disable texturing
                GL.Color3(color);   // Assign this polygon it's color
                GL.Begin(BeginMode.Triangles);
 
                // Draw every vertecie
                for (int i = 0; i < vertexListTriangulated.Count(); i++)
                {
                    GL.Vertex3(vertexListTriangulated[i].X, vertexListTriangulated[i].Y, zIndex);
                }
 
                GL.End();
            }

Maybe the solutions is really simple or obvious but like I said. I'm new to OpenGl, mostly I program datastructures...
Like I said, I did do research in this matter but without succes.

I would appreciate any help I get.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dimension314's picture

I think the issue you're running into is that you are are drawing all your polygons in a somewhat random order. For the textures that are transparent, they "blend" with the background at the point they are drawn. So the one you have in the red outline, it is probably getting drawn before the black polygon behind it, and therefore is blending with only the blue background. Try switching the order to draw from back to front and see what happens.

Try out the project I'm working on: http://www.voxelgame.com

Vidde's picture
dimension314 wrote:

I think the issue you're running into is that you are are drawing all your polygons in a somewhat random order. For the textures that are transparent, they "blend" with the background at the point they are drawn. So the one you have in the red outline, it is probably getting drawn before the black polygon behind it, and therefore is blending with only the blue background. Try switching the order to draw from back to front and see what happens.

Try out the project I'm working on: http://www.voxelgame.com

Ah well in that case thank you for the tip. I was away from the computer today and was thinking of the exact same thing, will try it now.
It is logical, but i was hoping i could avoid it.

Speaking of the project you mentioned, this is for a level editor for my xbox/android/psv/pc engine.
Your seems very nice though.

Vidde's picture

Sorry for double post but i found my answer.

If you are drawing polygons at the same z-coordinate and they do not comply with eachothers transparency the trick is to:
Enable alpha test and set and alpha function to greater with a float such as 0.1-0.9

Like this:

            GL.AlphaFunc(AlphaFunction.Greater, 0.5f);
            GL.Enable(EnableCap.AlphaTest);

Thanks for the help, I hope someone also finds it useful.