SuperimposeMesh
All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Mesh.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
8 #include "SuperimposeMesh/Mesh.h"
9 
10 #include <string>
11 
12 #include <glm/glm.hpp>
13 #include <glm/gtc/matrix_transform.hpp>
14 #include <glm/gtc/type_ptr.hpp>
15 
16 
18 (
19  std::vector<Vertex> vertices,
20  std::vector<GLuint> indices,
21  std::vector<Texture> textures
22 ) :
23  vertices_(vertices),
24  indices_(indices),
25  textures_(textures)
26 {
27  glGenVertexArrays(1, &VAO_);
28  glGenBuffers(1, &VBO_);
29  glGenBuffers(1, &EBO_);
30 
31  glBindVertexArray(VAO_);
32 
33  glBindBuffer(GL_ARRAY_BUFFER, VBO_);
34  glBufferData(GL_ARRAY_BUFFER, vertices_.size() * sizeof(Vertex), &vertices_[0], GL_STATIC_DRAW);
35 
36  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_);
37  glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_.size() * sizeof(GLuint), &indices_[0], GL_STATIC_DRAW);
38 
39  /* Vertex Positions */
40  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) 0);
41  glEnableVertexAttribArray(0);
42 
43  /* Vertex Normals. */
44  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)sizeof(glm::vec3));
45  glEnableVertexAttribArray(1);
46 
47  /* Vertex Texture Coords */
48  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (2 * sizeof(glm::vec3)));
49  glEnableVertexAttribArray(2);
50 
51  glBindVertexArray(0);
52 }
53 
54 
55 void Mesh::Draw(Shader shader)
56 {
57  /* FIXME
58  * This part of code assumes that the fragment shader has several uniform variables with names
59  * - texture_diffuse<number>
60  * - texture_specular<number>
61  */
62  GLuint diffuseNr = 1;
63  GLuint specularNr = 1;
64  for (GLuint i = 0; i < textures_.size(); ++i)
65  {
66  /* Activate proper texture unit before binding. */
67  glActiveTexture(GL_TEXTURE0 + i);
68 
69  /* Retrieve texture number (the N in diffuse_textureN). */
70  std::string number;
71  std::string name = textures_[i].type;
72 
73  /* Transfer GLuint to stream. */
74  if (name == "texture_diffuse")
75  {
76  number = std::to_string(diffuseNr++);
77  }
78  else if (name == "texture_specular")
79  {
80  number = std::to_string(specularNr++);
81  }
82 
83  glUniform1i(glGetUniformLocation(shader.get_program(), (name + number).c_str()), i);
84  glBindTexture(GL_TEXTURE_2D, textures_[i].id);
85  }
86  glActiveTexture(GL_TEXTURE0);
87 
88  /* Draw mesh. */
89  glBindVertexArray(VAO_);
90  glDrawElements(GL_TRIANGLES, indices_.size(), GL_UNSIGNED_INT, 0);
91  glBindVertexArray(0);
92 }
Mesh(std::vector< Vertex > vertices, std::vector< GLuint > indices, std::vector< Texture > textures)
Definition: Mesh.cpp:18
Definition: Shader.h:17
void Draw(Shader shader)
Definition: Mesh.cpp:55
GLuint VAO_
Definition: Mesh.h:44
std::vector< GLuint > indices_
Definition: Mesh.h:52
const GLuint get_program()
Definition: Shader.h:36
std::vector< Texture > textures_
Definition: Mesh.h:54