7
votes

Hey. I new to OpenGL ES but I've had my share of experience with normal OpenGL. I've been told that using interlaced arrays for the vertex buffers is a lot faster due to the optimisation for avoiding cache misses.
I've developed a vertex format that I will use that looks like this

struct SVertex
{
    float x,y,z;
    float nx,ny,nz;
    float tx,ty,tz;
    float bx,by,bz;
    float tu1,tv1;
    float tu2,tv2;
};

Then I used "glVertexAttribPointer(index,3,GL_FLOAT,GL_FALSE,stride,v);" to point to the vertex array. The index is the one of the attribute I want to use and everything else is ok except the stride. It worked before I decided to add this into the equation. I passed the stride both as sizeof(SVertex) and like 13*4 but none of them seem to work.
If it has any importance I draw the primitives like this

glDrawElements(GL_TRIANGLES,surface->GetIndexCount()/3,GL_UNSIGNED_INT,surface->IndPtr());
In the OpenGL specs it's written that the stride should be the size in bytes from the end of the attribute( in this case z) to the next attribute of the same kind(in this case x). So by my calculations this should be 13(nx,ny,nz,tx,ty....tuv2,tv2) times 4 (the size of a float).
Oh and one more thing is that the display is just empty.
Could anyone please help me with this?
Thanks a lot.
2
I said 13 values thinking I have to substract the first 3(if you have just x,y,z then stride is 0);Sanctus2099

2 Answers

6
votes

If you have a structure like this, then stride is just sizeof SVertex and it's the same for every attribute. There's nothing complicated here.

If this didn't work, look for your error somewhere else.

For example here:

surface->GetIndexCount()/3

This parameter should be the number of vertices, not primitives to be sent - hence I'd say that this division by three is wrong. Leave it as:

surface->GetIndexCount()

2
votes

Then I used "glVertexAttribPointer(index,3,GL_FLOAT,GL_FALSE,stride,v);" to point to the vertex array. The index is the one of the attribute I want to use and everything else is ok except the stride

This does not work for texcoord (you have 2x 2 floats or 1x 4 floats).

About the stride, like Kos said, I think you should pass a stride of 16 * sizeof(float) (the size of your SVertex).

Also another thing worth mentioning. You say you want to optimize for performance. Why dont you compress your vertex to the max, and suppress redundant values? This would save a lot of bandwidth.

x, y, z are OK, but nx and ny are self sufficient if your normals are normalized (which may be the case). You can extract in the vertex shader nz (assuming you have shader capabilities). The same thing applies for tx and ty. You don't need bx, by, bz at all since you know it's the cross product of normal and tangent.

struct SPackedVertex
{
    float x,y,z,w;       //pack all on vector4
    float nx,ny,tx,ty;
    float tu1,tv1;tu2,tv2;
};