feat: rewrite to c
This commit is contained in:
4
.clangd
4
.clangd
@@ -1,3 +1,3 @@
|
|||||||
|
|
||||||
CompileFlags:
|
#CompileFlags:
|
||||||
Add: [-std=c++20]
|
# Add: [-std=c++20]
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ cmake_minimum_required(VERSION 3.15)
|
|||||||
|
|
||||||
project(SDFVisual VERSION 0.1
|
project(SDFVisual VERSION 0.1
|
||||||
DESCRIPTION "CPU SDF Renderer"
|
DESCRIPTION "CPU SDF Renderer"
|
||||||
LANGUAGES CXX)
|
LANGUAGES C)
|
||||||
|
|
||||||
find_package(Qt6 REQUIRED COMPONENTS
|
# find_package(Qt6 REQUIRED COMPONENTS
|
||||||
Widgets Gui
|
# Widgets Gui
|
||||||
)
|
# )
|
||||||
|
|
||||||
set (CMAKE_CXX_STANDARD 20)
|
# set (CMAKE_CXX_STANDARD 20)
|
||||||
add_executable(one main.cpp)
|
set (C_STANDARD 11)
|
||||||
|
add_executable(one src/main.c)
|
||||||
|
|
||||||
target_link_libraries(one m Qt6::Widgets Qt6::Gui)
|
# target_link_libraries(one m Qt6::Widgets Qt6::Gui)
|
||||||
# target_link_libraries(one m)
|
target_link_libraries(one m)
|
||||||
|
|||||||
43
parseObj.py
43
parseObj.py
@@ -1,5 +1,5 @@
|
|||||||
# Open the file in read mode
|
# Open the file in read mode
|
||||||
file = open("test.obj", "r")
|
file = open("plane.obj", "r")
|
||||||
|
|
||||||
# Read the entire content of the file
|
# Read the entire content of the file
|
||||||
content = file.read().split("\n")
|
content = file.read().split("\n")
|
||||||
@@ -8,13 +8,13 @@ file.close()
|
|||||||
|
|
||||||
startVerts = 0
|
startVerts = 0
|
||||||
verts = []
|
verts = []
|
||||||
for index,line in enumerate(content):
|
for index, line in enumerate(content):
|
||||||
if line[0] == 'o':
|
if line[0] == "o":
|
||||||
startVerts = index + 1
|
startVerts = index + 1
|
||||||
content = content[startVerts:]
|
content = content[startVerts:]
|
||||||
break
|
break
|
||||||
|
|
||||||
for index,line in enumerate(content):
|
for index, line in enumerate(content):
|
||||||
if "vn" in line:
|
if "vn" in line:
|
||||||
endVerts = index
|
endVerts = index
|
||||||
verts = content[:endVerts]
|
verts = content[:endVerts]
|
||||||
@@ -23,28 +23,41 @@ for index,line in enumerate(content):
|
|||||||
|
|
||||||
|
|
||||||
normals = []
|
normals = []
|
||||||
for index,line in enumerate(content):
|
for index, line in enumerate(content):
|
||||||
if "vt" in line:
|
if "vt" in line:
|
||||||
normals = content[:index]
|
normals = content[:index]
|
||||||
content = content[index:]
|
content = content[index:]
|
||||||
break
|
break
|
||||||
|
|
||||||
startFaces = 0;
|
startFaces = 0
|
||||||
faces = []
|
faces = []
|
||||||
for index,line in enumerate(content):
|
for index, line in enumerate(content):
|
||||||
if line[0] == 'f':
|
if line[0] == "f":
|
||||||
startFaces = index
|
startFaces = index
|
||||||
faces = content[startFaces:-1]
|
faces = content[startFaces:-1]
|
||||||
break
|
break
|
||||||
|
|
||||||
colors = ["vec3(" +",".join(vert.split(" ")[4:7]) + ")" for vert in verts]
|
colors = ["vec3(" + ",".join(vert.split(" ")[4:7]) + ")" for vert in verts]
|
||||||
verts = ["vec3(" +",".join(vert.split(" ")[1:4]) + ")" for vert in verts]
|
verts = ["{" + ",".join(vert.split(" ")[1:4]) + "}" for vert in verts]
|
||||||
faces = [ ",".join(["{" + str(int((d.split("/")[0])) - 1) + "," + str(int((d.split("/")[2])) - 1) + "}" for d in face.split(" ")[1:4]]) for face in faces]
|
faces = [
|
||||||
|
",".join([str(int((d.split("/")[0])) - 1) for d in face.split(" ")[1:4]])
|
||||||
|
for face in faces
|
||||||
|
]
|
||||||
normals = ["vec3(" + ",".join(normal.split(" ")[1:4]) + ")" for normal in normals]
|
normals = ["vec3(" + ",".join(normal.split(" ")[1:4]) + ")" for normal in normals]
|
||||||
|
|
||||||
out = "#include \"model.hpp\" \n const model testModel({" + ",".join(verts) +"},{" + ",".join(faces) + "},{" + ",".join(normals) + "},{"+ ",".join(colors)+"});"
|
out = (
|
||||||
|
'#include "../renderer.h" \n const model testModel = {(vec3[]){'
|
||||||
|
+ ",".join(verts)
|
||||||
|
+ "},(int[]){"
|
||||||
|
+ ",".join(faces)
|
||||||
|
+ "},"
|
||||||
|
+ str(len(verts))
|
||||||
|
+ ","
|
||||||
|
+ str(len(faces) * 3)
|
||||||
|
+ "};"
|
||||||
|
)
|
||||||
|
|
||||||
with open("testModel.hpp", "w") as f:
|
with open("src/models/plane.h", "w") as f:
|
||||||
f.write(out)
|
f.write(out)
|
||||||
print(out)
|
print(faces)
|
||||||
# Close the file
|
# Close the file
|
||||||
|
|||||||
600
src/linmath.h
Normal file
600
src/linmath.h
Normal file
@@ -0,0 +1,600 @@
|
|||||||
|
#ifndef LINMATH_H
|
||||||
|
#define LINMATH_H
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef LINMATH_NO_INLINE
|
||||||
|
#define LINMATH_H_FUNC static
|
||||||
|
#else
|
||||||
|
#define LINMATH_H_FUNC static inline
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||||
|
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||||
|
|
||||||
|
#define REPEAT_1(FN) FN(0)
|
||||||
|
#define REPEAT_2(FN) REPEAT_1(FN) FN(1)
|
||||||
|
#define REPEAT_3(FN) REPEAT_2(FN) FN(2)
|
||||||
|
#define REPEAT_4(FN) REPEAT_3(FN) FN(3)
|
||||||
|
|
||||||
|
#define add(i) r[i] = a[i] + b[i];
|
||||||
|
#define sub(i) r[i] = a[i] - b[i];
|
||||||
|
#define scale(i) r[i] = v[i] * s;
|
||||||
|
#define add_scale(i) r[i] = v[i] * s;
|
||||||
|
#define dot(i) p += a[i] * b[i];
|
||||||
|
#define min(i) r[i] = a[i] < b[i] ? a[i] : b[i];
|
||||||
|
#define max(i) r[i] = a[i] > b[i] ? a[i] : b[i];
|
||||||
|
#define dup(i) r[i] = src[i];
|
||||||
|
|
||||||
|
#define LINMATH_H_DEFINE_VEC(n) \
|
||||||
|
typedef float vec##n[n]; \
|
||||||
|
LINMATH_H_FUNC void vec##n##_add(vec##n r, vec##n const a, vec##n const b) { \
|
||||||
|
REPEAT_##n(add); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
LINMATH_H_FUNC void vec##n##_sub(vec##n r, vec##n const a, vec##n const b) { \
|
||||||
|
REPEAT_##n(sub); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
LINMATH_H_FUNC void vec##n##_scale(vec##n r, vec##n const v, \
|
||||||
|
float const s) { \
|
||||||
|
REPEAT_##n(scale); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_add_scale(vec##n r, vec##n const v, \
|
||||||
|
float const s) { \
|
||||||
|
REPEAT_##n(add_scale); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_to_str(vec##n const v, char *b) { \
|
||||||
|
strcat(b, "("); \
|
||||||
|
for (int i = 0; i < n; ++i) { \
|
||||||
|
char s[20] = {0}; \
|
||||||
|
sprintf(s, "%f, ", v[i]); \
|
||||||
|
strcat(b, s); \
|
||||||
|
} \
|
||||||
|
b[strlen(b) - 2] = '\0'; \
|
||||||
|
strcat(b, ")"); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_print(vec##n const v) { \
|
||||||
|
char r[n * 20 + 5] = {0}; \
|
||||||
|
vec##n##_to_str(v, r); \
|
||||||
|
strcat(r, "\n"); \
|
||||||
|
printf("%s", r); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC float vec##n##_dot(vec##n const a, vec##n const b) { \
|
||||||
|
float p = 0.f; \
|
||||||
|
REPEAT_##n(dot); \
|
||||||
|
return p; \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC float vec##n##_len(vec##n const v) { \
|
||||||
|
return sqrtf(vec##n##_dot(v, v)); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_norm(vec##n r, vec##n const v) { \
|
||||||
|
float k = 1.f / vec##n##_len(v); \
|
||||||
|
vec##n##_scale(r, v, k); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_min(vec##n r, vec##n const a, vec##n const b) { \
|
||||||
|
REPEAT_##n(min); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_max(vec##n r, vec##n const a, vec##n const b) { \
|
||||||
|
REPEAT_##n(max); \
|
||||||
|
} \
|
||||||
|
LINMATH_H_FUNC void vec##n##_dup(vec##n r, vec##n const src) { \
|
||||||
|
REPEAT_##n(dup) \
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_DEFINE_VEC(2)
|
||||||
|
LINMATH_H_DEFINE_VEC(3)
|
||||||
|
LINMATH_H_DEFINE_VEC(4)
|
||||||
|
|
||||||
|
#undef add
|
||||||
|
#undef sub
|
||||||
|
#undef scale
|
||||||
|
#undef add_scale
|
||||||
|
#undef dot
|
||||||
|
#undef min
|
||||||
|
#undef max
|
||||||
|
#undef dup
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b) {
|
||||||
|
r[0] = a[1] * b[2] - a[2] * b[1];
|
||||||
|
r[1] = a[2] * b[0] - a[0] * b[2];
|
||||||
|
r[2] = a[0] * b[1] - a[1] * b[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void vec3_reflect(vec3 r, vec3 const v, vec3 const n) {
|
||||||
|
float p = 2.f * vec3_dot(v, n);
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 3; ++i)
|
||||||
|
r[i] = v[i] - p * n[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void vec4_mul_cross(vec4 r, vec4 const a, vec4 const b) {
|
||||||
|
r[0] = a[1] * b[2] - a[2] * b[1];
|
||||||
|
r[1] = a[2] * b[0] - a[0] * b[2];
|
||||||
|
r[2] = a[0] * b[1] - a[1] * b[0];
|
||||||
|
r[3] = 1.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void vec4_reflect(vec4 r, vec4 const v, vec4 const n) {
|
||||||
|
float p = 2.f * vec4_dot(v, n);
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
r[i] = v[i] - p * n[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef vec4 mat4x4[4];
|
||||||
|
LINMATH_H_FUNC void mat4x4_identity(mat4x4 M) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
for (j = 0; j < 4; ++j)
|
||||||
|
M[i][j] = i == j ? 1.f : 0.f;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_dup(mat4x4 M, mat4x4 const N) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
vec4_dup(M[i], N[i]);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_row(vec4 r, mat4x4 const M, int i) {
|
||||||
|
int k;
|
||||||
|
for (k = 0; k < 4; ++k)
|
||||||
|
r[k] = M[k][i];
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_col(vec4 r, mat4x4 const M, int i) {
|
||||||
|
int k;
|
||||||
|
for (k = 0; k < 4; ++k)
|
||||||
|
r[k] = M[i][k];
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_transpose(mat4x4 M, mat4x4 const N) {
|
||||||
|
// Note: if M and N are the same, the user has to
|
||||||
|
// explicitly make a copy of M and set it to N.
|
||||||
|
int i, j;
|
||||||
|
for (j = 0; j < 4; ++j)
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
M[i][j] = N[j][i];
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_add(mat4x4 M, mat4x4 const a, mat4x4 const b) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
vec4_add(M[i], a[i], b[i]);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_sub(mat4x4 M, mat4x4 const a, mat4x4 const b) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
vec4_sub(M[i], a[i], b[i]);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_scale(mat4x4 M, mat4x4 const a, float k) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
vec4_scale(M[i], a[i], k);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_scale_aniso(mat4x4 M, mat4x4 const a, float x,
|
||||||
|
float y, float z) {
|
||||||
|
vec4_scale(M[0], a[0], x);
|
||||||
|
vec4_scale(M[1], a[1], y);
|
||||||
|
vec4_scale(M[2], a[2], z);
|
||||||
|
vec4_dup(M[3], a[3]);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_mul(mat4x4 M, mat4x4 const a, mat4x4 const b) {
|
||||||
|
mat4x4 temp;
|
||||||
|
int k, r, c;
|
||||||
|
for (c = 0; c < 4; ++c)
|
||||||
|
for (r = 0; r < 4; ++r) {
|
||||||
|
temp[c][r] = 0.f;
|
||||||
|
for (k = 0; k < 4; ++k)
|
||||||
|
temp[c][r] += a[k][r] * b[c][k];
|
||||||
|
}
|
||||||
|
mat4x4_dup(M, temp);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_mul_vec4(vec4 r, mat4x4 const M, vec4 const v) {
|
||||||
|
int i, j;
|
||||||
|
for (j = 0; j < 4; ++j) {
|
||||||
|
r[j] = 0.f;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
r[j] += M[i][j] * v[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_translate(mat4x4 T, float x, float y, float z) {
|
||||||
|
mat4x4_identity(T);
|
||||||
|
T[3][0] = x;
|
||||||
|
T[3][1] = y;
|
||||||
|
T[3][2] = z;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_translate_in_place(mat4x4 M, float x, float y,
|
||||||
|
float z) {
|
||||||
|
vec4 t = {x, y, z, 0};
|
||||||
|
vec4 r;
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 4; ++i) {
|
||||||
|
mat4x4_row(r, M, i);
|
||||||
|
M[3][i] += vec4_dot(r, t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 const a,
|
||||||
|
vec3 const b) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < 4; ++i)
|
||||||
|
for (j = 0; j < 4; ++j)
|
||||||
|
M[i][j] = i < 3 && j < 3 ? a[i] * b[j] : 0.f;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_rotate(mat4x4 R, mat4x4 const M, float x, float y,
|
||||||
|
float z, float angle) {
|
||||||
|
float s = sinf(angle);
|
||||||
|
float c = cosf(angle);
|
||||||
|
vec3 u = {x, y, z};
|
||||||
|
|
||||||
|
if (vec3_len(u) > 1e-4) {
|
||||||
|
vec3_norm(u, u);
|
||||||
|
mat4x4 T;
|
||||||
|
mat4x4_from_vec3_mul_outer(T, u, u);
|
||||||
|
|
||||||
|
mat4x4 S = {{0, u[2], -u[1], 0},
|
||||||
|
{-u[2], 0, u[0], 0},
|
||||||
|
{u[1], -u[0], 0, 0},
|
||||||
|
{0, 0, 0, 0}};
|
||||||
|
mat4x4_scale(S, S, s);
|
||||||
|
|
||||||
|
mat4x4 C;
|
||||||
|
mat4x4_identity(C);
|
||||||
|
mat4x4_sub(C, C, T);
|
||||||
|
|
||||||
|
mat4x4_scale(C, C, c);
|
||||||
|
|
||||||
|
mat4x4_add(T, T, C);
|
||||||
|
mat4x4_add(T, T, S);
|
||||||
|
|
||||||
|
T[3][3] = 1.f;
|
||||||
|
mat4x4_mul(R, M, T);
|
||||||
|
} else {
|
||||||
|
mat4x4_dup(R, M);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_rotate_X(mat4x4 Q, mat4x4 const M, float angle) {
|
||||||
|
float s = sinf(angle);
|
||||||
|
float c = cosf(angle);
|
||||||
|
mat4x4 R = {{1.f, 0.f, 0.f, 0.f},
|
||||||
|
{0.f, c, s, 0.f},
|
||||||
|
{0.f, -s, c, 0.f},
|
||||||
|
{0.f, 0.f, 0.f, 1.f}};
|
||||||
|
mat4x4_mul(Q, M, R);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_rotate_Y(mat4x4 Q, mat4x4 const M, float angle) {
|
||||||
|
float s = sinf(angle);
|
||||||
|
float c = cosf(angle);
|
||||||
|
mat4x4 R = {{c, 0.f, -s, 0.f},
|
||||||
|
{0.f, 1.f, 0.f, 0.f},
|
||||||
|
{s, 0.f, c, 0.f},
|
||||||
|
{0.f, 0.f, 0.f, 1.f}};
|
||||||
|
mat4x4_mul(Q, M, R);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_rotate_Z(mat4x4 Q, mat4x4 const M, float angle) {
|
||||||
|
float s = sinf(angle);
|
||||||
|
float c = cosf(angle);
|
||||||
|
mat4x4 R = {{c, s, 0.f, 0.f},
|
||||||
|
{-s, c, 0.f, 0.f},
|
||||||
|
{0.f, 0.f, 1.f, 0.f},
|
||||||
|
{0.f, 0.f, 0.f, 1.f}};
|
||||||
|
mat4x4_mul(Q, M, R);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_invert(mat4x4 T, mat4x4 const M) {
|
||||||
|
float s[6];
|
||||||
|
float c[6];
|
||||||
|
s[0] = M[0][0] * M[1][1] - M[1][0] * M[0][1];
|
||||||
|
s[1] = M[0][0] * M[1][2] - M[1][0] * M[0][2];
|
||||||
|
s[2] = M[0][0] * M[1][3] - M[1][0] * M[0][3];
|
||||||
|
s[3] = M[0][1] * M[1][2] - M[1][1] * M[0][2];
|
||||||
|
s[4] = M[0][1] * M[1][3] - M[1][1] * M[0][3];
|
||||||
|
s[5] = M[0][2] * M[1][3] - M[1][2] * M[0][3];
|
||||||
|
|
||||||
|
c[0] = M[2][0] * M[3][1] - M[3][0] * M[2][1];
|
||||||
|
c[1] = M[2][0] * M[3][2] - M[3][0] * M[2][2];
|
||||||
|
c[2] = M[2][0] * M[3][3] - M[3][0] * M[2][3];
|
||||||
|
c[3] = M[2][1] * M[3][2] - M[3][1] * M[2][2];
|
||||||
|
c[4] = M[2][1] * M[3][3] - M[3][1] * M[2][3];
|
||||||
|
c[5] = M[2][2] * M[3][3] - M[3][2] * M[2][3];
|
||||||
|
|
||||||
|
/* Assumes it is invertible */
|
||||||
|
float idet = 1.0f / (s[0] * c[5] - s[1] * c[4] + s[2] * c[3] + s[3] * c[2] -
|
||||||
|
s[4] * c[1] + s[5] * c[0]);
|
||||||
|
|
||||||
|
T[0][0] = (M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
|
||||||
|
T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
|
||||||
|
T[0][2] = (M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
|
||||||
|
T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet;
|
||||||
|
|
||||||
|
T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
|
||||||
|
T[1][1] = (M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
|
||||||
|
T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
|
||||||
|
T[1][3] = (M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
|
||||||
|
|
||||||
|
T[2][0] = (M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
|
||||||
|
T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
|
||||||
|
T[2][2] = (M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
|
||||||
|
T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
|
||||||
|
|
||||||
|
T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
|
||||||
|
T[3][1] = (M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
|
||||||
|
T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
|
||||||
|
T[3][3] = (M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_orthonormalize(mat4x4 R, mat4x4 const M) {
|
||||||
|
mat4x4_dup(R, M);
|
||||||
|
float s = 1.f;
|
||||||
|
vec3 h;
|
||||||
|
|
||||||
|
vec3_norm(R[2], R[2]);
|
||||||
|
|
||||||
|
s = vec3_dot(R[1], R[2]);
|
||||||
|
vec3_scale(h, R[2], s);
|
||||||
|
vec3_sub(R[1], R[1], h);
|
||||||
|
vec3_norm(R[1], R[1]);
|
||||||
|
|
||||||
|
s = vec3_dot(R[0], R[2]);
|
||||||
|
vec3_scale(h, R[2], s);
|
||||||
|
vec3_sub(R[0], R[0], h);
|
||||||
|
|
||||||
|
s = vec3_dot(R[0], R[1]);
|
||||||
|
vec3_scale(h, R[1], s);
|
||||||
|
vec3_sub(R[0], R[0], h);
|
||||||
|
vec3_norm(R[0], R[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t,
|
||||||
|
float n, float f) {
|
||||||
|
M[0][0] = 2.f * n / (r - l);
|
||||||
|
M[0][1] = M[0][2] = M[0][3] = 0.f;
|
||||||
|
|
||||||
|
M[1][1] = 2.f * n / (t - b);
|
||||||
|
M[1][0] = M[1][2] = M[1][3] = 0.f;
|
||||||
|
|
||||||
|
M[2][0] = (r + l) / (r - l);
|
||||||
|
M[2][1] = (t + b) / (t - b);
|
||||||
|
M[2][2] = -(f + n) / (f - n);
|
||||||
|
M[2][3] = -1.f;
|
||||||
|
|
||||||
|
M[3][2] = -2.f * (f * n) / (f - n);
|
||||||
|
M[3][0] = M[3][1] = M[3][3] = 0.f;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t,
|
||||||
|
float n, float f) {
|
||||||
|
M[0][0] = 2.f / (r - l);
|
||||||
|
M[0][1] = M[0][2] = M[0][3] = 0.f;
|
||||||
|
|
||||||
|
M[1][1] = 2.f / (t - b);
|
||||||
|
M[1][0] = M[1][2] = M[1][3] = 0.f;
|
||||||
|
|
||||||
|
M[2][2] = -2.f / (f - n);
|
||||||
|
M[2][0] = M[2][1] = M[2][3] = 0.f;
|
||||||
|
|
||||||
|
M[3][0] = -(r + l) / (r - l);
|
||||||
|
M[3][1] = -(t + b) / (t - b);
|
||||||
|
M[3][2] = -(f + n) / (f - n);
|
||||||
|
M[3][3] = 1.f;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_perspective(mat4x4 m, float y_fov, float aspect,
|
||||||
|
float n, float f) {
|
||||||
|
/* NOTE: Degrees are an unhandy unit to work with.
|
||||||
|
* linmath.h uses radians for everything! */
|
||||||
|
float const a = 1.f / tanf(y_fov / 2.f);
|
||||||
|
|
||||||
|
m[0][0] = a / aspect;
|
||||||
|
m[0][1] = 0.f;
|
||||||
|
m[0][2] = 0.f;
|
||||||
|
m[0][3] = 0.f;
|
||||||
|
|
||||||
|
m[1][0] = 0.f;
|
||||||
|
m[1][1] = a;
|
||||||
|
m[1][2] = 0.f;
|
||||||
|
m[1][3] = 0.f;
|
||||||
|
|
||||||
|
m[2][0] = 0.f;
|
||||||
|
m[2][1] = 0.f;
|
||||||
|
m[2][2] = -((f + n) / (f - n));
|
||||||
|
m[2][3] = -1.f;
|
||||||
|
|
||||||
|
m[3][0] = 0.f;
|
||||||
|
m[3][1] = 0.f;
|
||||||
|
m[3][2] = -((2.f * f * n) / (f - n));
|
||||||
|
m[3][3] = 0.f;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_look_at(mat4x4 m, vec3 const eye, vec3 const center,
|
||||||
|
vec3 const up) {
|
||||||
|
/* Adapted from Android's OpenGL Matrix.java. */
|
||||||
|
/* See the OpenGL GLUT documentation for gluLookAt for a description */
|
||||||
|
/* of the algorithm. We implement it in a straightforward way: */
|
||||||
|
|
||||||
|
/* TODO: The negation of of can be spared by swapping the order of
|
||||||
|
* operands in the following cross products in the right way. */
|
||||||
|
vec3 f;
|
||||||
|
vec3_sub(f, center, eye);
|
||||||
|
vec3_norm(f, f);
|
||||||
|
|
||||||
|
vec3 s;
|
||||||
|
vec3_mul_cross(s, f, up);
|
||||||
|
vec3_norm(s, s);
|
||||||
|
|
||||||
|
vec3 t;
|
||||||
|
vec3_mul_cross(t, s, f);
|
||||||
|
|
||||||
|
m[0][0] = s[0];
|
||||||
|
m[0][1] = t[0];
|
||||||
|
m[0][2] = -f[0];
|
||||||
|
m[0][3] = 0.f;
|
||||||
|
|
||||||
|
m[1][0] = s[1];
|
||||||
|
m[1][1] = t[1];
|
||||||
|
m[1][2] = -f[1];
|
||||||
|
m[1][3] = 0.f;
|
||||||
|
|
||||||
|
m[2][0] = s[2];
|
||||||
|
m[2][1] = t[2];
|
||||||
|
m[2][2] = -f[2];
|
||||||
|
m[2][3] = 0.f;
|
||||||
|
|
||||||
|
m[3][0] = 0.f;
|
||||||
|
m[3][1] = 0.f;
|
||||||
|
m[3][2] = 0.f;
|
||||||
|
m[3][3] = 1.f;
|
||||||
|
|
||||||
|
mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef float quat[4];
|
||||||
|
#define quat_add vec4_add
|
||||||
|
#define quat_sub vec4_sub
|
||||||
|
#define quat_norm vec4_norm
|
||||||
|
#define quat_scale vec4_scale
|
||||||
|
#define quat_dot vec4_dot
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void quat_identity(quat q) {
|
||||||
|
q[0] = q[1] = q[2] = 0.f;
|
||||||
|
q[3] = 1.f;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void quat_mul(quat r, quat const p, quat const q) {
|
||||||
|
vec3 w, tmp;
|
||||||
|
|
||||||
|
vec3_mul_cross(tmp, p, q);
|
||||||
|
vec3_scale(w, p, q[3]);
|
||||||
|
vec3_add(tmp, tmp, w);
|
||||||
|
vec3_scale(w, q, p[3]);
|
||||||
|
vec3_add(tmp, tmp, w);
|
||||||
|
|
||||||
|
vec3_dup(r, tmp);
|
||||||
|
r[3] = p[3] * q[3] - vec3_dot(p, q);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void quat_conj(quat r, quat const q) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 3; ++i)
|
||||||
|
r[i] = -q[i];
|
||||||
|
r[3] = q[3];
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void quat_rotate(quat r, float angle, vec3 const axis) {
|
||||||
|
vec3 axis_norm;
|
||||||
|
vec3_norm(axis_norm, axis);
|
||||||
|
float s = sinf(angle / 2);
|
||||||
|
float c = cosf(angle / 2);
|
||||||
|
vec3_scale(r, axis_norm, s);
|
||||||
|
r[3] = c;
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void quat_mul_vec3(vec3 r, quat const q, vec3 const v) {
|
||||||
|
/*
|
||||||
|
* Method by Fabian 'ryg' Giessen (of Farbrausch)
|
||||||
|
t = 2 * cross(q.xyz, v)
|
||||||
|
v' = v + q.w * t + cross(q.xyz, t)
|
||||||
|
*/
|
||||||
|
vec3 t;
|
||||||
|
vec3 q_xyz = {q[0], q[1], q[2]};
|
||||||
|
vec3 u = {q[0], q[1], q[2]};
|
||||||
|
|
||||||
|
vec3_mul_cross(t, q_xyz, v);
|
||||||
|
vec3_scale(t, t, 2);
|
||||||
|
|
||||||
|
vec3_mul_cross(u, q_xyz, t);
|
||||||
|
vec3_scale(t, t, q[3]);
|
||||||
|
|
||||||
|
vec3_add(r, v, t);
|
||||||
|
vec3_add(r, r, u);
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void mat4x4_from_quat(mat4x4 M, quat const q) {
|
||||||
|
float a = q[3];
|
||||||
|
float b = q[0];
|
||||||
|
float c = q[1];
|
||||||
|
float d = q[2];
|
||||||
|
float a2 = a * a;
|
||||||
|
float b2 = b * b;
|
||||||
|
float c2 = c * c;
|
||||||
|
float d2 = d * d;
|
||||||
|
|
||||||
|
M[0][0] = a2 + b2 - c2 - d2;
|
||||||
|
M[0][1] = 2.f * (b * c + a * d);
|
||||||
|
M[0][2] = 2.f * (b * d - a * c);
|
||||||
|
M[0][3] = 0.f;
|
||||||
|
|
||||||
|
M[1][0] = 2 * (b * c - a * d);
|
||||||
|
M[1][1] = a2 - b2 + c2 - d2;
|
||||||
|
M[1][2] = 2.f * (c * d + a * b);
|
||||||
|
M[1][3] = 0.f;
|
||||||
|
|
||||||
|
M[2][0] = 2.f * (b * d + a * c);
|
||||||
|
M[2][1] = 2.f * (c * d - a * b);
|
||||||
|
M[2][2] = a2 - b2 - c2 + d2;
|
||||||
|
M[2][3] = 0.f;
|
||||||
|
|
||||||
|
M[3][0] = M[3][1] = M[3][2] = 0.f;
|
||||||
|
M[3][3] = 1.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void mat4x4o_mul_quat(mat4x4 R, mat4x4 const M, quat const q) {
|
||||||
|
/* XXX: The way this is written only works for orthogonal matrices. */
|
||||||
|
/* TODO: Take care of non-orthogonal case. */
|
||||||
|
quat_mul_vec3(R[0], q, M[0]);
|
||||||
|
quat_mul_vec3(R[1], q, M[1]);
|
||||||
|
quat_mul_vec3(R[2], q, M[2]);
|
||||||
|
|
||||||
|
R[3][0] = R[3][1] = R[3][2] = 0.f;
|
||||||
|
R[0][3] = M[0][3];
|
||||||
|
R[1][3] = M[1][3];
|
||||||
|
R[2][3] = M[2][3];
|
||||||
|
R[3][3] = M[3][3]; // typically 1.0, but here we make it general
|
||||||
|
}
|
||||||
|
LINMATH_H_FUNC void quat_from_mat4x4(quat q, mat4x4 const M) {
|
||||||
|
float r = 0.f;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
int perm[] = {0, 1, 2, 0, 1};
|
||||||
|
int *p = perm;
|
||||||
|
|
||||||
|
for (i = 0; i < 3; i++) {
|
||||||
|
float m = M[i][i];
|
||||||
|
if (m < r)
|
||||||
|
continue;
|
||||||
|
m = r;
|
||||||
|
p = &perm[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
r = sqrtf(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]]);
|
||||||
|
|
||||||
|
if (r < 1e-6) {
|
||||||
|
q[0] = 1.f;
|
||||||
|
q[1] = q[2] = q[3] = 0.f;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
q[0] = r / 2.f;
|
||||||
|
q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]]) / (2.f * r);
|
||||||
|
q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]]) / (2.f * r);
|
||||||
|
q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]]) / (2.f * r);
|
||||||
|
}
|
||||||
|
|
||||||
|
LINMATH_H_FUNC void mat4x4_arcball(mat4x4 R, mat4x4 const M, vec2 const _a,
|
||||||
|
vec2 const _b, float s) {
|
||||||
|
vec2 a;
|
||||||
|
memcpy(a, _a, sizeof(a));
|
||||||
|
vec2 b;
|
||||||
|
memcpy(b, _b, sizeof(b));
|
||||||
|
|
||||||
|
float z_a = 0.;
|
||||||
|
float z_b = 0.;
|
||||||
|
|
||||||
|
if (vec2_len(a) < 1.) {
|
||||||
|
z_a = sqrtf(1. - vec2_dot(a, a));
|
||||||
|
} else {
|
||||||
|
vec2_norm(a, a);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vec2_len(b) < 1.) {
|
||||||
|
z_b = sqrtf(1. - vec2_dot(b, b));
|
||||||
|
} else {
|
||||||
|
vec2_norm(b, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 a_ = {a[0], a[1], z_a};
|
||||||
|
vec3 b_ = {b[0], b[1], z_b};
|
||||||
|
|
||||||
|
vec3 c_;
|
||||||
|
vec3_mul_cross(c_, a_, b_);
|
||||||
|
|
||||||
|
float const angle = acos(vec3_dot(a_, b_)) * s;
|
||||||
|
mat4x4_rotate(R, M, c_[0], c_[1], c_[2], angle);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
173
src/main.c
Normal file
173
src/main.c
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
#include "linmath.h"
|
||||||
|
#include "models/plane.h"
|
||||||
|
#include "renderer.h"
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define PI 3.141592653589793f
|
||||||
|
#define WIDTH 512
|
||||||
|
#define HEIGHT 512
|
||||||
|
#define PIX_COUNT (WIDTH * HEIGHT)
|
||||||
|
|
||||||
|
volatile sig_atomic_t stop = 0;
|
||||||
|
struct termios oldt;
|
||||||
|
void handle_sigint(int sig) { stop = 1; }
|
||||||
|
|
||||||
|
void restore_terminal() {
|
||||||
|
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
||||||
|
printf("\033[?1049l");
|
||||||
|
}
|
||||||
|
static char encoding_table[] = {
|
||||||
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||||
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||||
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
||||||
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
|
||||||
|
|
||||||
|
void base64encode(render_target *t, char *buff) {
|
||||||
|
for (int i = 0; i < PIX_COUNT * 4; i += 4) {
|
||||||
|
uint8_t r = t->pixels[i];
|
||||||
|
uint8_t g = t->pixels[i + 1];
|
||||||
|
uint8_t b = t->pixels[i + 2];
|
||||||
|
int s = 0;
|
||||||
|
s |= r << 16;
|
||||||
|
s |= g << 8;
|
||||||
|
s |= b;
|
||||||
|
|
||||||
|
buff[i] = encoding_table[s >> 18];
|
||||||
|
s &= 0777777;
|
||||||
|
buff[i + 1] = encoding_table[s >> 12];
|
||||||
|
s &= 07777;
|
||||||
|
buff[i + 2] = encoding_table[s >> 6];
|
||||||
|
s &= 077;
|
||||||
|
buff[i + 3] = encoding_table[s];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupKeyboard() {
|
||||||
|
|
||||||
|
struct termios newt;
|
||||||
|
|
||||||
|
// Save original terminal settings
|
||||||
|
if (tcgetattr(STDIN_FILENO, &oldt) == -1) {
|
||||||
|
perror("tcgetattr failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy old settings to newt
|
||||||
|
newt = oldt;
|
||||||
|
|
||||||
|
// Disable canonical mode (ICANON) and echo (ECHO)
|
||||||
|
newt.c_lflag &= ~(ICANON | ECHO);
|
||||||
|
newt.c_cc[VMIN] = 0; // minimum bytes
|
||||||
|
newt.c_cc[VTIME] = 0; // timeout (0 = no wait)
|
||||||
|
|
||||||
|
// Apply new settings
|
||||||
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &newt) == -1) {
|
||||||
|
perror("tcsetattr failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printImage(char *buff) {
|
||||||
|
int buffSize = PIX_COUNT * 4;
|
||||||
|
int start = 0;
|
||||||
|
int chunkSize = MIN(4096, buffSize - start);
|
||||||
|
int notLast = start < buffSize - 4096;
|
||||||
|
|
||||||
|
printf("\033_Gf=24,s=%d,v=%d,a=T,t=d,m=%d;%.*s\033\\", HEIGHT, WIDTH, notLast,
|
||||||
|
chunkSize, buff);
|
||||||
|
start += 4096;
|
||||||
|
while (start < buffSize) {
|
||||||
|
notLast = start < buffSize - 4096;
|
||||||
|
int chunkSize = MIN(4096, buffSize - start);
|
||||||
|
printf("\033_Gm=%d;%.*s\033\\", notLast, chunkSize, buff + start);
|
||||||
|
start += 4096;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
signal(SIGINT, handle_sigint);
|
||||||
|
printf("\033[?1049h\n");
|
||||||
|
|
||||||
|
setupKeyboard();
|
||||||
|
|
||||||
|
render_target target = {WIDTH, HEIGHT, malloc(PIX_COUNT * 4)};
|
||||||
|
clearTarget(&target);
|
||||||
|
|
||||||
|
// model m;
|
||||||
|
// m = create_model((vec3[]){{-2.0f, -2.0f, -10.0f},
|
||||||
|
// {-2.0f, 2.0f, -10.0f},
|
||||||
|
// {2.0f, 2.0f, -10.0f},
|
||||||
|
// {2.0f, -2.0f, -10.0f}},
|
||||||
|
// (int[]){0, 2, 1, 0, 3, 2}, 4, 6);
|
||||||
|
// vec3_print(m.verts[0]);
|
||||||
|
vec4 *buffer = malloc(sizeof(vec4) * testModel.vert_size);
|
||||||
|
mat4x4 projMat = {};
|
||||||
|
mat4x4 viewMat = {};
|
||||||
|
mat4x4 drawMat = {};
|
||||||
|
mat4x4_identity(viewMat);
|
||||||
|
mat4x4_perspective(projMat, 45.0 / 180.0 * PI, 1, 2.f, 20.0f);
|
||||||
|
|
||||||
|
const int buffSize = PIX_COUNT * 4;
|
||||||
|
|
||||||
|
char *encodeBuff = malloc(buffSize);
|
||||||
|
memset(encodeBuff, 0, buffSize);
|
||||||
|
char lastKey;
|
||||||
|
|
||||||
|
clock_t start = clock();
|
||||||
|
clock_t now = clock();
|
||||||
|
clock_t renderC;
|
||||||
|
|
||||||
|
while (!stop) {
|
||||||
|
now = clock();
|
||||||
|
printf("total time: %fms\n",
|
||||||
|
((float)(now - start)) / (0.001 * CLOCKS_PER_SEC));
|
||||||
|
printf("render time: %fms\n", ((float)renderC) / (0.001 * CLOCKS_PER_SEC));
|
||||||
|
start = now;
|
||||||
|
|
||||||
|
if (read(STDIN_FILENO, &lastKey, 1) == -1) {
|
||||||
|
perror("read failed");
|
||||||
|
stop = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (lastKey) {
|
||||||
|
case 'w':
|
||||||
|
mat4x4_translate_in_place(viewMat, 0, 0, -0.2);
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
mat4x4_translate_in_place(viewMat, 0, 0, 0.2);
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
mat4x4_translate_in_place(viewMat, -0.2, 0, 0);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
mat4x4_translate_in_place(viewMat, 0.2, 0, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastKey = 0;
|
||||||
|
|
||||||
|
// mat4x4_translate_in_place(viewMat, 0, 0, .1);
|
||||||
|
mat4x4_mul(drawMat, projMat, viewMat);
|
||||||
|
clearTarget(&target);
|
||||||
|
renderC = clock();
|
||||||
|
render(&target, &testModel, &drawMat, buffer);
|
||||||
|
renderC = clock() - renderC;
|
||||||
|
|
||||||
|
base64encode(&target, encodeBuff);
|
||||||
|
printf("\033[H");
|
||||||
|
printImage(encodeBuff);
|
||||||
|
printf("\033[J");
|
||||||
|
}
|
||||||
|
|
||||||
|
restore_terminal();
|
||||||
|
}
|
||||||
2
src/models/plane.h
Normal file
2
src/models/plane.h
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#include "../renderer.h"
|
||||||
|
const model testModel = {(vec3[]){{1.000000,1.000000,-11.000000},{1.000000,-1.000000,-11.000000},{1.000000,1.000000,-9.000000},{1.000000,-1.000000,-9.000000},{-1.000000,1.000000,-11.000000},{-1.000000,-1.000000,-11.000000},{-1.000000,1.000000,-9.000000},{-1.000000,-1.000000,-9.000000}},(int[]){4,2,0,2,7,3,6,5,7,1,7,5,0,3,1,4,1,5,4,6,2,2,6,7,6,4,5,1,3,7,0,2,3,4,0,1},8,36};
|
||||||
83
src/polygon.h
Normal file
83
src/polygon.h
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
|
||||||
|
#ifndef POLYGON_H
|
||||||
|
#define POLYGON_H
|
||||||
|
|
||||||
|
#include "linmath.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
vec3 points[3];
|
||||||
|
vec3 colors[3];
|
||||||
|
vec3 normals[3];
|
||||||
|
|
||||||
|
vec3 barycentrics;
|
||||||
|
float baryFactor;
|
||||||
|
|
||||||
|
float dot00;
|
||||||
|
float dot01;
|
||||||
|
float dot11;
|
||||||
|
float v0[2];
|
||||||
|
vec2 v1;
|
||||||
|
|
||||||
|
float bounding[4]; // min x, max x, min y, max y
|
||||||
|
|
||||||
|
} polygon;
|
||||||
|
|
||||||
|
void preCalc(polygon *p) {
|
||||||
|
|
||||||
|
p->v0[0] = p->points[2][0] - p->points[0][0];
|
||||||
|
p->v0[1] = p->points[2][1] - p->points[0][1];
|
||||||
|
|
||||||
|
p->v1[0] = p->points[1][0] - p->points[0][0];
|
||||||
|
p->v1[1] = p->points[1][1] - p->points[0][1];
|
||||||
|
|
||||||
|
p->dot00 = vec3_dot(p->v0, p->v0);
|
||||||
|
p->dot01 = vec3_dot(p->v1, p->v0);
|
||||||
|
p->dot11 = vec3_dot(p->v1, p->v1);
|
||||||
|
|
||||||
|
p->baryFactor = 1.0f / (p->dot00 * p->dot11 - p->dot01 * p->dot01);
|
||||||
|
|
||||||
|
p->bounding[0] = p->points[0][0];
|
||||||
|
p->bounding[1] = p->points[0][0];
|
||||||
|
p->bounding[2] = p->points[0][1];
|
||||||
|
p->bounding[3] = p->points[0][1];
|
||||||
|
for (int i = 1; i < 3; i++) {
|
||||||
|
if (p->bounding[0] > p->points[i][0])
|
||||||
|
p->bounding[0] = p->points[i][0];
|
||||||
|
if (p->bounding[1] < p->points[i][0])
|
||||||
|
p->bounding[1] = p->points[i][0];
|
||||||
|
if (p->bounding[2] > p->points[i][1])
|
||||||
|
p->bounding[2] = p->points[i][1];
|
||||||
|
if (p->bounding[3] < p->points[i][1])
|
||||||
|
p->bounding[3] = p->points[i][1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void calcBarycentric(polygon *p, vec3 s) {
|
||||||
|
|
||||||
|
vec2 v2 = {s[0] - p->points[0][0], s[1] - p->points[0][1]};
|
||||||
|
float dot02 = vec3_dot(p->v0, v2);
|
||||||
|
float dot12 = vec3_dot(p->v1, v2);
|
||||||
|
|
||||||
|
p->barycentrics[0] = (p->dot11 * dot02 - p->dot01 * dot12) * p->baryFactor;
|
||||||
|
p->barycentrics[1] = (p->dot00 * dot12 - p->dot01 * dot02) * p->baryFactor;
|
||||||
|
|
||||||
|
p->barycentrics[2] = 1.f - p->barycentrics[1] - p->barycentrics[0];
|
||||||
|
}
|
||||||
|
void calcNormal(vec3 d, polygon *p) {
|
||||||
|
vec3_scale(d, p->normals[0], p->barycentrics[0]);
|
||||||
|
vec3_add_scale(d, p->normals[1], p->barycentrics[1]);
|
||||||
|
vec3_add_scale(d, p->normals[2], p->barycentrics[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void calcColor(vec3 d, polygon *p) {
|
||||||
|
vec3_scale(d, p->colors[0], p->barycentrics[0]);
|
||||||
|
vec3_add_scale(d, p->colors[1], p->barycentrics[1]);
|
||||||
|
vec3_add_scale(d, p->colors[2], p->barycentrics[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
float calcDepth(polygon *p) {
|
||||||
|
return p->points[0][0] * p->barycentrics[0] +
|
||||||
|
p->points[1][0] * p->barycentrics[1] +
|
||||||
|
p->points[2][0] * p->barycentrics[2];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
8
src/quick.h
Normal file
8
src/quick.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef QUICK_H
|
||||||
|
#define QUICK_H
|
||||||
|
|
||||||
|
struct vec3 {
|
||||||
|
int i[3];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
176
src/renderer.h
Normal file
176
src/renderer.h
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
#ifndef RENDERER_H
|
||||||
|
#define RENDERER_H
|
||||||
|
|
||||||
|
#include "linmath.h"
|
||||||
|
#include "rendertarget.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
vec3 *verts;
|
||||||
|
// vec3 *normals;
|
||||||
|
// vec3 *color;
|
||||||
|
int *index;
|
||||||
|
int vert_size;
|
||||||
|
int index_size;
|
||||||
|
} model;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
vec4 *points[3];
|
||||||
|
float dot00;
|
||||||
|
float dot01;
|
||||||
|
float dot11;
|
||||||
|
vec2 v0;
|
||||||
|
vec2 v1;
|
||||||
|
vec3 norm;
|
||||||
|
float baryFactor;
|
||||||
|
float bounding[4]; // min x, max x, min y, max y
|
||||||
|
|
||||||
|
vec3 barycentrics;
|
||||||
|
} polygon;
|
||||||
|
|
||||||
|
// model static inline create_model(vec3 *verts, int *index, int vert_size,
|
||||||
|
// int index_size) {
|
||||||
|
// model m = {verts, 0, 0, index, vert_size, index_size};
|
||||||
|
// // printf("%f\n", verts[0][0]);
|
||||||
|
// vec3_print(verts[0]);
|
||||||
|
// return m;
|
||||||
|
// };
|
||||||
|
|
||||||
|
void initPoly(polygon *p) {
|
||||||
|
vec4 **points = p->points;
|
||||||
|
float *bounding = p->bounding;
|
||||||
|
|
||||||
|
// vec2_sub(p->v0, *points[2], *points[0]);
|
||||||
|
// vec2_sub(p->v1, *points[1], *points[0]);
|
||||||
|
|
||||||
|
vec3 v0 = {};
|
||||||
|
vec3 v1 = {};
|
||||||
|
vec3_sub(v0, *points[2], *points[0]);
|
||||||
|
vec3_sub(v1, *points[1], *points[0]);
|
||||||
|
|
||||||
|
vec3_mul_cross(p->norm, v0, v1);
|
||||||
|
|
||||||
|
vec2_dup(p->v0, v0);
|
||||||
|
vec2_dup(p->v1, v1);
|
||||||
|
|
||||||
|
p->dot00 = vec2_dot(p->v0, p->v0);
|
||||||
|
p->dot01 = vec2_dot(p->v0, p->v1);
|
||||||
|
p->dot11 = vec2_dot(p->v1, p->v1);
|
||||||
|
|
||||||
|
p->baryFactor = 1.0f / (p->dot00 * p->dot11 - p->dot01 * p->dot01);
|
||||||
|
|
||||||
|
bounding[0] = (*points[0])[0];
|
||||||
|
bounding[1] = (*points[0])[0];
|
||||||
|
bounding[2] = (*points[0])[1];
|
||||||
|
bounding[3] = (*points[0])[1];
|
||||||
|
|
||||||
|
for (int i = 1; i < 3; i++) {
|
||||||
|
if (bounding[0] > (*points[i])[0])
|
||||||
|
bounding[0] = (*points[i])[0];
|
||||||
|
if (bounding[1] < (*points[i])[0])
|
||||||
|
bounding[1] = (*points[i])[0];
|
||||||
|
if (bounding[2] > (*points[i])[1])
|
||||||
|
bounding[2] = (*points[i])[1];
|
||||||
|
if (bounding[3] < (*points[i])[1])
|
||||||
|
bounding[3] = (*points[i])[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void calcBarycentrics(polygon *p, vec2 s, vec3 r) {
|
||||||
|
vec2 v2 = {};
|
||||||
|
vec2_sub(v2, s, *p->points[0]);
|
||||||
|
|
||||||
|
float dot02 = vec2_dot(p->v0, v2);
|
||||||
|
float dot12 = vec2_dot(p->v1, v2);
|
||||||
|
|
||||||
|
r[2] = p->dot11 * dot02 - p->dot01 * dot12;
|
||||||
|
r[1] = p->dot00 * dot12 - p->dot01 * dot02;
|
||||||
|
vec3_scale(r, r, p->baryFactor);
|
||||||
|
r[0] = 1.f - r[1] - r[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
float applyBary(const vec3 bary, float a, float b, float c) {
|
||||||
|
// float result = 0;
|
||||||
|
return bary[0] * a + bary[1] * b + bary[2] * c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void render(render_target *target, const model *model, mat4x4 *matrix,
|
||||||
|
vec4 *buffer) {
|
||||||
|
|
||||||
|
vec4 tmp = {};
|
||||||
|
|
||||||
|
for (int i = 0; i < model->vert_size; i++) {
|
||||||
|
tmp[0] = model->verts[i][0];
|
||||||
|
tmp[1] = model->verts[i][1];
|
||||||
|
tmp[2] = model->verts[i][2];
|
||||||
|
tmp[3] = 1.0f;
|
||||||
|
|
||||||
|
mat4x4_mul_vec4(buffer[i], *matrix, tmp);
|
||||||
|
|
||||||
|
vec4_scale(buffer[i], buffer[i], 1.0 / buffer[i][3]);
|
||||||
|
vec4_print(buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
polygon p = {};
|
||||||
|
|
||||||
|
for (int i = 0; i < model->index_size; i += 3) {
|
||||||
|
p.points[0] = buffer + model->index[i];
|
||||||
|
p.points[1] = buffer + model->index[i + 1];
|
||||||
|
p.points[2] = buffer + model->index[i + 2];
|
||||||
|
|
||||||
|
initPoly(&p);
|
||||||
|
|
||||||
|
if (p.norm[2] > 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
printf("baryFac: %f\n", p.baryFactor);
|
||||||
|
|
||||||
|
// printf("min x :%f, min y: %f\nmax x: %f, max y: %f \n", p.bounding[0],
|
||||||
|
// p.bounding[2], p.bounding[1], p.bounding[3]);
|
||||||
|
|
||||||
|
// int startX = floor((MAX(-1.0f, p.bounding[0])+1.0)*target->width*0.5);
|
||||||
|
// int startY = floor((MAX(-1.0f, p.bounding[2])+1.0)*target->height*0.5);
|
||||||
|
//
|
||||||
|
// int endX = floor((MIN(1.0f, p.bounding[1])+1.0f)*target->width*0.5);
|
||||||
|
// int endY = floor((MAX(1.0f, p.bounding[3])+1.0f)*target->height*0.5);
|
||||||
|
float texWidth = 2.0f / target->width;
|
||||||
|
float texHight = 2.0f / target->width;
|
||||||
|
|
||||||
|
float startX = MAX(-1.0f, p.bounding[0]);
|
||||||
|
float startY = MAX(-1.0f, p.bounding[2]);
|
||||||
|
float endX = MIN(1.0f, p.bounding[1]);
|
||||||
|
float endY = MIN(1.0f, p.bounding[3]);
|
||||||
|
|
||||||
|
// printf("min x :%f, min y: %f\n max x: %f, max y: %f \n", startX, startY,
|
||||||
|
// endX, endY);
|
||||||
|
//
|
||||||
|
vec2 sp = {};
|
||||||
|
vec3 bary = {};
|
||||||
|
vec4 val = {0};
|
||||||
|
|
||||||
|
for (float y = startY; y < endY; y += texHight) {
|
||||||
|
for (float x = startX; x < endX; x += texWidth) {
|
||||||
|
sp[0] = x;
|
||||||
|
sp[1] = y;
|
||||||
|
calcBarycentrics(&p, sp, bary);
|
||||||
|
if (bary[0] >= 0.0f && bary[1] >= 0.0f && bary[2] >= 0.0f) {
|
||||||
|
uint8_t depth = (uint8_t)(255.0 * applyBary(bary, (*p.points[0])[2],
|
||||||
|
(*p.points[1])[2],
|
||||||
|
(*p.points[2])[2]));
|
||||||
|
int pX = (int)((x + 1.f) * target->width * 0.5f);
|
||||||
|
int pY = (int)((-y + 1.f) * target->height * 0.5f);
|
||||||
|
if (depth < getDepth(target, pX, pY)) {
|
||||||
|
vec3_dup(val, bary);
|
||||||
|
// val[0] = ((float)depth) / 255.0f;
|
||||||
|
// val[1] = ((float)depth) / 255.0f;
|
||||||
|
// val[2] = ((float)depth) / 255.0f;
|
||||||
|
val[3] = ((float)depth) / 255.0f;
|
||||||
|
|
||||||
|
set(target, pX, pY, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
55
src/rendertarget.h
Normal file
55
src/rendertarget.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#ifndef RENDER_TARGET
|
||||||
|
#define RENDER_TARGET
|
||||||
|
#include "linmath.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
typedef struct {
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
uint8_t *pixels;
|
||||||
|
} render_target;
|
||||||
|
|
||||||
|
int bufferSize(int width, int height) {
|
||||||
|
return width * height * 4 * sizeof(uint8_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearTarget(render_target *r) {
|
||||||
|
memset(r->pixels, 0, 4 * r->height * r->width * sizeof(uint8_t));
|
||||||
|
for (int i = 3; i < r->height * r->height * 4; i += 4) {
|
||||||
|
r->pixels[i] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setColor(render_target *r, int x, int y, vec3 val) {
|
||||||
|
int start = (r->width * y + x) * 4;
|
||||||
|
r->pixels[start + 0] = (uint8_t)(val[0] * 255);
|
||||||
|
r->pixels[start + 1] = (uint8_t)(val[1] * 255);
|
||||||
|
r->pixels[start + 2] = (uint8_t)(val[2] * 255);
|
||||||
|
}
|
||||||
|
void getColor(render_target *r, int x, int y, int dst[3]) {
|
||||||
|
int start = (r->width * y + x) * 4;
|
||||||
|
dst[0] = r->pixels[start + 0];
|
||||||
|
dst[1] = r->pixels[start + 1];
|
||||||
|
dst[2] = r->pixels[start + 2];
|
||||||
|
}
|
||||||
|
void set(render_target *r, int x, int y, vec4 val) {
|
||||||
|
int start = (r->width * y + x) * 4;
|
||||||
|
r->pixels[start + 0] = (uint8_t)(val[0] * 255);
|
||||||
|
r->pixels[start + 1] = (uint8_t)(val[1] * 255);
|
||||||
|
r->pixels[start + 2] = (uint8_t)(val[2] * 255);
|
||||||
|
r->pixels[start + 3] = (uint8_t)(val[3] * 255);
|
||||||
|
}
|
||||||
|
void get(render_target *r, int x, int y, int dst[4]) {
|
||||||
|
int start = (r->width * y + x) * 4;
|
||||||
|
dst[0] = r->pixels[start + 0];
|
||||||
|
dst[1] = r->pixels[start + 1];
|
||||||
|
dst[2] = r->pixels[start + 2];
|
||||||
|
dst[3] = r->pixels[start + 3];
|
||||||
|
}
|
||||||
|
void setDepth(render_target *r, int x, int y, float val) {
|
||||||
|
r->pixels[(r->width * y + x) * 4 + 3] = (uint8_t)(val * 255);
|
||||||
|
}
|
||||||
|
uint8_t getDepth(render_target *r, int x, int y) {
|
||||||
|
return r->pixels[(r->width * y + x) * 4 + 3];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user