libgoatvr

annotate src/mesh.c @ 33:1102327fe85f

added red-cyan anaglyph fallback drawing
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 30 Oct 2015 06:34:00 +0200
parents 0fea7c2b25d7
children
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@30 12 static PFNGLGENBUFFERSARBPROC gl_gen_buffers;
nuclear@30 13 static PFNGLDELETEBUFFERSARBPROC gl_delete_buffers;
nuclear@30 14 static PFNGLBUFFERDATAARBPROC gl_buffer_data;
nuclear@30 15 static PFNGLBINDBUFFERARBPROC gl_bind_buffer;
nuclear@8 16
nuclear@8 17 int vrimp_mesh_init(struct mesh *m)
nuclear@8 18 {
nuclear@8 19 m->prim = GL_TRIANGLES;
nuclear@8 20
nuclear@8 21 m->varr = 0;
nuclear@8 22 m->iarr = 0;
nuclear@8 23 m->num_verts = m->num_faces = 0;
nuclear@8 24 m->vbo = m->ibo = 0;
nuclear@8 25
nuclear@30 26 if(!gl_gen_buffers) {
nuclear@30 27 gl_gen_buffers = (PFNGLGENBUFFERSARBPROC)vrimp_glfunc("glGenBuffersARB");
nuclear@30 28 gl_delete_buffers = (PFNGLDELETEBUFFERSARBPROC)vrimp_glfunc("glDeleteBuffersARB");
nuclear@30 29 gl_buffer_data = (PFNGLBUFFERDATAARBPROC)vrimp_glfunc("glBufferDataARB");
nuclear@30 30 gl_bind_buffer = (PFNGLBINDBUFFERARBPROC)vrimp_glfunc("glBindBufferARB");
nuclear@8 31
nuclear@30 32 if(!(gl_gen_buffers && gl_delete_buffers && gl_buffer_data && gl_bind_buffer)) {
nuclear@8 33 fprintf(stderr, "Failed to load VBO functions\n");
nuclear@8 34 return -1;
nuclear@8 35 }
nuclear@8 36 }
nuclear@8 37
nuclear@8 38 return 0;
nuclear@8 39 }
nuclear@8 40
nuclear@8 41 void vrimp_mesh_destroy(struct mesh *m)
nuclear@8 42 {
nuclear@8 43 free(m->varr);
nuclear@8 44 free(m->iarr);
nuclear@8 45
nuclear@30 46 if(m->vbo) gl_delete_buffers(1, &m->vbo);
nuclear@30 47 if(m->ibo) gl_delete_buffers(1, &m->ibo);
nuclear@8 48 }
nuclear@8 49
nuclear@8 50 void vrimp_mesh_draw(struct mesh *m)
nuclear@8 51 {
nuclear@8 52 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@8 53 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@8 54
nuclear@30 55 gl_bind_buffer(GL_ARRAY_BUFFER, m->vbo);
nuclear@8 56 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex), 0);
nuclear@8 57 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), (void*)offsetof(struct vertex, tx));
nuclear@8 58
nuclear@30 59 gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
nuclear@8 60 glDrawElements(GL_TRIANGLES, m->num_faces * 3, GL_UNSIGNED_INT, 0);
nuclear@8 61
nuclear@30 62 gl_bind_buffer(GL_ARRAY_BUFFER, 0);
nuclear@30 63 gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, 0);
nuclear@8 64
nuclear@8 65 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@8 66 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@8 67 }
nuclear@8 68
nuclear@8 69 int vrimp_mesh_barrel_distortion(struct mesh *m, int usub, int vsub, float aspect,
nuclear@8 70 float lens_center_offset, float scale, const float *dist_factors,
nuclear@8 71 float tex_scale_x, float tex_scale_y)
nuclear@8 72 {
nuclear@8 73 int i, j;
nuclear@8 74 int uverts, vverts;
nuclear@8 75 int num_verts, num_quads, num_tris;
nuclear@8 76 struct vertex *varr, *vptr;
nuclear@8 77 unsigned int *iarr, *iptr;
nuclear@8 78 float du, dv;
nuclear@8 79
nuclear@8 80 uverts = usub + 1;
nuclear@8 81 vverts = vsub + 1;
nuclear@8 82
nuclear@8 83 num_verts = uverts * vverts;
nuclear@8 84 num_quads = usub * vsub;
nuclear@8 85 num_tris = num_quads * 2;
nuclear@8 86
nuclear@8 87 if(!(varr = malloc(num_verts * sizeof *varr))) {
nuclear@8 88 return -1;
nuclear@8 89 }
nuclear@8 90 if(!(iarr = malloc(num_tris * 3 * sizeof *iarr))) {
nuclear@8 91 free(varr);
nuclear@8 92 return -1;
nuclear@8 93 }
nuclear@8 94
nuclear@8 95 du = 1.0 / (float)usub;
nuclear@8 96 dv = 1.0 / (float)vsub;
nuclear@8 97
nuclear@8 98 vptr = varr;
nuclear@8 99 for(i=0; i<vverts; i++) {
nuclear@8 100 float v = (float)i * dv;
nuclear@8 101 float y = 2.0 * v - 1.0;
nuclear@8 102
nuclear@8 103 for(j=0; j<uverts; j++) {
nuclear@8 104 float u = (float)j * du;
nuclear@8 105 float x = 2.0 * u - 1.0;
nuclear@8 106 float tc[2];
nuclear@8 107 tc[0] = u;
nuclear@8 108 tc[1] = v;
nuclear@8 109
nuclear@8 110 distort_texcoords(tc, aspect, lens_center_offset, scale, dist_factors);
nuclear@8 111
nuclear@8 112 vptr->x = x;
nuclear@8 113 vptr->y = y;
nuclear@8 114 vptr->z = 0;
nuclear@8 115 vptr->tx = tc[0] * tex_scale_x;
nuclear@8 116 vptr->ty = tc[1] * tex_scale_y;
nuclear@8 117 vptr++;
nuclear@8 118 }
nuclear@8 119 }
nuclear@8 120
nuclear@8 121 iptr = iarr;
nuclear@8 122 for(i=0; i<vsub; i++) {
nuclear@8 123 for(j=0; j<usub; j++) {
nuclear@8 124 *iptr++ = i * uverts + j;
nuclear@8 125 *iptr++ = (i + 1) * uverts + j;
nuclear@8 126 *iptr++ = (i + 1) * uverts + (j + 1);
nuclear@8 127
nuclear@8 128 *iptr++ = i * uverts + j;
nuclear@8 129 *iptr++ = (i + 1) * uverts + (j + 1);
nuclear@8 130 *iptr++ = i * uverts + (j + 1);
nuclear@8 131 }
nuclear@8 132 }
nuclear@8 133
nuclear@30 134 gl_gen_buffers(1, &m->vbo);
nuclear@30 135 gl_gen_buffers(1, &m->ibo);
nuclear@30 136 gl_bind_buffer(GL_ARRAY_BUFFER, m->vbo);
nuclear@30 137 gl_buffer_data(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW);
nuclear@30 138 gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
nuclear@30 139 gl_buffer_data(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW);
nuclear@8 140
nuclear@8 141 m->prim = GL_TRIANGLES;
nuclear@8 142 m->num_verts = num_verts;
nuclear@8 143 m->num_faces = num_tris;
nuclear@8 144 return 0;
nuclear@8 145 }
nuclear@8 146
nuclear@8 147 static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale,
nuclear@8 148 const float *dist_factors)
nuclear@8 149 {
nuclear@8 150 /* map tc [0, 1] -> [-1, 1] */
nuclear@8 151 float ptx = tc[0] * 2.0 - 1.0;
nuclear@8 152 float pty = tc[1] * 2.0 - 1.0;
nuclear@8 153 float rad;
nuclear@8 154
nuclear@8 155 ptx += lens_center_offset * 2.0;
nuclear@8 156 pty /= aspect; /* correct for aspect ratio */
nuclear@8 157
nuclear@8 158 rad = barrel_scale(ptx * ptx + pty * pty, dist_factors);
nuclear@8 159 ptx *= rad; /* scale the point by the computer distortion radius */
nuclear@8 160 pty *= rad;
nuclear@8 161
nuclear@8 162 ptx /= scale;
nuclear@8 163 pty /= scale;
nuclear@8 164
nuclear@8 165 pty *= aspect;
nuclear@8 166 ptx -= lens_center_offset * 2.0;
nuclear@8 167
nuclear@8 168 /* map back to range [0, 1] */
nuclear@8 169 tc[0] = ptx * 0.5 + 0.5;
nuclear@8 170 tc[1] = pty * 0.5 + 0.5;
nuclear@8 171 }
nuclear@8 172
nuclear@8 173 static float barrel_scale(float rad, const float *k)
nuclear@8 174 {
nuclear@8 175 float radsq = rad * rad;
nuclear@8 176 float radquad = radsq * radsq;
nuclear@8 177 return k[0] + k[1] * radsq + k[2] * radquad + k[3] * radquad * radsq;
nuclear@8 178 }