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

Loading…
Cancel
Save