Vertex buffers in opengl

I am making a small 3D graphics / demo for personal training. I know d3d9 and a little about d3d11, but a little about opengl at the moment, so I intend to ignore the actual rendering of the graphics so that my scene graph and everything β€œabove”, he needs to learn a little about how to actually draw graphics. I intend to get it working with d3d9, then add d3d11 support and finally opengl support. Just like a training exercise to learn about 3D graphics and abstraction.

I am not very good at opengl at this point, but I don’t want my abstract interface to display anything that is not easy to implement in opengl. In particular, I look at vertex buffers. In d3d, they are essentially an array of structures, but looking at the opengl interface, the equivalent seems to be a vertex array. However, they seem organized differently, where you need a separate array for the vertices, one for normals, one for texture coordinates, etc. And installation using glVertexPointer, glTexCoordPointer, etc.

I was hoping to implement the VertexBuffer interface, like Directx, but it looks like in d3d you have an array of structures, and in opengl you need a separate array for each element, which makes finding a common abstraction quite difficult to make efficient.

Is there a way to use opengl similarly to directx? Or any suggestions on how to come up with a higher level abstraction that will work effectively with both systems?

+4
source share
1 answer

Vertex arrays have step and offset attributes. This is specifically for structure arrays.

So, let's say you want to configure VBO with vertex float3 and texture coordinate float2, you would do the following:

// on creation of the buffer typedef struct { GLfloat vert[3]; GLfloat texcoord[2]; } PackedVertex; glBindBuffer(GL_ARRAY_BUFFER, vboname); glBufferData(...); // fill vboname with array of PackedVertex data // on using the buffer glBindBuffer(GL_ARRAY_BUFFER, vboname); glVertexPointer(3, GL_FLOAT, sizeof(PackedVertex), BUFFER_OFFSET(0))); glTexCoordPointer(2, GL_FLOAT, sizeof(PackedVertex), BUFFER_OFFSET(offsetof(PackedVertex, texcoord)); 

With a BUFFER_OFFSET macro to rotate the offsets to the corresponding pointers (vbos uses the pointer parameter as an offset) and the offset of another macro to find the texcoord offset inside the PackedVertex structure. Here, probably, sizeof (float) * 3, since there is unlikely to be any padding inside the structure.

+3
source

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


All Articles