tinygi

changeset 1:bc64090fe3d1

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Jul 2015 04:38:53 +0300
parents 16fdca2a1ef5
children 72752a1b3dbe
files src/object.c src/object.h src/scene.c src/tgi_impl.h src/tinygi.h
diffstat 5 files changed, 145 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/src/object.c	Sun Jul 19 18:30:29 2015 +0300
     1.2 +++ b/src/object.c	Mon Jul 20 04:38:53 2015 +0300
     1.3 @@ -1,16 +1,133 @@
     1.4 +#include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 -#include "object.h"
     1.8 -#include "logger.h"
     1.9 +#include "tgi_impl.h"
    1.10 +
    1.11 +static int namecount;
    1.12  
    1.13  struct tgi_object *tgi_create_object(const char *name)
    1.14  {
    1.15  	struct tgi_object *o;
    1.16 +	int nmsize;
    1.17  
    1.18  	if(!(o = malloc(sizeof *o))) {
    1.19  		tgi_log("failed to allocate memory\n");
    1.20  		return 0;
    1.21  	}
    1.22  	memset(o, 0, sizeof *o);
    1.23 +
    1.24 +	nmsize = name ? strlen(name) : 15;
    1.25 +	if(!(o->name = malloc(nmsize + 1))) {
    1.26 +		tgi_log("failed to allocate name string\n");
    1.27 +		free(o);
    1.28 +		return 0;
    1.29 +	}
    1.30 +
    1.31 +	if(!name) {
    1.32 +		sprintf(o->name, "tgi_object%03d", namecount++);
    1.33 +	} else {
    1.34 +		strcpy(o->name, name);
    1.35 +	}
    1.36 +
    1.37  	return o;
    1.38  }
    1.39 +
    1.40 +void tgi_destroy_object(struct tgi_object *o)
    1.41 +{
    1.42 +	if(o) {
    1.43 +		if(o->shape) {
    1.44 +			tgi_destroy_shape(o->shape);
    1.45 +		}
    1.46 +		if(o->mtl) {
    1.47 +			tgi_destroy_material(o->mtl);
    1.48 +		}
    1.49 +		free(o->name);
    1.50 +	}
    1.51 +}
    1.52 +
    1.53 +void tgi_set_object_shape(struct tgi_object *o, struct tgi_shape *s)
    1.54 +{
    1.55 +	if(o->shape) {
    1.56 +		tgi_destroy_shape(o->shape);
    1.57 +	}
    1.58 +	o->shape = s;
    1.59 +}
    1.60 +
    1.61 +void tgi_set_object_mtl(struct tgi_object *o, struct tgi_material *m)
    1.62 +{
    1.63 +	if(o->mtl) {
    1.64 +		tgi_destroy_material(o->mtl);
    1.65 +	}
    1.66 +	o->mtl = m;
    1.67 +}
    1.68 +
    1.69 +/* mat should point to an array of 16 floats (4x4 homogeneous transformation matrix) */
    1.70 +void tgi_load_matrix(struct tgi_object *o, const float *mat)
    1.71 +{
    1.72 +	mat4_t tmp;
    1.73 +	memcpy(tmp, mat, 16 * sizeof(float));
    1.74 +	m4_transpose(o->xform, tmp);
    1.75 +	m4_inverse(o->inv_xform, o->xform);
    1.76 +}
    1.77 +
    1.78 +void tgi_mult_matrix(struct tgi_object *o, const float *mat)
    1.79 +{
    1.80 +	mat4_t tmp;
    1.81 +	memcpy(tmp, mat, 16 * sizeof(float));
    1.82 +	m4_transpose(tmp, tmp);
    1.83 +	m4_mult(o->xform, o->xform, tmp);
    1.84 +	m4_inverse(o->inv_xform, o->xform);
    1.85 +}
    1.86 +
    1.87 +void tgi_load_identity(struct tgi_object *o)
    1.88 +{
    1.89 +	static const float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.90 +	tgi_load_matrix(o, id);
    1.91 +}
    1.92 +
    1.93 +void tgi_translate(struct tgi_object *o, float x, float y, float z)
    1.94 +{
    1.95 +	float mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.96 +	mat[12] = x;
    1.97 +	mat[13] = y;
    1.98 +	mat[14] = z;
    1.99 +	tgi_mult_matrix(o, mat);
   1.100 +}
   1.101 +
   1.102 +void tgi_rotate(struct tgi_object *o, float angle, float x, float y, float z)
   1.103 +{
   1.104 +	mat4_t mat;
   1.105 +	m4_rotate_axis(mat, DEG_TO_RAD(angle), x, y, z);
   1.106 +	m4_transpose(mat, mat);
   1.107 +	tgi_mult_matrix(o, mat[0]);
   1.108 +}
   1.109 +
   1.110 +void tgi_scale(struct tgi_object *o, float x, float y, float z)
   1.111 +{
   1.112 +	float mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.113 +	mat[0] = x;
   1.114 +	mat[5] = y;
   1.115 +	mat[10] = z;
   1.116 +	tgi_mult_matrix(o, mat);
   1.117 +}
   1.118 +
   1.119 +void tgi_lookat(struct tgi_object *o, float x, float y, float z,
   1.120 +		float tx, float ty, float tz, float ux, float uy, float uz)
   1.121 +{
   1.122 +	float mat[16];
   1.123 +	vec3_t vi, vj, vk;
   1.124 +
   1.125 +	vk = v3_normalize(v3_cons(tx - x, ty - y, tz - z));
   1.126 +	vj = v3_cons(ux, uy, uz);
   1.127 +	vi = v3_normalize(v3_cross(vj, vk));
   1.128 +	vj = v3_normalize(v3_cross(vk, vi));
   1.129 +
   1.130 +	mat[0] = vi.x; mat[1] = vj.x, mat[2] = vk.x;
   1.131 +	mat[4] = vi.y; mat[5] = vj.y, mat[6] = vk.y;
   1.132 +	mat[8] = vi.z; mat[9] = vj.z, mat[10] = vk.z;
   1.133 +	mat[3] = mat[7] = mat[11] = 0.0;
   1.134 +	mat[12] = x; mat[13] = y; mat[14] = z;
   1.135 +	mat[15] = 1.0f;
   1.136 +
   1.137 +	tgi_mult_matrix(o, mat);
   1.138 +}
     2.1 --- a/src/object.h	Sun Jul 19 18:30:29 2015 +0300
     2.2 +++ b/src/object.h	Mon Jul 20 04:38:53 2015 +0300
     2.3 @@ -2,10 +2,13 @@
     2.4  #define TGI_OBJECT_H_
     2.5  
     2.6  #include "tinygi.h"
     2.7 +#include "vmath/vmath.h"
     2.8  
     2.9  struct tgi_object {
    2.10  	char *name;
    2.11  	struct tgi_shape *shape;
    2.12 +	struct tgi_material *mtl;
    2.13 +	mat4_t xform, inv_xform;
    2.14  
    2.15  	struct tgi_object *next;	/* for linking it into various lists */
    2.16  };
     3.1 --- a/src/scene.c	Sun Jul 19 18:30:29 2015 +0300
     3.2 +++ b/src/scene.c	Mon Jul 20 04:38:53 2015 +0300
     3.3 @@ -50,7 +50,7 @@
     3.4  	assert(tgi->objects);
     3.5  }
     3.6  
     3.7 -int tgi_del_object(struct tinygi *tgi, struct tgi_object *o)
     3.8 +int tgi_remove_object(struct tinygi *tgi, struct tgi_object *o)
     3.9  {
    3.10  	int i, idx = -1, sz = dynarr_size(tgi->objects);
    3.11  
     4.1 --- a/src/tgi_impl.h	Sun Jul 19 18:30:29 2015 +0300
     4.2 +++ b/src/tgi_impl.h	Mon Jul 20 04:38:53 2015 +0300
     4.3 @@ -4,6 +4,7 @@
     4.4  #include "tinygi.h"
     4.5  #include "object.h"
     4.6  #include "logger.h"
     4.7 +#include "vmath/vmath.h"
     4.8  
     4.9  struct tinygi {
    4.10  	struct tgi_object **objects;	/* dynarr */
     5.1 --- a/src/tinygi.h	Sun Jul 19 18:30:29 2015 +0300
     5.2 +++ b/src/tinygi.h	Mon Jul 20 04:38:53 2015 +0300
     5.3 @@ -6,21 +6,23 @@
     5.4  struct tinygi;
     5.5  struct tgi_object;
     5.6  struct tgi_shape;
     5.7 +struct tgi_material;
     5.8  
     5.9  struct tinygi *tgi_init(void);
    5.10  void tgi_destroy(struct tinygi *tgi);
    5.11  
    5.12  void tgi_clear_scene(struct tinygi *tgi);
    5.13  int tgi_load_scene(struct tinygi *tgi, const char *fname);
    5.14 +/* tinygi takes ownership of added objects */
    5.15  void tgi_add_object(struct tinygi *tgi, struct tgi_object *o);
    5.16 -int tgi_del_object(struct tinygi *tgi, struct tgi_object *o);
    5.17 +int tgi_remove_object(struct tinygi *tgi, struct tgi_object *o);
    5.18  
    5.19  struct tgi_object *tgi_find_object(struct tinygi *tgi, const char *name);
    5.20  struct tgi_object *tgi_get_object(struct tinygi *tgi, int idx);
    5.21  int tgi_get_object_count(struct tinygi *tgi);
    5.22  
    5.23  /* shapes */
    5.24 -struct tgi_shape *tgi_create_sphere(float rad);
    5.25 +struct tgi_shape *tgi_create_sphere(float x, float y, float z, float rad);
    5.26  struct tgi_shape *tgi_create_box(float x0, float y0, float z0, float x1, float y1, float z1);
    5.27  void tgi_destroy_shape(struct tgi_shape *s);
    5.28  
    5.29 @@ -28,7 +30,24 @@
    5.30  /* name can be null, in which case it's automatically generated */
    5.31  struct tgi_object *tgi_create_object(const char *name);
    5.32  void tgi_destroy_object(struct tgi_object *o);
    5.33 +/* object takes ownership of the shape */
    5.34  void tgi_set_object_shape(struct tgi_object *o, struct tgi_shape *s);
    5.35 +/* object takes ownership of the material */
    5.36  void tgi_set_object_mtl(struct tgi_object *o, struct tgi_material *m);
    5.37  
    5.38 +/* mat should point to an array of 16 floats (4x4 homogeneous transformation matrix) */
    5.39 +void tgi_load_matrix(struct tgi_object *o, const float *mat);
    5.40 +void tgi_mult_matrix(struct tgi_object *o, const float *mat);
    5.41 +void tgi_load_identity(struct tgi_object *o);
    5.42 +void tgi_translate(struct tgi_object *o, float x, float y, float z);
    5.43 +void tgi_rotate(struct tgi_object *o, float angle, float x, float y, float z);
    5.44 +void tgi_scale(struct tgi_object *o, float x, float y, float z);
    5.45 +void tgi_lookat(struct tgi_object *o, float x, float y, float z,
    5.46 +		float tx, float ty, float tz, float ux, float uy, float uz);
    5.47 +
    5.48 +/* materials */
    5.49 +struct tgi_material *tgi_create_material(void);
    5.50 +void tgi_destroy_material(struct tgi_material *mtl);
    5.51 +/* TODO */
    5.52 +
    5.53  #endif	/* TINYGI_H_ */