tinygi

annotate src/object.c @ 2:72752a1b3dbe

images and shapes
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Jul 2015 04:30:00 +0300
parents bc64090fe3d1
children
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 free(o->name);
nuclear@1 42 }
nuclear@1 43 }
nuclear@1 44
nuclear@1 45 void tgi_set_object_shape(struct tgi_object *o, struct tgi_shape *s)
nuclear@1 46 {
nuclear@1 47 if(o->shape) {
nuclear@1 48 tgi_destroy_shape(o->shape);
nuclear@1 49 }
nuclear@1 50 o->shape = s;
nuclear@1 51 }
nuclear@1 52
nuclear@1 53 /* mat should point to an array of 16 floats (4x4 homogeneous transformation matrix) */
nuclear@1 54 void tgi_load_matrix(struct tgi_object *o, const float *mat)
nuclear@1 55 {
nuclear@1 56 mat4_t tmp;
nuclear@1 57 memcpy(tmp, mat, 16 * sizeof(float));
nuclear@1 58 m4_transpose(o->xform, tmp);
nuclear@1 59 m4_inverse(o->inv_xform, o->xform);
nuclear@1 60 }
nuclear@1 61
nuclear@1 62 void tgi_mult_matrix(struct tgi_object *o, const float *mat)
nuclear@1 63 {
nuclear@1 64 mat4_t tmp;
nuclear@1 65 memcpy(tmp, mat, 16 * sizeof(float));
nuclear@1 66 m4_transpose(tmp, tmp);
nuclear@1 67 m4_mult(o->xform, o->xform, tmp);
nuclear@1 68 m4_inverse(o->inv_xform, o->xform);
nuclear@1 69 }
nuclear@1 70
nuclear@1 71 void tgi_load_identity(struct tgi_object *o)
nuclear@1 72 {
nuclear@1 73 static const float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 74 tgi_load_matrix(o, id);
nuclear@1 75 }
nuclear@1 76
nuclear@1 77 void tgi_translate(struct tgi_object *o, float x, float y, float z)
nuclear@1 78 {
nuclear@1 79 float mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 80 mat[12] = x;
nuclear@1 81 mat[13] = y;
nuclear@1 82 mat[14] = z;
nuclear@1 83 tgi_mult_matrix(o, mat);
nuclear@1 84 }
nuclear@1 85
nuclear@1 86 void tgi_rotate(struct tgi_object *o, float angle, float x, float y, float z)
nuclear@1 87 {
nuclear@1 88 mat4_t mat;
nuclear@1 89 m4_rotate_axis(mat, DEG_TO_RAD(angle), x, y, z);
nuclear@1 90 m4_transpose(mat, mat);
nuclear@1 91 tgi_mult_matrix(o, mat[0]);
nuclear@1 92 }
nuclear@1 93
nuclear@1 94 void tgi_scale(struct tgi_object *o, float x, float y, float z)
nuclear@1 95 {
nuclear@1 96 float mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 97 mat[0] = x;
nuclear@1 98 mat[5] = y;
nuclear@1 99 mat[10] = z;
nuclear@1 100 tgi_mult_matrix(o, mat);
nuclear@1 101 }
nuclear@1 102
nuclear@1 103 void tgi_lookat(struct tgi_object *o, float x, float y, float z,
nuclear@1 104 float tx, float ty, float tz, float ux, float uy, float uz)
nuclear@1 105 {
nuclear@1 106 float mat[16];
nuclear@1 107 vec3_t vi, vj, vk;
nuclear@1 108
nuclear@1 109 vk = v3_normalize(v3_cons(tx - x, ty - y, tz - z));
nuclear@1 110 vj = v3_cons(ux, uy, uz);
nuclear@1 111 vi = v3_normalize(v3_cross(vj, vk));
nuclear@1 112 vj = v3_normalize(v3_cross(vk, vi));
nuclear@1 113
nuclear@1 114 mat[0] = vi.x; mat[1] = vj.x, mat[2] = vk.x;
nuclear@1 115 mat[4] = vi.y; mat[5] = vj.y, mat[6] = vk.y;
nuclear@1 116 mat[8] = vi.z; mat[9] = vj.z, mat[10] = vk.z;
nuclear@1 117 mat[3] = mat[7] = mat[11] = 0.0;
nuclear@1 118 mat[12] = x; mat[13] = y; mat[14] = z;
nuclear@1 119 mat[15] = 1.0f;
nuclear@1 120
nuclear@1 121 tgi_mult_matrix(o, mat);
nuclear@1 122 }