libgoatvr

annotate src/mesh.c @ 14:604a9a0a8c37

fixed call to ovrHmd_ConfigureTracking to stop setting the ovrTrackingCap_Idle bit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Sep 2014 07:52:49 +0300
parents 3d9ec6fe97d7
children 827ec716c275
rev   line source
nuclear@8 1 #include <stdio.h>
nuclear@8 2 #include <stdlib.h>
nuclear@8 3 #include <string.h>
nuclear@9 4 #include <stddef.h>
nuclear@8 5 #include "opengl.h"
nuclear@8 6 #include "mesh.h"
nuclear@8 7
nuclear@8 8 static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale,
nuclear@8 9 const float *dist_factors);
nuclear@8 10 static float barrel_scale(float rad, const float *k);
nuclear@8 11
nuclear@8 12 /* let's avoid a glew dependency in the library just for this */
nuclear@8 13 #ifndef GL_ARRAY_BUFFER
nuclear@8 14 #define GL_ARRAY_BUFFER 0x8892
nuclear@8 15 #define GL_ELEMENT_ARRAY_BUFFER 0x8893
nuclear@8 16 #define GL_STATIC_DRAW 0x88E4
nuclear@8 17 #endif
nuclear@8 18
nuclear@9 19 #ifndef GL_VERSION_1_5
nuclear@8 20 static void (*glGenBuffers)(GLsizei, GLuint*);
nuclear@8 21 static void (*glDeleteBuffers)(GLsizei, GLuint*);
nuclear@8 22 static void (*glBufferData)(GLenum, unsigned int, const GLvoid*, GLenum);
nuclear@8 23 static void (*glBindBuffer)(GLenum, GLuint);
nuclear@8 24 #endif
nuclear@8 25
nuclear@8 26 int vrimp_mesh_init(struct mesh *m)
nuclear@8 27 {
nuclear@8 28 m->prim = GL_TRIANGLES;
nuclear@8 29
nuclear@8 30 m->varr = 0;
nuclear@8 31 m->iarr = 0;
nuclear@8 32 m->num_verts = m->num_faces = 0;
nuclear@8 33 m->vbo = m->ibo = 0;
nuclear@8 34
nuclear@9 35 #ifndef GL_VERSION_1_5
nuclear@8 36 if(!glGenBuffers) {
nuclear@8 37 glGenBuffers = vrimp_glfunc("glGenBuffersARB");
nuclear@8 38 glDeleteBuffers = vrimp_glfunc("glDeleteBuffersARB");
nuclear@8 39 glBufferData = vrimp_glfunc("glBufferDataARB");
nuclear@8 40 glBindBuffer = vrimp_glfunc("glBindBufferARB");
nuclear@8 41
nuclear@8 42 if(!(glGenBuffers && glDeleteBuffers && glBufferData && glBindBuffer)) {
nuclear@8 43 fprintf(stderr, "Failed to load VBO functions\n");
nuclear@8 44 return -1;
nuclear@8 45 }
nuclear@8 46 }
nuclear@9 47 #endif
nuclear@8 48
nuclear@8 49 return 0;
nuclear@8 50 }
nuclear@8 51
nuclear@8 52 void vrimp_mesh_destroy(struct mesh *m)
nuclear@8 53 {
nuclear@8 54 free(m->varr);
nuclear@8 55 free(m->iarr);
nuclear@8 56
nuclear@8 57 if(m->vbo) glDeleteBuffers(1, &m->vbo);
nuclear@8 58 if(m->ibo) glDeleteBuffers(1, &m->ibo);
nuclear@8 59 }
nuclear@8 60
nuclear@8 61 void vrimp_mesh_draw(struct mesh *m)
nuclear@8 62 {
nuclear@8 63 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@8 64 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@8 65
nuclear@8 66 glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
nuclear@8 67 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex), 0);
nuclear@8 68 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), (void*)offsetof(struct vertex, tx));
nuclear@8 69
nuclear@8 70 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
nuclear@8 71 glDrawElements(GL_TRIANGLES, m->num_faces * 3, GL_UNSIGNED_INT, 0);
nuclear@8 72
nuclear@8 73 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@8 74 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
nuclear@8 75
nuclear@8 76 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@8 77 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@8 78 }
nuclear@8 79
nuclear@8 80 int vrimp_mesh_barrel_distortion(struct mesh *m, int usub, int vsub, float aspect,
nuclear@8 81 float lens_center_offset, float scale, const float *dist_factors,
nuclear@8 82 float tex_scale_x, float tex_scale_y)
nuclear@8 83 {
nuclear@8 84 int i, j;
nuclear@8 85 int uverts, vverts;
nuclear@8 86 int num_verts, num_quads, num_tris;
nuclear@8 87 struct vertex *varr, *vptr;
nuclear@8 88 unsigned int *iarr, *iptr;
nuclear@8 89 float du, dv;
nuclear@8 90
nuclear@8 91 uverts = usub + 1;
nuclear@8 92 vverts = vsub + 1;
nuclear@8 93
nuclear@8 94 num_verts = uverts * vverts;
nuclear@8 95 num_quads = usub * vsub;
nuclear@8 96 num_tris = num_quads * 2;
nuclear@8 97
nuclear@8 98 if(!(varr = malloc(num_verts * sizeof *varr))) {
nuclear@8 99 return -1;
nuclear@8 100 }
nuclear@8 101 if(!(iarr = malloc(num_tris * 3 * sizeof *iarr))) {
nuclear@8 102 free(varr);
nuclear@8 103 return -1;
nuclear@8 104 }
nuclear@8 105
nuclear@8 106 du = 1.0 / (float)usub;
nuclear@8 107 dv = 1.0 / (float)vsub;
nuclear@8 108
nuclear@8 109 vptr = varr;
nuclear@8 110 for(i=0; i<vverts; i++) {
nuclear@8 111 float v = (float)i * dv;
nuclear@8 112 float y = 2.0 * v - 1.0;
nuclear@8 113
nuclear@8 114 for(j=0; j<uverts; j++) {
nuclear@8 115 float u = (float)j * du;
nuclear@8 116 float x = 2.0 * u - 1.0;
nuclear@8 117 float tc[2];
nuclear@8 118 tc[0] = u;
nuclear@8 119 tc[1] = v;
nuclear@8 120
nuclear@8 121 distort_texcoords(tc, aspect, lens_center_offset, scale, dist_factors);
nuclear@8 122
nuclear@8 123 vptr->x = x;
nuclear@8 124 vptr->y = y;
nuclear@8 125 vptr->z = 0;
nuclear@8 126 vptr->tx = tc[0] * tex_scale_x;
nuclear@8 127 vptr->ty = tc[1] * tex_scale_y;
nuclear@8 128 vptr++;
nuclear@8 129 }
nuclear@8 130 }
nuclear@8 131
nuclear@8 132 iptr = iarr;
nuclear@8 133 for(i=0; i<vsub; i++) {
nuclear@8 134 for(j=0; j<usub; j++) {
nuclear@8 135 *iptr++ = i * uverts + j;
nuclear@8 136 *iptr++ = (i + 1) * uverts + j;
nuclear@8 137 *iptr++ = (i + 1) * uverts + (j + 1);
nuclear@8 138
nuclear@8 139 *iptr++ = i * uverts + j;
nuclear@8 140 *iptr++ = (i + 1) * uverts + (j + 1);
nuclear@8 141 *iptr++ = i * uverts + (j + 1);
nuclear@8 142 }
nuclear@8 143 }
nuclear@8 144
nuclear@8 145 glGenBuffers(1, &m->vbo);
nuclear@8 146 glGenBuffers(1, &m->ibo);
nuclear@8 147 glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
nuclear@8 148 glBufferData(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW);
nuclear@8 149 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
nuclear@8 150 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW);
nuclear@8 151
nuclear@8 152 m->prim = GL_TRIANGLES;
nuclear@8 153 m->num_verts = num_verts;
nuclear@8 154 m->num_faces = num_tris;
nuclear@8 155 return 0;
nuclear@8 156 }
nuclear@8 157
nuclear@8 158 static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale,
nuclear@8 159 const float *dist_factors)
nuclear@8 160 {
nuclear@8 161 /* map tc [0, 1] -> [-1, 1] */
nuclear@8 162 float ptx = tc[0] * 2.0 - 1.0;
nuclear@8 163 float pty = tc[1] * 2.0 - 1.0;
nuclear@8 164 float rad;
nuclear@8 165
nuclear@8 166 ptx += lens_center_offset * 2.0;
nuclear@8 167 pty /= aspect; /* correct for aspect ratio */
nuclear@8 168
nuclear@8 169 rad = barrel_scale(ptx * ptx + pty * pty, dist_factors);
nuclear@8 170 ptx *= rad; /* scale the point by the computer distortion radius */
nuclear@8 171 pty *= rad;
nuclear@8 172
nuclear@8 173 ptx /= scale;
nuclear@8 174 pty /= scale;
nuclear@8 175
nuclear@8 176 pty *= aspect;
nuclear@8 177 ptx -= lens_center_offset * 2.0;
nuclear@8 178
nuclear@8 179 /* map back to range [0, 1] */
nuclear@8 180 tc[0] = ptx * 0.5 + 0.5;
nuclear@8 181 tc[1] = pty * 0.5 + 0.5;
nuclear@8 182 }
nuclear@8 183
nuclear@8 184 static float barrel_scale(float rad, const float *k)
nuclear@8 185 {
nuclear@8 186 float radsq = rad * rad;
nuclear@8 187 float radquad = radsq * radsq;
nuclear@8 188 return k[0] + k[1] * radsq + k[2] * radquad + k[3] * radquad * radsq;
nuclear@8 189 }