Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opengl Vertex attribute stride

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.
like image 266
Sanctus2099 Avatar asked Nov 27 '10 10:11

Sanctus2099


People also ask

What is stride in OpenGL?

The stride​ is used to decide if there should be bytes between vertices. If it is set to 0, then OpenGL will assume that the vertex data is tightly packed. So OpenGL will compute the stride from the given other components.

What are vertex attributes in OpenGL?

A vertex attribute is an input variable to a shader that is supplied with per-vertex data. In OpenGL core profile, they are specified as in variables in a vertex shader and are backed by a GL_ARRAY_BUFFER . These variable can contain, for example, positions, normals or texture coordinates.

When should I call Enablevertexattribarray?

If you're not using VAOs, then you would usually call glVertexAttribPointer (and the corresponding glEnableVertexAttribArray ) right before rendering to setup the state properly.

What is a VAO?

A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information for a complete rendered object.


1 Answers

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()

like image 118
Kos Avatar answered Oct 02 '22 17:10

Kos