near plane clipping partial

This commit is contained in:
2026-02-21 16:28:09 +01:00
parent 7c03b1b81d
commit 7ca48a5080
9 changed files with 271 additions and 37 deletions
+52 -8
View File
@@ -6,6 +6,7 @@
#include <string>
#include <algorithm>
#include <vector>
#include <float.h>
#include "globals.hpp"
#include "math.hpp"
@@ -19,11 +20,11 @@ static SDL_Renderer *renderer = NULL;
DeltaVector2 temp1(125, 150), temp2(150, 120), temp3(100, 100);
DeltaVector2 topleft, bottomright, p;
Player player = Player(70, 110, 20);
Player player = Player(200, -200, 20);
double prev = 0, now = 0, deltaTime;
SDL_Surface* backbuffer = NULL;
std::vector<DeltaTriangle> triangles;
std::vector<DeltaRenderTriangle> triangles;
SDL_Texture* gpuTexture = NULL;
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
@@ -36,7 +37,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("game", SIMULATED_WIDTH, SIMULATED_HEIGHT, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
if (!SDL_CreateWindowAndRenderer("game", SIMULATED_WIDTH, SIMULATED_HEIGHT, 0, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
@@ -45,7 +46,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
SDL_SetRenderLogicalPresentation(renderer, SIMULATED_WIDTH, SIMULATED_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
triangles.push_back({ {40, 10, 0}, {40, 210, 0}, {40, 10, 200} });
triangles.push_back({ {40, 10, 200}, {40, 210, 0}, {40, 10, 0} , {0, 0}, {1, 1}, {0, 1}});
triangles.push_back({ {40, 210, 200}, {40, 210, 0}, {40, 10, 200}, {1, 0}, {1, 1}, {0, 0} });
backbuffer = SDL_CreateSurface(SIMULATED_WIDTH, SIMULATED_HEIGHT, SDL_PIXELFORMAT_ARGB8888);
gpuTexture = SDL_CreateTexture(
renderer,
@@ -55,6 +57,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
SIMULATED_HEIGHT
);
SDL_SetRenderVSync(renderer, 1);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
@@ -101,12 +105,52 @@ SDL_AppResult SDL_AppIterate(void *appstate)
// rendered = player.camera.projectPointVec({ 40, 210, -170 });
// SDL_RenderPoint(renderer, rendered.x, rendered.y);
for (auto triangle : triangles) {
DeltaVector2 pointA = player.camera.projectPointVec(triangle.a), pointB = player.camera.projectPointVec(triangle.b), pointC = player.camera.projectPointVec(triangle.c);
rasterizeTriangleFast(backbuffer, pointA, pointB, pointC, SDLColorToUint32({ 255, 0, 255, 255 }));
if (SDL_MUSTLOCK(backbuffer)) {
SDL_LockSurface(backbuffer);
}
DeltaTriangle triangle1, triangle2;
for (auto triangle : triangles) {
// DeltaVector2 pointA = player.camera.projectPointVec(triangle.a), pointB = player.camera.projectPointVec(triangle.b), pointC = player.camera.projectPointVec(triangle.c);
DeltaTriangle2D out1, out2;
int numtriangles = player.camera.projectTriangle({ triangle.a, triangle.b, triangle.c }, out1, out2);
// SDL_Log(std::to_string(numtriangles).c_str());
if (numtriangles == 1) {
// rasterizeTriangleFast(backbuffer, out1.a, out1.b, out1.c, SDLColorToUint32({ 255, 0, 0, 255 }));
// rasterizeTriangleFast(backbuffer, out1.c, out1.b, out1.a, SDLColorToUint32({ 0, 0, 255, 255 }));
rasterizeTriangleSurface(backbuffer, out1.a, out1.b, out1.c, SDLColorToUint32({ 0, 255, 0, 255 }), triangle.uva.x, triangle.uva.y, triangle.uvb.x, triangle.uvb.y, triangle.uvc.x, triangle.uvc.y);
// SDL_Log(std::to_string(triangle.a.y).c_str());
// SDL_Log(std::to_string(triangle.b.y).c_str());
// SDL_Log(std::to_string(triangle.c.y).c_str());
}
if (numtriangles == 2) {
// rasterizeTriangleFast(backbuffer, out1.a, out1.b, out1.c, SDLColorToUint32({ 255, 0, 0, 255 }));
rasterizeTriangleSurface(backbuffer, out1.a, out1.b, out1.c, SDLColorToUint32({ 0, 255, 0, 255 }), triangle.uva.x, triangle.uva.y, triangle.uvb.x, triangle.uvb.y, triangle.uvc.x, triangle.uvc.y);
// rasterizeTriangleFast(backbuffer, out1.c, out1.b, out1.a, SDLColorToUint32({ 0, 255, 0, 255 }));
rasterizeTriangleSurface(backbuffer, out2.a, out2.b, out2.c, SDLColorToUint32({ 0, 255, 0, 255 }), triangle.uva.x, triangle.uva.y, triangle.uvb.x, triangle.uvb.y, triangle.uvc.x, triangle.uvc.y);
}
// int intersected = clipTriangleAgainstNearPlane(0.1, { triangle.a, triangle.b, triangle.c }, triangle1, triangle2);
// rasterizeTriangleFast(backbuffer, pointA, pointB, pointC, SDLColorToUint32({ 255, 0, 0, 255 }));
// if (intersected == 0) continue;
// rasterizeTriangleFast(backbuffer, pointA, pointB, pointC, SDLColorToUint32({ 255, 0, 0, 255 }));
// SDL_Log("===");
}
SDL_Log("=====");
if (SDL_MUSTLOCK(backbuffer)) {
SDL_UnlockSurface(backbuffer);
}
// for (int y = 0; y < SIMULATED_HEIGHT; y++) {
// for (int x = 0; x < SIMULATED_WIDTH; x++) {
// setPixel(backbuffer, x, y, SDLColorToUint32({ 255, 0, 0 }));
// }
// }
SDL_UpdateTexture(gpuTexture, NULL, backbuffer->pixels, backbuffer->pitch);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);