Default Scene

/* Loading a wavefront mesh */
int32_t status = Scene::LoadWavefront(&m_scene,HAIKU_MESHES_DIR+"lowpoly_bunny.obj", OBJ_LOAD_POSITION | OBJ_LOAD_NORMALS );
if( (status & MESH_HAS_NONE) != 0 )
{
    printf("An error occured while loading the mesh.\n");
}

status = Scene::LoadWavefront(&m_scene,HAIKU_MESHES_DIR+"square.obj", OBJ_LOAD_POSITION | OBJ_LOAD_NORMALS );
if( (status & MESH_HAS_NONE) != 0 )
{
    printf("An error occured while loading the mesh.\n");
}

mat4  bunny_transform(1.f);
Mat4f plane_transform = translate4x4(Vec3f(0.f,-1.f,0.f)) * scale4x4(2.f,2.f,2.f);

Scene::Push(&m_scene,0,&m_bunny_transform.at[0]);
Scene::Push(&m_scene,1,&plane_transform.at[0]);
Scene::Prepare(&m_scene, BufferDesc::Storage(GL_SHADER_STORAGE_BUFFER,GL_DYNAMIC_STORAGE_BIT), /* binding unit = */ 0u);
/* Loading a wavefront mesh */
int32_t status = Scene::LoadWavefront(&m_scene,HAIKU_MESHES_DIR+"lowpoly_bunny.obj", OBJ_LOAD_POSITION | OBJ_LOAD_NORMALS );
if( (status & MESH_HAS_NONE) != 0 )
{
    printf("An error occured while loading the mesh.\n");
}

status = Scene::LoadWavefront(&m_scene,HAIKU_MESHES_DIR+"square.obj", OBJ_LOAD_POSITION | OBJ_LOAD_NORMALS );
if( (status & MESH_HAS_NONE) != 0 )
{
    printf("An error occured while loading the mesh.\n");
}

mat4  bunny_transform(1.f);
Mat4f plane_transform = translate4x4(Vec3f(0.f,-1.f,0.f)) * scale4x4(2.f,2.f,2.f);

Scene::Push(&m_scene,0,&m_bunny_transform.at[0]);
Scene::Push(&m_scene,1,&plane_transform.at[0]);
Scene::Prepare(&m_scene, BufferDesc::Data(GL_UNIFORM_BUFFER,GL_STATIC_DRAW), /* binding unit = */ 0u);

To render meshes :

Pipeline::Activate(m_prog);
ShaderStage::SetUniformMat4f(m_vert,1,Camera::View(&m_camera));
ShaderStage::SetUniformMat4f(m_vert,2,Camera::Proj(&m_camera));
Scene::Render(m_scene);
Pipeline::Activate(m_prog);
ShaderStage::SetUniformMat4f(m_vert, Pipeline::UniformLocation(m_prog,"uView"), Camera::View(&m_camera));
ShaderStage::SetUniformMat4f(m_vert, Pipeline::UniformLocation(m_prog,"uProj"), Camera::Proj(&m_camera));
Scene::Render(m_scene,&m_vert, Pipeline::UniformLocation(m_prog,"DrawID"));

On GPU side :

out gl_PerVertex { vec4 gl_Position; };
/* Attributes */
layout(location = 0) in vec4 pos_xyz_u; /**< | P.X | P.Y | P.Z | U |  */
layout(location = 1) in vec4 nor_xyz_v; /**< | N.X | N.Y | N.Z | V |  */

/* Transformation matrices */
layout(std430, binding = 0) buffer matrices
{
    mat4 model_matrices[];
};

/* Uniforms */
layout(location = 1) uniform mat4 uView;
layout(location = 2) uniform mat4 uProj;

void main()
{
    mat4 uModel = model_matrices[gl_DrawID];
    /* ... */
}
/* Attributes */
layout(location = 0) in vec4 pos_xyz_u;
layout(location = 1) in vec4 nor_xyz_v;


/* Transformation matrices */
#define MAX_MATRICES 100
layout(std140) uniform Matrices
{
    mat4 model_matrices[MAX_MATRICES];
};

/* Uniforms */
uniform int DrawID;
uniform mat4 uView;
uniform mat4 uProj;

void main()
{
    mat4 uModel = model_matrices[DrawID];
    /* ... */
}

Structures Description

struct SceneVertex

Scene vertex format (see data layout of each attribute)

Public Members

Maths::Vec4f pos_xyz_u

