Browse Source

ADDED TEXTURESS !!!!!

Iver 1 month ago
parent
commit
bb03c50156

+ 6 - 3
Makefile

@@ -1,13 +1,16 @@
 COMPILER=gcc
 FLAGS_ALL=-g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -DCL_TARGET_OPENCL_VERSION=300
-FLAGS_EXAMPLE=-Lbuilds/ -lrasteriver -Wl,-rpath=builds/ -lm -lSDL2
-FLAGS_LIB=-fPIC -shared -lc -lSDL2 -lm -lOpenCL
+FLAGS_EXAMPLE=-Lbuilds/ -lrasteriver -lpitmap -Wl,-rpath=builds/ -lm -lSDL2
+FLAGS_LIB=-fPIC -shared -lc -lSDL2 -lm -lOpenCL 
 
-main.bin: rasteriver.so
+main.bin: rasteriver.so pitmap.so
 	$(COMPILER) $(FLAGS_ALL) src/launch\ program/main.c -o builds/main.bin $(FLAGS_EXAMPLE) 
 
 rasteriver.so:
 	$(COMPILER) $(FLAGS_ALL) src/main/main.c -o builds/librasteriver.so $(FLAGS_LIB) 
 
+pitmap.so:
+	cp src/libraries/libpitmap.so builds/
+
 clean:
 	rm builds/*

BIN
builds/libpitmap.so


BIN
builds/librasteriver.so


BIN
builds/main.bin


+ 5 - 0
changelog.txt

@@ -0,0 +1,5 @@
+-integrated PitMap
+-added RI_load_image function
+-fixed wrong arg indecies in setKernelArg comments in RI_render
+-removed grid example scene
+-ADDED TEXTURESS !!!! IT WORKS !!!!! FIRST !!! TRY !!! OMGG!!!!!! >W<

BIN
image.tga


+ 1 - 1
readme.md

@@ -2,7 +2,7 @@
 
 ## Roadmap
 
-### By End of December, 2026
+### By End of December, 2025
 
  - [ ] Basic texture support
     - using custom image parser, PitMap

+ 5 - 3
src/headers/functions.h

@@ -13,9 +13,8 @@ int RI_init();
 // ticks RasterIver (updated window, check for events, renders scene, etc)
 void RI_tick();
 
-// renders a scene onto a texture
-// set the texture to NULL to render to the window's framebuffer
-void RI_render(RI_texture* texture, RI_scene *scene);
+// renders a scene to the screen
+void RI_render(RI_scene *scene);
 
 // loads an OBJ file into memory as a mesh
 RI_mesh *RI_load_mesh(char* filename);
@@ -32,4 +31,7 @@ RI_material *RI_new_material();
 // allocates and returns a pointer to a new texture
 RI_texture *RI_new_texture(int width, int height);
 
+// loads an image file as a texture
+RI_texture* RI_load_image(char* filename);
+
 #endif

+ 13 - 0
src/headers/pitmap.h

@@ -0,0 +1,13 @@
+#ifndef PITMAP_H
+#define PITMAP_H
+
+#include <stdint.h>
+
+typedef struct {
+    uint32_t* frame_buffer;
+    int width, height;
+} PM_image;
+
+PM_image* PM_load_image(char* filename);
+
+#endif

+ 16 - 10
src/headers/types.h

@@ -21,6 +21,7 @@ enum {
     RI_DEBUG_TRANSFORMER_TIME            = 1 << 8,
     RI_DEBUG_TRANSFORMER_MESSAGE         = 1 << 9,
     RI_DEBUG_TRANSFORMER_ERROR           = 1 << 11,
+    RI_DEBUG_TRANSFORMER_TEXTURE         = 1 << 29,
 
     RI_DEBUG_TRANSFORMER_ALL = (
         RI_DEBUG_TRANSFORMER_CURRENT_ACTOR |
@@ -29,7 +30,8 @@ enum {
         RI_DEBUG_TRANSFORMER_EXTRA_WORK_ITEMS |
         RI_DEBUG_TRANSFORMER_TIME |
         RI_DEBUG_TRANSFORMER_MESSAGE |
-        RI_DEBUG_TRANSFORMER_ERROR ),
+        RI_DEBUG_TRANSFORMER_ERROR |
+        RI_DEBUG_TRANSFORMER_TEXTURE ),
 
     // rasterizer
     RI_DEBUG_RASTERIZER_TIME             = 1 << 12,
@@ -103,9 +105,10 @@ typedef enum {
     ri_false = 0,
 } ri_bool;
 
-typedef struct { // A loaded texture file
-    uint32_t *image_buffer;
-    RI_vector_2i resolution;
+typedef struct {
+    uint16_t width;
+    uint16_t height;
+    uint32_t index;
 } RI_texture;
 
 typedef struct {
@@ -158,6 +161,7 @@ typedef struct {
     RI_vector_4 rotation;
     RI_vector_3 scale;
     RI_mesh *mesh;
+    RI_texture* texture;
     int active;
     int material_index;
 } RI_actor;
@@ -166,10 +170,10 @@ typedef struct {
     RI_vector_3 position_0, position_1, position_2;
     RI_vector_3 normal_0, normal_1, normal_2;
     RI_vector_2 uv_0, uv_1, uv_2;
-    int min_screen_x, max_screen_x, min_screen_y, max_screen_y;
-    int should_render;
-    int shrunk;
-    int split;
+    int16_t min_screen_x, max_screen_x, min_screen_y, max_screen_y;
+    unsigned char should_render;
+    uint16_t texture_width, height;
+    uint32_t texture_index;
 } RI_renderable_face;
 
 typedef struct {
@@ -203,8 +207,7 @@ typedef struct {
     SDL_Window *window;
     SDL_Renderer *renderer;
     SDL_Texture *frame_buffer_texture;
-    uint32_t *frame_buffer_intermediate;
-    RI_texture *frame_buffer;
+    uint32_t *frame_buffer;
     int pitch;
 } RI_SDL;
 
@@ -215,6 +218,7 @@ typedef struct {
     cl_command_queue queue;
     cl_kernel rasterization_kernel;
     cl_kernel transformation_kernel;
+    cl_mem textures_mem_buffer;
     cl_mem renderable_faces_mem_buffer;
     cl_mem faces_mem_buffer;
     cl_mem frame_buffer_mem_buffer;
@@ -227,12 +231,14 @@ typedef struct {
     RI_vector_3 *temp_vertecies;
     RI_vector_3 *temp_normals;
     RI_vector_2 *temp_uvs;
+    uint32_t* textures;
     int face_count;
     int vertex_count;
     int normal_count;
     int uv_count;
     int length_of_renderable_faces_array;
     int number_of_faces_just_rendered;
+    int length_of_textures_array;
 } RI_CL;
 
 typedef struct {

+ 41 - 38
src/kernels/kernels.cl

@@ -22,10 +22,10 @@ typedef struct {
     RI_vector_3 position_0, position_1, position_2;
     RI_vector_3 normal_0, normal_1, normal_2;
     RI_vector_2 uv_0, uv_1, uv_2;
-    int min_screen_x, max_screen_x, min_screen_y, max_screen_y;
-    int should_render;
-    int shrunk;
-    int split;
+    short min_screen_x, max_screen_x, min_screen_y, max_screen_y;
+    uchar should_render;
+    ushort texture_width, texture_height;
+    uint texture_index;
 } RI_renderable_face;
 
 typedef struct {
@@ -41,19 +41,19 @@ typedef struct {
     RI_vector_2 uv_1;
     RI_vector_2 uv_2;
 
-    int should_render;
+    uchar should_render;
 } RI_face;
 
 typedef struct {
     RI_vector_3 position;
     RI_vector_4 rotation;
     RI_vector_3 scale;;
-    int active;
-    int has_normals;
-    int has_uvs;
-    int material_index;
-    int face_index;
-    int face_count;
+    uchar active;
+    uchar has_normals;
+    uchar has_uvs;
+    ushort material_index;
+    uint face_index;
+    uint face_count;
 } RI_actor;
 
 typedef struct {
@@ -265,7 +265,7 @@ void global_quaternion_rotate(__global RI_vector_3 *position, RI_vector_4 rotati
     *position = (RI_vector_3){rotation.x, rotation.y, rotation.z};
 }
 
-__kernel void transformer(__global RI_face *faces, __global RI_renderable_face *renderable_faces, double actor_x, double actor_y, double actor_z, double actor_r_w, double actor_r_x, double actor_r_y, double actor_r_z, double actor_s_x, double actor_s_y, double actor_s_z, int has_normals, int has_uvs, int face_array_offset_index, int face_count, int width, int height, double horizontal_fov_factor, double vertical_fov_factor, float min_clip, float max_clip, double camera_x, double camera_y, double camera_z, double camera_r_w, double camera_r_x, double camera_r_y, double camera_r_z, int renderable_face_offset, int face_sqrt){
+__kernel void transformer(__global RI_face *faces, __global RI_renderable_face *renderable_faces, double actor_x, double actor_y, double actor_z, double actor_r_w, double actor_r_x, double actor_r_y, double actor_r_z, double actor_s_x, double actor_s_y, double actor_s_z, int has_normals, int has_uvs, int face_array_offset_index, int face_count, int width, int height, double horizontal_fov_factor, double vertical_fov_factor, float min_clip, float max_clip, double camera_x, double camera_y, double camera_z, double camera_r_w, double camera_r_x, double camera_r_y, double camera_r_z, int renderable_face_offset, int face_sqrt, ushort texture_width, ushort texture_height, uint texture_index){
     int face_index = get_global_id(1) * face_sqrt + get_global_id(0); if (face_index >= face_count) return;
 
     RI_vector_3 current_actor_position = (RI_vector_3){actor_x, actor_y, actor_z};
@@ -283,10 +283,6 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
 
     cur_r_face->should_render = 1;
 
-    // cur_r_face->parent_actor = current_actor;
-    cur_r_face->shrunk = 0;
-    cur_r_face->split = 0;
-
     // cur_r_face->material = current_actor.material;
 
     cur_r_face->position_0 = cur_face->position_0;
@@ -303,6 +299,10 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
         cur_r_face->uv_2 = cur_face->uv_2;
     }
 
+    cur_r_face->texture_width = texture_width;
+    cur_r_face->texture_height = texture_height;
+    cur_r_face->texture_index = texture_index;
+
     // scale
     global_vector_3_hadamard(&cur_r_face->position_0, current_actor_scale);
     global_vector_3_hadamard(&cur_r_face->position_1, current_actor_scale);
@@ -406,8 +406,6 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
             global_vector_2_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
             global_vector_2_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
 
-            cur_r_face->shrunk = 1;     
-
             break;}
 
         case 1: {// split polygon
@@ -415,8 +413,6 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
             RI_vector_3 clipped_normal, normal_a, normal_b;
             RI_vector_2 clipped_uv, uv_a, uv_b;
 
-            cur_r_face->split = 1;
-
             if (is_0_clipped){ 
                 clipped_point = cur_r_face->position_0;
                 point_a = cur_r_face->position_1;
@@ -519,25 +515,29 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
             cur_r_split_face->min_screen_x = cur_r_split_face->position_0.x; 
             if (cur_r_split_face->position_1.x < cur_r_split_face->min_screen_x) cur_r_split_face->min_screen_x = cur_r_split_face->position_1.x;
             if (cur_r_split_face->position_2.x < cur_r_split_face->min_screen_x) cur_r_split_face->min_screen_x = cur_r_split_face->position_2.x;
-            cur_r_split_face->min_screen_x = max(cur_r_split_face->min_screen_x, -width / 2); 
+            cur_r_split_face->min_screen_x = max(cur_r_split_face->min_screen_x, (short)(-width / 2)); 
 
             cur_r_split_face->max_screen_x = cur_r_split_face->position_0.x; 
             if (cur_r_split_face->position_1.x > cur_r_split_face->max_screen_x) cur_r_split_face->max_screen_x = cur_r_split_face->position_1.x;
             if (cur_r_split_face->position_2.x > cur_r_split_face->max_screen_x) cur_r_split_face->max_screen_x = cur_r_split_face->position_2.x;
-            cur_r_split_face->max_screen_x = min(cur_r_split_face->max_screen_x, width / 2); 
+            cur_r_split_face->max_screen_x = min(cur_r_split_face->max_screen_x, (short)(width / 2)); 
 
             cur_r_split_face->min_screen_y = cur_r_split_face->position_0.y; 
             if (cur_r_split_face->position_1.y < cur_r_split_face->min_screen_y) cur_r_split_face->min_screen_y = cur_r_split_face->position_1.y;
             if (cur_r_split_face->position_2.y < cur_r_split_face->min_screen_y) cur_r_split_face->min_screen_y = cur_r_split_face->position_2.y;
-            cur_r_split_face->min_screen_y = max(cur_r_split_face->min_screen_y, -height / 2); 
+            cur_r_split_face->min_screen_y = max(cur_r_split_face->min_screen_y, (short)(-height / 2)); 
 
             cur_r_split_face->max_screen_y = cur_r_split_face->position_0.y; 
             if (cur_r_split_face->position_1.y > cur_r_split_face->max_screen_y) cur_r_split_face->max_screen_y = cur_r_split_face->position_1.y;
             if (cur_r_split_face->position_2.y > cur_r_split_face->max_screen_y) cur_r_split_face->max_screen_y = cur_r_split_face->position_2.y;
-            cur_r_split_face->max_screen_y = min(cur_r_split_face->max_screen_y, height / 2); 
+            cur_r_split_face->max_screen_y = min(cur_r_split_face->max_screen_y, (short)(height / 2)); 
 
             cur_r_split_face->should_render = 1;
 
+            cur_r_split_face->texture_width = texture_width;
+            cur_r_split_face->texture_height = texture_height;
+            cur_r_split_face->texture_index = texture_index;
+
             break;
         }
 
@@ -560,32 +560,32 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
     cur_r_face->min_screen_x = pos_0->x; 
     if (pos_1->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_1->x;
     if (pos_2->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_2->x;
-    cur_r_face->min_screen_x = max(cur_r_face->min_screen_x, -width / 2); 
+    cur_r_face->min_screen_x = max(cur_r_face->min_screen_x, (short)(-width / 2)); 
 
     cur_r_face->max_screen_x = pos_0->x; 
     if (pos_1->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_1->x;
     if (pos_2->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_2->x;
-    cur_r_face->max_screen_x = min(cur_r_face->max_screen_x, width / 2); 
+    cur_r_face->max_screen_x = min(cur_r_face->max_screen_x, (short)(width / 2)); 
 
     cur_r_face->min_screen_y = pos_0->y; 
     if (pos_1->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_1->y;
     if (pos_2->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_2->y;
-    cur_r_face->min_screen_y = max(cur_r_face->min_screen_y, -height / 2); 
+    cur_r_face->min_screen_y = max(cur_r_face->min_screen_y, (short)(-height / 2)); 
 
     cur_r_face->max_screen_y = pos_0->y; 
     if (pos_1->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_1->y;
     if (pos_2->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_2->y;
-    cur_r_face->max_screen_y = min(cur_r_face->max_screen_y, height / 2); 
+    cur_r_face->max_screen_y = min(cur_r_face->max_screen_y, (short)(height / 2)); 
     
     return;
 }
 
-__kernel void rasterizer(__global RI_renderable_face *renderable_faces, __global uint *frame_buffer, int width, int height, int half_width, int half_height, int number_of_renderable_faces, int number_of_split_renderable_faces){
+__kernel void rasterizer(__global RI_renderable_face *renderable_faces, __global uint* textures, __global uint *frame_buffer, int width, int height, int half_width, int half_height, int number_of_renderable_faces, int number_of_split_renderable_faces){
     int pixel_x = get_global_id(0); if (pixel_x >= width) return;
     int pixel_y = get_global_id(1); if (pixel_y >= height) return;
     int idx = pixel_y * width + pixel_x;
 
-    int x = width - pixel_x - half_width;
+    int x = pixel_x - half_width;
     int y = height - pixel_y - half_height;
 
     double z = INFINITY;
@@ -635,16 +635,19 @@ __kernel void rasterizer(__global RI_renderable_face *renderable_faces, __global
         
         double alpha = 1;
         
-        double ux, uy;
-        ux = uy = -1;
-        
+        float ux = (w0 * (uv_0.x / pos_0.z) + w1 * (uv_1.x / pos_1.z) + w2 * (uv_2.x / pos_2.z)) / w_over_z;
+        float uy = (w0 * (uv_0.y / pos_0.z) + w1 * (uv_1.y / pos_1.z) + w2 * (uv_2.y / pos_2.z)) / w_over_z;    
+
         RI_vector_3 interpolated_normal = {0};
         
-        interpolated_normal.x = (w0 * (normal_0.x / pos_0.z) + w1 * (normal_1.x / pos_1.z) + w2 * (normal_2.x / pos_2.z)) / w_over_z;    
-        interpolated_normal.y = (w0 * (normal_0.y / pos_0.z) + w1 * (normal_1.y / pos_1.z) + w2 * (normal_2.y / pos_2.z)) / w_over_z;        
-        interpolated_normal.z = (w0 * (normal_0.z / pos_0.z) + w1 * (normal_1.z / pos_1.z) + w2 * (normal_2.z / pos_2.z)) / w_over_z;        
-        
-        pixel_color = 255 << 24 | (int)((w0 + 1.0) * 127.5) << 16 | (int)((w1 + 1.0) * 127.5) << 8 | (int)((w2 + 1.0) * 127.5);
+        uint texel_x = current_face->texture_width * ux;
+        uint texel_y = current_face->texture_height * uy;
+
+        uint texel_index = current_face->texture_index + 
+            texel_y * current_face->texture_width + texel_x;
+
+        pixel_color = textures[texel_index];
+
         z = interpolated_z;
     }
     

+ 17 - 28
src/launch program/main.c

@@ -21,34 +21,24 @@ int main(){
 
     RI_mesh *cube_mesh = RI_load_mesh("objects/cube.obj");
 
-    int actor_count = 10 * 10;
-
-    scene->actors = malloc(sizeof(RI_actor) * actor_count);
+    scene->actors = malloc(sizeof(RI_actor));
 
     float min_x = -100;
     float max_x = 100;
     float min_y = -100;
     float max_y = 100;
 
-    for (int i = 0; i < (int)sqrt(actor_count); ++i){
-        for (int j = 0; j < (int)sqrt(actor_count); ++j){
-            scene->actors[i * (int)sqrt(actor_count) + j] = RI_new_actor();
+    RI_texture* texture = RI_load_image("textures/test_texture_4_cube.bmp");
+
+    scene->actors[0] = RI_new_actor();
          
-            scene->actors[i * (int)sqrt(actor_count) + j]->mesh = cube_mesh;
+    scene->actors[0]->mesh = cube_mesh;
+    scene->actors[0]->texture = texture;
          
-            float offset_x = fabs(min_x - max_x) / ((int)sqrt(actor_count) - 1) * i;
-            float offset_y = fabs(min_y - max_y) / ((int)sqrt(actor_count) - 1) * j;
-
-            scene->actors[i * (int)sqrt(actor_count) + j]->scale = (RI_vector_3){100, 100, 100};
-            scene->actors[i * (int)sqrt(actor_count) + j]->position = (RI_vector_3){
-                min_x + offset_x, 
-                min_y + offset_y, 
-                600
-            };
-        }
-    }
-
-    scene->length_of_actors_array = actor_count;
+    scene->actors[0]->scale = (RI_vector_3){50, 50, 50};
+    scene->actors[0]->position = (RI_vector_3){0, 0, 300};
+    
+    scene->length_of_actors_array = 1;
 
     long int start, end;
     double fps = 0;
@@ -56,22 +46,21 @@ int main(){
     float total_fps = 0;
 
     double delta_time = 0;
-    double delta_min = 0.00001;
+    double delta_min = 0.0001;
     double delta_max = 100000;
+    
+    double rotation = 0;
+    
     while (context->is_running){
         start = clock();
         
         // scene->camera.FOV = context->current_frame;
         
-        for (int i = 0; i < (int)sqrt(actor_count); ++i){
-            for (int j = 0; j < (int)sqrt(actor_count); ++j){
-                RI_euler_rotation_to_quaternion(&scene->actors[i * (int)sqrt(actor_count) + j]->rotation, (RI_vector_3){context->current_frame * 0.01 * (i + 1), context->current_frame * 0.01 * (j + 1), context->current_frame * 0.001});
-                
+        RI_euler_rotation_to_quaternion(&scene->actors[0]->rotation, (RI_vector_3){rotation, rotation, rotation});
 
-            }
-        }
+        rotation += delta_time;
 
-        RI_render(NULL, scene);
+        RI_render(scene);
 
         RI_tick();
 

BIN
src/libraries/libpitmap.so


+ 126 - 64
src/main/main.c

@@ -4,6 +4,7 @@
 #include <SDL2/SDL.h>
 #include "../headers/rasteriver.h"
 #include "../headers/memory.h"
+#include "../headers/pitmap.h"
 #include <time.h>
 
 cl_int clerror;
@@ -38,14 +39,57 @@ void debug(char *string, int debug_flag, ...){
     va_end(args);
 }
 
-RI_texture *RI_new_texture(int width, int height){
-    RI_texture *new_texture = RI_malloc(sizeof(RI_texture));
+RI_texture* RI_load_image(char* filename){
+    PM_image* image = PM_load_image(filename);
 
-    new_texture->image_buffer = RI_malloc(sizeof(uint32_t) * width * height);
-    new_texture->resolution.x = width;
-    new_texture->resolution.y = height;
+    RI_texture* texture = RI_malloc(sizeof(RI_texture));
 
-    return new_texture;
+    int previous_length_of_textures_array = context.opencl.length_of_textures_array;
+
+    texture->width = image->width;
+    texture->height = image->height;
+    texture->index = previous_length_of_textures_array;
+
+    context.opencl.length_of_textures_array += image->width * image->height;
+
+    context.opencl.textures = RI_realloc(context.opencl.textures, context.opencl.length_of_textures_array * sizeof(uint32_t));
+
+    printf("%d\n", previous_length_of_textures_array);
+
+    memcpy(context.opencl.textures + previous_length_of_textures_array, image->frame_buffer, sizeof(uint32_t) * image->width * image->height);
+
+    if (context.opencl.textures_mem_buffer) clReleaseMemObject(context.opencl.textures_mem_buffer);
+
+    context.opencl.textures_mem_buffer = clCreateBuffer(
+        context.opencl.context, 
+        CL_MEM_READ_WRITE, 
+        sizeof(uint32_t) * context.opencl.length_of_textures_array, 
+        NULL, NULL
+    );
+    
+    clEnqueueWriteBuffer(
+        context.opencl.queue, 
+        context.opencl.textures_mem_buffer, 
+        CL_TRUE, 
+        0, 
+        sizeof(uint32_t) * context.opencl.length_of_textures_array, 
+        context.opencl.textures, 
+        0, NULL, NULL
+    );
+    
+    clFinish(context.opencl.queue);
+
+    clSetKernelArg(
+        context.opencl.rasterization_kernel, 
+        1, 
+        sizeof(cl_mem), 
+        &context.opencl.textures_mem_buffer
+    );
+
+    free(image->frame_buffer);
+    free(image);
+
+    return texture;
 }
 
 RI_material *RI_new_material(){
@@ -331,6 +375,9 @@ RI_mesh *RI_load_mesh(char *filename){
             0, NULL, NULL
         );
 
+    clFinish(context.opencl.queue);
+
+
         clSetKernelArg(
             context.opencl.transformation_kernel, 
             0, 
@@ -397,7 +444,7 @@ RI_mesh *RI_load_mesh(char *filename){
     return new_mesh;
 }
 
-void RI_render(RI_texture *target_texture, RI_scene *scene){
+void RI_render(RI_scene *scene){
     clock_t start_time, end_time;
     
     start_time = clock();
@@ -406,42 +453,38 @@ void RI_render(RI_texture *target_texture, RI_scene *scene){
         RI_DEBUG_FRAME_START_END_MARKERS
     );
 
-    if (!target_texture){
-        target_texture = context.sdl.frame_buffer;
-    }
-
     // transformer 
     
-    double horizontal_fov_factor = (double)target_texture->resolution.x / tanf(0.5 * scene->camera.FOV);
-    double vertical_fov_factor = (double)target_texture->resolution.y / tanf(0.5 * scene->camera.FOV);
+    double horizontal_fov_factor = (double)context.window.width / tanf(0.5 * scene->camera.FOV);
+    double vertical_fov_factor = (double)context.window.height / tanf(0.5 * scene->camera.FOV);
     
 
     // kernel args    
     
-    // 21, double horizontal_fov_factor
+    // 18, double horizontal_fov_factor
     clSetKernelArg(context.opencl.transformation_kernel, 18, sizeof(double), &horizontal_fov_factor);
-    // 22, double vertical_fov_factor
+    // 19, double vertical_fov_factor
     clSetKernelArg(context.opencl.transformation_kernel, 19, sizeof(double), &vertical_fov_factor);
 
-    // 23, double min_clip
+    // 20, double min_clip
     clSetKernelArg(context.opencl.transformation_kernel, 20, sizeof(float), &scene->camera.min_clip);
-    // 24, double max_clip
+    // 21, double max_clip
     clSetKernelArg(context.opencl.transformation_kernel, 21, sizeof(float), &scene->camera.max_clip);
 
-    // 25, double camera_x
+    // 22, double camera_x
     clSetKernelArg(context.opencl.transformation_kernel, 22, sizeof(double), &scene->camera.position.x);
-    // 26, double camera_y
+    // 23, double camera_y
     clSetKernelArg(context.opencl.transformation_kernel, 23, sizeof(double), &scene->camera.position.y);
-    // 27, double camera_z
+    // 24, double camera_z
     clSetKernelArg(context.opencl.transformation_kernel, 24, sizeof(double), &scene->camera.position.z);
 
-    // 28, double camera_r_w
+    // 25, double camera_r_w
     clSetKernelArg(context.opencl.transformation_kernel, 25, sizeof(double), &scene->camera.rotation.w);
-    // 29, double camera_r_x
+    // 26, double camera_r_x
     clSetKernelArg(context.opencl.transformation_kernel, 26, sizeof(double), &scene->camera.rotation.x);
-    // 30, double camera_r_y
+    // 27, double camera_r_y
     clSetKernelArg(context.opencl.transformation_kernel, 27, sizeof(double), &scene->camera.rotation.y);
-    // 31, double camera_r_z
+    // 28, double camera_r_z
     clSetKernelArg(context.opencl.transformation_kernel, 28, sizeof(double), &scene->camera.rotation.z);
 
 
@@ -567,44 +610,53 @@ void RI_render(RI_texture *target_texture, RI_scene *scene){
             scene->actors[actor_index]->mesh->face_count
         );
 
-        // 5, double actor_x
+        // 2, double actor_x
         clSetKernelArg(context.opencl.transformation_kernel, 2, sizeof(double), &actor->position.x);
-        // 6, double actor_y
+        // 3, double actor_y
         clSetKernelArg(context.opencl.transformation_kernel, 3, sizeof(double), &actor->position.y);
-        // 7, double actor_z
+        // 4, double actor_z
         clSetKernelArg(context.opencl.transformation_kernel, 4, sizeof(double), &actor->position.z);
 
-        // 8, double actor_r_w
+        // 5, double actor_r_w
         clSetKernelArg(context.opencl.transformation_kernel, 5, sizeof(double), &actor->rotation.w);
-        // 9, double actor_r_x
+        // 6, double actor_r_x
         clSetKernelArg(context.opencl.transformation_kernel, 6, sizeof(double), &actor->rotation.x);
-        // 10, double actor_r_y
+        // 7, double actor_r_y
         clSetKernelArg(context.opencl.transformation_kernel, 7, sizeof(double), &actor->rotation.y);
-        // 11, double actor_r_z
+        // 8, double actor_r_z
         clSetKernelArg(context.opencl.transformation_kernel, 8, sizeof(double), &actor->rotation.z);
 
-        // 12, double actor_s_x
+        // 9, double actor_s_x
         clSetKernelArg(context.opencl.transformation_kernel, 9, sizeof(double), &actor->scale.x);
-        // 13, double actor_s_y
+        // 10, double actor_s_y
         clSetKernelArg(context.opencl.transformation_kernel, 10, sizeof(double), &actor->scale.y);
-        // 14, double actor_s_z
+        // 11, double actor_s_z
         clSetKernelArg(context.opencl.transformation_kernel, 11, sizeof(double), &actor->scale.z);
 
-        // 15, int has_normals
+        // 12, int has_normals
         clSetKernelArg(context.opencl.transformation_kernel, 12, sizeof(int), &actor->mesh->has_normals);
-        // 16, int has_uvs
+        // 13, int has_uvs
         clSetKernelArg(context.opencl.transformation_kernel, 13, sizeof(int), &actor->mesh->has_uvs);
-        // 17, int face_array_offset_index
+        // 14, int face_array_offset_index
         clSetKernelArg(context.opencl.transformation_kernel, 14, sizeof(int), &actor->mesh->face_index);
-        // 18, int face_count
+        // 15, int face_count
         clSetKernelArg(context.opencl.transformation_kernel, 15, sizeof(int), &actor->mesh->face_count);
 
-        // 32, int renderable_face_offset
+        // 29, int renderable_face_offset
         clSetKernelArg(context.opencl.transformation_kernel, 29, sizeof(int), &renderable_face_index);
         
-        // 33, int face_sqrt
+        // 30, int face_sqrt
         clSetKernelArg(context.opencl.transformation_kernel, 30, sizeof(int), &face_sqrt);
 
+        debug("texture width: %d texture height: %d texture index %d", RI_DEBUG_TRANSFORMER_TEXTURE, actor->texture->width, actor->texture->height, actor->texture->index);
+
+        // 31: uint16_t texture_width
+        clSetKernelArg(context.opencl.transformation_kernel, 31, sizeof(uint16_t), &actor->texture->width);
+        // 32: uint16_t texture_height
+        clSetKernelArg(context.opencl.transformation_kernel, 32, sizeof(uint16_t), &actor->texture->height);
+        // 33: uint32_t texture_index
+        clSetKernelArg(context.opencl.transformation_kernel, 33, sizeof(uint32_t), &actor->texture->index);
+
         debug("running actor #%d's transformation kernel...", 
             RI_DEBUG_TRANSFORMER_MESSAGE, 
             actor_index
@@ -678,10 +730,10 @@ void RI_render(RI_texture *target_texture, RI_scene *scene){
         sizeof(cl_mem), 
         &context.opencl.renderable_faces_mem_buffer
     );
-    clSetKernelArg(context.opencl.rasterization_kernel, 6, sizeof(int), &scene->face_count);
+    clSetKernelArg(context.opencl.rasterization_kernel, 7, sizeof(int), &scene->face_count);
     clSetKernelArg(
         context.opencl.rasterization_kernel, 
-        7, 
+        8, 
         sizeof(int), 
         &context.current_split_renderable_face_index
     );
@@ -715,7 +767,7 @@ void RI_render(RI_texture *target_texture, RI_scene *scene){
         CL_TRUE, 
         0, 
         context.window.width * context.window.height * sizeof(uint32_t), 
-        context.sdl.frame_buffer->image_buffer, 
+        context.sdl.frame_buffer, 
         0, NULL, &clevent
     );
     clFinish(context.opencl.queue);
@@ -762,16 +814,10 @@ void RI_tick(){
     SDL_LockTexture(
         context.sdl.frame_buffer_texture, 
         NULL, 
-        (void*)&context.sdl.frame_buffer_intermediate, 
+        (void*)&context.sdl.frame_buffer, 
         &context.sdl.pitch
     );
 
-    memcpy(
-        context.sdl.frame_buffer_intermediate, 
-        context.sdl.frame_buffer->image_buffer, 
-        context.window.width * context.window.height * sizeof(uint32_t)
-    );
-
     SDL_UnlockTexture(context.sdl.frame_buffer_texture);
 
     SDL_RenderCopy(context.sdl.renderer, context.sdl.frame_buffer_texture, NULL, NULL);
@@ -798,8 +844,8 @@ void RI_tick(){
 }
 
 RI_context *RI_get_context(){
-    context.sdl = (RI_SDL){NULL, NULL, NULL, NULL, NULL, -1};
-    context.opencl = (RI_CL){NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 1, 0};
+    context.sdl = (RI_SDL){NULL, NULL, NULL, NULL, -1};
+    context.opencl = (RI_CL){NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 1, 0, 0};
     context.window = (RI_window){800, 800, 400, 400, "RasterIver Window"};
     
     context.debug_flags = RI_DEBUG_ERRORS;
@@ -846,16 +892,15 @@ int RI_init(){
     context.sdl.renderer = SDL_CreateRenderer(context.sdl.window, -1, SDL_RENDERER_ACCELERATED);
     context.sdl.frame_buffer_texture = SDL_CreateTexture(
         context.sdl.renderer, 
-        SDL_PIXELFORMAT_ARGB8888, 
+        SDL_PIXELFORMAT_BGRA8888, 
         SDL_TEXTUREACCESS_STREAMING, 
         context.window.width, 
         context.window.height
     );
 
-    context.sdl.frame_buffer_intermediate = malloc(
+    context.sdl.frame_buffer = malloc(
         sizeof(uint32_t) * context.window.width * context.window.height
     );
-    context.sdl.frame_buffer = RI_new_texture(context.window.width, context.window.height);
     
     if (!context.debug_flags)
         context.debug_flags = RI_DEBUG_ERRORS;
@@ -940,7 +985,7 @@ int RI_init(){
     cl_int result = clBuildProgram(rasterization_program, 1, &context.opencl.device, "", NULL, NULL);
 
     if (result != CL_SUCCESS){
-        char log[256];
+        char log[10001];
 
         clGetProgramBuildInfo(
             rasterization_program, 
@@ -993,18 +1038,30 @@ int RI_init(){
         return 1;
     }
 
+    context.opencl.textures_mem_buffer = clCreateBuffer(
+        context.opencl.context, 
+        CL_MEM_READ_WRITE, 
+        1, 
+        NULL, &clerror);
+
+    if (clerror != CL_SUCCESS || !context.opencl.textures_mem_buffer){
+        debug("couldn't create textures memory buffer", RI_DEBUG_OPENCL_ERROR);
+        return 1;
+    }
+
 // rasterizer(__global RI_renderable_face *renderable_faces, __global uint *frame_buffer, 
 //            int width, int height, int half_width, int half_height, int number_of_renderable_faces, 
 //            int number_of_split_renderable_faces)
 
     clSetKernelArg(context.opencl.rasterization_kernel, 0, sizeof(cl_mem), &context.opencl.renderable_faces_mem_buffer);
-    clSetKernelArg(context.opencl.rasterization_kernel, 1, sizeof(cl_mem), &context.opencl.frame_buffer_mem_buffer);
-    clSetKernelArg(context.opencl.rasterization_kernel, 2, sizeof(int), &context.window.width);
-    clSetKernelArg(context.opencl.rasterization_kernel, 3, sizeof(int), &context.window.height);
-    clSetKernelArg(context.opencl.rasterization_kernel, 4, sizeof(int), &context.window.half_width);
-    clSetKernelArg(context.opencl.rasterization_kernel, 5, sizeof(int), &context.window.half_height);
-    clSetKernelArg(context.opencl.rasterization_kernel, 6, sizeof(int), &context.current_renderable_face_index);
-    clSetKernelArg(context.opencl.rasterization_kernel, 7, sizeof(int), &context.current_split_renderable_face_index);
+    clSetKernelArg(context.opencl.rasterization_kernel, 1, sizeof(cl_mem), &context.opencl.textures_mem_buffer);
+    clSetKernelArg(context.opencl.rasterization_kernel, 2, sizeof(cl_mem), &context.opencl.frame_buffer_mem_buffer);
+    clSetKernelArg(context.opencl.rasterization_kernel, 3, sizeof(int), &context.window.width);
+    clSetKernelArg(context.opencl.rasterization_kernel, 4, sizeof(int), &context.window.height);
+    clSetKernelArg(context.opencl.rasterization_kernel, 5, sizeof(int), &context.window.half_width);
+    clSetKernelArg(context.opencl.rasterization_kernel, 6, sizeof(int), &context.window.half_height);
+    clSetKernelArg(context.opencl.rasterization_kernel, 7, sizeof(int), &context.current_renderable_face_index);
+    clSetKernelArg(context.opencl.rasterization_kernel, 8, sizeof(int), &context.current_split_renderable_face_index);
 
     // transformer
 
@@ -1096,9 +1153,14 @@ int RI_init(){
     // clSetKernelArg(context.opencl.transformation_kernel, 29, sizeof(int), &renderable_face_offset);
     // // 30: int face_sqrt
     // clSetKernelArg(context.opencl.transformation_kernel, 30, sizeof(int), &face_sqrt);
+    // // 31: uint16_t texture_width
+    // clSetKernelArg(context.opencl.transformation_kernel, 31, sizeof(uint16_t), &texture_width);
+    // // 32: uint16_t texture_height
+    // clSetKernelArg(context.opencl.transformation_kernel, 32, sizeof(uint16_t), &texture_height);
+    // // 33: uint32_t texture_index
+    // clSetKernelArg(context.opencl.transformation_kernel, 33, sizeof(uint32_t), &texture_index);
 
     context.defaults.default_actor->mesh = RI_load_mesh("objects/error_object.obj");
 
-
     return 0;
 }

BIN
textures/bill_mcdinner.png


BIN
textures/color_test.png


BIN
textures/cube_texture.png


BIN
textures/test_guy_texture.png


BIN
textures/test_texture.bmp


BIN
textures/test_texture.png


BIN
textures/test_texture_4_cube.bmp