move types to type.h
This commit is contained in:
parent
04df9ab0d5
commit
3c6acb86fa
64
main.c
64
main.c
|
@ -16,7 +16,7 @@
|
|||
#define H 600
|
||||
#define PI 3.14159265358979323844
|
||||
|
||||
int wireframe = 0;
|
||||
int wireframe = 1;
|
||||
|
||||
void
|
||||
die(char *msg)
|
||||
|
@ -25,15 +25,14 @@ die(char *msg)
|
|||
exit(1);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
unsigned char r, g, b, a;
|
||||
} rgb;
|
||||
const rgb WHITE = {255, 255, 255, 255};
|
||||
const rgb BLACK = {0, 0, 0, 255};
|
||||
const rgb RED = {255, 0, 0, 255};
|
||||
const rgb GREEN = {0, 255, 0, 255};
|
||||
const rgb BLUE = {0, 0, 255, 255};
|
||||
|
||||
const vec3 UP = {0, 1, 0};
|
||||
|
||||
typedef struct {
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Window *window;
|
||||
|
@ -57,10 +56,6 @@ typedef struct {
|
|||
uint32_t *indices;
|
||||
} mesh;
|
||||
|
||||
typedef struct {
|
||||
double nums[3][3];
|
||||
} matrix3x3;
|
||||
|
||||
vec3
|
||||
vec3_matrix3x3_multiply(vec3 v, matrix3x3 r)
|
||||
{
|
||||
|
@ -92,11 +87,7 @@ new_camera(double range, vec3 pos, vec3 target, vec3 up)
|
|||
}
|
||||
|
||||
mesh
|
||||
new_mesh(uint32_t vertex_num,
|
||||
uint32_t tri_num,
|
||||
uint32_t index_num,
|
||||
vec3 vertices[],
|
||||
uint32_t indices[])
|
||||
new_mesh(uint32_t vertex_num, uint32_t tri_num, uint32_t index_num, vec3 vertices[], uint32_t indices[])
|
||||
{
|
||||
mesh ret;
|
||||
|
||||
|
@ -208,6 +199,7 @@ render(sdl_state state)
|
|||
SDL_RenderPresent(state.renderer);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
plot_rgb(rgb canvas[], int32_t x, int32_t y, rgb colour)
|
||||
{
|
||||
|
@ -244,16 +236,14 @@ draw_line(rgb canvas[], rgb c, vec2 p0, vec2 p1)
|
|||
swap(y0, y1);
|
||||
}
|
||||
|
||||
int32_t dx = abs(x1 - x0);
|
||||
int32_t dy = abs(y1 - y0);
|
||||
int32_t s = y1 > y0 ? 1 : -1;
|
||||
int32_t eps = 0;
|
||||
|
||||
int32_t y = y0;
|
||||
int32_t x = x0;
|
||||
int dx = abs(x1 - x0);
|
||||
int dy = abs(y1 - y0);
|
||||
int s = y1 > y0 ? 1 : -1;
|
||||
int eps = 0;
|
||||
|
||||
if (dy < dx) {
|
||||
for (; x <= x1; x++) {
|
||||
int y = y0;
|
||||
for (int x = x0; x <= x1; x++) {
|
||||
plot_rgb(canvas, x, y, c);
|
||||
eps += dy;
|
||||
if ((eps * 2) >= dx) {
|
||||
|
@ -263,7 +253,12 @@ draw_line(rgb canvas[], rgb c, vec2 p0, vec2 p1)
|
|||
}
|
||||
}
|
||||
else {
|
||||
for (; y <= y1; y++) {
|
||||
if (y0 >= y1) {
|
||||
swap(y0, y1);
|
||||
swap(x0, x1);
|
||||
}
|
||||
int x = x0;
|
||||
for (int y = y0; y <= y1; y++) {
|
||||
plot_rgb(canvas, x, y, c);
|
||||
eps += dx;
|
||||
if ((eps * 2) >= dy) {
|
||||
|
@ -277,9 +272,10 @@ draw_line(rgb canvas[], rgb c, vec2 p0, vec2 p1)
|
|||
void
|
||||
draw_horizontal_line(rgb canvas[], rgb c, int32_t x0, int32_t x1, int32_t y)
|
||||
{
|
||||
if (y < 0 || y >= H) return;
|
||||
if (y < 0) return;
|
||||
if (x0 < 0) x0 = 0;
|
||||
if (x1 >= W) x1 = W - 1;
|
||||
if (y >= H) return;
|
||||
|
||||
for (int32_t x = x0; x <= x1; x++)
|
||||
canvas[x + y * W] = c;
|
||||
|
@ -430,7 +426,7 @@ fill_triangle(rgb canvas[], rgb c, triangle2 tri)
|
|||
}
|
||||
else {
|
||||
vec2 trid = {
|
||||
(tri.a.x + (tri.b.y-tri.a.y) / (tri.c.y-tri.a.y) * (tri.c.x-tri.a.x)),
|
||||
(tri.a.x + (tri.b.y - tri.a.y) / (tri.c.y - tri.a.y) * (tri.c.x - tri.a.x)),
|
||||
tri.b.y};
|
||||
|
||||
if (tri.a.y > tri.b.y) {
|
||||
|
@ -479,10 +475,7 @@ screenshot(rgb canvas[])
|
|||
{
|
||||
time_t t = time(NULL);
|
||||
char output[64];
|
||||
strftime(output,
|
||||
sizeof(output),
|
||||
"screenshot-%Y%m%d-%H%M%S.ppm",
|
||||
localtime(&t));
|
||||
strftime(output, sizeof(output), "screenshot-%Y%m%d-%H%M%S.ppm", localtime(&t));
|
||||
FILE *out = fopen(output, "w");
|
||||
fprintf(out, "P6\n%d %d\n255\n", W, H);
|
||||
for (int y = 0; y < H; y++) {
|
||||
|
@ -622,10 +615,18 @@ main(void)
|
|||
|
||||
uint64_t t = 0;
|
||||
|
||||
matrix3x3 rotation = matrix3x3_multiply(
|
||||
matrix3x3_multiply(
|
||||
z_rotation(1.2),
|
||||
y_rotation(1.1)),
|
||||
x_rotation(1.0));
|
||||
|
||||
print_matrix3x3(rotation);
|
||||
|
||||
for (;;) {
|
||||
double elapsed, start = SDL_GetPerformanceCounter();
|
||||
|
||||
matrix3x3 m = matrix3x3_multiply(
|
||||
matrix3x3 rotation = matrix3x3_multiply(
|
||||
matrix3x3_multiply(
|
||||
z_rotation(1.2 * t),
|
||||
y_rotation(1.1 * t)),
|
||||
|
@ -642,7 +643,7 @@ main(void)
|
|||
clear_canvas(state.canvas, BLACK);
|
||||
|
||||
for (size_t i = 0; i < box.vertex_num; i++) {
|
||||
box_copy.vertices[i] = vec3_matrix3x3_multiply(box.vertices[i], m);
|
||||
box_copy.vertices[i] = vec3_matrix3x3_multiply(box.vertices[i], rotation);
|
||||
}
|
||||
|
||||
draw_mesh(state.canvas, WHITE, cam, box_copy);
|
||||
|
@ -651,8 +652,7 @@ main(void)
|
|||
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
|
||||
break;
|
||||
|
||||
elapsed = (SDL_GetPerformanceCounter() - start) /
|
||||
(double)SDL_GetPerformanceFrequency() * 1000.0;
|
||||
elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0;
|
||||
SDL_Delay(clamp(16.666f - elapsed, 0, 1000));
|
||||
|
||||
t++;
|
||||
|
|
48
types.h
48
types.h
|
@ -1,6 +1,22 @@
|
|||
#ifndef TYPEDEFS
|
||||
#define TYPEDEFS
|
||||
|
||||
typedef unsigned char byte;
|
||||
typedef double grid3x3[3][3];
|
||||
typedef double grid4x4[4][4];
|
||||
|
||||
#endif /* TYPEDEFS */
|
||||
|
||||
#ifdef SUB_INCLUDE_TYPES
|
||||
#undef SUB_INCLUDE_TYPES
|
||||
|
||||
STRUCT(rgb)
|
||||
FIELD(r, byte)
|
||||
FIELD(g, byte)
|
||||
FIELD(b, byte)
|
||||
FIELD(a, byte)
|
||||
END_STRUCT
|
||||
|
||||
STRUCT(vec2)
|
||||
FIELD(x, double)
|
||||
FIELD(y, double)
|
||||
|
@ -19,6 +35,14 @@ STRUCT(vec4)
|
|||
FIELD(w, double)
|
||||
END_STRUCT
|
||||
|
||||
STRUCT(matrix3x3)
|
||||
FIELD(nums, grid3x3)
|
||||
END_STRUCT
|
||||
|
||||
STRUCT(matrix4x4)
|
||||
FIELD(nums, grid4x4)
|
||||
END_STRUCT
|
||||
|
||||
STRUCT(camera)
|
||||
FIELD(range, double)
|
||||
FIELD(pos, vec3)
|
||||
|
@ -28,7 +52,7 @@ STRUCT(camera)
|
|||
FIELD(right, vec3)
|
||||
END_STRUCT
|
||||
|
||||
#else // sub include guard
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -37,15 +61,28 @@ END_STRUCT
|
|||
#define FIELD(N, T) T N;
|
||||
#define SUB_INCLUDE_TYPES
|
||||
#include __FILE__
|
||||
// undefs to get rid of redef warnings
|
||||
/* undefs to get rid of redef warnings */
|
||||
#undef STRUCT
|
||||
#undef FIELD
|
||||
#undef END_STRUCT
|
||||
|
||||
void print_double(double d) {
|
||||
printf(" %f ", d);
|
||||
void
|
||||
print_grid(size_t y_sz, size_t x_sz, double grid[y_sz][x_sz])
|
||||
{
|
||||
printf("\n");
|
||||
for (size_t y = 0; y < y_sz; y++) {
|
||||
for (size_t x = 0; x < x_sz; x++) {
|
||||
printf(" %f ", grid[y][x]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void print_double(double d) { printf(" %f ", d); }
|
||||
void print_byte(byte b) { printf(" %d ", b); }
|
||||
void print_grid3x3(grid3x3 grid) { print_grid(3, 3, grid); }
|
||||
void print_grid4x4(grid4x4 grid) { print_grid(4, 4, grid); }
|
||||
|
||||
#define STRUCT(N) \
|
||||
void print_##N (N s) { \
|
||||
printf("(" #N ") {");
|
||||
|
@ -61,8 +98,6 @@ void print_double(double d) {
|
|||
#undef FIELD
|
||||
#undef END_STRUCT
|
||||
|
||||
const vec3 UP = {0, 1, 0};
|
||||
|
||||
vec3
|
||||
vec3_add(vec3 u, vec3 v)
|
||||
{
|
||||
|
@ -126,5 +161,4 @@ vec4_sub(vec4 u, vec4 v)
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
#endif /* SUB_INCLUDE_TYPES */
|
||||
|
|
Loading…
Reference in New Issue