yorkdu
yorkdu

Reputation: 103

opengl draw texture with triangle strip upside down

I am doing some opengl in cocos2d draw() method, as I need some 3d effects. To test, I draw a texture by a triangle strip. The problem is that the result picture is just upside-down. Code is quite simple, I cannot figure out why it is upside down:

ccVertex3F newPoint[4] = {{-20,0, -100}, 
    {20,0, -100},
    {-20,40, -100},
    {20,40, -100}
};

ccVertex2F _textCoordArray[4] = {{0,0}, {1,0}, {0,1}, {1,1}};

glDisableClientState(GL_COLOR_ARRAY);

glBindTexture(GL_TEXTURE_2D, [lineTexture name]);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glLoadIdentity();

glVertexPointer(3, GL_FLOAT, 0, newPoint);
glTexCoordPointer(2, GL_FLOAT, 0, _textCoordArray);
glEnableClientState(GL_VERTEX_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glPopMatrix();

Upvotes: 2

Views: 2000

Answers (1)

Tim
Tim

Reputation: 35933

Some libraries just have different ideas of which point on a texture is (0,0) (top left or bottom left corner). I'm guessing that whatever image loading library you've used considers (0,0) to be the top left, while opengl considers it to be the bottom left.

To correct it you can either tell cocoa to load it upside down (don't know how or if its even possible), or flip your UV's veritcal orientation:

from:

_textCoordArray[4] = {{0,0}, {1,0}, {0,1}, {1,1}};

to:

_textCoordArray[4] = {{0,1}, {1,1}, {0,0}, {1,0}};

Upvotes: 1

Related Questions