Jump to content

  • Log In with Google      Sign In   
  • Create Account

mistake with textures coord


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 progman_rus   Members   -  Reputation: 126

Like
0Likes
Like

Posted 04 May 2012 - 09:11 AM

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?

Edited by progman_rus, 04 May 2012 - 09:19 AM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8631

Like
0Likes
Like

Posted 04 May 2012 - 09:32 AM

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:
  • Explicitly specify the slot in the shader source. Define your attributes as attribute (location=n) vec4 vPosition.
  • Force the location of an attribute to a specific slot. Check out the function glBindAttribLocation.
  • 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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS