Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
GeometricObject.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "GeometricObject.h"
3 
4 using namespace vis2;
5 
6 GeometricObject::GeometricObject( glm::mat4 _modelMatrix,
7  Shader* _shader,
8  const float* _positions_Ptr,
9  const unsigned int _pos_size,
10  const float* _normals_Ptr,
11  const unsigned int _norm_size,
12  const unsigned int* _index_Ptr,
13  const unsigned int _ind_size)
14  : DrawableObject(_modelMatrix), shader_Ptr(_shader), index_count(_ind_size)
15 {
16  // ------------------------- LOAD VBOs ----------------------------------- //
17  // load positions to buffer
18  glGenBuffers(1,&position_buffer_handle); // takes n, array of handles as well
19  glBindBuffer(GL_ARRAY_BUFFER, position_buffer_handle); // activate buffer
20  glBufferData(GL_ARRAY_BUFFER, _pos_size * 3 * sizeof(float), _positions_Ptr, GL_STATIC_DRAW); // load data
21  glBindBuffer(GL_ARRAY_BUFFER,0); // deactivate buffer
22 
23  // load normals to buffer
24  glGenBuffers(1,&normal_buffer_handle);
25  glBindBuffer(GL_ARRAY_BUFFER, normal_buffer_handle);
26  glBufferData(GL_ARRAY_BUFFER, _norm_size * 3 * sizeof(float), _normals_Ptr, GL_STATIC_DRAW);
27  glBindBuffer(GL_ARRAY_BUFFER,0);
28 
29  // load face indices to buffer
30  glGenBuffers(1,&index_buffer_handle);
31  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_handle);
32  glBufferData(GL_ELEMENT_ARRAY_BUFFER, _ind_size * sizeof(unsigned int), _index_Ptr, GL_STATIC_DRAW);
33  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
34 
35  // ------------------------- BIND VBOs to VAO ----------------------------------- //
36  // bind vao
37  glGenVertexArrays(1,&vao_handle);
38  glBindVertexArray(vao_handle);
39 
40  // bind positions
41  glBindBuffer(GL_ARRAY_BUFFER,position_buffer_handle); // bind vbo to vao
42  GLint shader_in_position_handle = glGetAttribLocation(shader_Ptr->program_handle,"position"); // get handle of the in vars from shader
43  glEnableVertexAttribArray(shader_in_position_handle); // communicate handle to vao
44  glVertexAttribPointer(shader_in_position_handle, 3, GL_FLOAT, GL_FALSE, 0, 0); // specify array elems size and if they should be normalized
45 
46  // bind normals
47  glBindBuffer(GL_ARRAY_BUFFER,normal_buffer_handle);
48  GLint shader_in_normal_handle = glGetAttribLocation(shader_Ptr->program_handle,"normal");
49  glEnableVertexAttribArray(shader_in_normal_handle);
50  glVertexAttribPointer(shader_in_normal_handle, 3, GL_FLOAT, GL_FALSE, 0, 0);
51 
52  // bind indices
53  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_handle);
54 
55  // ------------------------- DEACTIVATE ALL BUFFERS ----------------------------- //
56  glBindVertexArray(0); // deactivate vao
57  glBindBuffer(GL_ARRAY_BUFFER, 0); // deactivate vbo
58  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // deactivate vbo
59 
60 }
61 
63 {
64  glDeleteBuffers(1,&position_buffer_handle);
65  glDeleteBuffers(1,&normal_buffer_handle);
66  glDeleteBuffers(1,&index_buffer_handle);
67  glDeleteVertexArrays(1,&vao_handle);
68 }
69 
70 
71 void GeometricObject::draw(glm::mat4 &_projectionMatrix)
72 {
73  // activate shader
74  this->shader_Ptr->useShader();
75  // pass uniforms to shader of the object
76  glUniformMatrix4fv( glGetUniformLocation(shader_Ptr->program_handle,"projectionMatrix"),
77  1,
78  GL_FALSE,
79  glm::value_ptr(_projectionMatrix));
80  glUniformMatrix4fv( glGetUniformLocation(this->shader_Ptr->program_handle, "modelMatrix"),
81  1,
82  GL_FALSE,
83  glm::value_ptr(this->modelMatrix));
84 
85  // activate vao
86  glBindVertexArray(vao_handle);
87  // place draw call with the vao above
88  glDrawElements(GL_TRIANGLES, this->index_count, GL_UNSIGNED_INT,0);
89  // deactivate vao
90  glBindVertexArray(0);
91 
92  // deactivate shader
93  this->shader_Ptr->releaseShader();
94 }
95