Learning the proper use of VBOs

So, I tried to teach myself how to use VBOs to improve the performance of my OpenGL project and learn more complex things than rendering a fixed function. But I did not find much suitable textbook; the best of which I have found so far Songho tutorials and material in OpenGL.org , but I do not seem to know any background knowledge to fully understand what is happening, although I can’t say exactly what I don’t get save the use of several parameters.

In any case, I forged ahead and came up with some cannibalized code that, at least, does not break, but leads to unusual results. What I want to visualize is (displayed using a fixed function, it should be brown and background gray, but all of my OpenGL screenshots seem to accept magenta as their favorite color, maybe because I use SFML for the window ?).

Target image

However, I get the following:

Actual image

I'm at a loss. Here is the corresponding code that I use, first for setting up buffer objects (I allocate a lot of memory according to the recommendation of this guy for allocating 4-8 MB)

GLuint WorldBuffer;
GLuint IndexBuffer;

...

glGenBuffers(1, &WorldBuffer);
glBindBuffer(GL_ARRAY_BUFFER, WorldBuffer);
int SizeInBytes = 1024 * 2048;
glBufferData(GL_ARRAY_BUFFER, SizeInBytes, NULL, GL_STATIC_DRAW);

glGenBuffers(1, &IndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBuffer);
SizeInBytes = 1024 * 2048;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, SizeInBytes, NULL, GL_STATIC_DRAW);

. , CreateVertexArray() , 3 3 ( - , ):

std::vector<float>* VertArray = new std::vector<float>;
pWorld->CreateVertexArray(VertArray);
unsigned short Indice = 0;
for (int i = 0; i < VertArray->size(); ++i)
{
    std::cout << (*VertArray)[i] << std::endl;
    glBufferSubData(GL_ARRAY_BUFFER, i * sizeof(float), sizeof(float), &((*VertArray)[i]));
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, i * sizeof(unsigned short), sizeof(unsigned short), &(Indice));
    ++Indice;
}
delete VertArray;
Indice -= 1;

:

    glBindBuffer(GL_ARRAY_BUFFER, WorldBuffer);        
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBuffer);    

    glEnableClientState(GL_VERTEX_ARRAY);         
    glVertexPointer(3, GL_FLOAT, 0, 0);
    glNormalPointer(GL_FLOAT, 0, 0);

    glDrawElements(GL_TRIANGLES, Indice, GL_UNSIGNED_SHORT, 0);

    glDisableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

- , , glVertexPointer() glNormalPointer() ( - , Songho 0 - ?) . 0; . / . BUFFER_OFFSET(0) BUFFER_OFFSET(12), , , BUFFER_OFFSET() undefined.

, glDrawElements() , , Songho 0. &IndexBuffer 0, - , .

- , , -, ? !

+5
3

0; .

( OpenGL) . gl * Pointer Buffer GL_ARRAY_BUFFER, . Buffer GL_ARRAY_BUFFER, (, BO , gl * ).

std::vector<float>* VertArray = new std::vector<float>;

STL new, RAII.

pWorld->CreateVertexArray(VertArray);

, VertexArray, . .

unsigned short Indice = 0;
for (int i = 0; i < VertArray->size(); ++i)
{
    std::cout << (*VertArray)[i] << std::endl;
    glBufferSubData(GL_ARRAY_BUFFER, i * sizeof(float), sizeof(float), &((*VertArray)[i]));

glBufferSubData, .

    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, i * sizeof(unsigned short), sizeof(unsigned short), &(Indice));

GL_ELEMENT_ARRAY_BUFFER, . ? , , glDrawArrays insteaf glDrawElements.

    ++Indice;
}
delete VertArray;

VertArray, .

Indice -= 1;

i?

, ? :

std::vector<float> VertexArray;
pWorld->LoadVertexArray(VertexArray); // World::LoadVertexArray(std::vector<float> &);

glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*VertexArray->size(), &VertexArray[0] );

glDrawArrays; , , → , glDrawElements .

+7

glBufferSubData . VBO. , OpenGL .

http://www.opengl.org/sdk/docs/man/xhtml/glVertexPointer.xml

VBOs VBO. 0 .

stride = 0 , , OpenGL stride .

VBO :

struct Vertex
{
    vec3f position;
    vec3f normal;
};

Vertex[size] data;

...

glBufferData(GL_ARRAY_BUFFER, size*sizeof(Vertex), data, GL_STATIC_DRAW);

...

glVertexPointer(3,GL_FLOAT,sizeof(Vertex),offsetof(Vertex,position));
glNormalPointer(3,GL_FLOAT,sizeof(Vertex),offsetof(Vertex,normal));

. gl*Pointer, , , offsetof.

+4

To find out about the offset of the last parameter, just look at this post .... What is the "offset" parameter in GLES20.glVertexAttribPointer / glDrawElements, and where does ptr / index come from?

0
source

All Articles