conworlds

annotate src/vr/vr.c @ 4:e6948e131526

adding a vr wrapper
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 20 Aug 2014 06:33:43 +0300
parents
children 3c36bc28c6c2
rev   line source
nuclear@4 1 #include <stdio.h>
nuclear@4 2 #include <string.h>
nuclear@4 3 #include "vr.h"
nuclear@4 4 #include "vr_impl.h"
nuclear@4 5
nuclear@4 6
nuclear@4 7 static struct vr_module *vrm;
nuclear@4 8 static float idmat[] = {
nuclear@4 9 1, 0, 0, 0,
nuclear@4 10 0, 1, 0, 0,
nuclear@4 11 0, 0, 1, 0,
nuclear@4 12 0, 0, 0, 1
nuclear@4 13 };
nuclear@4 14 static float fbtex_rect[] = {
nuclear@4 15 0, 0, 1, 1
nuclear@4 16 };
nuclear@4 17
nuclear@4 18 int vr_init(void)
nuclear@4 19 {
nuclear@4 20 int i, nmodules;
nuclear@4 21
nuclear@4 22 if(vrm) {
nuclear@4 23 vr_shutdown();
nuclear@4 24 }
nuclear@4 25
nuclear@4 26 vr_init_modules();
nuclear@4 27
nuclear@4 28 nmodules = vr_get_num_modules();
nuclear@4 29 for(i=0; i<nmodules; i++) {
nuclear@4 30 struct vr_module *m = vr_get_module(i);
nuclear@4 31 if(m->init() != -1) {
nuclear@4 32 /* add to the active modules array */
nuclear@4 33 vr_activate_module(i);
nuclear@4 34 if(!vrm) {
nuclear@4 35 vr_use_module(0);
nuclear@4 36 }
nuclear@4 37 }
nuclear@4 38 }
nuclear@4 39
nuclear@4 40 if(!vrm) {
nuclear@4 41 return -1;
nuclear@4 42 }
nuclear@4 43 return 0;
nuclear@4 44 }
nuclear@4 45
nuclear@4 46 void vr_shutdown(void)
nuclear@4 47 {
nuclear@4 48 vr_clear_modules();
nuclear@4 49 vrm = 0;
nuclear@4 50 fbtex_rect[0] = fbtex_rect[1] = 0;
nuclear@4 51 fbtex_rect[2] = fbtex_rect[3] = 1;
nuclear@4 52 }
nuclear@4 53
nuclear@4 54 int vr_module_count(void)
nuclear@4 55 {
nuclear@4 56 return vr_get_num_active_modules();
nuclear@4 57 }
nuclear@4 58
nuclear@4 59 const char *vr_module_name(int idx)
nuclear@4 60 {
nuclear@4 61 struct vr_module *m = vr_get_active_module(idx);
nuclear@4 62 if(!m) {
nuclear@4 63 return 0;
nuclear@4 64 }
nuclear@4 65 return m->name;
nuclear@4 66 }
nuclear@4 67
nuclear@4 68 int vr_use_module(int idx)
nuclear@4 69 {
nuclear@4 70 if(idx >= 0 && idx < vr_get_num_active_modules()) {
nuclear@4 71 vrm = vr_get_active_module(idx);
nuclear@4 72 printf("using vr module: %s\n", vrm->name);
nuclear@4 73 return 0;
nuclear@4 74 }
nuclear@4 75 return -1;
nuclear@4 76 }
nuclear@4 77
nuclear@4 78 int vr_use_module_named(const char *name)
nuclear@4 79 {
nuclear@4 80 int i, count = vr_get_num_active_modules();
nuclear@4 81
nuclear@4 82 for(i=0; i<count; i++) {
nuclear@4 83 struct vr_module *m = vr_get_active_module(i);
nuclear@4 84 if(strcmp(m->name, name) == 0) {
nuclear@4 85 return vr_use_module(i);
nuclear@4 86 }
nuclear@4 87 }
nuclear@4 88 return -1;
nuclear@4 89 }
nuclear@4 90
nuclear@4 91 int vr_view_matrix(int eye, float *mat)
nuclear@4 92 {
nuclear@4 93 if(vrm && vrm->view_matrix) {
nuclear@4 94 vrm->view_matrix(eye, mat);
nuclear@4 95 return 1;
nuclear@4 96 }
nuclear@4 97 memcpy(mat, idmat, sizeof idmat);
nuclear@4 98 return 0;
nuclear@4 99 }
nuclear@4 100
nuclear@4 101 int vr_proj_matrix(int eye, float *mat)
nuclear@4 102 {
nuclear@4 103 if(vrm && vrm->proj_matrix) {
nuclear@4 104 vrm->proj_matrix(eye, mat);
nuclear@4 105 return 1;
nuclear@4 106 }
nuclear@4 107 memcpy(mat, idmat, sizeof idmat);
nuclear@4 108 return 0;
nuclear@4 109 }
nuclear@4 110
nuclear@4 111 void vr_present(unsigned int fbtex)
nuclear@4 112 {
nuclear@4 113 if(vrm && vrm->draw) {
nuclear@4 114 vrm->draw(fbtex, fbtex_rect[0], fbtex_rect[2], fbtex_rect[1], fbtex_rect[3]);
nuclear@4 115 }
nuclear@4 116 }
nuclear@4 117
nuclear@4 118 void vr_fbrect(float u, float umax, float v, float vmax)
nuclear@4 119 {
nuclear@4 120 fbtex_rect[0] = u;
nuclear@4 121 fbtex_rect[1] = v;
nuclear@4 122 fbtex_rect[2] = umax;
nuclear@4 123 fbtex_rect[3] = vmax;
nuclear@4 124 }