tinygi

annotate src/object.c @ 1:bc64090fe3d1

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Jul 2015 04:38:53 +0300
parents 16fdca2a1ef5
children 72752a1b3dbe
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@1 4 #include "tgi_impl.h"
nuclear@1 5
nuclear@1 6 static int namecount;
nuclear@0 7
nuclear@0 8 struct tgi_object *tgi_create_object(const char *name)
nuclear@0 9 {
nuclear@0 10 struct tgi_object *o;
nuclear@1 11 int nmsize;
nuclear@0 12
nuclear@0 13 if(!(o = malloc(sizeof *o))) {
nuclear@0 14 tgi_log("failed to allocate memory\n");
nuclear@0 15 return 0;
nuclear@0 16 }
nuclear@0 17 memset(o, 0, sizeof *o);
nuclear@1 18
nuclear@1 19 nmsize = name ? strlen(name) : 15;
nuclear@1 20 if(!(o->name = malloc(nmsize + 1))) {
nuclear@1 21 tgi_log("failed to allocate name string\n");
nuclear@1 22 free(o);
nuclear@1 23 return 0;
nuclear@1 24 }
nuclear@1 25
nuclear@1 26 if(!name) {
nuclear@1 27 sprintf(o->name, "tgi_object%03d", namecount++);
nuclear@1 28 } else {
nuclear@1 29 strcpy(o->name, name);
nuclear@1 30 }
nuclear@1 31
nuclear@0 32 return o;
nuclear@0 33 }
nuclear@1 34
nuclear@1 35 void tgi_destroy_object(struct tgi_object *o)
nuclear@1 36 {
nuclear@1 37 if(o) {
nuclear@1 38 if(o->shape) {
nuclear@1 39 tgi_destroy_shape(o->shape);
nuclear@1 40 }
nuclear@1 41 if(o->mtl) {
nuclear@1 42 tgi_destroy_material(o->mtl);
nuclear@1 43 }
nuclear@1 44 free(o->name);
nuclear@1 45 }
nuclear@1 46 }
nuclear@1 47
nuclear@1 48 void tgi_set_object_shape(struct tgi_object *o, struct tgi_shape *s)
nuclear@1 49 {
nuclear@1 50 if(o->shape) {
nuclear@1 51 tgi_destroy_shape(o->shape);
nuclear@1 52 }
nuclear@1 53 o->shape = s;
nuclear@1 54 }
nuclear@1 55
nuclear@1 56 void tgi_set_object_mtl(struct tgi_object *o, struct tgi_material *m)
nuclear@1 57 {
nuclear@1 58 if(o->mtl) {
nuclear@1 59 tgi_destroy_material(o->mtl);
nuclear@1 60 }
nuclear@1 61 o->mtl = m;
nuclear@1 62 }
nuclear@1 63
nuclear@1 64 /* mat should point to an array of 16 floats (4x4 homogeneous transformation matrix) */
nuclear@1 65 void tgi_load_matrix(struct tgi_object *o, const float *mat)
nuclear@1 66 {
nuclear@1 67 mat4_t tmp;
nuclear@1 68 memcpy(tmp, mat, 16 * sizeof(float));
nuclear@1 69 m4_transpose(o->xform, tmp);
nuclear@1 70 m4_inverse(o->inv_xform, o->xform);
nuclear@1 71 }
nuclear@1 72
nuclear@1 73 void tgi_mult_matrix(struct tgi_object *o, const float *mat)
nuclear@1 74 {
nuclear@1 75 mat4_t tmp;
nuclear@1 76 memcpy(tmp, mat, 16 * sizeof(float));
nuclear@1 77 m4_transpose(tmp, tmp);
nuclear@1 78 m4_mult(o->xform, o->xform, tmp);
nuclear@1 79 m4_inverse(o->inv_xform, o->xform);
nuclear@1 80 }
nuclear@1 81
nuclear@1 82 void tgi_load_identity(struct tgi_object *o)
nuclear@1 83 {
nuclear@1 84 static const float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 85 tgi_load_matrix(o, id);
nuclear@1 86 }
nuclear@1 87
nuclear@1 88 void tgi_translate(struct tgi_object *o, float x, float y, float z)
nuclear@1 89 {
nuclear@1 90 float mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 91 mat[12] = x;
nuclear@1 92 mat[13] = y;
nuclear@1 93 mat[14] = z;
nuclear@1 94 tgi_mult_matrix(o, mat);
nuclear@1 95 }
nuclear@1 96
nuclear@1 97 void tgi_rotate(struct tgi_object *o, float angle, float x, float y, float z)
nuclear@1 98 {
nuclear@1 99 mat4_t mat;
nuclear@1 100 m4_rotate_axis(mat, DEG_TO_RAD(angle), x, y, z);
nuclear@1 101 m4_transpose(mat, mat);
nuclear@1 102 tgi_mult_matrix(o, mat[0]);
nuclear@1 103 }
nuclear@1 104
nuclear@1 105 void tgi_scale(struct tgi_object *o, float x, float y, float z)
nuclear@1 106 {
nuclear@1 107 float mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 108 mat[0] = x;
nuclear@1 109 mat[5] = y;
nuclear@1 110 mat[10] = z;
nuclear@1 111 tgi_mult_matrix(o, mat);
nuclear@1 112 }
nuclear@1 113
nuclear@1 114 void tgi_lookat(struct tgi_object *o, float x, float y, float z,
nuclear@1 115 float tx, float ty, float tz, float ux, float uy, float uz)
nuclear@1 116 {
nuclear@1 117 float mat[16];
nuclear@1 118 vec3_t vi, vj, vk;
nuclear@1 119
nuclear@1 120 vk = v3_normalize(v3_cons(tx - x, ty - y, tz - z));
nuclear@1 121 vj = v3_cons(ux, uy, uz);
nuclear@1 122 vi = v3_normalize(v3_cross(vj, vk));
nuclear@1 123 vj = v3_normalize(v3_cross(vk, vi));
nuclear@1 124
nuclear@1 125 mat[0] = vi.x; mat[1] = vj.x, mat[2] = vk.x;
nuclear@1 126 mat[4] = vi.y; mat[5] = vj.y, mat[6] = vk.y;
nuclear@1 127 mat[8] = vi.z; mat[9] = vj.z, mat[10] = vk.z;
nuclear@1 128 mat[3] = mat[7] = mat[11] = 0.0;
nuclear@1 129 mat[12] = x; mat[13] = y; mat[14] = z;
nuclear@1 130 mat[15] = 1.0f;
nuclear@1 131
nuclear@1 132 tgi_mult_matrix(o, mat);
nuclear@1 133 }