Init grapher
This commit is contained in:
parent
7774f0a1c6
commit
b40c175e75
12
lib/array.c
12
lib/array.c
|
|
@ -9,7 +9,7 @@ int sizeof_str(char in[]) {
|
|||
return i;
|
||||
}
|
||||
|
||||
void print_strArray(char in[]) {
|
||||
void print_str_array(char in[]) {
|
||||
int i;
|
||||
printf("{ ");
|
||||
for (i = 0; i < sizeof_str(in); ++i) {
|
||||
|
|
@ -22,7 +22,7 @@ void print_strArray(char in[]) {
|
|||
}
|
||||
}
|
||||
|
||||
void print_intArray(intArray *in) {
|
||||
void print_int_array(int_array *in) {
|
||||
int i;
|
||||
printf("{ ");
|
||||
for (i = 0; i < in->length; ++i) {
|
||||
|
|
@ -35,7 +35,7 @@ void print_intArray(intArray *in) {
|
|||
}
|
||||
}
|
||||
|
||||
int check_intArray_sorted(intArray *in) {
|
||||
int check_int_array_sorted(int_array *in) {
|
||||
int i = 0;
|
||||
while(i < in->length - 1) {
|
||||
if (in->data[i] > in->data[i + 1])
|
||||
|
|
@ -47,7 +47,7 @@ int check_intArray_sorted(intArray *in) {
|
|||
}
|
||||
|
||||
/* Convert an integer array into a combined integer */
|
||||
int intArray_to_int(intArray *in) {
|
||||
int int_array_to_int(int_array *in) {
|
||||
int i;
|
||||
int out = 0;
|
||||
int current_digit;
|
||||
|
|
@ -57,10 +57,10 @@ int intArray_to_int(intArray *in) {
|
|||
}
|
||||
|
||||
/* Split an integer into an integer array */
|
||||
intArray int_to_intArray(int in) {
|
||||
int_array int_to_int_array(int in) {
|
||||
int in_length = int_count_digits(in);
|
||||
int out_data[in_length];
|
||||
intArray out;
|
||||
int_array out;
|
||||
out.data = out_data;
|
||||
out.length = in_length;
|
||||
int i;
|
||||
|
|
|
|||
14
lib/array.h
14
lib/array.h
|
|
@ -1,21 +1,21 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
typedef struct intArray {
|
||||
typedef struct int_array {
|
||||
unsigned int length;
|
||||
int *data;
|
||||
} intArray;
|
||||
} int_array;
|
||||
|
||||
int sizeof_str(char in[]);
|
||||
|
||||
void print_strArray(char in[]);
|
||||
void print_str_array(char in[]);
|
||||
|
||||
void print_intArray(intArray *in);
|
||||
void print_int_array(int_array *in);
|
||||
|
||||
int check_intArray_sorted(intArray *in);
|
||||
int check_int_array_sorted(int_array *in);
|
||||
|
||||
intArray int_to_intArray(int in);
|
||||
int_array int_to_int_array(int in);
|
||||
|
||||
int intArray_to_int(intArray *in);
|
||||
int int_array_to_int(int_array *in);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdint.h>
|
||||
#include "bmp.h"
|
||||
#include "math.h"
|
||||
|
||||
void grapher_draw_line(bitmap_file *bitmap_file_in, int32_t x_origin_in, int32_t y_origin_in, int32_t x_end_in, int32_t y_end_in) {
|
||||
float x_step = 0;
|
||||
float y_step = 0;
|
||||
int32_t x_origin_lerped;
|
||||
int32_t y_origin_lerped;
|
||||
bitmap_pixel_color target_pixel;
|
||||
target_pixel.red = 0;
|
||||
target_pixel.green = 0;
|
||||
target_pixel.blue = 0;
|
||||
|
||||
while (x_origin_in < x_end_in || y_origin_in < y_end_in) {
|
||||
x_origin_lerped = lerp(x_origin_in, x_end_in, x_step);
|
||||
y_origin_lerped = lerp(y_origin_in, y_end_in, y_step);
|
||||
|
||||
x_step += 0.1;
|
||||
y_step += 0.1;
|
||||
|
||||
if (x_step >= 1 || y_step >= 1)
|
||||
return;
|
||||
|
||||
write_bitmap_pixel(bitmap_file_in, &target_pixel, x_origin_in, y_origin_in);
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef GRAPHER_H
|
||||
#define GRAPHER_H
|
||||
|
||||
#include "bmp.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void grapher_draw_line(const bitmap_file *bitmap_file_in, const int32_t x_origin_in, const int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
#include "array.h"
|
||||
#include <stdint.h>
|
||||
|
||||
intArray find_divisors(int in) {
|
||||
int32_t lerp(int32_t x_in, int32_t y_in, float position) {
|
||||
return x_in + ((y_in - x_in) * position);
|
||||
}
|
||||
|
||||
int_array find_divisors(int in) {
|
||||
int i;
|
||||
intArray out;
|
||||
int_array out;
|
||||
for (i = 1; i <= in; ++i) {
|
||||
if (in % i == 0) {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
#ifndef MATH_H
|
||||
#define MATH_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "array.h"
|
||||
|
||||
intArray find_divisors();
|
||||
int32_t lerp(int32_t x_in, int32_t y_in, float position);
|
||||
int_array find_divisors();
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
#include "raytracer.h"
|
||||
|
||||
origin_3d multiply_origin_3d_by_scalar(origin_3d origin_3d_in, double scalar) {
|
||||
origin_3d_in.x *= scalar;
|
||||
origin_3d_in.y *= scalar;
|
||||
origin_3d_in.z *= scalar;
|
||||
return origin_3d_in;
|
||||
}
|
||||
|
||||
origin_3d add_origin_3d_together(origin_3d origin_3d_in_x, origin_3d *origin_3d_in_y) {
|
||||
origin_3d_in_x.x += origin_3d_in_y->x;
|
||||
origin_3d_in_x.y += origin_3d_in_y->y;
|
||||
origin_3d_in_x.z += origin_3d_in_y->z;
|
||||
return origin_3d_in_x;
|
||||
}
|
||||
|
||||
origin_3d get_point_along_vector_3d(vector_3d *vector_3d_in, double scalar) {
|
||||
origin_3d scaled_direction = multiply_origin_3d_by_scalar(vector_3d_in->direction, scalar);
|
||||
return add_origin_3d_together(vector_3d_in->origin, &scaled_direction);
|
||||
};
|
||||
|
||||
camera init_camera(origin_3d origin, double distance_to_canvas, double width_canvas_pixel_spacing, double height_canvas_pixel_spacing) {
|
||||
camera new_camera;
|
||||
new_camera.origin = origin;
|
||||
new_camera.distance_to_canvas = distance_to_canvas;
|
||||
new_camera.width_canvas_pixel_spacing = width_canvas_pixel_spacing;
|
||||
new_camera.height_canvas_pixel_spacing = height_canvas_pixel_spacing;
|
||||
};
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
#ifndef RAYTRACER_H
|
||||
#define RAYTRACER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct origin_3d {
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
} origin_3d;
|
||||
|
||||
typedef struct vector_3d {
|
||||
origin_3d origin;
|
||||
origin_3d direction;
|
||||
double magnitude;
|
||||
} vector_3d;
|
||||
|
||||
typedef struct raytracer_pixel_color {
|
||||
uint8_t blue;
|
||||
uint8_t green;
|
||||
uint8_t red;
|
||||
} raytracer_pixel_color;
|
||||
|
||||
typedef struct sphere_object {
|
||||
origin_3d origin;
|
||||
double radius;
|
||||
} sphere_object;
|
||||
|
||||
/* TODO: Probably better to have this allocated dynamically/make more advanced resizeable array datatype. Need to write decent memory allocator first. Also add more object types */
|
||||
typedef struct object_array {
|
||||
unsigned int length;
|
||||
sphere_object data[];
|
||||
} object_array;
|
||||
|
||||
typedef struct camera {
|
||||
origin_3d origin;
|
||||
double distance_to_canvas;
|
||||
double width_canvas_pixel_spacing;
|
||||
double height_canvas_pixel_spacing;
|
||||
double total_canvas_width;
|
||||
double total_canvas_height;
|
||||
} camera;
|
||||
|
||||
origin_3d multiply_origin_3d_by_scalar(origin_3d origin_3d_in, double scalar);
|
||||
origin_3d add_origin_3d_together(origin_3d origin_3d_in_x, origin_3d *origin_3d_in_y);
|
||||
origin_3d get_point_along_vector_3d(vector_3d *vector_3d_in, double magnitude);
|
||||
camera init_camera(origin_3d origin, double distance_to_canvas, double width_canvas_pixel_spacing, double height_canvas_pixel_spacing);
|
||||
|
||||
#endif
|
||||
21
main.c
21
main.c
|
|
@ -2,13 +2,14 @@
|
|||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <wctype.h>
|
||||
/*#include <wctype.h>
|
||||
#include "lib/temperature.h"
|
||||
#include "lib/array.h"
|
||||
#include "lib/base10.h"
|
||||
// #include "lib/math.h"
|
||||
#include "lib/alloc.h"
|
||||
#include "lib/alloc.h"*/
|
||||
#include "lib/math.h"
|
||||
#include "lib/bmp.h"
|
||||
#include "lib/grapher.h"
|
||||
|
||||
void swap_int(int *pX, int *pY) {
|
||||
int temp = *pX;
|
||||
|
|
@ -40,7 +41,7 @@ int main() {
|
|||
ss_alloc_free(reserved_memory);
|
||||
printf("Memory : %d/%d", ALLOC_SIZE, (int)ss_alloc_get_used_bytes());*/
|
||||
|
||||
const bitmap_header test = init_bitmap_header(1920, 1080);
|
||||
const bitmap_header test = init_bitmap_header(255, 255);
|
||||
const bitmap_file test_file = init_bitmap_file(&test, "test.bmp");
|
||||
bitmap_pixel_color target_pixel;
|
||||
|
||||
|
|
@ -48,12 +49,24 @@ int main() {
|
|||
int32_t height;
|
||||
for (height = 1; height <= test_file.bitmap_metadata->image_height; ++height) {
|
||||
for (width = 1; width <= test_file.bitmap_metadata->image_width; ++width) {
|
||||
/* Test hello world */
|
||||
/*
|
||||
target_pixel.red = ((float)width / (float)test_file.bitmap_metadata->image_width) * 255;
|
||||
target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
||||
target_pixel.blue = 100;
|
||||
*/
|
||||
|
||||
/* Simple skybox generator */
|
||||
target_pixel.red = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
||||
target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
||||
target_pixel.blue = 255;
|
||||
|
||||
write_bitmap_pixel(&test_file, &target_pixel, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
grapher_draw_line(&test_file, 50, 50, 200, 50);
|
||||
|
||||
close(test_file.fd);
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue