angeluriot / Dimension3D

A simple graphics library (2D, 3D and windows).
MIT License
46 stars 10 forks source link

add indexed element #3

Closed Maghin closed 2 years ago

Maghin commented 2 years ago

Allow to draw indexed vertexbuffer.

The element buffer "ebo" is included in the DImension3D VertexBuffer object. It is binded automatically via the ArrayBuffer (as well as the vbo).

An exemple to test the code is available with the mesh "CubeIndexed". It's drawable.

Test code using the squeleton project :

// Create objects
dim::Object object_1(dim::Mesh::Sphere(256, 256), dim::Material(dim::Color(1.f, 0.1f, 0.1f), 0.1f, 0.5f, 0.6f, 30.f));

dim::Object object_2(dim::Mesh::Cone(256), dim::Material(dim::Color(0.1f, 1.f, 0.1f), 0.1f, 0.5f, 0.6f, 30.f));
object_2.move(dim::Vector3(1.1f, 0.f, 0.f));

dim::Object object_3(dim::Mesh::Cylinder(256), dim::Material(dim::Color(0.1f, 0.1f, 1.f), 0.1f, 0.5f, 0.6f, 30.f));
object_3.move(dim::Vector3(-1.1f, 0.f, 0.f));

dim::Object object_4(dim::Mesh::CubeIndexed(), dim::Material(dim::Color(1.f, 0.1f, 1.f), 0.1f, 0.5f, 0.6f, 30.f));
object_4.move(dim::Vector3(-2.2f, 0.f, 0.f));

// ...

// Draw the objects
scene.draw(object_1);
scene.draw(object_2);
scene.draw(object_3);
scene.draw(object_4);

Result : image

Another example displaying a terrain : Heighmap

Maghin commented 2 years ago

Closes #2

Maghin commented 2 years ago

Closes #4

angeluriot commented 2 years ago

Il y a des lignes de code en commentaire, c'est normal ?

Maghin commented 2 years ago

Je me posais la question de retirer les glBindBuffer superflux. Pour le coup je n'ai pas eu de problèmes donc on peux retirer les 3 lignes commentées. Sauf erreur dans ma compréhension c'est le but du VERTEX_ARRAY : se souvenir de ce qui est Bind comme buffer :

1 - Bind VERTEX_ARRAY Bind VERTEX_BUFFER Bind ELEMENT_BUFFER

2 - unBind VERTEX_ARRAY = implique > unBind VERTEX_BUFFER + unBind ELEMENT_BUFFER

3 - Bind VERTEX_ARRAY = implique > Bind VERTEX_BUFFER + Bind ELEMENT_BUFFER

Par conséquence si :

4 - Bind VERTEX_ARRAY unBind VERTEX_BUFFER unBind ELEMENT_BUFFER unBind VERTEX_ARRAY

5 - Bind VERTEX_ARRAY => là on a plus nos buffer, il faut les bind à nouveau.

Donc pour ne pas avoir à les bind à chaque fois, il suffit de ne pas le Unbind avant le VERTEX_ARRAY. Par contre pour la destruction il faut bien passer les glDeleteBuffers, pas de changement de ce coté là.