Jump to content
  • Advertisement
Sign in to follow this  

mistake with textures coord

This topic is 2391 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
struct Vertex
GLfloat x, y;
GLfloat r, g, b, a;
GLfloat tu, tv;

#define VERTEX_ARRAY 0

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

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:

  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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!