cubemapper

view src/app.cc @ 2:e308561f9889

correct cubemap export and visualization
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 28 Jul 2017 13:24:34 +0300
parents d7a29cb7ac8d
children f5cc465eb735
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include <imago2.h>
7 #include "app.h"
8 #include "opengl.h"
9 #include "texture.h"
10 #include "mesh.h"
11 #include "meshgen.h"
13 static void draw_equilateral();
14 static void draw_cubemap();
15 static bool parse_args(int argc, char **argv);
17 static void flip_image(float *pixels, int xsz, int ysz);
19 static const char *img_fname;
20 static float cam_theta, cam_phi;
22 static Texture *pano_tex;
23 static Mesh *pano_mesh;
25 static int win_width, win_height;
26 static int show_cubemap;
28 static unsigned int fbo;
29 static unsigned int cube_tex;
30 static int cube_size;
33 bool app_init(int argc, char **argv)
34 {
35 if(!parse_args(argc, argv)) {
36 return false;
37 }
38 if(!img_fname) {
39 fprintf(stderr, "please specify an equilateral panoramic image\n");
40 return false;
41 }
43 if(!init_opengl()) {
44 return false;
45 }
47 glEnable(GL_MULTISAMPLE);
49 Mesh::use_custom_sdr_attr = false;
50 pano_mesh = new Mesh;
51 gen_sphere(pano_mesh, 1.0, 80, 40);
52 pano_mesh->flip();
53 Mat4 xform;
54 xform.rotation_y(-M_PI / 2.0); // rotate the sphere to face the "front" part of the image
55 pano_mesh->apply_xform(xform, xform);
57 xform.scaling(-1, 1, 1); // flip horizontal texcoord since we're inside the sphere
58 pano_mesh->texcoord_apply_xform(xform);
60 pano_tex = new Texture;
61 if(!pano_tex->load(img_fname)) {
62 return false;
63 }
64 printf("loaded image: %dx%d\n", pano_tex->get_width(), pano_tex->get_height());
66 // create cubemap
67 cube_size = pano_tex->get_height();
68 glGenTextures(1, &cube_tex);
69 glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
70 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
71 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
72 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
73 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
74 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
76 for(int i=0; i<6; i++) {
77 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, cube_size, cube_size,
78 0, GL_RGB, GL_FLOAT, 0);
79 }
82 // create fbo
83 glGenFramebuffers(1, &fbo);
85 // tex-gen for cubemap visualization
86 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
87 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
88 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
89 float planes[][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}};
90 glTexGenfv(GL_S, GL_OBJECT_PLANE, planes[0]);
91 glTexGenfv(GL_T, GL_OBJECT_PLANE, planes[1]);
92 glTexGenfv(GL_R, GL_OBJECT_PLANE, planes[2]);
93 return true;
94 }
96 void app_cleanup()
97 {
98 delete pano_mesh;
99 delete pano_tex;
100 }
102 void app_draw()
103 {
104 glClear(GL_COLOR_BUFFER_BIT);
106 Mat4 view_matrix;
107 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
108 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
110 glMatrixMode(GL_MODELVIEW);
111 glLoadMatrixf(view_matrix[0]);
113 if(show_cubemap) {
114 draw_cubemap();
116 glColor3f(0, 0, 0);
117 app_print_text(10, 10, "cubemap");
118 glColor3f(0, 0.8, 1);
119 app_print_text(8, 13, "cubemap");
120 } else {
121 draw_equilateral();
123 glColor3f(0, 0, 0);
124 app_print_text(10, 10, "equilateral");
125 glColor3f(1, 0.8, 0);
126 app_print_text(8, 13, "equilateral");
127 }
128 glColor3f(1, 1, 1);
130 app_swap_buffers();
131 assert(glGetError() == GL_NO_ERROR);
132 }
134 void render_cubemap()
135 {
136 printf("rendering cubemap %dx%d\n", cube_size, cube_size);
138 float *pixels = new float[cube_size * cube_size * 3];
140 glViewport(0, 0, cube_size, cube_size);
142 Mat4 viewmat[6];
143 viewmat[0].rotation_y(deg_to_rad(90)); // +X
144 viewmat[1].rotation_y(deg_to_rad(-90)); // -X
145 viewmat[2].rotation_x(deg_to_rad(90)); // +Y
146 viewmat[2].rotate_y(deg_to_rad(180));
147 viewmat[3].rotation_x(deg_to_rad(-90)); // -Y
148 viewmat[3].rotate_y(deg_to_rad(180));
149 viewmat[4].rotation_y(deg_to_rad(180)); // +Z
151 // this must coincide with the order of GL_TEXTURE_CUBE_MAP_* values
152 static const char *fname[] = {
153 "cubemap_px.jpg",
154 "cubemap_nx.jpg",
155 "cubemap_py.jpg",
156 "cubemap_ny.jpg",
157 "cubemap_pz.jpg",
158 "cubemap_nz.jpg"
159 };
161 glMatrixMode(GL_PROJECTION);
162 glPushMatrix();
163 glLoadIdentity();
164 gluPerspective(90, 1.0, 0.5, 500.0);
165 glScalef(-1, -1, 1);
167 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
169 for(int i=0; i<6; i++) {
170 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
171 GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cube_tex, 0);
173 glClear(GL_COLOR_BUFFER_BIT);
175 glMatrixMode(GL_MODELVIEW);
176 glLoadMatrixf(viewmat[i][0]);
178 draw_equilateral();
180 //glReadPixels(0, 0, cube_size, cube_size, GL_RGB, GL_FLOAT, pixels);
181 glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, GL_FLOAT, pixels);
182 //flip_image(pixels, cube_size, cube_size);
184 if(img_save_pixels(fname[i], pixels, cube_size, cube_size, IMG_FMT_RGBF) == -1) {
185 fprintf(stderr, "failed to save %dx%d image: %s\n", cube_size, cube_size, fname[i]);
186 }
187 }
189 glBindFramebuffer(GL_FRAMEBUFFER, 0);
190 glViewport(0, 0, win_width, win_height);
192 glMatrixMode(GL_PROJECTION);
193 glPopMatrix();
195 delete [] pixels;
197 glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
198 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
199 }
201 static void draw_equilateral()
202 {
203 pano_tex->bind();
204 glEnable(GL_TEXTURE_2D);
205 pano_mesh->draw();
206 glDisable(GL_TEXTURE_2D);
207 }
209 static void draw_cubemap()
210 {
211 glPushAttrib(GL_ENABLE_BIT);
213 glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
214 glEnable(GL_TEXTURE_CUBE_MAP);
215 glEnable(GL_TEXTURE_GEN_S);
216 glEnable(GL_TEXTURE_GEN_T);
217 glEnable(GL_TEXTURE_GEN_R);
219 pano_mesh->draw();
221 glPopAttrib();
222 }
224 void app_reshape(int x, int y)
225 {
226 glViewport(0, 0, x, y);
228 glMatrixMode(GL_PROJECTION);
229 glLoadIdentity();
230 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
232 win_width = x;
233 win_height = y;
234 }
236 void app_keyboard(int key, bool press)
237 {
238 if(press) {
239 switch(key) {
240 case 27:
241 app_quit();
242 break;
244 case ' ':
245 show_cubemap = !show_cubemap;
246 app_redisplay();
247 break;
249 case 'c':
250 render_cubemap();
251 break;
252 }
253 }
254 }
256 static float prev_x, prev_y;
257 static bool bnstate[16];
259 void app_mouse_button(int bn, bool press, int x, int y)
260 {
261 if(bn < (int)(sizeof bnstate / sizeof *bnstate)) {
262 bnstate[bn] = press;
263 }
264 prev_x = x;
265 prev_y = y;
266 }
268 void app_mouse_motion(int x, int y)
269 {
270 float dx = x - prev_x;
271 float dy = y - prev_y;
272 prev_x = x;
273 prev_y = y;
275 if(!dx && !dy) return;
277 if(bnstate[0]) {
278 cam_theta += dx * 0.5;
279 cam_phi += dy * 0.5;
281 if(cam_phi < -90) cam_phi = -90;
282 if(cam_phi > 90) cam_phi = 90;
283 app_redisplay();
284 }
285 }
287 static bool parse_args(int argc, char **argv)
288 {
289 for(int i=1; i<argc; i++) {
290 if(argv[i][0] == '-') {
291 /*
292 } else if(strcmp(argv[i], "-help") == 0) {
293 printf("usage: %s [options]\noptions:\n", argv[0]);
294 printf(" -help: print usage information and exit\n");
295 exit(0);
296 } else {*/
297 fprintf(stderr, "invalid option: %s\n", argv[i]);
298 return false;
299 //}
300 } else {
301 if(img_fname) {
302 fprintf(stderr, "unexpected option: %s\n", argv[i]);
303 return false;
304 }
305 img_fname = argv[i];
306 }
307 }
309 return true;
310 }
312 static void flip_image(float *pixels, int xsz, int ysz)
313 {
314 float *top_ptr = pixels;
315 float *bot_ptr = pixels + xsz * (ysz - 1) * 3;
316 float *line = new float[xsz * 3];
317 int scansz = xsz * 3 * sizeof(float);
319 for(int i=0; i<ysz / 2; i++) {
320 memcpy(line, top_ptr, scansz);
321 memcpy(top_ptr, bot_ptr, scansz);
322 memcpy(bot_ptr, line, scansz);
323 top_ptr += xsz * 3;
324 bot_ptr -= xsz * 3;
325 }
327 delete [] line;
328 }