Using VBO - no geometry

I am trying to configure VBO for my rendering code to get more fps. It worked for a separate VBO for vertex position, color and texture coordinates, but after transferring vertex data to alternating data, there is no geometry. Here is my func setup:

const GLsizeiptr data_size = NUMBER_OF_CUBE_VERTICES * 9 *sizeof(float); // allocate a new buffer glGenBuffers(1, &cubeVBO); glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW); float* ptr = (float*)data; glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 0)); glEnableVertexAttribArray(ATTRIB_VERTEX); glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 3)); glEnableVertexAttribArray(ATTRIB_COLOR); glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 7)); glEnableVertexAttribArray(ATTRIB_TEXCOORD0); glGenBuffers(1, &cubeIBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUMBER_OF_CUBE_INDICES*sizeof(GLubyte), s_cubeIndices, GL_STATIC_DRAW); 

The data array is as follows:

 static float data[] = { // position // // color // //UV// -1.0, +1.0, +1.0, 255,0,0,255, 0,0, -1.0, -1.0, +1.0, 0,255,0,255, 0,0, +1.0, +1.0, +1.0, 255,0,255,255, 0,0, +1.0, -1.0, +1.0, 255,0,0,255, 0,0, +1.0, +1.0, +1.0, 255,0,0,255, 0,0, +1.0, -1.0, +1.0, 255,0,0,255, 0,0, +1.0, +1.0, -1.0, 255,255,0,255, 0,0, +1.0, -1.0, -1.0, 255,0,0,255, 0,0, +1.0, +1.0, -1.0, 255,0,255,255, 0,0, +1.0, -1.0, -1.0, 255,255,0,255, 0,0, -1.0, +1.0, -1.0, 0,255,0,255, 0,0, -1.0, -1.0, -1.0, 255,0,0,255, 0,0, -1.0, +1.0, -1.0, 0,0,255,255, 0,0, -1.0, -1.0, -1.0, 255,0,0,255, 0,0, -1.0, +1.0, +1.0, 255,255,0,255, 0,0, -1.0, -1.0, +1.0, 255,0,0,255, 0,0, }; 

And this is my rendering code:

 glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO); glDrawElements(GL_TRIANGLE_STRIP, NUMBER_OF_CUBE_INDICES, GL_UNSIGNED_BYTE, s_cubeIndices); 

I also tried using the DrawArrays function without an index buffer, but the result was the same - no geometric rendering. My window bar also has a GLError 1282, while my program is running. I would appreciate any help on my issue, thanks.

+4
source share
2 answers

When you use a buffer object, the last parameter glVertexAttribPointer should be offset to the required data. For example, your ATTRIB_VERTEX array will start with an offset value of 0, an ATTRIB_COLOR array with an offset of sizeof (float) * 3 (since the position occupies three floats), etc.

If you are not using buffer objects, but rather vertex arrays, you need to untie the currently bound buffer object to the GL_ARRAY_BUFFER target by calling

 glBindBuffer(GL_ARRAY_BUFFER, 0); 
+4
source

Ok, I worked using the functions glVertexPointer and glEnableClientState. But for glVertexAttribPointer and glEnableVertexAttribArray, geometry is still missing. Now the code is as follows: VBO init:

 struct Vertex { GLfloat x, y, z; GLubyte r, g, b, a; }; ...... const GLsizeiptr data_size = NUMBER_OF_CUBE_VERTICES *sizeof(struct Vertex); glGenBuffers(1, &cubeVBO); glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); glBufferData(GL_ARRAY_BUFFER, data_size, vertices, GL_STATIC_DRAW); glVertexPointer(3, GL_FLOAT, sizeof(struct Vertex), (GLvoid*)((char*)NULL)); glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(struct Vertex), (GLvoid*)offsetof(struct Vertex, r)); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); 

rendering:

 glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); glDrawArrays(GL_TRIANGLE_STRIP, 0, NUMBER_OF_CUBE_VERTICES); 

I have no idea why this code does not work if I switch to glVertexAttribPointer / glEnableVertexAttribArray. Any ideas? Maybe I need to move the pointer and enable functions from the initialization part for rendering?

0
source

Source: https://habr.com/ru/post/1437391/


All Articles