HashtagMarkus
HashtagMarkus

Reputation: 1661

OGL ES 2.0 Texture on plane

I'm having troube displaying a texture to a plane of two triangles. My texture is a 32x32 RGBA bitmap. I'm developing for android.

My plane is created like this:

    float[] vertices = new float[24];
    // first triangle
    vertices[0] = 0.0f;
    vertices[1] = 0.0f;
    vertices[2] = 0.0f;
    vertices[3] = 1.0f;

    vertices[4] = 0.0f;
    vertices[5] = fHeight;
    vertices[6] = 0.0f;
    vertices[7] = 1.0f;

    vertices[8] = fWidth;
    vertices[9] = fHeight;
    vertices[10] = 0.0f;
    vertices[11] = 1.0f;

    // second triangle
    vertices[12] = fWidth;
    vertices[13] = fHeight;
    vertices[14] = 0.0f;
    vertices[15] = 1.0f;

    vertices[16] = fWidth;
    vertices[17] = 0.0f;
    vertices[18] = 0.0f;
    vertices[19] = 1.0f;

    vertices[20] = 0.0f;
    vertices[21] = 0.0f;
    vertices[22] = 0.0f;
    vertices[23] = 1.0f;

What I managed is to create the plane and display half of the texture. The result looks like this: Result http://www.imagecrate.de/u/1346090743_texture.png

So. the texture is only visible on the first triangle. I read, that it might be a problem when the triangles are rendered in the wrong order, but when I change the order of the second triangle, it doesn't have any effect. Also I thought the texture should be transparent where the black color is visible. Any idea og what I might do wrong?

Here is my fragment shader code:

precision mediump float;
varying vec2 vTexCoord;
uniform sampler2D sTexture; 
void main()
{
    gl_FragColor = texture2D(sTexture, vTexCoord); 
}

And here are my texture coordinates:

    float[] mVertsData1 =
        { 
                0.0f, 0.0f, // TexCoord 0
                0.0f, 1.0f, // TexCoord 1
                1.0f, 1.0f, // TexCoord 2
                1.0f, 0.0f // TexCoord 3
        };

I'm not sure if my rendering code will help here, I can add it if you need it. But I don't want to make this question bigger as needed. Maybe someone knows what happens here!?

Thank you in advance! Zerd1984

Upvotes: 0

Views: 130

Answers (1)

Tim
Tim

Reputation: 35933

You've got 6 vertices and 4 texture coordinates. You need to have the same number of both (6 and 6).

For each vertex, you want to define the texcoord for that vertex at the same index; vertex 4 must match up with texcoord 4, vertex 5 with texcoord 5, etc.

For transparency, you need to enable blending : glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

You should generally always include your rendering code whenever you have a rendering problem as well.

Upvotes: 1

Related Questions