CG_Labs  2021.2
node.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "helpers.hpp"
4 #include "TRSTransform.h"
5 
6 #include <glad/glad.h>
7 #include <GLFW/glfw3.h>
8 #include <glm/glm.hpp>
9 
10 #include <functional>
11 #include <string>
12 #include <tuple>
13 #include <vector>
14 
16 class Node
17 {
18 public:
24  void render(glm::mat4 const& view_projection,
25  glm::mat4 const& parent_transform = glm::mat4(1.0f)) const;
26 
40  void render(glm::mat4 const& view_projection, glm::mat4 const& world,
41  GLuint program,
42  std::function<void (GLuint)> const& set_uniforms = [](GLuint /*programID*/){}) const;
43 
53  void set_geometry(bonobo::mesh_data const& shape);
54 
63  void set_material_constants(bonobo::material_data const& constants);
64 
68  size_t get_indices_nb() const;
69 
73  void set_indices_nb(size_t const& indices_nb);
74 
85  void set_program(GLuint const* const program,
86  std::function<void (GLuint)> const& set_uniforms = [](GLuint /*programID*/){});
87 
96  void set_name(std::string const& name);
97 
106  void add_texture(std::string const& name, GLuint tex_id, GLenum type);
107 
112  void add_child(Node const* child);
113 
117  size_t get_children_nb() const;
118 
124  Node const* get_child(size_t index) const;
125 
130  TRSTransformf const& get_transform() const;
132 
133 private:
134  // Geometry data
135  GLuint _vao{ 0u };
136  GLsizei _vertices_nb{ 0u };
137  GLsizei _indices_nb{ 0u };
138  GLenum _drawing_mode{ GL_TRIANGLES };
139  bool _has_indices{ false };
140 
141  // Program data
142  GLuint const* _program{ nullptr };
143  std::function<void (GLuint)> _set_uniforms;
144 
145  // Material data
146  std::vector<std::tuple<std::string, GLuint, GLenum>> _textures;
148 
149  // Transformation data
151 
152  // Children data
153  std::vector<Node const*> _children;
154 
155  // Debug data
156  std::string _name{"Render un-named node"};
157 };
Represents a node of a scene graph.
Definition: node.hpp:17
void add_texture(std::string const &name, GLuint tex_id, GLenum type)
Add a texture to this node.
Definition: node.cpp:128
void set_program(GLuint const *const program, std::function< void(GLuint)> const &set_uniforms=[](GLuint){})
Set the program of this node.
Definition: node.cpp:98
std::vector< Node const * > _children
Definition: node.hpp:153
Node const * get_child(size_t index) const
Return the ith child.
Definition: node.cpp:168
GLuint const * _program
Definition: node.hpp:142
GLenum _drawing_mode
Definition: node.hpp:138
TRSTransformf _transform
Definition: node.hpp:150
GLsizei _indices_nb
Definition: node.hpp:137
GLuint _vao
Definition: node.hpp:135
bonobo::material_data _constants
Definition: node.hpp:147
void set_name(std::string const &name)
Set the name of this node.
Definition: node.cpp:110
std::string _name
Definition: node.hpp:156
void render(glm::mat4 const &view_projection, glm::mat4 const &parent_transform=glm::mat4(1.0f)) const
Render this node.
Definition: node.cpp:11
GLsizei _vertices_nb
Definition: node.hpp:136
void add_child(Node const *child)
Add a child to this node.
Definition: node.cpp:151
size_t get_indices_nb() const
Get the number of indices to use.
Definition: node.cpp:116
void set_indices_nb(size_t const &indices_nb)
Set the number of indices to use.
Definition: node.cpp:122
TRSTransformf const & get_transform() const
Return this node transformation matrix.
Definition: node.cpp:175
bool _has_indices
Definition: node.hpp:139
size_t get_children_nb() const
Return the number of children to this node.
Definition: node.cpp:162
std::function< void(GLuint)> _set_uniforms
Definition: node.hpp:143
void set_material_constants(bonobo::material_data const &constants)
Set the material constants of this node.
Definition: node.cpp:92
std::vector< std::tuple< std::string, GLuint, GLenum > > _textures
Definition: node.hpp:146
void set_geometry(bonobo::mesh_data const &shape)
Set the geometry of this node.
Definition: node.cpp:74
GLuint world
Definition: helpers.cpp:29
Definition: helpers.hpp:31
Contains the data for a mesh in OpenGL.
Definition: helpers.hpp:42