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
| #include "vbo.h"
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>
#include <openctm.h>
#include <iostream>
VBO::VBO(string file):
tex(NULL),
Color(true)
{
CTMimporter ctm;
try
{
ctm.Load(file.c_str());
}
catch (const ctm_error& error)
{
string msg=string("impposible de charger le fichier: ")+file;
throw msg;
}
if(ctm.GetNamedAttribMap("Color") == CTM_NONE)
Color=false;
// one VAOs allocation
glGenVertexArrays(1, &m_vaoID);
// VAO setup
glBindVertexArray(m_vaoID);
glGenBuffers(2, m_vboID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
glBufferData(GL_ARRAY_BUFFER, ctm.GetInteger(CTM_VERTEX_COUNT)*3*sizeof(GLfloat), ctm.GetFloatArray(CTM_VERTICES), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
if(Color)
{
glEnableVertexAttribArray(1);
glGenBuffers(1, &m_vboColorID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[2]);
glBufferData(GL_ARRAY_BUFFER, ctm.GetInteger(CTM_VERTEX_COUNT)*4*sizeof(GLfloat), ctm.GetFloatArray(ctm.GetNamedAttribMap("Color")), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 4, GL_FLOAT, GL_FALSE, 0, 0);
}
//texture
if(ctm.GetInteger(CTM_UV_MAP_COUNT) > 0)
{
tex = new Texture((string)ctm.GetUVMapString(CTM_UV_MAP_1, CTM_FILE_NAME));
glGenBuffers(1, &m_vbotexCoordID);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, m_vbotexCoordID);
glBufferData(GL_ARRAY_BUFFER, ctm.GetInteger(CTM_VERTEX_COUNT)*2*sizeof(GLfloat),ctm.GetFloatArray(CTM_UV_MAP_1), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
numTriangles=ctm.GetInteger(CTM_TRIANGLE_COUNT);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboID[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ctm.GetInteger(CTM_TRIANGLE_COUNT)*3*sizeof(uint), ctm.GetIntegerArray(CTM_INDICES), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)3, 1, GL_UNSIGNED_INT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}
VBO::~VBO()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(2, m_vboID);
glDeleteBuffers(1, &m_vboColorID);
glBindVertexArray(0);
glDeleteVertexArrays(1, &m_vaoID);
if(tex)
delete tex;
}
void VBO::draw()
{
if(!Color)
glVertexAttrib3f((GLuint)1, 1.0, 1.0, 1.0);
glBindVertexArray(m_vaoID);
if(tex)
tex->bind();
glDrawElements(GL_TRIANGLES, numTriangles*3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
} |
Partager