سؤال

In the past I used to bind textures to QUADS and then render them with it. If I had an animation in which I had to bind to the quad a specific section of the image, then I just had to use the universal coordinates [0..1] and thats all.

The problem is now I'm trying again with openGL, I have seen GL_QUADS are being deprecated and now all I see is being rendered with TRIANGLES. My issue is I don't know how to bind the textures using triangles.

Thanks

هل كانت مفيدة؟

المحلول

Here is an example how you can bind textures into GL_TRIANGLE_STRIP

float verts[] = {
    0, 0,
    w, 0,
    0, h,
    w, h,
};

const float texcoord[] = {
    u1 + 0 , v1 + v2,
    u1 + u2, v1 + v2,
    u1 + 0 , 0  + v2,
    u1 + u2, 0  + v2
};

/*
 1. uv(0,1)    +-----+   2. uv(1,1)
               |    /|
               |   / |
               |  /  |
               | /   |
               |/    |
 3. uv(0,0)    +-----+   4. uv(1,0)
*/

And you can use it as simple as

glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glTexCoordPointer(2, GL_FLOAT, 0, tex_coord);
glVertexPointer(2, GL_FLOAT, 0, verts);

glBindTexture(GL_TEXTURE_2D, texture_id);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisable(GL_TEXTURE_2D);

نصائح أخرى

Instead of a single quad, split the quad into 2 triangles, similar to cutting the quad across the diagonal. Lot of examples exist, a recent discussion in stackoverflow is below:

Index Buffer Object and UV Coordinates don't play nice

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top