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