mistake with textures coord

Started by
0 comments, last by Brother Bob 11 years, 11 months ago
I have the vertex structure
struct Vertex
{
GLfloat x, y;
GLfloat r, g, b, a;
GLfloat tu, tv;
};

#define VERTEX_ARRAY 0
#define VERTEXCOLOR_ARRAY 1
#define TEXCOORD_ARRAY 2



I use it for draw 2D primitives.
test platform - Samsung Galaxy Tab

today I lunch app on the Sumsung Galaxy S II
I am in shock - there x/y is texture coord and tu/tv is world coordinats. They are reversed!!!

this is my shader


attribute vec4 vPosition;
attribute mediump vec4 vColor;
attribute mediump vec2 uv;
varying mediump vec4 vVertexColor;
varying mediump vec2 myTexCoord;
void main()
{
gl_Position = vPosition;
vVertexColor = vColor;
myTexCoord = uv;
}


precision mediump float;
uniform sampler2D sampler2d;
varying mediump vec4 vVertexColor;
varying mediump vec2 myTexCoord;
void main()
{
gl_FragColor = texture2D( sampler2d, myTexCoord) * vVertexColor ;
}



this is render function for draw all primitives


glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer );// Bind the VBO.
glBufferSubData( GL_ARRAY_BUFFER, 0, m_CurrentVertexIndex * g_ui32VertexStride, vertices );

glEnableVertexAttribArray( VERTEX_ARRAY );
glVertexAttribPointer( VERTEX_ARRAY, 2, GL_FLOAT, GL_FALSE, g_ui32VertexStride, 0 );

glEnableVertexAttribArray( VERTEXCOLOR_ARRAY );
glVertexAttribPointer( VERTEXCOLOR_ARRAY, 4, GL_FLOAT, GL_FALSE, g_ui32VertexStride, (void*) (2 * sizeof(GLfloat) ) );

glEnableVertexAttribArray( TEXCOORD_ARRAY );
glVertexAttribPointer( TEXCOORD_ARRAY, 2, GL_FLOAT, GL_FALSE, g_ui32VertexStride, (void*) (6 * sizeof(GLfloat) ) );

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer );
glDrawElements( GL_TRIANGLES, 3 * m_RenderTriangleCount, GL_UNSIGNED_SHORT, (void*)0);


How to fix it? How to setup for GL that first is world coord, next is color and next is texture coord?
Advertisement
Your code is hard coding that the position attribute array is located at slot 0, the color attribute array is at slot 1, and the texture texture coordinate attribute array is at slot 2. You must ensure that you actually bind the attributes to the correct slots. You have three basic options:

  1. Explicitly specify the slot in the shader source. Define your attributes as attribute (location=n) vec4 vPosition.
  2. Force the location of an attribute to a specific slot. Check out the function glBindAttribLocation.
  3. Query the shader for the location, and bind attributes to that location. Check out the function glGetAttribLocation.

I am not sure about the syntax in point 1 though. The n is the slot you want to force the attribute to.

What is possibly happening in your code is that you assume that position is at slot 0, color is at slot 1 and texture coordinate is at slot 2, but after compiling the shader the slots may in fact be in the opposite order. If you then bind the position attribute to slot 0 without verifying that position is, in fact, at slot 0, then the position attribute may be bound to the wrong attribute in the shader.

This topic is closed to new replies.

Advertisement