Sign in to follow this  

mistake with textures coord

This topic is 2049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the vertex structure
[CODE]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

[/CODE]

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

[CODE]
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;
}
[/CODE]
[CODE]
precision mediump float;
uniform sampler2D sampler2d;
varying mediump vec4 vVertexColor;
varying mediump vec2 myTexCoord;
void main()
{
gl_FragColor = texture2D( sampler2d, myTexCoord) * vVertexColor ;
}
[/CODE]


this is render function for draw all primitives

[CODE]
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);
[/CODE]

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

Share this post


Link to post
Share on other sites
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:[list=1]
[*]Explicitly specify the slot in the shader source. Define your attributes as [i]attribute (location=n) vec4 vPosition[/i].
[*]Force the location of an attribute to a specific slot. Check out the function [i]glBindAttribLocation[/i].
[*]Query the shader for the location, and bind attributes to that location. Check out the function [i]glGetAttribLocation[/i].
[/list]
I am not sure about the syntax in point 1 though. The [i]n[/i] 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.

Share this post


Link to post
Share on other sites

This topic is 2049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this