polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 87d5d264003728b2a043e3aabff83afd3ab56934
parent 5b9cecaf6d566ee3f2b0cb0684eec2ce247dbbca
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 24 Apr 2018 19:07:22 -0700

looks nice now

Diffstat:
MMakefile | 1+
Metc/shaders/test.v.glsl | 2+-
Msrc/game.c | 2+-
Msrc/render.c | 2+-
Msrc/terrain.c | 76++++++++++++++++++++++++++++++++++++++++++----------------------------------
Msrc/util.h | 7-------
Asrc/vec3/vec3.c | 204+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/vec3/vec3.h | 7+++++++
8 files changed, 257 insertions(+), 44 deletions(-)

diff --git a/Makefile b/Makefile @@ -19,6 +19,7 @@ OBJS += $(SRC)/event.o OBJS += $(SRC)/file.o OBJS += $(SRC)/game.o OBJS += $(SRC)/mat4/mat4.o +OBJS += $(SRC)/vec3/vec3.o OBJS += $(SRC)/render.o OBJS += $(SRC)/shader.o OBJS += $(SRC)/update.o diff --git a/etc/shaders/test.v.glsl b/etc/shaders/test.v.glsl @@ -15,6 +15,6 @@ void main() { vec4 trans_normal = normal_matrix * vec4(normal, 1); gl_Position = mvp * vec4(position.xyz, 1.0); - v_dot = dot(trans_normal.xyz, vec3(1,1,0)); + v_dot = dot(trans_normal.xyz, vec3(1.0,1.0,0.0)); v_norm = normal; } diff --git a/src/game.c b/src/game.c @@ -18,7 +18,7 @@ void game_init(struct game *game) { mat4_id(camera); // move the camera a bit - /* mat4_translate(camera, -1.0f, -1.0f, -7.0f, camera); */ + /* mat4_translate(camera, 1.0f, 1.0f, 20.0f, camera); */ mat4_copy(cam_init, camera); input_init(&game->input); diff --git a/src/render.c b/src/render.c @@ -207,9 +207,9 @@ void render (struct resources * resources, struct geometry *geom) { mat4_multiply(persp, camera, tmp_matrix); mat4_multiply(tmp_matrix, mvp, tmp_matrix); recalc_normals(resources->uniforms.normal_matrix, tmp_matrix, normal); + glUniform3f(resources->uniforms.light_dir, 1.0f, 1.0f, 0.0f); glUseProgram(resources->program); - glUniform3f(resources->uniforms.light_dir, -1, 1, -0.099f); glUniform1f(resources->uniforms.fade_factor, fade_factor); glUniformMatrix4fv(resources->uniforms.mvp, 1, 0, tmp_matrix); diff --git a/src/terrain.c b/src/terrain.c @@ -2,6 +2,7 @@ #include "terrain.h" #include "util.h" #include "delaunay.h" +#include "vec3/vec3.h" static const float plane_verts[] = { -1,-1,0, -1,1,0, 1,1,0, 1,-1,0 @@ -17,8 +18,11 @@ static const u32 plane_indices[] = { double old_noisy_boi(double x, double y, double *dx, double *dy) { - double z = sin(x/5.0) * cos(y/5.0) * 5.0; - /* *dx = */ + double c = 5.0; + double h = c; + double z = sin(x/c) * cos(y/c) * h; + *dx = (cos(x/c) * cos(y/c) * h)/c; + *dy = (sin(x/c) * sin(y/c) * h)/c; return z; } @@ -31,7 +35,10 @@ terrain_init(struct terrain *terrain) { void terrain_create(struct terrain *terrain) { u32 i; - const int num_verts = 10000; + const int num_verts = 5000; + float tmp1[3]; + float tmp2[3]; + float norm[3]; del_point2d_t *points = calloc(num_verts, sizeof(*points)); float *zs = calloc(num_verts * 3, sizeof(*zs)); float *verts = calloc(num_verts * 3, sizeof(*verts)); @@ -39,7 +46,7 @@ terrain_create(struct terrain *terrain) { /* int *indices = calloc(num_verts, sizeof(*indices)); */ // 100 random samples from our noise function - for (i = 0; i < num_verts; i++) { + for (i = 0; i < (u32)num_verts; i++) { int n = i*3; double dx, dy; double x = rand_0to1() * 100.0; @@ -54,9 +61,21 @@ terrain_create(struct terrain *terrain) { verts[n+1] = (float)y; verts[n+2] = (float)z; - normals[n] = 0; - normals[n+1] = 0; - normals[n+2] = 1; + + // ^i * dx + vec3_scale((float[3]){1,0,0}, dx, tmp1); + // ^k - (^i * dx) + vec3_subtract((float[3]){0,0,1}, tmp1, tmp2); + + // ^j * dy + vec3_scale((float[3]){0,1,0}, dy, tmp1); + + // (^k - (^i * dx)) - ^j * dy + vec3_subtract(tmp2, tmp1, tmp2); + + normals[n] = tmp2[0]; + normals[n+1] = tmp2[1]; + normals[n+2] = tmp2[2]; } delaunay2d_t *del = delaunay2d_from(points, num_verts); @@ -72,9 +91,9 @@ terrain_create(struct terrain *terrain) { verts[n] = (float)tri->points[i].x; verts[n+1] = (float)tri->points[i].y; - normals[n] = rand_0to1(); - normals[n+1] = rand_0to1(); - normals[n+2] = rand_0to1(); + /* normals[n] = rand_0to1(); */ + /* normals[n+1] = rand_0to1(); */ + /* normals[n+2] = rand_0to1(); */ } /* for (i = 0; i < tri->num_triangles; ++i) { */ @@ -82,37 +101,26 @@ terrain_create(struct terrain *terrain) { /* int p0 = tri->tris[i * 3 + 0]; */ /* int p1 = tri->tris[i * 3 + 1]; */ /* int p2 = tri->tris[i * 3 + 2]; */ + /* vec3 *v0 = (vec3*)&verts[p0]; */ /* vec3 *v1 = (vec3*)&verts[p1]; */ /* vec3 *v2 = (vec3*)&verts[p2]; */ - /* float x0 = v0->x; */ - /* float x1 = v1->x; */ - /* float x2 = v2->x; */ - - /* float y0 = v0->y; */ - /* float y1 = v1->y; */ - /* float y2 = v2->y; */ - - /* float z0 = v0->z; */ - /* float z1 = v1->z; */ - /* float z2 = v2->z; */ - - /* float nx = (y1-y0)*(z2-z0)-(y2-y0)*(z1-z0); */ - /* float ny = (z1-z0)*(x2-x0)-(x1-x0)*(z2-z0); */ - /* float nz = (x1-x0)*(y2-x0)-(x2-x1)*(y1-y0); */ + /* vec3_subtract(v1, v0, tmp1); */ + /* vec3_subtract(v2, v0, tmp2); */ + /* vec3_cross(tmp1, tmp2, norm); */ - /* normals[p0] = nx; */ - /* normals[p0+1] = ny; */ - /* normals[p0+2] = nz; */ + /* normals[p0] = norm[0]; */ + /* normals[p0+1] = norm[1]; */ + /* normals[p0+2] = norm[2]; */ - /* normals[p1] = nx; */ - /* normals[p1+1] = ny; */ - /* normals[p1+2] = nz; */ + /* normals[p1] = norm[0]; */ + /* normals[p1+1] = norm[1]; */ + /* normals[p1+2] = norm[2]; */ - /* normals[p2] = nx; */ - /* normals[p2+1] = ny; */ - /* normals[p2+2] = nz; */ + /* normals[p2] = norm[0]; */ + /* normals[p2+1] = norm[1]; */ + /* normals[p2+2] = norm[2]; */ /* } */ diff --git a/src/util.h b/src/util.h @@ -7,13 +7,6 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -typedef struct vec3 { - union { - float x,y,z; - float xyz[3]; - }; -} vec3; - void check_gl(void); double rand_0to1(); diff --git a/src/vec3/vec3.c b/src/vec3/vec3.c @@ -0,0 +1,204 @@ + +#include <stdlib.h> +#include <math.h> + +#include "mat4/mat4.h" +#include "vec3.h" + +vec3 *vec3_create(vec3 *vec) { + vec3 *dest = calloc(sizeof(double_t), 3); + + if (vec) { + dest[0] = vec[0]; + dest[1] = vec[1]; + dest[2] = vec[2]; + } else { + dest[0] = dest[1] = dest[2] = 0; + } + + return dest; +} + +vec3 *vec3_set(vec3 *vec, vec3 *dest) { + dest[0] = vec[0]; + dest[1] = vec[1]; + dest[2] = vec[2]; + + return dest; +} + +vec3 *vec3_add(vec3 *vec, vec3 *vec2, vec3 *dest) { + if (!dest || vec == dest) { + vec[0] += vec2[0]; + vec[1] += vec2[1]; + vec[2] += vec2[2]; + return vec; + } + + dest[0] = vec[0] + vec2[0]; + dest[1] = vec[1] + vec2[1]; + dest[2] = vec[2] + vec2[2]; + + return dest; +} + +vec3 *vec3_subtract(vec3 *vec, vec3 *vec2, vec3 *dest) { + if (!dest || vec == dest) { + vec[0] -= vec2[0]; + vec[1] -= vec2[1]; + vec[2] -= vec2[2]; + return vec; + } + + dest[0] = vec[0] - vec2[0]; + dest[1] = vec[1] - vec2[1]; + dest[2] = vec[2] - vec2[2]; + return dest; +} + +vec3 *vec3_multiply(vec3 *vec, vec3 *vec2, vec3 *dest) { + if (!dest || vec == dest) { + vec[0] *= vec2[0]; + vec[1] *= vec2[1]; + vec[2] *= vec2[2]; + return vec; + } + + dest[0] = vec[0] * vec2[0]; + dest[1] = vec[1] * vec2[1]; + dest[2] = vec[2] * vec2[2]; + return dest; +} + +vec3 *vec3_negate(vec3 *vec, vec3 *dest) { + if (!dest) { dest = vec; } + + dest[0] = -vec[0]; + dest[1] = -vec[1]; + dest[2] = -vec[2]; + return dest; +} + +vec3 *vec3_scale(vec3 *vec, double val, vec3 *dest) { + if (!dest || vec == dest) { + vec[0] *= val; + vec[1] *= val; + vec[2] *= val; + return vec; + } + + dest[0] = vec[0] * val; + dest[1] = vec[1] * val; + dest[2] = vec[2] * val; + return dest; +} + +vec3 *vec3_normalize(vec3 *vec, vec3 *dest) { + if (!dest) { dest = vec; } + + double x = vec[0], y = vec[1], z = vec[2], + len = sqrt(x * x + y * y + z * z); + + if (!len) { + dest[0] = 0; + dest[1] = 0; + dest[2] = 0; + return dest; + } else if (len == 1) { + dest[0] = x; + dest[1] = y; + dest[2] = z; + return dest; + } + + len = 1 / len; + dest[0] = x * len; + dest[1] = y * len; + dest[2] = z * len; + return dest; +} + +vec3 *vec3_cross (vec3 *vec, vec3 *vec2, vec3 *dest) { + if (!dest) { dest = vec; } + + double x = vec[0], y = vec[1], z = vec[2], + x2 = vec2[0], y2 = vec2[1], z2 = vec2[2]; + + dest[0] = y * z2 - z * y2; + dest[1] = z * x2 - x * z2; + dest[2] = x * y2 - y * x2; + return dest; +} + +double vec3_length(vec3 *vec) { + double x = vec[0], y = vec[1], z = vec[2]; + return sqrt(x * x + y * y + z * z); +} + +double vec3_dot(vec3 *vec, vec3 *vec2) { + return vec[0] * vec2[0] + vec[1] * vec2[1] + vec[2] * vec2[2]; +} + +vec3 *vec3_direction (vec3 *vec, vec3 *vec2, vec3 *dest) { + if (!dest) { dest = vec; } + + double x = vec[0] - vec2[0], + y = vec[1] - vec2[1], + z = vec[2] - vec2[2], + len = sqrt(x * x + y * y + z * z); + + if (!len) { + dest[0] = 0; + dest[1] = 0; + dest[2] = 0; + return dest; + } + + len = 1 / len; + dest[0] = x * len; + dest[1] = y * len; + dest[2] = z * len; + return dest; +} + +vec3 *vec3_lerp(vec3 *vec, vec3 *vec2, double lerp, vec3 *dest) { + if (!dest) { dest = vec; } + + dest[0] = vec[0] + lerp * (vec2[0] - vec[0]); + dest[1] = vec[1] + lerp * (vec2[1] - vec[1]); + dest[2] = vec[2] + lerp * (vec2[2] - vec[2]); + + return dest; +} + +double vec3_dist(vec3 *vec, vec3 *vec2) { + double x = vec2[0] - vec[0], + y = vec2[1] - vec[1], + z = vec2[2] - vec[2]; + + return sqrt(x*x + y*y + z*z); +} + +/* vec3 *vec3_unproject(vec3 *vec, mat4 view, mat4 proj, vec4_t viewport, vec3 *dest) { */ +/* if (!dest) { dest = vec; } */ + +/* mat4 m = mat4_create(NULL); */ +/* double *v = malloc(sizeof(double) * 4); */ + +/* v[0] = (vec[0] - viewport[0]) * 2.0 / viewport[2] - 1.0; */ +/* v[1] = (vec[1] - viewport[1]) * 2.0 / viewport[3] - 1.0; */ +/* v[2] = 2.0 * vec[2] - 1.0; */ +/* v[3] = 1.0; */ + +/* mat4_multiply(proj, view, m); */ +/* if(!mat4_inverse(m, NULL)) { return NULL; } */ + +/* mat4_multiplyVec4(m, v, NULL); */ +/* if(v[3] == 0.0) { return NULL; } */ + +/* dest[0] = v[0] / v[3]; */ +/* dest[1] = v[1] / v[3]; */ +/* dest[2] = v[2] / v[3]; */ + +/* return dest; */ +/* } */ diff --git a/src/vec3/vec3.h b/src/vec3/vec3.h @@ -0,0 +1,7 @@ +typedef float vec3; + +vec3 *vec3_subtract(vec3 *vec, vec3 *vec2, vec3 *dest); +vec3 *vec3_cross (vec3 *vec, vec3 *vec2, vec3 *dest); +vec3 *vec3_multiply(vec3 *vec, vec3 *vec2, vec3 *dest); +vec3 *vec3_scale(vec3 *vec, double val, vec3 *dest); +