|P.X | P.Y | P.Z | U |

Maths::Vec4f nor_xyz_v

|N.X | N.Y | N.Z | V |

Maths::Vec4f tng_xyz__

|T.X | T.Y | T.Z | 0 |

Maths::Vec4f btg_xyz__

|B.X | B.Y | B.Z | 0 |

struct SceneMesh

Scene mesh data structure.

Public Members

Maths::AABB bounding_box

Mesh bounding box

size_t id

Mesh ID in the scene queue.

uint32_t base_prim

Mesh base primitive index.

uint32_t base_index

Mesh base index.

uint32_t base_vertex

Mesh base vertex.

uint32_t vertex_count

Mesh vertex count.

uint32_t instance_count

Mesh instance count.

struct SceneDrawCommand

OpenGL DrawIndirectCommand data structure.

Public Members

uint32_t vertexCount

Specifies the number of indices to be rendered.

uint32_t instanceCount

Specifies the number of instances of the specified range of indices to be rendered.

uint32_t firstIndex

Specifies the starting index in the enabled arrays.

uint32_t baseVertex

Specifies a constant that should be added to each element of indices when chosing elements from the enabled vertex arrays.

uint32_t baseInstance

Specifies the base instance for use in fetching instanced vertex attributes.

struct DefaultScene

Scene data structure.

Public Members

std::vector<SceneDrawCommand> commands

The scene command buffer

std::vector<SceneVertex> vertices

The scene vertex buffer

std::vector<SceneMesh> meshes

The scene loaded mesh

std::vector<uint32_t> indices

The scene index buffer

std::vector<Maths::Mat4f> transforms

The scene transformation matrix buffer

GPUBuffer buff_matrices

A SSBO for the transformation matrices

uint32_t buff_matrices_id

The bindind point of the the transformation matrices SSBO

uint32_t vao

The scene OpenGL vertex array object ID

uint32_t vbo

The scene OpenGL vertex buffer object ID

uint32_t ibo

The scene OpenGL index buffer object ID

uint32_t cmd

The scene OpenGL command buffer ID

bool ready

Checks if the scene was correctly initialized.

Functions Description

namespace Scene

Functions

int32_t LoadWavefront(DefaultScene *s, const std::string &filepath, int32_t flags)

Loads a Mesh from a wavefront file format.

TODO: Material Support ?

Parameters:
  • s – The scene pointer.

  • filepath – The filepath of the wavefront mesh (.obj).

  • flags – Bitwise flags to request specific informations (ObjLoaderOption)

    • OBJ_LOAD_POSITION : try loading vertex positions (default)

    • OBJ_LOAD_NORMALS : try loading vertex normals

    • OBJ_LOAD_TEXCOORDS : try loading vertex texcoords

    • OBJ_COMPUTE_NORMALS : computing normals from vertex positions

    • OBJ_COMPUTE_TANGENT : computing tangents and bitangent from vertex position and texcoords

Returns:

MeshDesc Returns another bitwise flag telling (MeshDesc)

  • MESH_HAS_NONE : No information has been returned.

  • MESH_HAS_POSITION : Returned vertices with positions.

  • MESH_HAS_NORMALS : Returned vertices with normals.

  • MESH_HAS_TEXCOORDS : Returned vertices with texcoords.

  • MESH_HAS_TANGENTS : Returned vertices with tangents.

  • MESH_HAS_BITANGENTS : Returned vertices with bitangents.

void Push(DefaultScene *s, size_t ID, float *object_transform)

Pushes a mesh in the scene draw list with a custom id.

void PushInstanced(DefaultScene *s, size_t ID, uint32_t instance_count)

Pushes an instanced mesh in the scene draw list with a custom id.

void Prepare(DefaultScene *s, const BufferDesc &transform_buffer_desc, uint32_t transform_buffer_id)

Prepares the scene draw list and buffers. Sets the transform matrices buffer object id

void Cleanup(DefaultScene *s)

Cleans up the scene data structure.

void Render(const DefaultScene &s)

Renders the scene using OpenGL 4.6 functions and allows access to gl_DrawId in vertex shader.

void Render(const DefaultScene &s, ShaderProgram *shader, uint32_t uniform_draw_id)

Renders the scene using OpenGL 4.5/3.3 functions without gl_DrawId in vertex shader.

void UpdateMatrix(DefaultScene *s, uint32_t id, float *model)

Updating the transform matrix of a specific model.