Texturing using gles 2

Apprentice
Posts: 9
Joined: 2010.04
Post: #1
Hello again

Here is some background on my problem:
I have this shader code

vert
Code:
attribute highp  vec2  inVertex;

uniform   mediump mat4  myMVPMatrix;
varying mediump vec2  TexCoord;

void main()
{
    gl_Position = myMVPMatrix * myVertex;

    TexCoord = inVertex;

}

frag
Code:
uniform sampler2D            texture2d;
varying mediump vec2        TexCoord;

void main()
{
    gl_FragColor = texture2D(texture2d, TexCoord);
}


Pretty basic stuff im loading a triangle strip array using basic init code

Code:
    GLfloat vertices[] = { 0, 0,  1, 0,  0, 1,  1, 1 };

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    
    glBufferData(GL_ARRAY_BUFFER, sizeof(basicGeometry), vertices,GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

....

Draw Function
        ... enable shader, setup uniforms etc...

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
           glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

EVerything works, but now what I want to do is to have specific textcoords for my triangle but I am really stumped!

What I did so far was to modify the vertex shader to have a separate attribute now;

Code:
attribute highp  vec2  inVertex;
attribute mediump  vec2  inTexcoords;

uniform   mediump mat4  myMVPMatrix;
varying mediump vec2  TexCoord;

void main()
{
    gl_Position = myMVPMatrix * myVertex;

    TexCoord = inTexcoords;

}

I then activated the attrib for inTexCoord using glbindattrib

The only other thing was to actually modify the rendering code; so as an experiment i did the following

Code:
static GLfloat texcoords[] = { 0.5, 0.5,  1, 0.5,  0.5, 1,  1, 1 };

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
           glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
        glBindAttribLocation(m_pShaderProgram, 1,"inTexcoords");
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        glDisableVertexAttribArray(1);
        glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

Nothing happens, what am i missing??

the thing i do not understand is that if i instead call
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

it works but using the original vertex coordinates! How can i tell opengl to use the other texcoords? Am i forgetting to load a a pointer into gl memory?? glTexCoordPointer is nowhere to be found on gl es 2 Sad

Please help!

Thanks In Advance!
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2010.03
Post: #2
I'm just guessing here but I've never mixed using a vbo for the vertices and a client side array for the UV's. If you interleave the UV's into the vbo and set the appropriate stride does that work?

This is just a guess btw. I'm assuming you've got texturing enabled and you've bound the current texture since you are getting something which you 'fiddle' the uv's as you mention.
Quote this message in a reply
Sage
Posts: 1,234
Joined: 2002.10
Post: #3
jeckil Wrote:
Code:
static GLfloat texcoords[] = { 0.5, 0.5,  1, 0.5,  0.5, 1,  1, 1 };

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
           glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoords);

Nothing happens, what am i missing??

Read the documentation for glVertexAttribPointer:
Quote:If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target (see glBindBuffer) while a generic vertex attribute array is specified, pointer is treated as a byte offset into the buffer object's data store.

You've just told GL to source texture coordinates from (address of vbo) + (address of texcoords). You're lucky that doesn't immediately crash.

Don't mix client arrays and vbos. Create two vbos, or interleave your data in one vbo. In either case, use the correct offsets.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.04
Post: #4
Quote:Don't mix client arrays and vbos. Create two vbos, or interleave your data in one vbo. In either case, use the correct offsets.

if I make 2 vbo how would i move texture coordinates to different billboards? What I want to achieve is to have a lot of textures inside the same texture file (for things such as sprites and different 3d gui elements)
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.04
Post: #5
Awesome, finally got my gles book today so I got it working

I had to use just pointers no vbos, for some strange reason i thought es2 was only vbos.. silly me ^_^

The glVertexAttribPointer was not the actual problem, it was me mixing vbos and pointers; it was not crashing because it was actually working (yet not binding to the server store):

the code
Code:
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoords);

is correct (because its using offset 0 if the texcoord object not the data store) its just that i was not binding another vbo for texture coordinates; but i am not looking for static data; i needed dynamic data for texture coordinates so im not using vbos so anyways.

My fixed experiment code:
Code:
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

static    GLfloat vertexcoords[] = { 0, 0,  1, 0,  0, 1,  1, 1 };

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertexcoords);

static    GLfloat texcoords[] = { 0, .5,  1, 0,  0, 1,  1, 1 };

    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoords);

    
    // Draws a short triangle strip
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

(I previously bound the shader attributes in the init function)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone GLES 2 Orthogonal frame jeckil 2 3,003 Apr 16, 2010 08:33 AM
Last Post: dazza
  Setting A Non-Rectangular Clip Area In GLES muleskinner 5 3,965 Oct 20, 2009 01:03 AM
Last Post: muleskinner