67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
#include <vector>
|
|
#include <glbinding/gl/gl.h>
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <glm/gtx/transform.hpp>
|
|
|
|
#include "shaders.hh"
|
|
#include "Object.hh"
|
|
|
|
using namespace gl;
|
|
|
|
Object::Object(std::vector<objVertexAttribs> const& vas,
|
|
std::vector<uint16_t> indices)
|
|
: _indices(std::move(indices)), _vaID(0)
|
|
{
|
|
construct(vas);
|
|
}
|
|
|
|
Object::Object(std::string const& filename)
|
|
: _vaID(0)
|
|
{
|
|
auto tmp = readObject(filename);
|
|
_indices = std::get<1>(tmp);
|
|
construct(std::get<0>(tmp));
|
|
}
|
|
|
|
Object::~Object()
|
|
{
|
|
glDeleteVertexArrays(1, &_vaID);
|
|
}
|
|
|
|
void Object::draw() const
|
|
{
|
|
glBindVertexArray(_vaID);
|
|
|
|
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_SHORT,
|
|
_indices.data());
|
|
}
|
|
|
|
void Object::construct(std::vector<objVertexAttribs> const& vas)
|
|
{
|
|
_vbo = VBOManager::getInstance().alloc(sizeof(objVertexAttribs)*vas.size());
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo.getVBOId());
|
|
glBufferSubData(GL_ARRAY_BUFFER, _vbo.getOfs(),
|
|
sizeof(objVertexAttribs)*vas.size(), (void*)vas.data());
|
|
|
|
glGenVertexArrays(1, &_vaID);
|
|
glBindVertexArray(_vaID);
|
|
|
|
glEnableVertexAttribArray(ATTRIB_VERTEX_POS);
|
|
glVertexAttribPointer(ATTRIB_VERTEX_POS, 3, GL_FLOAT, GL_FALSE, sizeof(objVertexAttribs),
|
|
(void*)(_vbo.getOfs()+offsetof(objVertexAttribs, vertex)));
|
|
|
|
glEnableVertexAttribArray(ATTRIB_VERTEX_TC);
|
|
glVertexAttribPointer(ATTRIB_VERTEX_TC, 2, GL_UNSIGNED_SHORT, GL_TRUE,
|
|
sizeof(objVertexAttribs),
|
|
(void*)(_vbo.getOfs()+offsetof(objVertexAttribs, texCoords)));
|
|
|
|
glEnableVertexAttribArray(ATTRIB_VERTEX_NORM);
|
|
glVertexAttribPointer(ATTRIB_VERTEX_NORM, 4, GL_INT_2_10_10_10_REV, GL_TRUE,
|
|
sizeof(objVertexAttribs),
|
|
(void*)(_vbo.getOfs()+offsetof(objVertexAttribs, normal)));
|
|
}
|