UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
Model Class Reference

#include <opengl_utils.hpp>

Collaboration diagram for Model:
Collaboration graph

Public Member Functions

 Model ()=default
 
 Model (const Model &)=delete
 
 Model (Model &&o)
 
 ~Model ()
 
GLuint get_vao () const
 
Model & operator= (const Model &)=delete
 
Model & operator= (Model &&o)
 
void render ()
 

Static Public Member Functions

static Model get_quad ()
 
static Model get_sphere ()
 

Private Member Functions

void swap (Model &o)
 

Private Attributes

GLuint elem_buf = 0
 
GLsizei indices_num = 0
 
GLuint vao = 0
 
GLuint vbo = 0
 

Detailed Description

RAII wrapper for OpenGL VBO and VAO containing a 3D model

Definition at line 102 of file opengl_utils.hpp.

Constructor & Destructor Documentation

◆ Model() [1/3]

Model::Model ( )
default

◆ Model() [2/3]

Model::Model ( const Model & )
delete

◆ Model() [3/3]

Model::Model ( Model && o)
inline

Definition at line 106 of file opengl_utils.hpp.

106{ swap(o); }
void swap(Model &o)

References swap().

Here is the call graph for this function:

◆ ~Model()

Model::~Model ( )

Definition at line 133 of file opengl_utils.cpp.

133 {
134 if(vao == 0)
135 return;
136
137 glDeleteBuffers(1, &vbo);
138 glDeleteBuffers(1, &elem_buf);
139 glDeleteVertexArrays(1, &vao);
140}
GLuint vao
GLuint vbo
GLuint elem_buf

References elem_buf, vao, and vbo.

Member Function Documentation

◆ get_quad()

Model Model::get_quad ( )
static

Returns an instance containing a plane formed by 2 triangles

Definition at line 198 of file opengl_utils.cpp.

198 {
199 Model model;
200 glGenVertexArrays(1, &model.vao);
201 glBindVertexArray(model.vao);
202
203 glGenBuffers(1, &model.vbo);
204 glBindBuffer(GL_ARRAY_BUFFER, model.vbo);
205 glBufferData(GL_ARRAY_BUFFER, sizeof(rectangle), rectangle, GL_STATIC_DRAW);
206
207 glEnableVertexAttribArray(0);
208 glBindBuffer(GL_ARRAY_BUFFER, model.vbo);
209 glVertexAttribPointer(
210 0,
211 2,
212 GL_FLOAT,
213 GL_FALSE,
214 4 * sizeof(float),
215 (void*)0
216 );
217 glEnableVertexAttribArray(1);
218 glBindBuffer(GL_ARRAY_BUFFER, model.vbo);
219 glVertexAttribPointer(
220 1,
221 2,
222 GL_FLOAT,
223 GL_FALSE,
224 4 * sizeof(float),
225 (void*)(2 * sizeof(float))
226 );
227
228 glBindVertexArray(0);
229 model.indices_num = 6;
230
231 return model;
232}
GLsizei indices_num
static const GLfloat rectangle[]

References indices_num, rectangle, vao, and vbo.

Referenced by FlatVideoScene::init().

◆ get_sphere()

Model Model::get_sphere ( )
static

Returns an instance containing a 3D sphere

Definition at line 155 of file opengl_utils.cpp.

155 {
156 Model model;
157 glGenVertexArrays(1, &model.vao);
158 glBindVertexArray(model.vao);
159
160 auto vertices = gen_sphere_vertices(1, 64, 64);
161 auto indices = gen_sphere_indices(64, 64);
162
163 glGenBuffers(1, &model.vbo);
164 glBindBuffer(GL_ARRAY_BUFFER, model.vbo);
165 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
166
167 glGenBuffers(1, &model.elem_buf);
168 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.elem_buf);
169 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned), indices.data(), GL_STATIC_DRAW);
170
171 glEnableVertexAttribArray(0);
172 glBindBuffer(GL_ARRAY_BUFFER, model.vbo);
173 glVertexAttribPointer(
174 0,
175 3,
176 GL_FLOAT,
177 GL_FALSE,
178 5 * sizeof(float),
179 (void*)0
180 );
181 glEnableVertexAttribArray(1);
182 glBindBuffer(GL_ARRAY_BUFFER, model.vbo);
183 glVertexAttribPointer(
184 1,
185 2,
186 GL_FLOAT,
187 GL_FALSE,
188 5 * sizeof(float),
189 (void*)(3 * sizeof(float))
190 );
191
192 glBindVertexArray(0);
193 model.indices_num = indices.size();
194
195 return model;
196}
static std::vector< unsigned > gen_sphere_indices(int latitude_n, int longtitude_n)
static std::vector< float > gen_sphere_vertices(int r, int latitude_n, int longtitude_n)

References elem_buf, gen_sphere_indices(), gen_sphere_vertices(), indices_num, vao, and vbo.

Here is the call graph for this function:

◆ get_vao()

GLuint Model::get_vao ( ) const
inline

Return the underlying OpenGL VAO id

Definition at line 114 of file opengl_utils.hpp.

114{ return vao; }

References vao.

◆ operator=() [1/2]

Model & Model::operator= ( const Model & )
delete

◆ operator=() [2/2]

Model & Model::operator= ( Model && o)
inline

Definition at line 108 of file opengl_utils.hpp.

108{ swap(o); return *this; }

References swap().

Here is the call graph for this function:

◆ render()

void Model::render ( )

Renders the contained model by calling glDrawElements or glDrawArrays

Definition at line 142 of file opengl_utils.cpp.

142 {
144
145 glBindVertexArray(vao);
146 if(elem_buf != 0){
147 glDrawElements(GL_TRIANGLES, indices_num, GL_UNSIGNED_INT, (void *) 0);
148 } else {
149 glDrawArrays(GL_TRIANGLES, 0, indices_num);
150 }
151
152 glBindVertexArray(0);
153}
#define PROFILE_FUNC

References elem_buf, indices_num, PROFILE_FUNC, and vao.

Referenced by FlatVideoScene::render(), and PanoramaScene::render().

◆ swap()

void Model::swap ( Model & o)
inlineprivate

Definition at line 132 of file opengl_utils.hpp.

132 {
133 std::swap(vao, o.vao);
134 std::swap(vbo, o.vbo);
135 std::swap(elem_buf, o.elem_buf);
136 std::swap(indices_num, o.indices_num);
137 }

References elem_buf, indices_num, vao, and vbo.

Referenced by Model(), and operator=().

Member Data Documentation

◆ elem_buf

GLuint Model::elem_buf = 0
private

Definition at line 140 of file opengl_utils.hpp.

Referenced by ~Model(), get_sphere(), render(), and swap().

◆ indices_num

GLsizei Model::indices_num = 0
private

Definition at line 141 of file opengl_utils.hpp.

Referenced by get_quad(), get_sphere(), render(), and swap().

◆ vao

GLuint Model::vao = 0
private

Definition at line 138 of file opengl_utils.hpp.

Referenced by ~Model(), get_quad(), get_sphere(), get_vao(), render(), and swap().

◆ vbo

GLuint Model::vbo = 0
private

Definition at line 139 of file opengl_utils.hpp.

Referenced by ~Model(), get_quad(), get_sphere(), and swap().


The documentation for this class was generated from the following files: