load basic 3d model
This commit is contained in:
160
src/render/loader/MobLoader.cpp
Normal file
160
src/render/loader/MobLoader.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#include "Defines.h"
|
||||
#include "render/loader/MobLoader.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
namespace td {
|
||||
namespace MobLoader {
|
||||
|
||||
const static int POSITION_VERTEX_SIZE = 3;
|
||||
const static int TEXTURE_VERTEX_SIZE = 2;
|
||||
|
||||
typedef Vec3f Vertex;
|
||||
typedef Vec2f TextureUV;
|
||||
typedef Vec3f Normal;
|
||||
|
||||
struct Index {
|
||||
int vertexIndex;
|
||||
int textureIndex;
|
||||
int normalIndex;
|
||||
};
|
||||
|
||||
enum DataType : std::uint8_t {
|
||||
dt_Vertex,
|
||||
dt_Texture,
|
||||
dt_Normal,
|
||||
dt_Index,
|
||||
dt_None
|
||||
};
|
||||
|
||||
static DataType GetDataType(const std::string& type) {
|
||||
static const std::map<std::string, DataType> Types = {
|
||||
{"v", dt_Vertex},
|
||||
{"vt", dt_Texture},
|
||||
{"vn", dt_Normal},
|
||||
{"f", dt_Index}
|
||||
};
|
||||
|
||||
auto it = Types.find(type);
|
||||
|
||||
return it != Types.end() ? it->second : dt_None;
|
||||
}
|
||||
|
||||
GL::VertexArray LoadMobModel() {
|
||||
std::ifstream fileStream{ "Assets/zombie.obj" };
|
||||
std::string line;
|
||||
|
||||
std::vector<Vertex> tempVertecies;
|
||||
std::vector<TextureUV> tempTextureUvs;
|
||||
std::vector<Normal> tempNormals;
|
||||
|
||||
std::vector<float> vertecies;
|
||||
std::vector<float> textureUvs;
|
||||
std::vector<float> normals;
|
||||
|
||||
while (getline(fileStream, line)) {
|
||||
|
||||
std::replace(line.begin(), line.end(), '/', ' ');
|
||||
|
||||
std::stringstream ss;
|
||||
ss << line;
|
||||
|
||||
std::string typeStr;
|
||||
ss >> typeStr;
|
||||
|
||||
DataType dataType = GetDataType(typeStr);
|
||||
|
||||
switch (dataType) {
|
||||
case dt_Vertex: {
|
||||
Vertex vertex;
|
||||
|
||||
ss >> vertex.x;
|
||||
ss >> vertex.y;
|
||||
ss >> vertex.z;
|
||||
|
||||
tempVertecies.push_back(vertex);
|
||||
break;
|
||||
}
|
||||
|
||||
case dt_Texture: {
|
||||
TextureUV texture;
|
||||
|
||||
ss >> texture.x;
|
||||
ss >> texture.y;
|
||||
|
||||
tempTextureUvs.push_back(texture);
|
||||
break;
|
||||
}
|
||||
|
||||
case dt_Normal: {
|
||||
Normal normal;
|
||||
|
||||
ss >> normal.x;
|
||||
ss >> normal.y;
|
||||
ss >> normal.z;
|
||||
|
||||
tempNormals.push_back(normal);
|
||||
break;
|
||||
}
|
||||
|
||||
case dt_Index: {
|
||||
|
||||
Index tempIndicies[4];
|
||||
|
||||
for (size_t i = 0; i < 4; i++){
|
||||
ss >> tempIndicies[i].vertexIndex;
|
||||
ss >> tempIndicies[i].textureIndex;
|
||||
ss >> tempIndicies[i].normalIndex;
|
||||
}
|
||||
|
||||
static const std::vector<int> vertexOrder = {0, 1, 2, 0, 2, 3};
|
||||
|
||||
for(int i = 0; i < vertexOrder.size(); i++) {
|
||||
Index& index = tempIndicies[vertexOrder[i]];
|
||||
|
||||
int vertexIndex = index.vertexIndex - 1;
|
||||
int textureIndex = index.textureIndex - 1;
|
||||
int normalIndex = index.normalIndex - 1;
|
||||
|
||||
Vertex vertex = tempVertecies[vertexIndex];
|
||||
TextureUV texture = tempTextureUvs[textureIndex];
|
||||
Normal normal = tempNormals[normalIndex];
|
||||
|
||||
vertecies.push_back(vertex.x);
|
||||
vertecies.push_back(vertex.y);
|
||||
vertecies.push_back(vertex.z);
|
||||
|
||||
textureUvs.push_back(texture.x);
|
||||
textureUvs.push_back(1.0f - texture.y);
|
||||
|
||||
normals.push_back(normal.x);
|
||||
normals.push_back(normal.y);
|
||||
normals.push_back(normal.z);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case dt_None:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
GL::VertexBuffer positionVBO(vertecies, POSITION_VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
|
||||
GL::VertexBuffer textureVBO(textureUvs, TEXTURE_VERTEX_SIZE);
|
||||
textureVBO.AddVertexAttribPointer(1, TEXTURE_VERTEX_SIZE, 0);
|
||||
|
||||
GL::VertexArray mobVao(vertecies.size() / POSITION_VERTEX_SIZE); // each pos = 1 color
|
||||
mobVao.Bind();
|
||||
mobVao.BindVertexBuffer(positionVBO);
|
||||
mobVao.BindVertexBuffer(textureVBO);
|
||||
mobVao.Unbind();
|
||||
return mobVao;
|
||||
}
|
||||
|
||||
} // namespace loader
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user