Browse Source

Lesson 6.1

Godzil 3 years ago
parent
commit
9c2e5a405c
2 changed files with 30 additions and 8 deletions
  1. 5 2
      source/display.c
  2. 25 6
      source/main.c

+ 5 - 2
source/display.c

@@ -102,7 +102,10 @@ void renderFrameBuffer()
 
 void drawPixel(uint32_t x, uint32_t y, uint32_t colour)
 {
-    frameBuffer[x + (y * windowWidth)] = colour;
+    if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))
+    {
+        frameBuffer[x + (y * windowWidth)] = colour;
+    }
 }
 
 void clearFrameBuffer(uint32_t colour)
@@ -140,7 +143,7 @@ void drawRectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t colo
     {
         for (i = 0 ; i < w ; i++)
         {
-            frameBuffer[i + x + ((j + y) * windowWidth)] = colour;
+            drawPixel(x + i, y + j, colour);
         }
     }
 }

+ 25 - 6
source/main.c

@@ -22,6 +22,9 @@ bool isRunning = true;
 
 #define N_POINTS (9*9*9)
 vec3_t cubePoints[N_POINTS];
+vec2_t projectedPoints[N_POINTS];
+
+double fovFactor = 120;
 
 void setup()
 {
@@ -81,20 +84,36 @@ void processInput()
 
 }
 
-void update()
+vec2_t orthographicPointProjection(vec3_t point)
 {
+    vec2_t ret;
+
+    ret.x = point.x * fovFactor + (windowWidth / 2.);
+    ret.y = point.y * fovFactor + (windowHeight / 2.);
 
+    return ret;
+}
+
+void update()
+{
+    int i;
+    for(i = 0; i < N_POINTS; i++)
+    {
+        projectedPoints[i] = orthographicPointProjection(cubePoints[i]);
+    }
 }
 
 void render()
 {
-    SDL_SetRenderDrawColor(renderer, 255, 64, 13, 255);
+    int i;
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
     SDL_RenderClear(renderer);
 
-    drawGrid(100, 0xFF003353);
-
-    drawPixel(20, 20, 0xFFFF00FF);
-    drawRectangle(100, 100, 50, 50, 0xFF9A2378);
+    for(i = 0; i < N_POINTS; i++)
+    {
+        vec2_t projPoint = projectedPoints[i];
+        drawRectangle(projPoint.x, projPoint.y, 4, 4, 0xFFFFFF00);
+    }
 
     renderFrameBuffer();
     clearFrameBuffer(0xFF000000);