fixed much stuff with bad usage of constructor mixed with virtual functions in gl stuff. added model loading with assimp.

This commit is contained in:
Andreas Ortmann 2016-09-28 13:00:40 +02:00
parent aedda9d48e
commit 0105bfe430
17 changed files with 402 additions and 175 deletions

View file

@ -0,0 +1,116 @@
#pragma once
#include <string>
#include <iostream>
#include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
#include <epoxy/gl.h>
#include <epoxy/glx.h>
class PolygonModel {
public:
PolygonModel(const std::string &filename) : m_filename(filename)
{
if (!import()) {
m_loaded = false;
} else {
m_loaded = true;
}
}
bool import()
{
// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// propably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(m_filename,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
// If the import failed, report it
if (!scene) {
std::cout<<"[polygonmodel] loading file "
<< m_filename << " failed with: "
<< importer.GetErrorString() << std::endl;
return false;
}
// Now we can access the file's contents.
copyVertices(scene);
// We're done. Everything will be cleaned up by the importer destructor
return true;
}
private:
bool copyVertices(const aiScene *scene)
{
if (scene->mMeshes == 0) {
std::cout << "[polygonmodel : no meshes loaded for " << m_filename << std::endl;
return false;
}
aiNode *node = scene->mRootNode;
const aiMesh* mesh = scene->mMeshes[node->mMeshes[0]];
// 3 vertices per face, 3 floats per vertex
m_numVertices = mesh->mNumFaces*3;
m_data_position.reserve(m_numVertices);
m_data_normal.reserve(m_numVertices);
size_t t, i;
for (t=0; t<mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
if (face->mNumIndices != 3) {
std::cout << "[polygonmodel] need triangles, got something different with: "
<< face->mNumIndices << " vertices" << std::endl;
return false;
}
for (i=0; i<face->mNumIndices; i++) {
const size_t index = face->mIndices[i];
m_data_position.push_back(mesh->mVertices[index].x);
m_data_position.push_back(mesh->mVertices[index].y);
m_data_position.push_back(mesh->mVertices[index].z);
m_data_normal.push_back(mesh->mNormals[index].x);
m_data_normal.push_back(mesh->mNormals[index].y);
m_data_normal.push_back(mesh->mNormals[index].z);
}
}
size_t totalBytes = 3*m_numVertices*sizeof(float);
std::cout<<"[polygonmodel] loaded " << m_numVertices << " vertices ("
<< totalBytes << " bytes)" << std::endl;
return true;
}
const std::string &filename() const { return m_filename; }
bool ready() const { return m_loaded; }
private:
std::string m_filename;
bool m_loaded;
size_t m_numVertices;
// both will hold 3 * numVertices floats
std::vector<float> m_data_position;
std::vector<float> m_data_normal;
GLuint m_vbo_id_position;
GLuint m_vbo_id_normal;
};

View file

@ -2,16 +2,21 @@
#include <iostream>
#include "polygon_model.hpp"
namespace endofthejedi {
void RendererPolygon3d::setup()
{
std::cout<<"setup 3d" << std::endl;
PolygonModel atomicBomb("../data/mesh/small_atomic_bomb.stl");
exit(-1);
}
void RendererPolygon3d::render(const game::State *state)
{
(void) state;
//std::cout<<"render 3d" << std::endl;
std::cout<<"render 3d" << std::endl;
}
}

View file

@ -12,14 +12,18 @@
#include "state/ship.hpp"
#include "state/explosion.hpp"
class PolygonModel;
namespace endofthejedi {
class RendererPolygon3d : Renderer {
class RendererPolygon3d : public Renderer {
private:
protected:
public:
void setup();
void render(const game::State *state) override;
};
private:
std::vector<PolygonModel*> m_models;
};
}