71 lines
2.2 KiB
C++
71 lines
2.2 KiB
C++
#ifndef __OPENGLPLAYGROUND_OBJECT_HH__
|
|
#define __OPENGLPLAYGROUND_OBJECT_HH__
|
|
|
|
#include <memory>
|
|
|
|
#include "objectParser.hh"
|
|
#include "VBOManager.hh"
|
|
|
|
class Object {
|
|
public:
|
|
Object(VBOManager& vboManager, std::vector<objVertexAttribs> const& vas, std::vector<uint16_t> const& indices,
|
|
Program& prog)
|
|
: _vboManager(vboManager), _prog(prog), _indices(indices) {
|
|
construct(vas);
|
|
}
|
|
|
|
Object(VBOManager& vboManager, std::string const& filename, Program& prog)
|
|
: _vboManager(vboManager), _prog(prog) {
|
|
auto tmp = readObject(filename);
|
|
_indices = std::get<1>(tmp);
|
|
construct(std::get<0>(tmp));
|
|
}
|
|
|
|
~Object() {
|
|
}
|
|
|
|
void draw(glm::mat4 const& modelview) const {
|
|
glBindVertexArray(_vaID);
|
|
_prog.use();
|
|
glUniformMatrix4fv(_prog.getUniformLocation("model_matrix"), 1, GL_FALSE,
|
|
glm::value_ptr(modelview));
|
|
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_SHORT, _indices.data());
|
|
}
|
|
|
|
private:
|
|
void construct(std::vector<objVertexAttribs> const& vas) {
|
|
_vbo = _vboManager.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);
|
|
|
|
GLint al;
|
|
if((al = _prog.getAttribLocation("vertex")) != -1) {
|
|
glEnableVertexAttribArray(al);
|
|
glVertexAttribPointer(al, 3, GL_FLOAT, GL_FALSE, sizeof(objVertexAttribs),
|
|
(void*)(_vbo.getOfs()+offsetof(objVertexAttribs, vertex)));
|
|
}
|
|
if((al = _prog.getAttribLocation("vertexTC")) != -1) {
|
|
glEnableVertexAttribArray(al);
|
|
glVertexAttribPointer(al, 2, GL_UNSIGNED_SHORT, GL_TRUE, sizeof(objVertexAttribs),
|
|
(void*)(_vbo.getOfs()+offsetof(objVertexAttribs, texCoords)));
|
|
}
|
|
if((al = _prog.getAttribLocation("vertexNorm")) != -1) {
|
|
glEnableVertexAttribArray(al);
|
|
glVertexAttribPointer(al, 4, GL_INT_2_10_10_10_REV, GL_TRUE, sizeof(objVertexAttribs),
|
|
(void*)(_vbo.getOfs()+offsetof(objVertexAttribs, normal)));
|
|
}
|
|
}
|
|
|
|
VBOManager& _vboManager;
|
|
VBOManager::VBOAlloc _vbo;
|
|
Program& _prog;
|
|
std::vector<uint16_t> _indices;
|
|
GLuint _vaID;
|
|
};
|
|
|
|
#endif
|