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@20
|
24 #else
|
nuclear@20
|
25 #ifndef GL_GLEXT_PROTOTYPES
|
nuclear@20
|
26 GLAPI void APIENTRY glGenBuffers(GLsizei, GLuint*);
|
nuclear@20
|
27 GLAPI void APIENTRY glDeleteBuffers(GLsizei, GLuint*);
|
nuclear@20
|
28 GLAPI void APIENTRY glBufferData(GLenum, unsigned int, const GLvoid*, GLenum);
|
nuclear@20
|
29 GLAPI void APIENTRY glBindBuffer(GLenum, GLuint);
|
nuclear@20
|
30 #endif
|
nuclear@8
|
31 #endif
|
nuclear@8
|
32
|
nuclear@8
|
33 int vrimp_mesh_init(struct mesh *m)
|
nuclear@8
|
34 {
|
nuclear@8
|
35 m->prim = GL_TRIANGLES;
|
nuclear@8
|
36
|
nuclear@8
|
37 m->varr = 0;
|
nuclear@8
|
38 m->iarr = 0;
|
nuclear@8
|
39 m->num_verts = m->num_faces = 0;
|
nuclear@8
|
40 m->vbo = m->ibo = 0;
|
nuclear@8
|
41
|
nuclear@9
|
42 #ifndef GL_VERSION_1_5
|
nuclear@8
|
43 if(!glGenBuffers) {
|
nuclear@8
|
44 glGenBuffers = vrimp_glfunc("glGenBuffersARB");
|
nuclear@8
|
45 glDeleteBuffers = vrimp_glfunc("glDeleteBuffersARB");
|
nuclear@8
|
46 glBufferData = vrimp_glfunc("glBufferDataARB");
|
nuclear@8
|
47 glBindBuffer = vrimp_glfunc("glBindBufferARB");
|
nuclear@8
|
48
|
nuclear@8
|
49 if(!(glGenBuffers && glDeleteBuffers && glBufferData && glBindBuffer)) {
|
nuclear@8
|
50 fprintf(stderr, "Failed to load VBO functions\n");
|
nuclear@8
|
51 return -1;
|
nuclear@8
|
52 }
|
nuclear@8
|
53 }
|
nuclear@9
|
54 #endif
|
nuclear@8
|
55
|
nuclear@8
|
56 return 0;
|
nuclear@8
|
57 }
|
nuclear@8
|
58
|
nuclear@8
|
59 void vrimp_mesh_destroy(struct mesh *m)
|
nuclear@8
|
60 {
|
nuclear@8
|
61 free(m->varr);
|
nuclear@8
|
62 free(m->iarr);
|
nuclear@8
|
63
|
nuclear@8
|
64 if(m->vbo) glDeleteBuffers(1, &m->vbo);
|
nuclear@8
|
65 if(m->ibo) glDeleteBuffers(1, &m->ibo);
|
nuclear@8
|
66 }
|
nuclear@8
|
67
|
nuclear@8
|
68 void vrimp_mesh_draw(struct mesh *m)
|
nuclear@8
|
69 {
|
nuclear@8
|
70 glEnableClientState(GL_VERTEX_ARRAY);
|
nuclear@8
|
71 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@8
|
72
|
nuclear@8
|
73 glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
|
nuclear@8
|
74 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex), 0);
|
nuclear@8
|
75 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), (void*)offsetof(struct vertex, tx));
|
nuclear@8
|
76
|
nuclear@8
|
77 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
|
nuclear@8
|
78 glDrawElements(GL_TRIANGLES, m->num_faces * 3, GL_UNSIGNED_INT, 0);
|
nuclear@8
|
79
|
nuclear@8
|
80 glBindBuffer(GL_ARRAY_BUFFER, 0);
|
nuclear@8
|
81 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
nuclear@8
|
82
|
nuclear@8
|
83 glDisableClientState(GL_VERTEX_ARRAY);
|
nuclear@8
|
84 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@8
|
85 }
|
nuclear@8
|
86
|
nuclear@8
|
87 int vrimp_mesh_barrel_distortion(struct mesh *m, int usub, int vsub, float aspect,
|
nuclear@8
|
88 float lens_center_offset, float scale, const float *dist_factors,
|
nuclear@8
|
89 float tex_scale_x, float tex_scale_y)
|
nuclear@8
|
90 {
|
nuclear@8
|
91 int i, j;
|
nuclear@8
|
92 int uverts, vverts;
|
nuclear@8
|
93 int num_verts, num_quads, num_tris;
|
nuclear@8
|
94 struct vertex *varr, *vptr;
|
nuclear@8
|
95 unsigned int *iarr, *iptr;
|
nuclear@8
|
96 float du, dv;
|
nuclear@8
|
97
|
nuclear@8
|
98 uverts = usub + 1;
|
nuclear@8
|
99 vverts = vsub + 1;
|
nuclear@8
|
100
|
nuclear@8
|
101 num_verts = uverts * vverts;
|
nuclear@8
|
102 num_quads = usub * vsub;
|
nuclear@8
|
103 num_tris = num_quads * 2;
|
nuclear@8
|
104
|
nuclear@8
|
105 if(!(varr = malloc(num_verts * sizeof *varr))) {
|
nuclear@8
|
106 return -1;
|
nuclear@8
|
107 }
|
nuclear@8
|
108 if(!(iarr = malloc(num_tris * 3 * sizeof *iarr))) {
|
nuclear@8
|
109 free(varr);
|
nuclear@8
|
110 return -1;
|
nuclear@8
|
111 }
|
nuclear@8
|
112
|
nuclear@8
|
113 du = 1.0 / (float)usub;
|
nuclear@8
|
114 dv = 1.0 / (float)vsub;
|
nuclear@8
|
115
|
nuclear@8
|
116 vptr = varr;
|
nuclear@8
|
117 for(i=0; i<vverts; i++) {
|
nuclear@8
|
118 float v = (float)i * dv;
|
nuclear@8
|
119 float y = 2.0 * v - 1.0;
|
nuclear@8
|
120
|
nuclear@8
|
121 for(j=0; j<uverts; j++) {
|
nuclear@8
|
122 float u = (float)j * du;
|
nuclear@8
|
123 float x = 2.0 * u - 1.0;
|
nuclear@8
|
124 float tc[2];
|
nuclear@8
|
125 tc[0] = u;
|
nuclear@8
|
126 tc[1] = v;
|
nuclear@8
|
127
|
nuclear@8
|
128 distort_texcoords(tc, aspect, lens_center_offset, scale, dist_factors);
|
nuclear@8
|
129
|
nuclear@8
|
130 vptr->x = x;
|
nuclear@8
|
131 vptr->y = y;
|
nuclear@8
|
132 vptr->z = 0;
|
nuclear@8
|
133 vptr->tx = tc[0] * tex_scale_x;
|
nuclear@8
|
134 vptr->ty = tc[1] * tex_scale_y;
|
nuclear@8
|
135 vptr++;
|
nuclear@8
|
136 }
|
nuclear@8
|
137 }
|
nuclear@8
|
138
|
nuclear@8
|
139 iptr = iarr;
|
nuclear@8
|
140 for(i=0; i<vsub; i++) {
|
nuclear@8
|
141 for(j=0; j<usub; j++) {
|
nuclear@8
|
142 *iptr++ = i * uverts + j;
|
nuclear@8
|
143 *iptr++ = (i + 1) * uverts + j;
|
nuclear@8
|
144 *iptr++ = (i + 1) * uverts + (j + 1);
|
nuclear@8
|
145
|
nuclear@8
|
146 *iptr++ = i * uverts + j;
|
nuclear@8
|
147 *iptr++ = (i + 1) * uverts + (j + 1);
|
nuclear@8
|
148 *iptr++ = i * uverts + (j + 1);
|
nuclear@8
|
149 }
|
nuclear@8
|
150 }
|
nuclear@8
|
151
|
nuclear@8
|
152 glGenBuffers(1, &m->vbo);
|
nuclear@8
|
153 glGenBuffers(1, &m->ibo);
|
nuclear@8
|
154 glBindBuffer(GL_ARRAY_BUFFER, m->vbo);
|
nuclear@8
|
155 glBufferData(GL_ARRAY_BUFFER, num_verts * sizeof *varr, varr, GL_STATIC_DRAW);
|
nuclear@8
|
156 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m->ibo);
|
nuclear@8
|
157 glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_tris * 3 * sizeof *iarr, iarr, GL_STATIC_DRAW);
|
nuclear@8
|
158
|
nuclear@8
|
159 m->prim = GL_TRIANGLES;
|
nuclear@8
|
160 m->num_verts = num_verts;
|
nuclear@8
|
161 m->num_faces = num_tris;
|
nuclear@8
|
162 return 0;
|
nuclear@8
|
163 }
|
nuclear@8
|
164
|
nuclear@8
|
165 static void distort_texcoords(float *tc, float aspect, float lens_center_offset, float scale,
|
nuclear@8
|
166 const float *dist_factors)
|
nuclear@8
|
167 {
|
nuclear@8
|
168 /* map tc [0, 1] -> [-1, 1] */
|
nuclear@8
|
169 float ptx = tc[0] * 2.0 - 1.0;
|
nuclear@8
|
170 float pty = tc[1] * 2.0 - 1.0;
|
nuclear@8
|
171 float rad;
|
nuclear@8
|
172
|
nuclear@8
|
173 ptx += lens_center_offset * 2.0;
|
nuclear@8
|
174 pty /= aspect; /* correct for aspect ratio */
|
nuclear@8
|
175
|
nuclear@8
|
176 rad = barrel_scale(ptx * ptx + pty * pty, dist_factors);
|
nuclear@8
|
177 ptx *= rad; /* scale the point by the computer distortion radius */
|
nuclear@8
|
178 pty *= rad;
|
nuclear@8
|
179
|
nuclear@8
|
180 ptx /= scale;
|
nuclear@8
|
181 pty /= scale;
|
nuclear@8
|
182
|
nuclear@8
|
183 pty *= aspect;
|
nuclear@8
|
184 ptx -= lens_center_offset * 2.0;
|
nuclear@8
|
185
|
nuclear@8
|
186 /* map back to range [0, 1] */
|
nuclear@8
|
187 tc[0] = ptx * 0.5 + 0.5;
|
nuclear@8
|
188 tc[1] = pty * 0.5 + 0.5;
|
nuclear@8
|
189 }
|
nuclear@8
|
190
|
nuclear@8
|
191 static float barrel_scale(float rad, const float *k)
|
nuclear@8
|
192 {
|
nuclear@8
|
193 float radsq = rad * rad;
|
nuclear@8
|
194 float radquad = radsq * radsq;
|
nuclear@8
|
195 return k[0] + k[1] * radsq + k[2] * radquad + k[3] * radquad * radsq;
|
nuclear@8
|
196 }
|