polyadvent

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

commit be76dc0d0d5e50bf17b7d244d0eefe131d6ad322
parent fe404d05973bd84e45bc4f24e871cd777ae16d5c
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 30 Apr 2018 17:33:11 -0700

wip quat orientation

Diffstat:
MMakefile | 24+++++++++++++-----------
Msrc/camera.c | 4+---
Msrc/game.c | 5++---
Msrc/mat4.c | 36------------------------------------
Asrc/mat_util.c | 29+++++++++++++++++++++++++++++
Asrc/mat_util.h | 12++++++++++++
Msrc/node.c | 58+++++++++++++++++++++++++++++-----------------------------
Msrc/quat.c | 32++++++++++++++++++++++++++++++++
Msrc/quat.h | 1+
Msrc/util.c | 2++
Msrc/util.h | 2--
11 files changed, 121 insertions(+), 84 deletions(-)

diff --git a/Makefile b/Makefile @@ -15,26 +15,28 @@ OBJS = $(SRC)/window.o OBJS += $(SRC)/buffer.o OBJS += $(SRC)/camera.o OBJS += $(SRC)/debug.o +OBJS += $(SRC)/delaunay.o OBJS += $(SRC)/event.o OBJS += $(SRC)/file.o -OBJS += $(SRC)/perlin.o -OBJS += $(SRC)/main.o -OBJS += $(SRC)/poisson.o -OBJS += $(SRC)/uniform.o OBJS += $(SRC)/game.o +OBJS += $(SRC)/geometry.o +OBJS += $(SRC)/input.o +OBJS += $(SRC)/main.o OBJS += $(SRC)/mat4.o -OBJS += $(SRC)/vec3.o +OBJS += $(SRC)/mat_util.o +OBJS += $(SRC)/node.o +OBJS += $(SRC)/perlin.o +OBJS += $(SRC)/poisson.o +OBJS += $(SRC)/quat.o OBJS += $(SRC)/render.o OBJS += $(SRC)/shader.o -OBJS += $(SRC)/update.o -OBJS += $(SRC)/terrain.o OBJS += $(SRC)/slab.o OBJS += $(SRC)/slab_geom.o -OBJS += $(SRC)/delaunay.o -OBJS += $(SRC)/geometry.o -OBJS += $(SRC)/input.o +OBJS += $(SRC)/terrain.o +OBJS += $(SRC)/uniform.o +OBJS += $(SRC)/update.o OBJS += $(SRC)/util.o -OBJS += $(SRC)/node.o +OBJS += $(SRC)/vec3.o SRCS=$(OBJS:.o=.c) diff --git a/src/camera.c b/src/camera.c @@ -1,8 +1,6 @@ #include "camera.h" -#include "util.h" -#include "mat4.h" -#include "vec3.h" +#include "mat_util.h" // should probably be a scene node struct camera *camera_init(struct camera *cam) { diff --git a/src/game.c b/src/game.c @@ -1,6 +1,5 @@ -#include "mat4.h" -#include "vec3.h" +#include "mat_util.h" #include "game.h" mat4 *cam_init = (float[16]){ @@ -51,7 +50,7 @@ void game_init(struct game *game) { /* player_camera->mirrored = 1; */ /* camera->parent = player_camera; */ - camera->rot[0] = 45; + quat_axis_angle(V3(1,0,0), 45, camera->orientation); // move the camera a bit /* mat4_translate(camera, 1.0f, 1.0f, 20.0f, camera); */ diff --git a/src/mat4.c b/src/mat4.c @@ -328,39 +328,3 @@ void mat4_print(const mat4 *m) { printf("\n"); } -/* mat4 *mat4_create_transform(float *pos, float *scale, float *rot, mat4 *dest) { */ -/* if (!dest) { dest = mat4.create(); } */ - -/* float halfAngle = 0.5*angle; */ -/* float sin_ = sin(halfAngle); */ - -/* dest[0] = sin_*axis[0]; */ -/* dest[1] = sin_*axis[1]; */ -/* dest[2] = sin_*axis[2]; */ -/* dest[3] = cos(halfAngle); */ - -/* var rot = quat4.toMat3(orientation); */ - -/* dest[0] = scale[0] * rot[0]; */ -/* dest[1] = scale[0] * rot[1]; */ -/* dest[2] = scale[0] * rot[2]; */ -/* dest[3] = 0; */ - -/* dest[4] = scale[1] * rot[3]; */ -/* dest[5] = scale[1] * rot[4]; */ -/* dest[6] = scale[1] * rot[5]; */ -/* dest[7] = 0; */ - -/* dest[8] = scale[2] * rot[6]; */ -/* dest[9] = scale[2] * rot[7]; */ -/* dest[10] = scale[2] * rot[8]; */ -/* dest[11] = 0; */ - -/* dest[12] = position[0]; */ -/* dest[13] = position[1]; */ -/* dest[14] = position[2]; */ -/* dest[15] = 1; */ - -/* return dest; */ -/* } */ -/* } */ diff --git a/src/mat_util.c b/src/mat_util.c @@ -0,0 +1,29 @@ + +#include "mat_util.h" + +mat4 *mat4_create_transform(float *pos, float *scale, quat *orientation, mat4 *dest) { + float rot[9]; + quat_to_mat3(orientation, rot); + + dest[0] = scale[0] * rot[0]; + dest[1] = scale[0] * rot[1]; + dest[2] = scale[0] * rot[2]; + dest[3] = 0; + + dest[4] = scale[1] * rot[3]; + dest[5] = scale[1] * rot[4]; + dest[6] = scale[1] * rot[5]; + dest[7] = 0; + + dest[8] = scale[2] * rot[6]; + dest[9] = scale[2] * rot[7]; + dest[10] = scale[2] * rot[8]; + dest[11] = 0; + + dest[12] = pos[0]; + dest[13] = pos[1]; + dest[14] = pos[2]; + dest[15] = 1; + + return dest; +} diff --git a/src/mat_util.h b/src/mat_util.h @@ -0,0 +1,12 @@ + +#ifndef POLYADVENT_MAT_UTIL_H +#define POLYADVENT_MAT_UTIL_H + +#include "vec3.h" +#include "mat4.h" +#include "quat.h" + +void look_at(vec3 *eye, vec3 *target, vec3 *up, mat4 *dest); +mat4 *mat4_create_transform(float *pos, float *scale, quat *orientation, mat4 *dest); + +#endif /* POLYADVENT_MAT_UTIL_H */ diff --git a/src/node.c b/src/node.c @@ -1,8 +1,7 @@ #include "node.h" -#include "mat4.h" -#include "vec3.h" +#include "mat_util.h" #include <stdio.h> #include <assert.h> @@ -69,37 +68,38 @@ int node_recalc(struct node *node) { printf("recalculating %s\n", node->label); - mat4_id(node->mat); - node->needs_recalc = 0; - if (node->scale[0] != 1 || node->scale[1] != 1 || node->scale[2] != 1) - mat4_scale(node->mat, node->scale, node->mat); - - //seomthing - float m = node->mirrored ? -1 : 1; - - // FIXME: this seems bad? - for (int i = 0; i < 3; ++i) { - if (node->rot[i] != 0) { - rotate_axis[0] = 0; - rotate_axis[1] = 0; - rotate_axis[2] = 0; - rotate_axis[i] = 1; - float x = node->pos[0]; - float y = node->pos[1]; - float z = node->pos[2]; - if (!node->mirrored) - mat4_translate(node->mat, V3(x, y, z), node->mat); - mat4_rotate(node->mat, node->rot[i]*m, rotate_axis, node->mat); - if (!node->mirrored) - mat4_translate(node->mat, V3(-x, -y, -z), node->mat); - } + if (node->mirrored) { + mat4_create_transform(V3(-node->pos[0], -node->pos[1], -node->pos[2]), + node->scale, node->orientation, node->mat); } + else + mat4_create_transform(node->pos, node->scale, node->orientation, node->mat); + + /* float m = node->mirrored ? -1 : 1; */ + + /* // FIXME: this seems bad? */ + /* for (int i = 0; i < 3; ++i) { */ + /* if (node->rot[i] != 0) { */ + /* rotate_axis[0] = 0; */ + /* rotate_axis[1] = 0; */ + /* rotate_axis[2] = 0; */ + /* rotate_axis[i] = 1; */ + /* float x = node->pos[0]; */ + /* float y = node->pos[1]; */ + /* float z = node->pos[2]; */ + /* if (!node->mirrored) */ + /* mat4_translate(node->mat, V3(x, y, z), node->mat); */ + /* mat4_rotate(node->mat, node->rot[i]*m, rotate_axis, node->mat); */ + /* if (!node->mirrored) */ + /* mat4_translate(node->mat, V3(-x, -y, -z), node->mat); */ + /* } */ + /* } */ - if (node->pos[0] || node->pos[1] || node->pos[2]) - mat4_translate(node->mat, V3(node->pos[0]*m,node->pos[1]*m,node->pos[2]*m), - node->mat); + /* if (node->pos[0] || node->pos[1] || node->pos[2]) */ + /* mat4_translate(node->mat, V3(node->pos[0]*m,node->pos[1]*m,node->pos[2]*m), */ + /* node->mat); */ /* if (node->pos[0] || node->pos[1] || node->pos[2]) */ /* mat4_translate(node->mat, node->pos, node->mat); */ diff --git a/src/quat.c b/src/quat.c @@ -36,3 +36,35 @@ void quat_axis_angle(float *axis, float angle, quat *dest) { dest[2] = axis[2] * s; dest[3] = cos(half_angle); } + +void quat_to_mat3(quat *quat, float *dest) { + float x = quat[0], y = quat[1], z = quat[2], w = quat[3]; + + float x2 = x + x; + float y2 = y + y; + float z2 = z + z; + + float xx = x*x2; + float xy = x*y2; + float xz = x*z2; + + float yy = y*y2; + float yz = y*z2; + float zz = z*z2; + + float wx = w*x2; + float wy = w*y2; + float wz = w*z2; + + dest[0] = 1 - (yy + zz); + dest[1] = xy - wz; + dest[2] = xz + wy; + + dest[3] = xy + wz; + dest[4] = 1 - (xx + zz); + dest[5] = yz - wx; + + dest[6] = xz - wy; + dest[7] = yz + wx; + dest[8] = 1 - (xx + yy); +} diff --git a/src/quat.h b/src/quat.h @@ -8,5 +8,6 @@ typedef float quat; void quat_id(quat *dest); void quat_multiply(quat *a, quat *b, quat *dest); void quat_axis_angle(float *axis, float angle, quat *dest); +void quat_to_mat3(quat *quat, float *dest); #endif /* POLYADVENT_QUAT4_ */ diff --git a/src/util.c b/src/util.c @@ -2,6 +2,7 @@ #include "util.h" #include "vec3.h" #include "mat4.h" +#include "quat.h" #include <stdlib.h> int clampi(int a, int mina, int maxa) { @@ -87,3 +88,4 @@ void look_at(vec3 *eye, vec3 *target, vec3 *up, mat4 *dest) { /* dest[11] = -vec3_dot(z, eye); */ } + diff --git a/src/util.h b/src/util.h @@ -15,8 +15,6 @@ double clamp(double a, double mina, double maxa); double max(double a, double b); double min(double a, double b); -void look_at(vec3 *eye, vec3 *target, vec3 *up, mat4 *dest); - double rand_0to1(); #endif /* PA_UTIL_H */