Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
ModelMultiTextured.cpp
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <iostream>
3 #include "ModelMultiTextured.h"
4 
5 using namespace vis2;
6 
7 // %%%%%%%%%%%%%%%%%%%%%%%%%%% CONSTRUCTOR & DESTRUCTOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
8 
10  const glm::vec3 _id_color,
11  const glm::vec3 _contour_color,
12  glm::mat4 & _modelMatrix,
13  LoaderOBJ* _loader,
14  ShaderController* _shader_controller)
15  : DrawableObject(_modelMatrix),
16  id_name(_id_name),
17  id_color(_id_color),
18  contour_color(_contour_color),
19  loader_Ptr(_loader),
20  index_count(0),
21  position_buffer_handle(0), normal_buffer_handle(0), uv_buffer_handle(0), index_buffer_handle(0)
22 {
23  shader_ctrl[0] = _shader_controller;
24  vao_handle[0] = 0; vao_handle[1] = 0; vao_handle[2] = 0;
25  vao_setup_done[0] = false; vao_setup_done[1] = false; vao_setup_done[2] = false;
26 
27  // ------------------------- LOAD GEOMETRY ------------------------------- //
28 
29  if (!loader_Ptr)
30  {
31  std::cout << "Geometry loader for object \"" << this->id_name << "\" invalid!" << std::endl;
32  system("PAUSE");
33  exit(-1);
34  }
35 
36  loader_Ptr->obj2Container(); // parse the OBJ file
37  const GLfloat* _positions_Ptr = loader_Ptr->vert_positions;
38  const GLfloat* _normals_Ptr = loader_Ptr->vert_normals;
39  const GLfloat* _uv_Ptr = loader_Ptr->vert_uvs;
40  const GLuint* _index_Ptr = loader_Ptr->face_ind;
41  const unsigned int _vertex_count = loader_Ptr->getNrVertices();
42  const unsigned int _face_ind_count = loader_Ptr->getNrFaceInd();
43  index_count = _face_ind_count;
44 
45  // ------------------------- LOAD VBOs ----------------------------------- //
46 
47  // load positions to buffer
48  glGenBuffers(1,&position_buffer_handle); // takes n, array of handles as well
49  glBindBuffer(GL_ARRAY_BUFFER, position_buffer_handle); //activate buffer
50  glBufferData(GL_ARRAY_BUFFER, _vertex_count * 3 * sizeof(float), _positions_Ptr, GL_STATIC_DRAW); // load data
51  glBindBuffer(GL_ARRAY_BUFFER,0); // deactivate buffer
52 
53  // load normals to buffer
54  glGenBuffers(1,&normal_buffer_handle);
55  glBindBuffer(GL_ARRAY_BUFFER, normal_buffer_handle);
56  glBufferData(GL_ARRAY_BUFFER, _vertex_count * 3 * sizeof(float), _normals_Ptr, GL_STATIC_DRAW);
57  glBindBuffer(GL_ARRAY_BUFFER,0);
58 
59  // load texture coordinates to buffer
60  glGenBuffers(1,&uv_buffer_handle);
61  glBindBuffer(GL_ARRAY_BUFFER, uv_buffer_handle);
62  glBufferData(GL_ARRAY_BUFFER, _vertex_count * 2 * sizeof(float), _uv_Ptr, GL_STATIC_DRAW);
63  glBindBuffer(GL_ARRAY_BUFFER,0);
64 
65  // load face indices to buffer
66  glGenBuffers(1,&index_buffer_handle);
67  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_handle);
68  glBufferData(GL_ELEMENT_ARRAY_BUFFER, _face_ind_count * sizeof(unsigned int), _index_Ptr, GL_STATIC_DRAW);
69  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
70 
71  // ------------------------- BIND VBOs to VAO ----------------------------------- //
72  // ------------- this section needs to be repeated for each shader --------------- //
77  if (success != VIS2_SHADER_INVARS_SETUP_SUCCESS)
78  {
79  std::cout << "Failed to set up the shader program for object \"" << this->id_name << "\" !" << std::endl;
80  system("PAUSE");
81  exit(-1);
82  }
84  vao_setup_done[0] = true;
85 
86 }
87 
89 {
90  // first VBOs, then VAOs !!!
91  glDeleteBuffers(1,&position_buffer_handle);
92  glDeleteBuffers(1,&normal_buffer_handle);
93  glDeleteBuffers(1,&uv_buffer_handle);
94  glDeleteBuffers(1,&index_buffer_handle);
95 
96  glDeleteVertexArrays(3, vao_handle);
97 }
98 
99 // %%%%%%%%%%%%%%%%%%%%%% ASSIGN CLASS ELEMENTS, UPDATE AND DRAW %%%%%%%%%%%%%%%%%%%% //
100 
101 void ModelMultiTextured::update(double _deltaT)
102 {
103 }
104 
105 void ModelMultiTextured::draw(glm::mat4 & _projectionMatrix)
106 {
107 
108  if (!vao_setup_done[0])
109  return;
110 
111  // activate STANDARD shader
112  this->shader_ctrl[0]->shader_Ptr->useShader();
113  // setup of Uniforms
114  int success = this->shader_ctrl[0]->updateShaderUniforms(_projectionMatrix, this->modelMatrix, this->id_color, this->contour_color);
115  if (success != VIS2_SHADER_UNIFORMS_SETUP_SUCCESS)
116  return;
117 
118  // activate vao
119  glBindVertexArray(vao_handle[0]);
120  // place draw call with the vao above
121  glDrawElements(GL_TRIANGLES, this->index_count, GL_UNSIGNED_INT,0);
122  // deactivate vao
123  glBindVertexArray(0);
124 
125  // deactivate shader
126  this->shader_ctrl[0]->shader_Ptr->releaseShader();
127 
128 }
129 
130 void ModelMultiTextured::assignShaderController(ShaderController* _shader_ctrl, const unsigned int _slot)
131 {
132  if (!_shader_ctrl || (_slot != 1 && _slot != 2))
133  return;
134 
135  int success = 0;
136  this->shader_ctrl[_slot] = _shader_ctrl;
137  success = this->shader_ctrl[_slot]->setupShaderInVars( position_buffer_handle,
141  if (success != VIS2_SHADER_INVARS_SETUP_SUCCESS)
142  {
143  std::cout << "Failed to set up the color ID shader program for object \"" << this->id_name << "\" !" << std::endl;
144  }
145  else
146  {
147  vao_handle[_slot] = shader_ctrl[_slot]->getVAOHandle();
148  vao_setup_done[_slot] = true;
149  }
150 }
151 
152 void ModelMultiTextured::updateModelMatrix(glm::mat4 & _modelMatrix)
153 {
154  this->modelMatrix = _modelMatrix;
155 }
156 
157 void ModelMultiTextured::draw(glm::mat4 & _projectionMatrix, const unsigned int _slot)
158 {
159 
160  if ((_slot != 0 && _slot != 1 && _slot != 2) || !vao_setup_done[_slot])
161  return;
162 
163  // activate shader
164  this->shader_ctrl[_slot]->shader_Ptr->useShader();
165  // setup of Uniforms
166  int success = this->shader_ctrl[_slot]->updateShaderUniforms(_projectionMatrix, this->modelMatrix, this->id_color, this->contour_color);
167  if (success != VIS2_SHADER_UNIFORMS_SETUP_SUCCESS)
168  return;
169 
170  // activate vao
171  glBindVertexArray(vao_handle[_slot]);
172  // place draw call with the vao above
173  glDrawElements(GL_TRIANGLES, this->index_count, GL_UNSIGNED_INT,0);
174  // deactivate vao
175  glBindVertexArray(0);
176 
177  // deactivate shader
178  this->shader_ctrl[_slot]->shader_Ptr->releaseShader();
179 
180 }
181