Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
GeometricObjectTextured.cpp
Go to the documentation of this file.
1 #include <iostream>
3 
4 using namespace vis2;
5 
7  Shader* _shader,
8  Texture* _diffuse_texture,
9  const float* _positions_Ptr,
10  const unsigned int _pos_size,
11  const float* _normals_Ptr,
12  const unsigned int _norm_size,
13  const float* _uv_Ptr,
14  const unsigned int _uv_size,
15  const unsigned int* _index_Ptr,
16  const unsigned int _ind_size)
17 : GeometricObject( _modelMatrix,
18  _shader,
19  _positions_Ptr,
20  _pos_size,
21  _normals_Ptr,
22  _norm_size,
23  _index_Ptr,
24  _ind_size),
25  diffuse_texture_Ptr(_diffuse_texture)
26 {
27  // ------------------------- LOAD additional VBOs ------------------------ //
28  // load texture coordinates to buffer
29  glGenBuffers(1,&uv_buffer_handle);
30  glBindBuffer(GL_ARRAY_BUFFER, uv_buffer_handle);
31  glBufferData(GL_ARRAY_BUFFER, _uv_size * 2 * sizeof(float), _uv_Ptr, GL_STATIC_DRAW);
32  glBindBuffer(GL_ARRAY_BUFFER,0);
33 
34  // ------------------------- BIND VBOs to VAO ---------------------------- //
35  // bind vao (it was already generated in GeometricObject)
36  glBindVertexArray(vao_handle);
37 
38  // bind texture coordinates
39  glBindBuffer(GL_ARRAY_BUFFER,uv_buffer_handle);
40  GLint shader_in_uv_handle = glGetAttribLocation(shader_Ptr->program_handle,"uv");
41  glEnableVertexAttribArray(shader_in_uv_handle);
42  glVertexAttribPointer(shader_in_uv_handle, 2, GL_FLOAT, GL_FALSE, 0, 0);
43 
44  // ------------------------- DEACTIVATE ALL BUFFERS ----------------------------- //
45  glBindVertexArray(0); // deactivate vao
46  glBindBuffer(GL_ARRAY_BUFFER, 0); // deactivate vbo
47  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // deactivate vbo
48 }
49 
51 {
52  glDeleteBuffers(1,&position_buffer_handle);
53  glDeleteBuffers(1,&normal_buffer_handle);
54  glDeleteBuffers(1,&uv_buffer_handle);
55  glDeleteBuffers(1,&index_buffer_handle);
56  glDeleteVertexArrays(1,&vao_handle);
57 }
58 
59 void GeometricObjectTextured::draw(glm::mat4 & _projectionMatrix)
60 {
61  // activate shader
62  this->shader_Ptr->useShader();
63  // pass uniforms to shader of the object
64  glUniformMatrix4fv( glGetUniformLocation(shader_Ptr->program_handle,"projectionMatrix"),
65  1,
66  GL_FALSE,
67  glm::value_ptr(_projectionMatrix));
68  glUniformMatrix4fv( glGetUniformLocation(this->shader_Ptr->program_handle, "modelMatrix"),
69  1,
70  GL_FALSE,
71  glm::value_ptr(this->modelMatrix));
72 
73  int unit = 0;
75  glUniform1i(glGetUniformLocation(shader_Ptr->program_handle,"diffColorTexture"), unit);
76 
77  // activate vao
78  glBindVertexArray(vao_handle);
79  // place draw call with the vao above
80  glDrawElements(GL_TRIANGLES, this->index_count, GL_UNSIGNED_INT,0);
81  // deactivate vao
82  glBindVertexArray(0);
83 
84  // deactivate shader
85  this->shader_Ptr->releaseShader();
86 }