Split class Model into classes Model, Models::Static
This commit is contained in:
parent
aee8cf6099
commit
7ab0664f0d
11
src/main.cpp
11
src/main.cpp
|
@ -3,6 +3,7 @@
|
|||
#include "scene.hpp"
|
||||
#include "camera.hpp"
|
||||
#include "store.hpp"
|
||||
#include "model/static.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
|
@ -81,11 +82,11 @@ int main()
|
|||
|
||||
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f);
|
||||
|
||||
protagonist = store.load<Model>("protagonist.obj");
|
||||
car = store.load<Model>("car.obj");
|
||||
suzanne = store.load<Model>("suzanne.obj");
|
||||
teapot = store.load<Model>("teapot.obj");
|
||||
bunny = store.load<Model>("bunny.obj");
|
||||
protagonist = store.load<Models::Static>("protagonist.obj");
|
||||
car = store.load<Models::Static>("car.obj");
|
||||
suzanne = store.load<Models::Static>("suzanne.obj");
|
||||
teapot = store.load<Models::Static>("teapot.obj");
|
||||
bunny = store.load<Models::Static>("bunny.obj");
|
||||
|
||||
protagonist1 = new Object(*protagonist);
|
||||
protagonist1->position.z = 4;
|
||||
|
|
|
@ -1,108 +1,5 @@
|
|||
#include "model.hpp"
|
||||
|
||||
#include "../mtllib.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
const std::string Model::filename(const std::string &name)
|
||||
{
|
||||
return "/data/models/" + name;
|
||||
}
|
||||
|
||||
Model::Model(Store &store, const std::string &name)
|
||||
{
|
||||
std::ifstream file(filename(name), std::ios::in);
|
||||
|
||||
const Mtllib *mtllib = nullptr;
|
||||
|
||||
std::vector<glm::vec3> tmp_positions;
|
||||
std::vector<glm::vec2> tmp_tex_coords;
|
||||
std::vector<glm::vec3> tmp_normals;
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
int attr_count = 0;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line.substr(0, 2) == "v ")
|
||||
{
|
||||
glm::vec3 v;
|
||||
sscanf(line.data(), "v %f %f %f", &v.x, &v.y, &v.z);
|
||||
tmp_positions.push_back(v);
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 3) == "vt ")
|
||||
{
|
||||
glm::vec2 vt;
|
||||
sscanf(line.data(), "vt %f %f", &vt.x, &vt.y);
|
||||
tmp_tex_coords.push_back(vt);
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 3) == "vn ")
|
||||
{
|
||||
glm::vec3 vn;
|
||||
sscanf(line.data(), "vn %f %f %f", &vn.x, &vn.y, &vn.z);
|
||||
tmp_normals.push_back(vn);
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 2) == "f ")
|
||||
{
|
||||
if (attr_count == 0)
|
||||
{
|
||||
if (line.find("//") == std::string::npos)
|
||||
attr_count = 3;
|
||||
else
|
||||
attr_count = 2;
|
||||
}
|
||||
|
||||
GLushort v[3];
|
||||
GLushort vt[3];
|
||||
GLushort vn[3];
|
||||
|
||||
if (attr_count == 2)
|
||||
sscanf(line.data(), "f %hu//%hu %hu//%hu %hu//%hu", &v[0], &vn[0], &v[1], &vn[1], &v[2], &vn[2]);
|
||||
else
|
||||
sscanf(line.data(), "f %hu/%hu/%hu %hu/%hu/%hu %hu/%hu/%hu", &v[0], &vt[0], &vn[0],
|
||||
&v[1], &vt[1], &vn[1],
|
||||
&v[2], &vt[2], &vn[2]);
|
||||
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
positions.push_back(tmp_positions[v[i] - 1]);
|
||||
if (attr_count == 3)
|
||||
tex_coords.push_back(tmp_tex_coords[vt[i] - 1]);
|
||||
normals.push_back(tmp_normals[vn[i] - 1]);
|
||||
elements.push_back(index++);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "mtllib ")
|
||||
{
|
||||
mtllib = (Mtllib*)store.load<Mtllib>(line.substr(7));
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "usemtl ")
|
||||
{
|
||||
_material = (*mtllib)[line.substr(7)];
|
||||
}
|
||||
}
|
||||
|
||||
positions_id = create_array_buffer(GL_ARRAY_BUFFER, 3 * positions.size() * sizeof(GLfloat), positions.data());
|
||||
|
||||
if (attr_count == 3)
|
||||
tex_coords_id = create_array_buffer(GL_ARRAY_BUFFER, 2 * tex_coords.size() * sizeof(GLfloat), tex_coords.data());
|
||||
else
|
||||
tex_coords_id = 0;
|
||||
|
||||
normals_id = create_array_buffer(GL_ARRAY_BUFFER, 3 * normals.size() * sizeof(GLfloat), normals.data());
|
||||
|
||||
id = create_array_buffer(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(GLushort), elements.data());
|
||||
}
|
||||
|
||||
GLuint Model::create_array_buffer(const GLenum type, const GLsizeiptr size, const GLvoid *const data)
|
||||
{
|
||||
GLuint id;
|
||||
|
@ -111,27 +8,3 @@ GLuint Model::create_array_buffer(const GLenum type, const GLsizeiptr size, cons
|
|||
glBufferData(type, size, data, GL_STATIC_DRAW);
|
||||
return id;
|
||||
}
|
||||
|
||||
void Model::draw() const
|
||||
{
|
||||
_material->use();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, positions_id);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
|
||||
if (tex_coords_id == 0)
|
||||
glDisableVertexAttribArray(1);
|
||||
else
|
||||
{
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, tex_coords_id);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, normals_id);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
||||
|
||||
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_SHORT, 0);
|
||||
}
|
||||
|
|
|
@ -2,32 +2,17 @@
|
|||
#define _MODEL_HPP_
|
||||
|
||||
#include "../resource.hpp"
|
||||
#include "../textured_mesh.hpp"
|
||||
#include "../gl.hpp"
|
||||
#include "../material.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Model:
|
||||
public Resource,
|
||||
public TexturedMesh
|
||||
public Resource
|
||||
{
|
||||
RESOURCE(Model)
|
||||
|
||||
public:
|
||||
void draw() const;
|
||||
|
||||
private:
|
||||
GLuint positions_id;
|
||||
GLuint tex_coords_id;
|
||||
GLuint normals_id;
|
||||
|
||||
GLuint id;
|
||||
|
||||
const Material *_material;
|
||||
virtual void draw() const = 0;
|
||||
|
||||
protected:
|
||||
static GLuint create_array_buffer(GLenum type, GLsizeiptr size, const GLvoid *data);
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
#include "static.hpp"
|
||||
|
||||
#include "../mtllib.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace Models;
|
||||
|
||||
const std::string Static::filename(const std::string &name)
|
||||
{
|
||||
return "/data/models/" + name;
|
||||
}
|
||||
|
||||
Static::Static(Store &store, const std::string &name)
|
||||
{
|
||||
std::ifstream file(filename(name), std::ios::in);
|
||||
|
||||
const Mtllib *mtllib = nullptr;
|
||||
|
||||
std::vector<glm::vec3> tmp_positions;
|
||||
std::vector<glm::vec2> tmp_tex_coords;
|
||||
std::vector<glm::vec3> tmp_normals;
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
int attr_count = 0;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (line.substr(0, 2) == "v ")
|
||||
{
|
||||
glm::vec3 v;
|
||||
sscanf(line.data(), "v %f %f %f", &v.x, &v.y, &v.z);
|
||||
tmp_positions.push_back(v);
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 3) == "vt ")
|
||||
{
|
||||
glm::vec2 vt;
|
||||
sscanf(line.data(), "vt %f %f", &vt.x, &vt.y);
|
||||
tmp_tex_coords.push_back(vt);
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 3) == "vn ")
|
||||
{
|
||||
glm::vec3 vn;
|
||||
sscanf(line.data(), "vn %f %f %f", &vn.x, &vn.y, &vn.z);
|
||||
tmp_normals.push_back(vn);
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 2) == "f ")
|
||||
{
|
||||
if (attr_count == 0)
|
||||
{
|
||||
if (line.find("//") == std::string::npos)
|
||||
attr_count = 3;
|
||||
else
|
||||
attr_count = 2;
|
||||
}
|
||||
|
||||
GLushort v[3];
|
||||
GLushort vt[3];
|
||||
GLushort vn[3];
|
||||
|
||||
if (attr_count == 2)
|
||||
sscanf(line.data(), "f %hu//%hu %hu//%hu %hu//%hu", &v[0], &vn[0], &v[1], &vn[1], &v[2], &vn[2]);
|
||||
else
|
||||
sscanf(line.data(), "f %hu/%hu/%hu %hu/%hu/%hu %hu/%hu/%hu", &v[0], &vt[0], &vn[0],
|
||||
&v[1], &vt[1], &vn[1],
|
||||
&v[2], &vt[2], &vn[2]);
|
||||
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
positions.push_back(tmp_positions[v[i] - 1]);
|
||||
if (attr_count == 3)
|
||||
tex_coords.push_back(tmp_tex_coords[vt[i] - 1]);
|
||||
normals.push_back(tmp_normals[vn[i] - 1]);
|
||||
elements.push_back(index++);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "mtllib ")
|
||||
{
|
||||
mtllib = (Mtllib*)store.load<Mtllib>(line.substr(7));
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "usemtl ")
|
||||
{
|
||||
_material = (*mtllib)[line.substr(7)];
|
||||
}
|
||||
}
|
||||
|
||||
positions_id = create_array_buffer(GL_ARRAY_BUFFER, 3 * positions.size() * sizeof(GLfloat), positions.data());
|
||||
|
||||
if (attr_count == 3)
|
||||
tex_coords_id = create_array_buffer(GL_ARRAY_BUFFER, 2 * tex_coords.size() * sizeof(GLfloat), tex_coords.data());
|
||||
else
|
||||
tex_coords_id = 0;
|
||||
|
||||
normals_id = create_array_buffer(GL_ARRAY_BUFFER, 3 * normals.size() * sizeof(GLfloat), normals.data());
|
||||
|
||||
id = create_array_buffer(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(GLushort), elements.data());
|
||||
}
|
||||
|
||||
void Static::draw() const
|
||||
{
|
||||
_material->use();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, positions_id);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
|
||||
if (tex_coords_id == 0)
|
||||
glDisableVertexAttribArray(1);
|
||||
else
|
||||
{
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, tex_coords_id);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, normals_id);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
||||
|
||||
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_SHORT, 0);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef _MODEL_STATIC_HPP_
|
||||
#define _MODEL_STATIC_HPP_
|
||||
|
||||
#include "model.hpp"
|
||||
#include "../textured_mesh.hpp"
|
||||
#include "../gl.hpp"
|
||||
#include "../material.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Models
|
||||
{
|
||||
class Static:
|
||||
public Model,
|
||||
protected TexturedMesh
|
||||
{
|
||||
RESOURCE(Static)
|
||||
|
||||
public:
|
||||
void draw() const;
|
||||
|
||||
private:
|
||||
GLuint positions_id;
|
||||
GLuint tex_coords_id;
|
||||
GLuint normals_id;
|
||||
|
||||
GLuint id;
|
||||
|
||||
const Material *_material;
|
||||
};
|
||||
};
|
||||
|
||||
#endif // _MODEL_STATIC_HPP_
|
|
@ -13,7 +13,7 @@ class Resource
|
|||
|
||||
#define RESOURCE(T) \
|
||||
private: \
|
||||
friend class Store; \
|
||||
friend class ::Store; \
|
||||
T(Store &store, const std::string &name); \
|
||||
static const std::string filename(const std::string &name);
|
||||
|
||||
|
|
Reference in New Issue