Added more structure, added abstraction to existing parts
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class Controller
|
class Controller
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -151,6 +151,7 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Controller.h" />
|
<ClInclude Include="Controller.h" />
|
||||||
|
<ClInclude Include="Enemy.h" />
|
||||||
<ClInclude Include="Header.h" />
|
<ClInclude Include="Header.h" />
|
||||||
<ClInclude Include="Keyboard.h" />
|
<ClInclude Include="Keyboard.h" />
|
||||||
<ClInclude Include="LoadingState.h" />
|
<ClInclude Include="LoadingState.h" />
|
||||||
@@ -158,13 +159,17 @@
|
|||||||
<ClInclude Include="Model.h" />
|
<ClInclude Include="Model.h" />
|
||||||
<ClInclude Include="ModelLoader.h" />
|
<ClInclude Include="ModelLoader.h" />
|
||||||
<ClInclude Include="Mouse.h" />
|
<ClInclude Include="Mouse.h" />
|
||||||
|
<ClInclude Include="Player.h" />
|
||||||
<ClInclude Include="State.h" />
|
<ClInclude Include="State.h" />
|
||||||
<ClInclude Include="StateHandler.h" />
|
<ClInclude Include="StateHandler.h" />
|
||||||
|
<ClInclude Include="Vector.h" />
|
||||||
|
<ClInclude Include="Weapon.h" />
|
||||||
<ClInclude Include="WorldModel.h" />
|
<ClInclude Include="WorldModel.h" />
|
||||||
<ClInclude Include="WorldState.h" />
|
<ClInclude Include="WorldState.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Controller.cpp" />
|
<ClCompile Include="Controller.cpp" />
|
||||||
|
<ClCompile Include="Enemy.cpp" />
|
||||||
<ClCompile Include="Keyboard.cpp" />
|
<ClCompile Include="Keyboard.cpp" />
|
||||||
<ClCompile Include="LoadingState.cpp" />
|
<ClCompile Include="LoadingState.cpp" />
|
||||||
<ClCompile Include="Main.cpp" />
|
<ClCompile Include="Main.cpp" />
|
||||||
@@ -172,8 +177,11 @@
|
|||||||
<ClCompile Include="Model.cpp" />
|
<ClCompile Include="Model.cpp" />
|
||||||
<ClCompile Include="ModelLoader.cpp" />
|
<ClCompile Include="ModelLoader.cpp" />
|
||||||
<ClCompile Include="Mouse.cpp" />
|
<ClCompile Include="Mouse.cpp" />
|
||||||
|
<ClCompile Include="Player.cpp" />
|
||||||
<ClCompile Include="State.cpp" />
|
<ClCompile Include="State.cpp" />
|
||||||
<ClCompile Include="StateHandler.cpp" />
|
<ClCompile Include="StateHandler.cpp" />
|
||||||
|
<ClCompile Include="Vector.cpp" />
|
||||||
|
<ClCompile Include="Weapon.cpp" />
|
||||||
<ClCompile Include="WorldModel.cpp" />
|
<ClCompile Include="WorldModel.cpp" />
|
||||||
<ClCompile Include="WorldState.cpp" />
|
<ClCompile Include="WorldState.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -22,6 +22,9 @@
|
|||||||
<Filter Include="Source Files\Model">
|
<Filter Include="Source Files\Model">
|
||||||
<UniqueIdentifier>{842778c9-ff0f-4a6d-9c86-0a178ad40fcc}</UniqueIdentifier>
|
<UniqueIdentifier>{842778c9-ff0f-4a6d-9c86-0a178ad40fcc}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Source Files\Model\Enemy">
|
||||||
|
<UniqueIdentifier>{1be14b84-6fa7-4cfb-94b9-666f3ee14198}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Model.h">
|
<ClInclude Include="Model.h">
|
||||||
@@ -60,6 +63,18 @@
|
|||||||
<ClInclude Include="WorldState.h">
|
<ClInclude Include="WorldState.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Enemy.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Player.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Weapon.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Vector.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Main.cpp">
|
<ClCompile Include="Main.cpp">
|
||||||
@@ -98,5 +113,17 @@
|
|||||||
<ClCompile Include="WorldState.cpp">
|
<ClCompile Include="WorldState.cpp">
|
||||||
<Filter>Source Files\State</Filter>
|
<Filter>Source Files\State</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Enemy.cpp">
|
||||||
|
<Filter>Source Files\Model\Enemy</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Player.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Weapon.cpp">
|
||||||
|
<Filter>Source Files\Model</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Vector.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
|
class Enemy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Enemy();
|
||||||
|
~Enemy();
|
||||||
|
};
|
||||||
|
|
||||||
@@ -12,10 +12,9 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <gl/GL.h>
|
#include <gl/GL.h>
|
||||||
|
|
||||||
//Own files
|
using namespace std;
|
||||||
#include "Model.h"
|
|
||||||
#include "ModelLoader.h"
|
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class Keyboard
|
class Keyboard
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class LoadingState
|
class LoadingState
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "Header.h"
|
#include "Header.h"
|
||||||
|
#include "Model.h"
|
||||||
|
|
||||||
//Prototypes
|
//Prototypes
|
||||||
void bindFuncOpenGL(void);
|
void bindFuncOpenGL(void);
|
||||||
@@ -11,7 +12,7 @@ static int Height;
|
|||||||
float lastFrameTime = 0;
|
float lastFrameTime = 0;
|
||||||
bool keys[255];
|
bool keys[255];
|
||||||
|
|
||||||
vector<ObjModel*> models;
|
vector<Model*> models;
|
||||||
int currentModel = 0;
|
int currentModel = 0;
|
||||||
|
|
||||||
struct Camera
|
struct Camera
|
||||||
@@ -169,5 +170,5 @@ void configureOpenGL()
|
|||||||
|
|
||||||
void loadModels()
|
void loadModels()
|
||||||
{
|
{
|
||||||
models.push_back(new ObjModel("models/ship/shipA_OBJ.obj"));
|
models.push_back(new Model("models/ship/shipA_OBJ.obj"));
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class MenuState
|
class MenuState
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,10 +1,236 @@
|
|||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
|
|
||||||
Model::Model()
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
|
Model::Model(string fileName)
|
||||||
|
{
|
||||||
|
std::string dirName = fileName;
|
||||||
|
if (dirName.rfind("/") != std::string::npos)
|
||||||
|
dirName = dirName.substr(0, dirName.rfind("/"));
|
||||||
|
if (dirName.rfind("\\") != std::string::npos)
|
||||||
|
dirName = dirName.substr(0, dirName.rfind("\\"));
|
||||||
|
if (fileName == dirName)
|
||||||
|
dirName = "";
|
||||||
|
|
||||||
|
|
||||||
|
std::ifstream pFile(fileName.c_str());
|
||||||
|
|
||||||
|
if (!pFile.is_open())
|
||||||
|
{
|
||||||
|
std::cout << "Could not open file " << fileName << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ObjGroup* currentGroup = new ObjGroup();
|
||||||
|
currentGroup->materialIndex = -1;
|
||||||
|
|
||||||
|
|
||||||
|
while (!pFile.eof())
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
std::getline(pFile, line);
|
||||||
|
|
||||||
|
line = replace(line, "\t", " ");
|
||||||
|
while (line.find(" ") != std::string::npos)
|
||||||
|
line = replace(line, " ", " ");
|
||||||
|
if (line == "")
|
||||||
|
continue;
|
||||||
|
if (line[0] == ' ')
|
||||||
|
line = line.substr(1);
|
||||||
|
if (line == "")
|
||||||
|
continue;
|
||||||
|
if (line[line.length() - 1] == ' ')
|
||||||
|
line = line.substr(0, line.length() - 1);
|
||||||
|
if (line == "")
|
||||||
|
continue;
|
||||||
|
if (line[0] == '#')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::vector<std::string> params = split(line, " ");
|
||||||
|
params[0] = toLower(params[0]);
|
||||||
|
|
||||||
|
if (params[0] == "v")
|
||||||
|
vertices.push_back(Vec3f((float)atof(params[1].c_str()), (float)atof(params[2].c_str()), (float)atof(params[3].c_str())));
|
||||||
|
else if (params[0] == "vn")
|
||||||
|
normals.push_back(Vec3f((float)atof(params[1].c_str()), (float)atof(params[2].c_str()), (float)atof(params[3].c_str())));
|
||||||
|
else if (params[0] == "vt")
|
||||||
|
texcoords.push_back(Vec2f((float)atof(params[1].c_str()), (float)atof(params[2].c_str())));
|
||||||
|
else if (params[0] == "f")
|
||||||
|
{
|
||||||
|
for (size_t ii = 4; ii <= params.size(); ii++)
|
||||||
|
{
|
||||||
|
Face face;
|
||||||
|
|
||||||
|
for (size_t i = ii - 3; i < ii; i++) //magische forlus om van quads triangles te maken ;)
|
||||||
|
{
|
||||||
|
Vertex vertex;
|
||||||
|
std::vector<std::string> indices = split(params[i == (ii - 3) ? 1 : i], "/");
|
||||||
|
if (indices.size() >= 1) //er is een positie
|
||||||
|
vertex.position = atoi(indices[0].c_str()) - 1;
|
||||||
|
if (indices.size() == 2) //alleen texture
|
||||||
|
vertex.texcoord = atoi(indices[1].c_str()) - 1;
|
||||||
|
if (indices.size() == 3) //v/t/n of v//n
|
||||||
|
{
|
||||||
|
if (indices[1] != "")
|
||||||
|
vertex.texcoord = atoi(indices[1].c_str()) - 1;
|
||||||
|
vertex.normal = atoi(indices[2].c_str()) - 1;
|
||||||
|
}
|
||||||
|
face.vertices.push_back(vertex);
|
||||||
|
}
|
||||||
|
currentGroup->faces.push_back(face);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (params[0] == "s")
|
||||||
|
{//smoothing
|
||||||
|
}
|
||||||
|
else if (params[0] == "mtllib")
|
||||||
|
{
|
||||||
|
loadMaterialFile(dirName + "/" + params[1], dirName);
|
||||||
|
}
|
||||||
|
else if (params[0] == "usemtl")
|
||||||
|
{
|
||||||
|
if (currentGroup->faces.size() != 0)
|
||||||
|
groups.push_back(currentGroup);
|
||||||
|
currentGroup = new ObjGroup();
|
||||||
|
currentGroup->materialIndex = -1;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < materials.size(); i++)
|
||||||
|
{
|
||||||
|
MaterialInfo* info = materials[i];
|
||||||
|
if (info->name == params[1])
|
||||||
|
{
|
||||||
|
currentGroup->materialIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentGroup->materialIndex == -1)
|
||||||
|
std::cout << "Could not find material name " << params[1] << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
groups.push_back(currentGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Model::~Model(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Model::~Model()
|
|
||||||
|
|
||||||
|
void Model::draw()
|
||||||
{
|
{
|
||||||
|
for (auto &g : groups)
|
||||||
|
{
|
||||||
|
if (materials[g->materialIndex]->hasTexture)
|
||||||
|
{
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
materials[g->materialIndex]->texture->bind();
|
||||||
|
}
|
||||||
|
|
||||||
|
glBegin(GL_TRIANGLES);
|
||||||
|
for (auto &f : g->faces)
|
||||||
|
{
|
||||||
|
for (auto &v : f.vertices)
|
||||||
|
{
|
||||||
|
glNormal3f(normals[v.normal].x, normals[v.normal].y, normals[v.normal].z);
|
||||||
|
glTexCoord2f(texcoords[v.texcoord].x, texcoords[v.texcoord].y);
|
||||||
|
glVertex3f(vertices[v.position].x, vertices[v.position].y, vertices[v.position].z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
glEnd();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Model::loadMaterialFile(std::string fileName, std::string dirName)
|
||||||
|
{
|
||||||
|
std::ifstream pFile(fileName.c_str());
|
||||||
|
if (!pFile.is_open())
|
||||||
|
{
|
||||||
|
std::cout << "Could not open file " << fileName << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MaterialInfo* currentMaterial = NULL;
|
||||||
|
|
||||||
|
while (!pFile.eof())
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
std::getline(pFile, line);
|
||||||
|
|
||||||
|
line = replace(line, "\t", " ");
|
||||||
|
while (line.find(" ") != std::string::npos)
|
||||||
|
line = replace(line, " ", " ");
|
||||||
|
if (line == "")
|
||||||
|
continue;
|
||||||
|
if (line[0] == ' ')
|
||||||
|
line = line.substr(1);
|
||||||
|
if (line == "")
|
||||||
|
continue;
|
||||||
|
if (line[line.length() - 1] == ' ')
|
||||||
|
line = line.substr(0, line.length() - 1);
|
||||||
|
if (line == "")
|
||||||
|
continue;
|
||||||
|
if (line[0] == '#')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::vector<std::string> params = split(line, " ");
|
||||||
|
params[0] = toLower(params[0]);
|
||||||
|
|
||||||
|
if (params[0] == "newmtl")
|
||||||
|
{
|
||||||
|
if (currentMaterial != NULL)
|
||||||
|
{
|
||||||
|
materials.push_back(currentMaterial);
|
||||||
|
}
|
||||||
|
currentMaterial = new MaterialInfo();
|
||||||
|
currentMaterial->name = params[1];
|
||||||
|
}
|
||||||
|
else if (params[0] == "map_kd")
|
||||||
|
{
|
||||||
|
currentMaterial->hasTexture = true;
|
||||||
|
currentMaterial->texture = new Texture(dirName + "/" + params[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cout << "Didn't parse " << params[0] << " in material file" << std::endl;
|
||||||
|
}
|
||||||
|
if (currentMaterial != NULL)
|
||||||
|
materials.push_back(currentMaterial);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Model::MaterialInfo::MaterialInfo()
|
||||||
|
{
|
||||||
|
Texture *texture;
|
||||||
|
hasTexture = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Model::Texture::Texture(const std::string & fileName)
|
||||||
|
{
|
||||||
|
int width, height, bpp;
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &bpp, 4);
|
||||||
|
|
||||||
|
glGenTextures(1, &index);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, index);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D,
|
||||||
|
0, //level
|
||||||
|
GL_RGBA, //internal format
|
||||||
|
width, //width
|
||||||
|
height, //height
|
||||||
|
0, //border
|
||||||
|
GL_RGBA, //data format
|
||||||
|
GL_UNSIGNED_BYTE, //data type
|
||||||
|
imgData); //data
|
||||||
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
stbi_image_free(imgData);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Model::Texture::bind()
|
||||||
|
{
|
||||||
|
glBindTexture(GL_TEXTURE_2D, index);
|
||||||
|
}
|
||||||
@@ -1,8 +1,60 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
#include "ModelLoader.h"
|
||||||
|
#include "Vector.h"
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
public:
|
private:
|
||||||
Model();
|
class Vertex
|
||||||
~Model();
|
{
|
||||||
};
|
public:
|
||||||
|
int position;
|
||||||
|
int normal;
|
||||||
|
int texcoord;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Face
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
list<Vertex> vertices;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Texture
|
||||||
|
{
|
||||||
|
GLuint index;
|
||||||
|
public:
|
||||||
|
Texture(const std::string &fileName);
|
||||||
|
void bind();
|
||||||
|
};
|
||||||
|
|
||||||
|
class MaterialInfo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MaterialInfo();
|
||||||
|
std::string name;
|
||||||
|
Texture* texture;
|
||||||
|
bool hasTexture;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ObjGroup
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
int materialIndex;
|
||||||
|
list<Face> faces;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<Vec3f> vertices;
|
||||||
|
std::vector<Vec3f> normals;
|
||||||
|
std::vector<Vec2f> texcoords;
|
||||||
|
std::vector<ObjGroup*> groups;
|
||||||
|
std::vector<MaterialInfo*> materials;
|
||||||
|
|
||||||
|
void loadMaterialFile(std::string fileName, std::string dirName);
|
||||||
|
public:
|
||||||
|
Model(std::string filename);
|
||||||
|
~Model(void);
|
||||||
|
|
||||||
|
void draw();
|
||||||
|
};
|
||||||
|
|||||||
+2
-303
@@ -1,19 +1,7 @@
|
|||||||
#include "Header.h"
|
#include "Header.h"
|
||||||
#include "ModelLoader.h"
|
#include "ModelLoader.h"
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
string replace(string str, string toReplace, string replacement)
|
||||||
#include "stb_image.h"
|
|
||||||
|
|
||||||
ModelLoader::ModelLoader()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ModelLoader::~ModelLoader()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string replace(std::string str, std::string toReplace, std::string replacement)
|
|
||||||
{
|
{
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
while (true)
|
while (true)
|
||||||
@@ -27,7 +15,7 @@ std::string replace(std::string str, std::string toReplace, std::string replacem
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> split(std::string str, std::string sep)
|
vector<string> split(string str, string sep)
|
||||||
{
|
{
|
||||||
std::vector<std::string> ret;
|
std::vector<std::string> ret;
|
||||||
size_t index;
|
size_t index;
|
||||||
@@ -41,293 +29,4 @@ std::vector<std::string> split(std::string str, std::string sep)
|
|||||||
}
|
}
|
||||||
ret.push_back(str);
|
ret.push_back(str);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
|
||||||
|
|
||||||
inline std::string toLower(std::string data)
|
|
||||||
{
|
|
||||||
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ObjModel::ObjModel(std::string fileName)
|
|
||||||
{
|
|
||||||
std::string dirName = fileName;
|
|
||||||
if (dirName.rfind("/") != std::string::npos)
|
|
||||||
dirName = dirName.substr(0, dirName.rfind("/"));
|
|
||||||
if (dirName.rfind("\\") != std::string::npos)
|
|
||||||
dirName = dirName.substr(0, dirName.rfind("\\"));
|
|
||||||
if (fileName == dirName)
|
|
||||||
dirName = "";
|
|
||||||
|
|
||||||
|
|
||||||
std::ifstream pFile(fileName.c_str());
|
|
||||||
|
|
||||||
if (!pFile.is_open())
|
|
||||||
{
|
|
||||||
std::cout << "Could not open file " << fileName << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ObjGroup* currentGroup = new ObjGroup();
|
|
||||||
currentGroup->materialIndex = -1;
|
|
||||||
|
|
||||||
|
|
||||||
while (!pFile.eof())
|
|
||||||
{
|
|
||||||
std::string line;
|
|
||||||
std::getline(pFile, line);
|
|
||||||
|
|
||||||
line = replace(line, "\t", " ");
|
|
||||||
while (line.find(" ") != std::string::npos)
|
|
||||||
line = replace(line, " ", " ");
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
if (line[0] == ' ')
|
|
||||||
line = line.substr(1);
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
if (line[line.length() - 1] == ' ')
|
|
||||||
line = line.substr(0, line.length() - 1);
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
if (line[0] == '#')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
std::vector<std::string> params = split(line, " ");
|
|
||||||
params[0] = toLower(params[0]);
|
|
||||||
|
|
||||||
if (params[0] == "v")
|
|
||||||
vertices.push_back(Vec3f((float)atof(params[1].c_str()), (float)atof(params[2].c_str()), (float)atof(params[3].c_str())));
|
|
||||||
else if (params[0] == "vn")
|
|
||||||
normals.push_back(Vec3f((float)atof(params[1].c_str()), (float)atof(params[2].c_str()), (float)atof(params[3].c_str())));
|
|
||||||
else if (params[0] == "vt")
|
|
||||||
texcoords.push_back(Vec2f((float)atof(params[1].c_str()), (float)atof(params[2].c_str())));
|
|
||||||
else if (params[0] == "f")
|
|
||||||
{
|
|
||||||
for (size_t ii = 4; ii <= params.size(); ii++)
|
|
||||||
{
|
|
||||||
Face face;
|
|
||||||
|
|
||||||
for (size_t i = ii - 3; i < ii; i++) //magische forlus om van quads triangles te maken ;)
|
|
||||||
{
|
|
||||||
Vertex vertex;
|
|
||||||
std::vector<std::string> indices = split(params[i == (ii - 3) ? 1 : i], "/");
|
|
||||||
if (indices.size() >= 1) //er is een positie
|
|
||||||
vertex.position = atoi(indices[0].c_str()) - 1;
|
|
||||||
if (indices.size() == 2) //alleen texture
|
|
||||||
vertex.texcoord = atoi(indices[1].c_str()) - 1;
|
|
||||||
if (indices.size() == 3) //v/t/n of v//n
|
|
||||||
{
|
|
||||||
if (indices[1] != "")
|
|
||||||
vertex.texcoord = atoi(indices[1].c_str()) - 1;
|
|
||||||
vertex.normal = atoi(indices[2].c_str()) - 1;
|
|
||||||
}
|
|
||||||
face.vertices.push_back(vertex);
|
|
||||||
}
|
|
||||||
currentGroup->faces.push_back(face);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (params[0] == "s")
|
|
||||||
{//smoothing
|
|
||||||
}
|
|
||||||
else if (params[0] == "mtllib")
|
|
||||||
{
|
|
||||||
loadMaterialFile(dirName + "/" + params[1], dirName);
|
|
||||||
}
|
|
||||||
else if (params[0] == "usemtl")
|
|
||||||
{
|
|
||||||
if (currentGroup->faces.size() != 0)
|
|
||||||
groups.push_back(currentGroup);
|
|
||||||
currentGroup = new ObjGroup();
|
|
||||||
currentGroup->materialIndex = -1;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < materials.size(); i++)
|
|
||||||
{
|
|
||||||
MaterialInfo* info = materials[i];
|
|
||||||
if (info->name == params[1])
|
|
||||||
{
|
|
||||||
currentGroup->materialIndex = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (currentGroup->materialIndex == -1)
|
|
||||||
std::cout << "Could not find material name " << params[1] << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
groups.push_back(currentGroup);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ObjModel::~ObjModel(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ObjModel::draw()
|
|
||||||
{
|
|
||||||
for (auto &g : groups)
|
|
||||||
{
|
|
||||||
if (materials[g->materialIndex]->hasTexture)
|
|
||||||
{
|
|
||||||
glEnable(GL_TEXTURE_2D);
|
|
||||||
materials[g->materialIndex]->texture->bind();
|
|
||||||
}
|
|
||||||
|
|
||||||
glBegin(GL_TRIANGLES);
|
|
||||||
for (auto &f : g->faces)
|
|
||||||
{
|
|
||||||
for (auto &v : f.vertices)
|
|
||||||
{
|
|
||||||
glNormal3f(normals[v.normal].x, normals[v.normal].y, normals[v.normal].z);
|
|
||||||
glTexCoord2f(texcoords[v.texcoord].x, texcoords[v.texcoord].y);
|
|
||||||
glVertex3f(vertices[v.position].x, vertices[v.position].y, vertices[v.position].z);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
glEnd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjModel::loadMaterialFile(std::string fileName, std::string dirName)
|
|
||||||
{
|
|
||||||
std::ifstream pFile(fileName.c_str());
|
|
||||||
if (!pFile.is_open())
|
|
||||||
{
|
|
||||||
std::cout << "Could not open file " << fileName << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MaterialInfo* currentMaterial = NULL;
|
|
||||||
|
|
||||||
while (!pFile.eof())
|
|
||||||
{
|
|
||||||
std::string line;
|
|
||||||
std::getline(pFile, line);
|
|
||||||
|
|
||||||
line = replace(line, "\t", " ");
|
|
||||||
while (line.find(" ") != std::string::npos)
|
|
||||||
line = replace(line, " ", " ");
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
if (line[0] == ' ')
|
|
||||||
line = line.substr(1);
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
if (line[line.length() - 1] == ' ')
|
|
||||||
line = line.substr(0, line.length() - 1);
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
if (line[0] == '#')
|
|
||||||
continue;
|
|
||||||
|
|
||||||
std::vector<std::string> params = split(line, " ");
|
|
||||||
params[0] = toLower(params[0]);
|
|
||||||
|
|
||||||
if (params[0] == "newmtl")
|
|
||||||
{
|
|
||||||
if (currentMaterial != NULL)
|
|
||||||
{
|
|
||||||
materials.push_back(currentMaterial);
|
|
||||||
}
|
|
||||||
currentMaterial = new MaterialInfo();
|
|
||||||
currentMaterial->name = params[1];
|
|
||||||
}
|
|
||||||
else if (params[0] == "map_kd")
|
|
||||||
{
|
|
||||||
currentMaterial->hasTexture = true;
|
|
||||||
currentMaterial->texture = new Texture(dirName + "/" + params[1]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
std::cout << "Didn't parse " << params[0] << " in material file" << std::endl;
|
|
||||||
}
|
|
||||||
if (currentMaterial != NULL)
|
|
||||||
materials.push_back(currentMaterial);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjModel::MaterialInfo::MaterialInfo()
|
|
||||||
{
|
|
||||||
Texture *texture;
|
|
||||||
hasTexture = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Vec3f::Vec3f(float x, float y, float z)
|
|
||||||
{
|
|
||||||
this->x = x;
|
|
||||||
this->y = y;
|
|
||||||
this->z = z;
|
|
||||||
}
|
|
||||||
Vec3f::Vec3f()
|
|
||||||
{
|
|
||||||
this->x = 0;
|
|
||||||
this->y = 0;
|
|
||||||
this->z = 0;
|
|
||||||
}
|
|
||||||
Vec3f::Vec3f(Vec3f &other)
|
|
||||||
{
|
|
||||||
this->x = other.x;
|
|
||||||
this->y = other.y;
|
|
||||||
this->z = other.z;
|
|
||||||
}
|
|
||||||
|
|
||||||
float& Vec3f::operator [](int index)
|
|
||||||
{
|
|
||||||
return v[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Vec2f::Vec2f(float x, float y)
|
|
||||||
{
|
|
||||||
this->x = x;
|
|
||||||
this->y = y;
|
|
||||||
}
|
|
||||||
Vec2f::Vec2f()
|
|
||||||
{
|
|
||||||
this->x = 0;
|
|
||||||
this->y = 0;
|
|
||||||
}
|
|
||||||
Vec2f::Vec2f(Vec2f &other)
|
|
||||||
{
|
|
||||||
this->x = other.x;
|
|
||||||
this->y = other.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
float& Vec2f::operator [](int index)
|
|
||||||
{
|
|
||||||
return v[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjModel::Texture::Texture(const std::string & fileName)
|
|
||||||
{
|
|
||||||
int width, height, bpp;
|
|
||||||
stbi_set_flip_vertically_on_load(true);
|
|
||||||
unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &bpp, 4);
|
|
||||||
|
|
||||||
glGenTextures(1, &index);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, index);
|
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D,
|
|
||||||
0, //level
|
|
||||||
GL_RGBA, //internal format
|
|
||||||
width, //width
|
|
||||||
height, //height
|
|
||||||
0, //border
|
|
||||||
GL_RGBA, //data format
|
|
||||||
GL_UNSIGNED_BYTE, //data type
|
|
||||||
imgData); //data
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
stbi_image_free(imgData);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjModel::Texture::bind()
|
|
||||||
{
|
|
||||||
glBindTexture(GL_TEXTURE_2D, index);
|
|
||||||
}
|
}
|
||||||
+8
-99
@@ -1,101 +1,10 @@
|
|||||||
#include "Header.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
class ModelLoader
|
#include "Header.h"
|
||||||
|
|
||||||
|
string replace(string str, string toReplace, string replacement);
|
||||||
|
vector<string> split(string str, string sep);
|
||||||
|
inline string toLower(string data)
|
||||||
{
|
{
|
||||||
public:
|
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
|
||||||
ModelLoader();
|
return data;
|
||||||
~ModelLoader();
|
}
|
||||||
};
|
|
||||||
|
|
||||||
class Vec3f
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
union
|
|
||||||
{
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
float x, y, z;
|
|
||||||
};
|
|
||||||
float v[3];
|
|
||||||
};
|
|
||||||
Vec3f();
|
|
||||||
Vec3f(Vec3f &other);
|
|
||||||
Vec3f(float x, float y, float z);
|
|
||||||
float& operator [](int);
|
|
||||||
};
|
|
||||||
|
|
||||||
class Vec2f
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
union
|
|
||||||
{
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
float x, y;
|
|
||||||
};
|
|
||||||
float v[2];
|
|
||||||
};
|
|
||||||
Vec2f();
|
|
||||||
Vec2f(float x, float y);
|
|
||||||
Vec2f(Vec2f &other);
|
|
||||||
float& operator [](int);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class ObjModel
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
class Vertex
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
int position;
|
|
||||||
int normal;
|
|
||||||
int texcoord;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Face
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
list<Vertex> vertices;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Texture
|
|
||||||
{
|
|
||||||
GLuint index;
|
|
||||||
public:
|
|
||||||
Texture(const std::string &fileName);
|
|
||||||
void bind();
|
|
||||||
};
|
|
||||||
|
|
||||||
class MaterialInfo
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
MaterialInfo();
|
|
||||||
std::string name;
|
|
||||||
Texture* texture;
|
|
||||||
bool hasTexture;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ObjGroup
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
std::string name;
|
|
||||||
int materialIndex;
|
|
||||||
list<Face> faces;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<Vec3f> vertices;
|
|
||||||
std::vector<Vec3f> normals;
|
|
||||||
std::vector<Vec2f> texcoords;
|
|
||||||
std::vector<ObjGroup*> groups;
|
|
||||||
std::vector<MaterialInfo*> materials;
|
|
||||||
|
|
||||||
void loadMaterialFile(std::string fileName, std::string dirName);
|
|
||||||
public:
|
|
||||||
ObjModel(std::string filename);
|
|
||||||
~ObjModel(void);
|
|
||||||
|
|
||||||
void draw();
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class Mouse
|
class Mouse
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
#include "Player.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Player::Player()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Player::~Player()
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
#include "Weapon.h"
|
||||||
|
|
||||||
|
class Player
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Player();
|
||||||
|
~Player();
|
||||||
|
private:
|
||||||
|
int level;
|
||||||
|
int xp;
|
||||||
|
Weapon rigth;
|
||||||
|
Weapon left;
|
||||||
|
vector<Weapon> weapons;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class State
|
class State
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class StateHandler
|
class StateHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
#include "Vector.h"
|
||||||
|
|
||||||
|
Vec3f::Vec3f(float x, float y, float z)
|
||||||
|
{
|
||||||
|
this->x = x;
|
||||||
|
this->y = y;
|
||||||
|
this->z = z;
|
||||||
|
}
|
||||||
|
Vec3f::Vec3f()
|
||||||
|
{
|
||||||
|
this->x = 0;
|
||||||
|
this->y = 0;
|
||||||
|
this->z = 0;
|
||||||
|
}
|
||||||
|
Vec3f::Vec3f(Vec3f &other)
|
||||||
|
{
|
||||||
|
this->x = other.x;
|
||||||
|
this->y = other.y;
|
||||||
|
this->z = other.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
float& Vec3f::operator [](int index)
|
||||||
|
{
|
||||||
|
return v[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Vec2f::Vec2f(float x, float y)
|
||||||
|
{
|
||||||
|
this->x = x;
|
||||||
|
this->y = y;
|
||||||
|
}
|
||||||
|
Vec2f::Vec2f()
|
||||||
|
{
|
||||||
|
this->x = 0;
|
||||||
|
this->y = 0;
|
||||||
|
}
|
||||||
|
Vec2f::Vec2f(Vec2f &other)
|
||||||
|
{
|
||||||
|
this->x = other.x;
|
||||||
|
this->y = other.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
float& Vec2f::operator [](int index)
|
||||||
|
{
|
||||||
|
return v[index];
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
|
class Vec3f
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
union
|
||||||
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float x, y, z;
|
||||||
|
};
|
||||||
|
float v[3];
|
||||||
|
};
|
||||||
|
Vec3f();
|
||||||
|
Vec3f(Vec3f &other);
|
||||||
|
Vec3f(float x, float y, float z);
|
||||||
|
float& operator [](int);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Vec2f
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
union
|
||||||
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
float x, y;
|
||||||
|
};
|
||||||
|
float v[2];
|
||||||
|
};
|
||||||
|
Vec2f();
|
||||||
|
Vec2f(float x, float y);
|
||||||
|
Vec2f(Vec2f &other);
|
||||||
|
float& operator [](int);
|
||||||
|
};
|
||||||
|
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
#include "Weapon.h"
|
||||||
|
|
||||||
|
Weapon::Weapon()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Weapon::~Weapon()
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
|
class Weapon
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Weapon();
|
||||||
|
~Weapon();
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class WorldModel
|
class WorldModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Header.h"
|
||||||
|
|
||||||
class WorldState
|
class WorldState
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user