From 1e3567482a03c644cd86df93f60afb5ece5cdee7 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 6 Apr 2023 22:45:15 -0500 Subject: [PATCH] Basic Floor Generation --- Faceball2030/dot.png | Bin 0 -> 546 bytes Faceball2030/main.cpp | 125 +++++++++++++++++++++------------ Faceball2030/pixelGameEngine.h | 1 + 3 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 Faceball2030/dot.png diff --git a/Faceball2030/dot.png b/Faceball2030/dot.png new file mode 100644 index 0000000000000000000000000000000000000000..cd677ee0df12fa319dd8c03d795f7aee22e111f6 GIT binary patch literal 546 zcmV+-0^R+IP)EX>4Tx04R}tkv&MmKpe$i(@I4u4ptCxh)|s@h>AE$6^me@v=v%)FuC*#nlvOS zE{=k0!NHHks)LKOt`4q(Aou~|>f)s6A|?JWDYS_3;J6>}?mh0_0Yam~R5LIEsG4P@ z;xRFsTNQ(^2p|YQMlc{TQ%|H9Gw>W=_we!cF3PjK&;2=imAuISpGZ8%bi*RvAfDN@ zbk6(4VOEk9;&bA0gDyz?$aUG}H_ki6e@tQNECM zS>e3JS*_Gq>z@3Dp}e+|<~q$`#Ib|~k`N)IhB7L!5T#Wk#YBqsV;=q?$DbsZOs+B* zITlcb3d!+<|H1EW&BD~An-q!xT`#u%F$x5Cfo9#dzmILZc>?&Kfh(=;uQq_$Ptxmc zEpi0(Z37qAZB5<-E_Z;zCtWfmNAlAY3I*W(jJ_!c^xpzKYi@6?eVjf38R}~J1~@nb z#)_1^?(y#K&ffk#)9UXBJCSmc)nUe500009a7bBm001r{001r{0eGc9b^rhX2XskI zMF-~u2LmTB+>kDY0000PbVXQnLvL+uWo~o;Lvm$dbY)~9cWHEJAV*0}P*;Ht7XSbN k3`s;mR0!8&`2YVu00sU6{@l!2=Kufz07*qoM6N<$g7RAE#sB~S literal 0 HcmV?d00001 diff --git a/Faceball2030/main.cpp b/Faceball2030/main.cpp index c3cd689..9461d10 100644 --- a/Faceball2030/main.cpp +++ b/Faceball2030/main.cpp @@ -30,6 +30,13 @@ struct triangle Pixel col; }; +struct MapSquare { + Decal*wallN=NULL; + Decal* wallE=NULL; + Decal* wallS=NULL; + Decal* wallW=NULL; +}; + struct mesh { std::vector tris; @@ -96,23 +103,23 @@ struct mat4x4 float m[4][4] = { 0 }; }; -class olcEngine3D : public PixelGameEngine +class FaceBall : public PixelGameEngine { public: - Decal* texture; - olcEngine3D() + bool freeRoam=false; + FaceBall() { sAppName = "3D Demo"; } private: - mesh meshCube; + mesh mapMesh; mat4x4 matProj; - vec3d vCamera = { 0,0,0 }; + vec3d vCamera = { 5,5,5 }; vec3d vLookDir; float zOffset = 2; @@ -438,47 +445,14 @@ private: } } - -public: - bool OnUserCreate() override - { - texture = new Decal(new Sprite("High.png")); - meshCube.LoadFromObjectFile("Artisans Hub.obj"); - - matProj = Matrix_MakeProjection(90.0f, (float)ScreenHeight() / (float)ScreenWidth(), 0.1f, 1000.0f); - - return true; - } - - bool OnUserUpdate(float fElapsedTime) override - { - if (GetKey(olc::DOWN).bHeld) { - pitch -= 1 * fElapsedTime; - } - if (GetKey(olc::UP).bHeld) { - pitch += 1 * fElapsedTime; - } - vec3d vForward = Vector_Mul(vLookDir, 20 * fElapsedTime); - if (GetKey(olc::W).bHeld) { - vCamera = Vector_Add(vCamera, vForward); - } - if (GetKey(olc::S).bHeld) { - vCamera = Vector_Sub(vCamera, vForward); - } - if (GetKey(olc::A).bHeld) { - fYaw -= 2 * fElapsedTime; - } - if (GetKey(olc::D).bHeld) { - fYaw += 2 * fElapsedTime; - } - + void RenderWorld() { // Set up rotation matrices mat4x4 matRotZ, matRotX, matTrans, matWorld; matRotZ = Matrix_MakeRotationZ(fTheta * 0.5f); matRotX = Matrix_MakeRotationX(fTheta); - matTrans = Matrix_MakeTranslation(0.0f, 0.0f, 5.0f); + matTrans = Matrix_MakeTranslation(0.0f, 0.0f, 0.0f); matWorld = Matrix_MakeIdentity(); matWorld = Matrix_MultiplyMatrix(matRotZ, matRotX); matWorld = Matrix_MultiplyMatrix(matWorld, matTrans); @@ -494,7 +468,7 @@ public: std::vectorvecTrianglesToRaster; // Draw Triangles - for (auto& tri : meshCube.tris) + for (auto& tri : mapMesh.tris) { triangle triProjected, triTransformed, triViewed; @@ -504,6 +478,7 @@ public: triTransformed.uv[0] = tri.uv[0]; triTransformed.uv[1] = tri.uv[1]; triTransformed.uv[2] = tri.uv[2]; + triTransformed.col = tri.col; vec3d normal, line1, line2; line1 = Vector_Sub(triTransformed.p[1], triTransformed.p[0]); @@ -526,7 +501,7 @@ public: triViewed.uv[0] = triTransformed.uv[0]; triViewed.uv[1] = triTransformed.uv[1]; triViewed.uv[2] = triTransformed.uv[2]; - triViewed.col = Pixel(255 * dp * dp, 255 * dp * dp, 255 * dp * dp); + triViewed.col = Pixel(triTransformed.col.r * dp * dp, triTransformed.col.g * dp * dp, triTransformed.col.b * dp * dp); int nClippedTriangles = 0; triangle clipped[2]; @@ -628,7 +603,7 @@ public: // Rasterize triangle SetDecalStructure(DecalStructure::LIST); SetDecalMode(DecalMode::NORMAL); - DrawPolygonDecal(texture, { + DrawPolygonDecal(mapMesh.texture, { {t.p[0].x, t.p[0].y}, {t.p[1].x, t.p[1].y}, {t.p[2].x, t.p[2].y} @@ -637,7 +612,7 @@ public: {t.uv[1].u,t.uv[1].v}, {t.uv[2].u,t.uv[2].v}, }, { t.uv[0].w,t.uv[1].w,t.uv[2].w }, { t.p[0].z,t.p[1].z,t.p[2].z }, { t.col,t.col,t.col }); - /*SetDecalMode(DecalMode::WIREFRAME); + SetDecalMode(DecalMode::WIREFRAME); DrawPolygonDecal(nullptr,{ {t.p[0].x, t.p[0].y}, {t.p[1].x, t.p[1].y}, @@ -646,7 +621,7 @@ public: {0,0}, {0,0}, {0,0}, - },WHITE);*/ + },WHITE); SetDecalStructure(DecalStructure::FAN); triRenderCount++; } @@ -654,8 +629,66 @@ public: SetDecalMode(DecalMode::NORMAL); DrawStringDecal({ 0,0 }, "Triangles: " + std::to_string(triRenderCount), WHITE, { 2,2 }); + } + void HandleKeys(float fElapsedTime) { + vec3d vForward = Vector_Mul(vLookDir, 20 * fElapsedTime); + if (freeRoam) { + if (GetKey(olc::DOWN).bHeld) { + pitch -= 1 * fElapsedTime; + } + if (GetKey(olc::UP).bHeld) { + pitch += 1 * fElapsedTime; + } + } + else { + pitch = 0; + } + if (GetKey(olc::W).bHeld) { + vCamera = Vector_Add(vCamera, vForward); + } + if (GetKey(olc::S).bHeld) { + vCamera = Vector_Sub(vCamera, vForward); + } + if (GetKey(olc::A).bHeld) { + fYaw -= 2 * fElapsedTime; + } + if (GetKey(olc::D).bHeld) { + fYaw += 2 * fElapsedTime; + } + if (GetKey(olc::F1).bPressed) { + freeRoam = !freeRoam; + } + } + + vi2d MAP_SIZE = { 10,2 }; + std::vector>map; + +public: + bool OnUserCreate() override + { + matProj = Matrix_MakeProjection(90.0f, (float)ScreenHeight() / (float)ScreenWidth(), 0.1f, 1000.0f); + Decal* dot = new Decal(new Sprite("dot.png")); + + mapMesh.texture = dot; + for (int i = 0; i < MAP_SIZE.y; i++) { + std::vectorrow; + for (int j = 0; j < MAP_SIZE.x; j++) { + row.push_back({}); + mapMesh.tris.push_back({ {{(float)j * 10,0,(float)i * 10},{(float)j * 10,0,(float)i * 10 + 10},{(float)j * 10 + 10,0,(float)i * 10}},{{0,0},{1,0},{0,1}},Pixel{uint8_t(rand() * 255),uint8_t(rand() * 255),uint8_t(rand() * 255)} }); + mapMesh.tris.push_back({ {{(float)j * 10+10,0,(float)i * 10},{(float)j * 10,0,(float)i * 10 + 10},{(float)j * 10 + 10,0,(float)i * 10+10}},{{0,0},{1,0},{0,1}},Pixel{uint8_t(rand() * 255),uint8_t(rand() * 255),uint8_t(rand() * 255)} }); + } + map.push_back(row); + } + + return true; + } + + bool OnUserUpdate(float fElapsedTime) override + { + HandleKeys(fElapsedTime); + RenderWorld(); return true; } @@ -666,7 +699,7 @@ public: int main() { - olcEngine3D demo; + FaceBall demo; if (demo.Construct(1280, 720, 1, 1)) demo.Start(); return 0; diff --git a/Faceball2030/pixelGameEngine.h b/Faceball2030/pixelGameEngine.h index 89bd0bf..f19c135 100644 --- a/Faceball2030/pixelGameEngine.h +++ b/Faceball2030/pixelGameEngine.h @@ -2721,6 +2721,7 @@ namespace olc di.pos.resize(di.points); di.uv.resize(di.points); di.w.resize(di.points); + di.z.resize(di.points); di.tint.resize(di.points); for (uint32_t i = 0; i < di.points; i++) {