Decouple mesh from object code so we don't have to reload models constantly.

linux_template
sigonasr2 2 years ago
parent 76b7f41860
commit 89da6f65b1
  1. BIN
      Faceball2030/assets/enemies/#ShootMe.wings#
  2. 66
      Faceball2030/main.cpp

@ -30,7 +30,7 @@ struct vec3d
float w = 1; float w = 1;
}; };
struct triangle struct Triangle
{ {
vec3d p[3]; vec3d p[3];
vec2d uv[3]; vec2d uv[3];
@ -45,12 +45,19 @@ struct MapSquare {
Decal* wallW=NULL; Decal* wallW=NULL;
}; };
struct mesh struct Mesh
{ {
std::vector<triangle> tris; std::vector<Triangle> tris;
Decal* texture; Decal* texture=NULL;
float rot=0;
Mesh() {}
Mesh(std::string filename,Decal*tex)
:texture(tex){
LoadFromObjectFile(filename);
}
private:
void Parse(std::string str, int& v, int& uv) { void Parse(std::string str, int& v, int& uv) {
std::cout << str << "\n"; std::cout << str << "\n";
std::stringstream s(str.substr(0, str.find("/") + 1)); std::stringstream s(str.substr(0, str.find("/") + 1));
@ -107,6 +114,12 @@ struct mesh
}; };
struct Object {
Mesh mesh;
vf2d pos = { 0,0 };
float rot = 0;
};
struct mat4x4 struct mat4x4
{ {
float m[4][4] = { 0 }; float m[4][4] = { 0 };
@ -125,7 +138,7 @@ public:
private: private:
mesh mapMesh; Mesh mapMesh;
mat4x4 matProj; mat4x4 matProj;
vec3d vCamera = { 5,0.5,5 }; vec3d vCamera = { 5,0.5,5 };
@ -324,7 +337,7 @@ private:
} }
int Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, triangle& in_tri, triangle& out_tri1, triangle& out_tri2) int Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle& in_tri, Triangle& out_tri1, Triangle& out_tri2)
{ {
// Make sure plane normal is indeed normal // Make sure plane normal is indeed normal
plane_n = Vector_Normalise(plane_n); plane_n = Vector_Normalise(plane_n);
@ -477,17 +490,19 @@ private:
mat4x4 matCamera = Matrix_PointAt(vCamera, vTarget, vUp); mat4x4 matCamera = Matrix_PointAt(vCamera, vTarget, vUp);
mat4x4 matView = Matrix_QuickInverse(matCamera); mat4x4 matView = Matrix_QuickInverse(matCamera);
std::vector<triangle>vecTrianglesToRaster; std::vector<Triangle>vecTrianglesToRaster;
// Draw Triangles // Draw Triangles
for (auto& mesh : objects) { for (auto& obj : objects) {
for (auto& tri : mesh.tris){ for (auto& tri : obj.mesh.tris){
triangle triProjected, triTransformed, triViewed; Triangle triProjected, triTransformed, triViewed;
//mat4x4 matTrans = Matrix_MakeTranslation(-mesh.size.x/2, 0, -mesh.size.x/2); //mat4x4 matTrans = Matrix_MakeTranslation(-mesh.size.x/2, 0, -mesh.size.x/2);
mat4x4 localMat = Matrix_MakeIdentity(); mat4x4 localMat = Matrix_MakeIdentity();
//localMat = Matrix_MultiplyMatrix(localMat, matTrans); //localMat = Matrix_MultiplyMatrix(localMat, matTrans);
mat4x4 rotMat = Matrix_MakeRotationY(mesh.rot); mat4x4 rotMat = Matrix_MakeRotationY(obj.rot);
localMat = Matrix_MultiplyMatrix(localMat, rotMat); localMat = Matrix_MultiplyMatrix(localMat, rotMat);
mat4x4 matTrans = Matrix_MakeTranslation(obj.pos.x, 0, obj.pos.y);
localMat = Matrix_MultiplyMatrix(localMat, matTrans);
triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]); triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]);
triTransformed.p[1] = Matrix_MultiplyVector(localMat, tri.p[1]); triTransformed.p[1] = Matrix_MultiplyVector(localMat, tri.p[1]);
@ -521,7 +536,7 @@ private:
triViewed.col = Pixel(triTransformed.col.r * dp * dp, triTransformed.col.g * dp * dp, triTransformed.col.b * dp * dp); triViewed.col = Pixel(triTransformed.col.r * dp * dp, triTransformed.col.g * dp * dp, triTransformed.col.b * dp * dp);
int nClippedTriangles = 0; int nClippedTriangles = 0;
triangle clipped[2]; Triangle clipped[2];
nClippedTriangles = Triangle_ClipAgainstPlane({ 0.0f, 0.0f, 0.1f }, { 0.0f, 0.0f, 1.0f }, triViewed, clipped[0], clipped[1]); nClippedTriangles = Triangle_ClipAgainstPlane({ 0.0f, 0.0f, 0.1f }, { 0.0f, 0.0f, 1.0f }, triViewed, clipped[0], clipped[1]);
for (int n = 0; n < nClippedTriangles; n++) { for (int n = 0; n < nClippedTriangles; n++) {
@ -568,7 +583,7 @@ private:
triProjected.p[1].y *= 0.5f * (float)ScreenHeight(); triProjected.p[1].y *= 0.5f * (float)ScreenHeight();
triProjected.p[2].x *= 0.5f * (float)ScreenWidth(); triProjected.p[2].x *= 0.5f * (float)ScreenWidth();
triProjected.p[2].y *= 0.5f * (float)ScreenHeight(); triProjected.p[2].y *= 0.5f * (float)ScreenHeight();
triProjected.tex = mesh.texture; triProjected.tex = obj.mesh.texture;
vecTrianglesToRaster.push_back(triProjected); vecTrianglesToRaster.push_back(triProjected);
} }
@ -581,8 +596,8 @@ private:
int triRenderCount = 0; int triRenderCount = 0;
for (auto& triToRaster : vecTrianglesToRaster) { for (auto& triToRaster : vecTrianglesToRaster) {
triangle clipped[2]; Triangle clipped[2];
std::list<triangle>listTriangles; std::list<Triangle>listTriangles;
listTriangles.push_back(triToRaster); listTriangles.push_back(triToRaster);
int nNewTriangles = 1; int nNewTriangles = 1;
@ -592,7 +607,7 @@ private:
while (nNewTriangles > 0) while (nNewTriangles > 0)
{ {
// Take triangle from front of queue // Take triangle from front of queue
triangle test = listTriangles.front(); Triangle test = listTriangles.front();
listTriangles.pop_front(); listTriangles.pop_front();
nNewTriangles--; nNewTriangles--;
@ -718,7 +733,7 @@ private:
Decal* dot,*enemy_ShootMe_tex; Decal* dot,*enemy_ShootMe_tex;
vi2d MAP_SIZE = { 10,10 }; vi2d MAP_SIZE = { 10,10 };
std::vector<std::vector<MapSquare>>map; std::vector<std::vector<MapSquare>>map;
std::vector<mesh>objects; std::vector<Object>objects;
public: public:
bool OnUserCreate() override bool OnUserCreate() override
@ -728,13 +743,12 @@ public:
dot = new Decal(new Sprite("assets/dot.png")); dot = new Decal(new Sprite("assets/dot.png"));
enemy_ShootMe_tex = new Decal(new Sprite("assets/enemies/ShootMe.png")); enemy_ShootMe_tex = new Decal(new Sprite("assets/enemies/ShootMe.png"));
Mesh testEnemy("assets/enemies/ShootMe.obj", enemy_ShootMe_tex);
mapMesh.texture = dot; mapMesh.texture = dot;
for (int i = 0; i < 75; i++) {
mesh testEnemy; Object newEnemy({ testEnemy,{ float(rand() % 20),float(rand() % 20) }, (rand() % 1000) / 1000.f * 2 * PI });
testEnemy.LoadFromObjectFile("assets/enemies/ShootMe.obj"); objects.push_back(newEnemy);
testEnemy.texture = enemy_ShootMe_tex; }
objects.push_back(testEnemy);
for (int y = 0; y < MAP_SIZE.y; y++) { for (int y = 0; y < MAP_SIZE.y; y++) {
std::vector<MapSquare>row; std::vector<MapSquare>row;
@ -830,7 +844,9 @@ public:
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
objects[0].rot += PI / 8 * fElapsedTime; for (Object& o : objects) {
o.rot += PI / 8 * fElapsedTime;
}
HandleKeys(fElapsedTime); HandleKeys(fElapsedTime);
RenderWorld(); RenderWorld();
return true; return true;

Loading…
Cancel
Save