123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include "Cube.h"
- using namespace glm;
- Cube::Cube(float taille, std::string const vertexShader, std::string const fragmentShader)
- : m_shader(vertexShader, fragmentShader), m_vboID(0), m_tailleVerticesBytes(108 * sizeof(float)), m_tailleCouleursBytes(108 * sizeof(float)), m_vaoID(0)
- {
-
- m_shader.charger();
-
- taille /= 2;
-
- float verticesTmp[] = {-taille, -taille, -taille, taille, -taille, -taille, taille, taille, -taille,
- -taille, -taille, -taille, -taille, taille, -taille, taille, taille, -taille,
- taille, -taille, taille, taille, -taille, -taille, taille, taille, -taille,
- taille, -taille, taille, taille, taille, taille, taille, taille, -taille,
- -taille, -taille, taille, taille, -taille, taille, taille, -taille, -taille,
- -taille, -taille, taille, -taille, -taille, -taille, taille, -taille, -taille,
- taille, -taille, taille, -taille, -taille, taille, -taille, taille, taille,
- taille, -taille, taille, taille, taille, taille, -taille, taille, taille,
- -taille, -taille, -taille, -taille, -taille, taille, -taille, taille, taille,
- -taille, -taille, -taille, -taille, taille, -taille, -taille, taille, taille,
- -taille, taille, taille, taille, taille, taille, taille, taille, -taille,
- -taille, taille, taille, -taille, taille, -taille, taille, taille, -taille};
-
- float couleursTmp[] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
- 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
- 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
- 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
- 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
- 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0};
-
- for(int i(0); i < 108; i++)
- {
- m_vertices[i] = verticesTmp[i];
- m_couleurs[i] = couleursTmp[i];
- }
- }
- Cube::~Cube()
- {
-
- glDeleteBuffers(1, &m_vboID);
-
- glDeleteVertexArrays(1, &m_vaoID);
- }
- void Cube::charger()
- {
-
-
- if(glIsBuffer(m_vboID) == GL_TRUE)
- glDeleteBuffers(1, &m_vboID);
-
- glGenBuffers(1, &m_vboID);
-
- glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
-
- glBufferData(GL_ARRAY_BUFFER, m_tailleVerticesBytes + m_tailleCouleursBytes, 0, GL_STATIC_DRAW);
-
- glBufferSubData(GL_ARRAY_BUFFER, 0, m_tailleVerticesBytes, m_vertices);
- glBufferSubData(GL_ARRAY_BUFFER, m_tailleVerticesBytes, m_tailleCouleursBytes, m_couleurs);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
-
- if(glIsVertexArray(m_vaoID) == GL_TRUE)
- glDeleteVertexArrays(1, &m_vaoID);
-
- glGenVertexArrays(1, &m_vaoID);
-
- glBindVertexArray(m_vaoID);
-
- glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
-
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
- glEnableVertexAttribArray(0);
-
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(m_tailleVerticesBytes));
- glEnableVertexAttribArray(1);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- glBindVertexArray(0);
- }
- void Cube::afficher(glm::mat4 &projection, glm::mat4 &modelview)
- {
-
- glUseProgram(m_shader.getProgramID());
-
- glBindVertexArray(m_vaoID);
-
- glUniformMatrix4fv(glGetUniformLocation(m_shader.getProgramID(), "projection"), 1, GL_FALSE, value_ptr(projection));
- glUniformMatrix4fv(glGetUniformLocation(m_shader.getProgramID(), "modelview"), 1, GL_FALSE, value_ptr(modelview));
-
- glDrawArrays(GL_TRIANGLES, 0, 36);
-
- glBindVertexArray(0);
-
- glUseProgram(0);
- }
- void Cube::updateVBO(void *donnees, int tailleBytes, int decalage)
- {
-
- glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
-
- void *adresseVBO = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
-
- if(adresseVBO == NULL)
- {
- std::cout << "Erreur au niveau de la récupération du VBO" << std::endl;
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- return;
- }
-
- memcpy((char*)adresseVBO + decalage, donnees, tailleBytes);
-
- glUnmapBuffer(GL_ARRAY_BUFFER);
- adresseVBO = 0;
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
|