|
@@ -17,13 +17,11 @@
|
|
|
|
|
|
#include <display.h>
|
|
|
#include <vector.h>
|
|
|
+#include <mesh.h>
|
|
|
|
|
|
bool isRunning = true;
|
|
|
int previousFrameTime = 0;
|
|
|
|
|
|
-#define N_POINTS (9*9*9)
|
|
|
-vec3_t cubePoints[N_POINTS];
|
|
|
-vec2_t projectedPoints[N_POINTS];
|
|
|
vec3_t cameraPosition =
|
|
|
{
|
|
|
.x = 0,
|
|
@@ -37,7 +35,9 @@ vec3_t cubeRotation =
|
|
|
.z = 0,
|
|
|
};
|
|
|
|
|
|
-double fovFactor = 820 ;
|
|
|
+triangle_t projectedTriangles[N_MESH_FACES];
|
|
|
+
|
|
|
+double fovFactor = 640;
|
|
|
|
|
|
void setup()
|
|
|
{
|
|
@@ -56,20 +56,6 @@ void setup()
|
|
|
goto exit;
|
|
|
}
|
|
|
|
|
|
- int pointCount = 0;
|
|
|
-
|
|
|
- for(double x = -1; x <= 1 ; x += 0.25)
|
|
|
- {
|
|
|
- for(double y = -1; y <= 1 ; y += 0.25)
|
|
|
- {
|
|
|
- for(double z = -1; z <= 1 ; z += 0.25)
|
|
|
- {
|
|
|
- vec3_t new_point = { x, y, z };
|
|
|
- cubePoints[pointCount++] = new_point;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
exit:
|
|
|
return;
|
|
|
}
|
|
@@ -101,8 +87,8 @@ vec2_t orthographicPointProjection(vec3_t point)
|
|
|
{
|
|
|
vec2_t ret;
|
|
|
|
|
|
- ret.x = point.x * fovFactor + (windowWidth / 2.);
|
|
|
- ret.y = point.y * fovFactor + (windowHeight / 2.);
|
|
|
+ ret.x = point.x * fovFactor;
|
|
|
+ ret.y = point.y * fovFactor;
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
@@ -111,15 +97,15 @@ vec2_t perspectivePointProjection(vec3_t point)
|
|
|
{
|
|
|
vec2_t ret;
|
|
|
|
|
|
- ret.x = (point.x/point.z) * (fovFactor/2.) + (windowWidth / 2.);
|
|
|
- ret.y = (point.y/point.z) * (fovFactor/2.) + (windowHeight / 2.);
|
|
|
+ ret.x = (point.x/point.z) * (fovFactor/2.);
|
|
|
+ ret.y = (point.y/point.z) * (fovFactor/2.);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
void update()
|
|
|
{
|
|
|
- int i;
|
|
|
+ int i, j;
|
|
|
|
|
|
uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
|
|
|
if (timeToWait <= FRAME_TARGET_TIME)
|
|
@@ -128,21 +114,36 @@ void update()
|
|
|
}
|
|
|
previousFrameTime = SDL_GetTicks();
|
|
|
|
|
|
- cubeRotation.@x += 0.01;
|
|
|
+ cubeRotation.x += 0.01;
|
|
|
cubeRotation.y += 0.01;
|
|
|
cubeRotation.z += 0.01;
|
|
|
|
|
|
- for(i = 0; i < N_POINTS; i++)
|
|
|
+ for(i = 0; i < N_MESH_FACES; i++)
|
|
|
{
|
|
|
- vec3_t point = cubePoints[i];
|
|
|
+ vec3_t vertices[3] =
|
|
|
+ {
|
|
|
+ meshVertices[meshFaces[i].a - 1],
|
|
|
+ meshVertices[meshFaces[i].b - 1],
|
|
|
+ meshVertices[meshFaces[i].c - 1],
|
|
|
+ };
|
|
|
+
|
|
|
+ for(j = 0; j < 3; j++)
|
|
|
+ {
|
|
|
+ vec3_t transformed = vertices[j];
|
|
|
+
|
|
|
+ transformed = vec3RotateX(transformed, cubeRotation.x);
|
|
|
+ transformed = vec3RotateY(transformed, cubeRotation.y);
|
|
|
+ transformed = vec3RotateZ(transformed, cubeRotation.z);
|
|
|
|
|
|
- point = vec3RotateX(point, cubeRotation.x);
|
|
|
- point = vec3RotateY(point, cubeRotation.y);
|
|
|
- point = vec3RotateZ(point, cubeRotation.z);
|
|
|
+ transformed.z -= cameraPosition.z;
|
|
|
|
|
|
- point.z -= cameraPosition.z;
|
|
|
+ vec2_t projected = perspectivePointProjection(transformed);
|
|
|
|
|
|
- projectedPoints[i] = perspectivePointProjection(point);
|
|
|
+ projected.x += (windowWidth / 2);
|
|
|
+ projected.y += (windowHeight / 2);
|
|
|
+
|
|
|
+ projectedTriangles[i].points[j] = projected;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -152,12 +153,18 @@ void render()
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
|
- for(i = 0; i < N_POINTS; i++)
|
|
|
+
|
|
|
+ for(i = 0; i < N_MESH_FACES; i++)
|
|
|
{
|
|
|
- vec2_t projPoint = projectedPoints[i];
|
|
|
- drawRectangle(projPoint.x, projPoint.y, 4, 4, 0xFFFFFF00);
|
|
|
+ drawRectangle(projectedTriangles[i].points[0].x,
|
|
|
+ projectedTriangles[i].points[0].y,4, 4, 0xFFFFFF00);
|
|
|
+ drawRectangle(projectedTriangles[i].points[1].x,
|
|
|
+ projectedTriangles[i].points[1].y,4, 4, 0xFFFF00FF);
|
|
|
+ drawRectangle(projectedTriangles[i].points[2].x,
|
|
|
+ projectedTriangles[i].points[2].y,4, 4, 0xFF00FFFF);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
renderFrameBuffer();
|
|
|
clearFrameBuffer(0xFF000000);
|
|
|
|