Browse Source

reordered transformations

iver 7 months ago
parent
commit
ae9e2ac0ef

BIN
builds/final binaries/librasteriver.so


BIN
builds/final binaries/main.bin


+ 1 - 1
readme.md

@@ -77,7 +77,7 @@ Rasterizer + Iver = RasterIver
 - [ ] add ability to request objects multiple times 
 - [ ] give objects IDs or some way to track them so that you can remove them dynamically
 - [ ] make CUDA version because OpenCL is slow with NVIDIA
-- [ ] fix center of perspective being in the wrong spot
+- [x] [this wasn't actually a problem. I had -999999999 for r_w but switched how the inputs are. It was rotatiing things -99999999 rad and looked like it was off] fix center of perspective being in the wrong spot
 - [ ] make function for world scale/master scale (scales everything. from origin? from object origins?)
 - [x] I think calculations for debugging memory usage are outdated/wrong
 - [ ] use correct types for stuff (like size_t)

+ 18 - 18
src/RasterIver/kernels/transformer.h

@@ -138,15 +138,15 @@ __kernel void transformer_kernel(__global Object* objects, __global float* verti
         int i4 = (normal_index + triangles[triangle_base + 4]) * 3;\
         int i5 = (normal_index + triangles[triangle_base + 5]) * 3;\
         \
-        float z0 = verticies[i0 + 2];\
-        float x0 = verticies[i0 + 0];\
-        float y0 = verticies[i0 + 1];\
-        float z1 = verticies[i1 + 2];\
-        float x1 = verticies[i1 + 0];\
-        float y1 = verticies[i1 + 1];\
-        float z2 = verticies[i2 + 2];\
-        float x2 = verticies[i2 + 0];\
-        float y2 = verticies[i2 + 1];\
+        float x0 = verticies[i0 + 0] * object_s_x;\
+        float y0 = verticies[i0 + 1] * object_s_y;\
+        float z0 = verticies[i0 + 2] * object_s_z;\
+        float x1 = verticies[i1 + 0] * object_s_x;\
+        float y1 = verticies[i1 + 1] * object_s_y;\
+        float z1 = verticies[i1 + 2] * object_s_z;\
+        float x2 = verticies[i2 + 0] * object_s_x;\
+        float y2 = verticies[i2 + 1] * object_s_y;\
+        float z2 = verticies[i2 + 2] * object_s_z;\
         float n_x0 = normals[i3 + 0];\
         float n_y0 = normals[i3 + 1];\
         float n_z0 = normals[i3 + 2];\
@@ -173,15 +173,15 @@ __kernel void transformer_kernel(__global Object* objects, __global float* verti
         rotate_euler(&n_x1, &n_y1, &n_z1, object_r_x, object_r_y, object_r_z);\
         rotate_euler(&n_x2, &n_y2, &n_z2, object_r_x, object_r_y, object_r_z);\
         \
-        z0 = (z0 * object_s_z + object_z);\
-        x0 = (x0 * object_s_x + object_x) / z0 * horizontal_fov_factor;\
-        y0 = (y0 * object_s_y + object_y) / z0 * vertical_fov_factor;\
-        z1 = (z1 * object_s_z + object_z);\
-        x1 = (x1 * object_s_x + object_x) / z1 * horizontal_fov_factor;\
-        y1 = (y1 * object_s_y + object_y) / z1 * vertical_fov_factor;\
-        z2 = (z2 * object_s_z + object_z);\
-        y2 = (y2 * object_s_y + object_y) / z2 * horizontal_fov_factor;\
-        x2 = (x2 * object_s_x + object_x) / z2 * vertical_fov_factor;\
+        z0 = (z0 + object_z);\
+        x0 = (x0 + object_x) / z0 * horizontal_fov_factor;\
+        y0 = (y0 + object_y) / z0 * vertical_fov_factor;\
+        z1 = (z1 + object_z);\
+        x1 = (x1 + object_x) / z1 * horizontal_fov_factor;\
+        y1 = (y1 + object_y) / z1 * vertical_fov_factor;\
+        z2 = (z2 + object_z);\
+        y2 = (y2 + object_y) / z2 * horizontal_fov_factor;\
+        x2 = (x2 + object_x) / z2 * vertical_fov_factor;\
         \
         if ((x0 < -ri_h_width && x1 < -ri_h_width && x2 < -ri_h_width) || (y0 < -ri_h_height && y1 < -ri_h_height && y2 < -ri_h_height) || (x0 >= ri_h_width && x1 >= ri_h_width && x2 >= ri_h_width) || (y0 >= ri_h_height && y1 >= ri_h_height && y2 >= ri_h_height)){\
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = 999999999;\

+ 24 - 23
src/RasterIver/source code/rasteriver.c

@@ -1154,15 +1154,15 @@ RI_result RI_Tick(){
         int i4 = (normal_index + triangles[triangle_base + 4]) * 3;
         int i5 = (normal_index + triangles[triangle_base + 5]) * 3;
         
-        float x0 = verticies[i0 + 0];
-        float z0 = verticies[i0 + 2];
-        float y0 = verticies[i0 + 1];
-        float z1 = verticies[i1 + 2];
-        float x1 = verticies[i1 + 0];
-        float y1 = verticies[i1 + 1];
-        float z2 = verticies[i2 + 2];
-        float x2 = verticies[i2 + 0];
-        float y2 = verticies[i2 + 1];
+        float x0 = verticies[i0 + 0] * object_s_x;
+        float y0 = verticies[i0 + 1] * object_s_y;
+        float z0 = verticies[i0 + 2] * object_s_z;
+        float x1 = verticies[i1 + 0] * object_s_x;
+        float y1 = verticies[i1 + 1] * object_s_y;
+        float z1 = verticies[i1 + 2] * object_s_z;
+        float x2 = verticies[i2 + 0] * object_s_x;
+        float y2 = verticies[i2 + 1] * object_s_y;
+        float z2 = verticies[i2 + 2] * object_s_z;
         float n_x0 = normals[i3 + 0];
         float n_y0 = normals[i3 + 1];
         float n_z0 = normals[i3 + 2];
@@ -1189,20 +1189,20 @@ RI_result RI_Tick(){
         rotate_euler(&n_x1, &n_y1, &n_z1, object_r_x, object_r_y, object_r_z);
         rotate_euler(&n_x2, &n_y2, &n_z2, object_r_x, object_r_y, object_r_z);
         
-        z0 = (z0 * object_s_z + object_z);
-        x0 = (x0 * object_s_x + object_x) / z0 * horizontal_fov_factor;
-        y0 = (y0 * object_s_y + object_y) / z0 * vertical_fov_factor;
-        z1 = (z1 * object_s_z + object_z);
-        x1 = (x1 * object_s_x + object_x) / z1 * horizontal_fov_factor;
-        y1 = (y1 * object_s_y + object_y) / z1 * vertical_fov_factor;
-        z2 = (z2 * object_s_z + object_z);
-        y2 = (y2 * object_s_y + object_y) / z2 * horizontal_fov_factor;
-        x2 = (x2 * object_s_x + object_x) / z2 * vertical_fov_factor;
+        z0 = (z0 + object_z);
+        x0 = (x0 + object_x) / z0 * horizontal_fov_factor;
+        y0 = (y0 + object_y) / z0 * vertical_fov_factor;
+        z1 = (z1 + object_z);
+        x1 = (x1 + object_x) / z1 * horizontal_fov_factor;
+        y1 = (y1 + object_y) / z1 * vertical_fov_factor;
+        z2 = (z2 + object_z);
+        y2 = (y2 + object_y) / z2 * horizontal_fov_factor;
+        x2 = (x2 + object_x) / z2 * vertical_fov_factor;
         
-        if ((x0 < -ri_h_width && x1 < -ri_h_width && x2 < -ri_h_width) || (y0 < -ri_h_height && y1 < -ri_h_height && y2 < -ri_h_height) || (x0 >= ri_h_width && x1 >= ri_h_width && x2 >= ri_h_width) || (y0 >= ri_h_height && y1 >= ri_h_height && y2 >= ri_h_height)){
-            transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = 9999;
-        }
-        else{
+        // if ((x0 < -ri_h_width && x1 < -ri_h_width && x2 < -ri_h_width) || (y0 < -ri_h_height && y1 < -ri_h_height && y2 < -ri_h_height) || (x0 >= ri_h_width && x1 >= ri_h_width && x2 >= ri_h_width) || (y0 >= ri_h_height && y1 >= ri_h_height && y2 >= ri_h_height)){
+        //     transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = 9999;
+        // }
+        // else{
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 0] = x0;
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 1] = y0;
             transformed_verticies[(triangles[triangle_base + 0] + transformed_vertex_index) * 3 + 2] = z0;
@@ -1222,7 +1222,7 @@ RI_result RI_Tick(){
             transformed_normals[(triangles[triangle_base + 2] + transformed_normal_index) * 3 + 0] = n_x2;
             transformed_normals[(triangles[triangle_base + 2] + transformed_normal_index) * 3 + 1] = n_y2;
             transformed_normals[(triangles[triangle_base + 2] + transformed_normal_index) * 3 + 2] = n_z2;
-        }
+        // }
     }}
 
             for (int id_y = -ri_h_height; id_y < ri_h_height; id_y++){
@@ -1328,6 +1328,7 @@ RI_result RI_Tick(){
                                 if (!(material_flags & RI_MATERIAL_DOUBLE_SIDED) && denominator >= 0) { 
                                     continue; 
                                 } 
+                                
                                 w0 = ((y1 - y2) * (id_x - x2) + (x2 - x1) * (id_y - y2)) / denominator; 
                                 w1 = ((y2 - y0) * (id_x - x0) + (x0 - x2) * (id_y - y0)) / denominator; 
                                 w2 = 1.0 - w0 - w1; 

+ 23 - 62
src/launch program/main.c

@@ -3,8 +3,8 @@
 #include <time.h>
 #include <stdlib.h>
 
-int width = 100;
-int height = 100;
+int width = 200;
+int height = 200;
 
 int main(){ 
     srand(time(NULL));                                                         
@@ -33,73 +33,34 @@ int main(){
         return 1;
     }
 
-    RI_newObject object_buffer[49] = {
-        {0, 0, 50,          0, 0, 0, -9999999,          10, 10, 10,        RI_MATERIAL_WIREFRAME | RI_MATERIAL_DOUBLE_SIDED | RI_MATERIAL_HAS_TEXTURE, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, 0, 100,        0, 0, 0, -9999999,          10, 10, 10,         RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, 0, 100,         0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, 0, 100,       0, 0, 0, -9999999,          10, 10, 10,          RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, 0, 100,       0, 0, 0, -9999999,          10, 10, 10,         RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, 0, 100,       0, 0, 0, -9999999,          10, 10, 10,         RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, 0, 100,          0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {0, 10, 100,          0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, 10, 100,        0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, 10, 100,         0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, 10, 100,       0, 0, 0, -9999999,          10, 10, 10,         RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, 10, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, 10, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, 10, 100,          0, 0, 0, -9999999,          10, 10, 10,     RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {0, -10, 100,          0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, -10, 100,        0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, -10, 100,         0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, -10, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, -10, 100,       0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, -10, 100,       0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, -10, 100,          0, 0, 0, -9999999,          10, 10, 10,    RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {0, 20, 100,          0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, 20, 100,        0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, 20, 100,         0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, 20, 100,       0, 0, 0, -9999999,          10, 10, 10,         RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, 20, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, 20, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, 20, 100,          0, 0, 0, -9999999,          10, 10, 10,     RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {0, -20, 100,          0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, -20, 100,        0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, -20, 100,         0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, -20, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, -20, 100,       0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, -20, 100,       0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, -20, 100,          0, 0, 0, -9999999,          10, 10, 10,    RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {0, 30, 100,          0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, 30, 100,        0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, 30, 100,         0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, 30, 100,       0, 0, 0, -9999999,          10, 10, 10,         RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, 30, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, 30, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, 30, 100,          0, 0, 0, -9999999,          10, 10, 10,     RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {0, -30, 100,          0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {10, -30, 100,        0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {20, -30, 100,         0, 0, 0, -9999999,          10, 10, 10,      RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {30, -30, 100,       0, 0, 0, -9999999,          10, 10, 10,        RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-10, -30, 100,       0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-20, -30, 100,       0, 0, 0, -9999999,          10, 10, 10,       RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-        {-30, -30, 100,          0, 0, 0, -9999999,          10, 10, 10,    RI_PMP_TEXTURED, "objects/rotated_cube.obj", "textures/bill_mcdinner.png"},
-    };
+    RI_newObject object_buffer[9] = {
+        {0, 0, 30,          0, 0, 0, 0,          10, 20, 5,       RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {50, 0, 100,        0, 0, 0, 0,          10, 10, 100,        RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {-50, 0, 100,         0, 0, 0, 0,          10, 10, 100,      RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {0, 50, 100,       0, 0, 0, 0,          10, 10, 100,          RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {0, -50, 100,       0, 0, 0, 0,          10, 10, 100,         RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {-50, 50, 100,       0, 0, 0, 0,          10, 10, 100,         RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {-50, -50, 100,          0, 0, 0, 0,          10, 10, 100,      RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {50, 50, 100,          0, 0, 0, 0,          10, 10, 100,       RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        {50, -50, 100,        0, 0, 0, 0,          10, 10, 100,        RI_PMP_TEXTURED, "objects/cube.obj", "textures/bill_mcdinner.png"},
+        };
 
-    int objects_to_request = 1;
+    int objects_to_request = 9;
 
     RI_objects objects = RI_RequestObjects(object_buffer, objects_to_request);
 
-    while (RI_IsRunning() == RI_RUNNING){
         for (int i = 0; i < objects_to_request; i++){
-            objects[i].transform.rotation.x += 0.01;
-            objects[i].transform.rotation.y += 0.02;
-            objects[i].transform.rotation.z += 0.03;
-
-            objects[i].transform.rotation.x = fmod(objects[i].transform.rotation.x, RI_2PI);
-            objects[i].transform.rotation.y = fmod(objects[i].transform.rotation.y, RI_2PI);
-            objects[i].transform.rotation.z = fmod(objects[i].transform.rotation.z, RI_2PI);
+            objects[i].material.albedo.a = 255 * ((float)i / objects_to_request);
+            objects[i].material.albedo.r = 255 * ((float)i / objects_to_request);
+            objects[i].material.albedo.g = 255 * ((float)i / objects_to_request);
+            objects[i].material.albedo.b = 255 * ((float)i / objects_to_request);
         }
         
+    while (RI_IsRunning() == RI_RUNNING){        
+        objects[0].transform.rotation.x += 0.1;
+        objects[0].transform.rotation.y += 0.1;
+        objects[0].transform.rotation.z += 0.1;
+
         RI_Tick();
     }