pctm + foundation for obj models

This commit is contained in:
2026-02-22 14:32:52 +01:00
parent 7ca48a5080
commit c051008287
16 changed files with 279 additions and 185 deletions
+41
View File
@@ -0,0 +1,41 @@
#ifndef DELTA_ASSETLOADER
#define DELTA_ASSETLOADER
#include <SDL3/SDL.h>
#include <vector>
#include <unordered_map>
#include <map>
#include <string>
#include "math.hpp"
struct SDL_SurfaceDeleter {
void operator()(SDL_Surface* surface) const {
if (surface) {
SDL_DestroySurface(surface);
}
}
};
class DeltaAssetLoader {
public:
~DeltaAssetLoader() {
for (auto& [id, surface] : textures) {
SDL_DestroySurface(surface);
}
}
void loadTexture(std::string id, const char *path);
void loadModel(std::string id, const char *path);
SDL_Surface* getSurface(const std::string& id);
void getModel(std::string id, const char *path);
private:
std::string base_path = "assets";
std::string img_path = "images";
std::string model_path = "models";
std::unordered_map<std::string, SDL_Surface*> textures;
std::unordered_map<std::string, DeltaModel> models;
};
#endif
+2 -2
View File
@@ -1,8 +1,8 @@
#ifndef DELTA_GLOBALS
#define DELTA_GLOBALS
const int SIMULATED_WIDTH = 800;
const int SIMULATED_HEIGHT = 600;
const int SIMULATED_WIDTH = 1200;
const int SIMULATED_HEIGHT = 800;
const int FOCAL = 300;
#endif
+26 -7
View File
@@ -1,6 +1,8 @@
#ifndef DELTA_MATH
#define DELTA_MATH
#include <vector>
class DeltaVector2 {
public:
float x, y;
@@ -35,9 +37,6 @@ class DeltaVector3 {
float length() const;
};
float getDeterminant(const DeltaVector2 &pointA, const DeltaVector2 &pointB, const DeltaVector2 &candidate);
DeltaVector2 barycentricToScreen(const DeltaVector3 &baryPoint, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c);
DeltaVector3 intersect(DeltaVector3 a, DeltaVector3 b, float nearZ);
struct DeltaTriangle {
DeltaVector3 a, b, c;
@@ -47,14 +46,34 @@ struct DeltaTriangle2D {
DeltaVector2 a, b, c;
};
struct DeltaRenderTriangle {
DeltaVector3 a, b, c;
DeltaVector2 uva, uvb, uvc;
struct DeltaVertex {
DeltaVector3 pos;
DeltaVector2 uv;
};
struct DeltaModelConnector {
int ind1, ind2;
};
struct DeltaRenderTriangle {
DeltaVertex a, b, c;
bool invert;
};
struct DeltaModel {
std::vector<DeltaVertex> vertexes;
std::vector<DeltaModelConnector> lines;
std::vector<DeltaRenderTriangle> triangles;
};
float getDeterminant(const DeltaVector2 &pointA, const DeltaVector2 &pointB, const DeltaVector2 &candidate);
DeltaVector2 barycentricToScreen(const DeltaVector3 &baryPoint, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c);
DeltaVertex intersect(DeltaVertex a, DeltaVertex b, float nearZ);
int clipTriangleAgainstNearPlane(float nearY, const DeltaRenderTriangle &in, DeltaRenderTriangle &out1, DeltaRenderTriangle &out2);
std::vector<DeltaTriangle2D> triangulatePolygon(std::vector<DeltaVertex>& points);
uint32_t SDLColorToUint32(SDL_Color color);
#endif
+4 -2
View File
@@ -4,9 +4,11 @@
#include <SDL3/SDL.h>
#include "math.hpp"
#include "assetloader.hpp"
void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c);
// void rasterizeTriangle(SDL_Renderer *renderer, const DeltaVector2 &a, const DeltaVector2 &b, const DeltaVector2 &c);
void rasterizeTriangleFast(SDL_Surface* surface, const DeltaVector2& v0, const DeltaVector2& v1, const DeltaVector2& v2, uint32_t color);
void rasterizeTriangleSurface(SDL_Surface* surface, const DeltaVector2& v0, const DeltaVector2& v1, const DeltaVector2& v2, uint32_t color, float u0, float v0_uv, float u1, float v1_uv, float u2, float v2_uv);
void rasterizeTriangleSurface(DeltaAssetLoader& assets, std::vector<float>& depthbuffer, SDL_Surface* surface, const DeltaVector3& v0, const DeltaVector3& v1, const DeltaVector3& v2, const std::string& textureID, float u0, float v0_uv, float u1, float v1_uv, float u2, float v2_uv);
void setPixel(SDL_Surface* surface, int x, int y, uint32_t color);
uint32_t getPixel(SDL_Surface* surface, int x, int y);
#endif
+3 -3
View File
@@ -11,9 +11,9 @@ class DeltaCamera {
DeltaCamera() {pos = DeltaVector3(); angle = DeltaVector2();};
DeltaCamera(float x, float y, float z);
DeltaVector3 translatePoint(const DeltaVector3 &worldPos);
DeltaVector2 projectPointVec(const DeltaVector3 &worldPos);
int projectTriangle(const DeltaTriangle &in, DeltaTriangle2D &out1, DeltaTriangle2D &out2);
DeltaVector2 projectPoint(float worldX, float worldY, float worldZ);
DeltaVector3 projectPointVec(const DeltaVector3 &worldPos);
int projectTriangle(const DeltaRenderTriangle &in, DeltaTriangle &out1, DeltaTriangle &out2, DeltaTriangle2D &uv1, DeltaTriangle2D &uv2);
DeltaVector3 projectPoint(float worldX, float worldY, float worldZ);
};
#endif