1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include "Objet3D.hpp"
Objet3D::Objet3D(int nombreTriangles) :
m_nombreTriangles(nombreTriangles),
m_matriceTransformations(glm::mat4(1))
{
glGenBuffers(1, &m_identifiantVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_identifiantVBO);
glBufferData(GL_ARRAY_BUFFER, (m_nombreTriangles * 15)* sizeof(float), 0, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &m_identifiantVAO);
glBindVertexArray(m_identifiantVAO);
glBindBuffer(GL_ARRAY_BUFFER, m_identifiantVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)(m_nombreTriangles * 9 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
m_matriceTransformations = glm::rotate(m_matriceTransformations, 20.0f, glm::vec3(0, 1, 0));
}
bool Objet3D::modifierStructure(const std::vector<float>& coordonneesSommets, const std::vector<float>& coordonneesTexture, const std::vector<int>& identifiantsTextures)
{
m_identifiantsTextures = identifiantsTextures;
glBindBuffer(GL_ARRAY_BUFFER, m_identifiantVBO);
void *adresseVBO = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (adresseVBO == NULL)
{
std::cout << "[ERREUR] Récuperation du VBO" << std::endl;
glBindBuffer(GL_ARRAY_BUFFER, 0);
return false;
}
memcpy((char*)adresseVBO, coordonneesSommets.data(), (m_nombreTriangles * 9)* sizeof(float));
memcpy((char*)adresseVBO + m_nombreTriangles * 9 * sizeof(float), coordonneesTexture.data(), m_nombreTriangles * 6 * sizeof(float));
glUnmapBuffer(GL_ARRAY_BUFFER);
adresseVBO = 0;
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Objet3D::afficher(const Shader& shader, const glm::mat4& matriceProjection, const glm::mat4& matriceVue)
{
glUseProgram(shader.identifiant());
glBindVertexArray(m_identifiantVAO);
glUniformMatrix4fv(glGetUniformLocation(shader.identifiant(), "matriceAffichage"), 1, GL_FALSE, glm::value_ptr(matriceProjection * matriceVue * m_matriceTransformations));
glBindTexture(GL_TEXTURE_2D, m_textures[0].identifiant());
glDrawArrays(GL_TRIANGLES, 0, m_nombreTriangles * 3);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(0);
}
void Objet3D::tourner(float angle, glm::vec3 axes)
{
m_matriceTransformations = glm::rotate(m_matriceTransformations, angle, axes);
}
void Objet3D::translater(glm::vec3 translation)
{
m_matriceTransformations = glm::translate(m_matriceTransformations, translation);
}
void Objet3D::echelonner(glm::vec3 echelle)
{
m_matriceTransformations = glm::scale(m_matriceTransformations, echelle);
}
void Objet3D::reinitialiserTransformations()
{
m_matriceTransformations = glm::mat4(1);
}
int Objet3D::ajouterTexture(std::string fichierTexture)
{
m_textures.push_back(Texture());
m_textures[m_textures.size() - 1].charger(fichierTexture);
return m_textures.size() - 1;
}
void Objet3D::supprimerTexture(int identifiantTexture)
{
m_textures.erase(m_textures.begin() + identifiantTexture);
} |
Partager