cubemapper

view src/app.cc @ 3:f5cc465eb735

save cubemaps of the same file format as the opened file
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 28 Jul 2017 15:50:00 +0300
parents e308561f9889
children 2bfafdced01a
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, *img_suffix;
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 if(!(img_suffix = strrchr(img_fname, '.'))) {
67 img_suffix = ".jpg";
68 }
70 // create cubemap
71 cube_size = pano_tex->get_height();
72 glGenTextures(1, &cube_tex);
73 glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
74 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
75 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
77 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
78 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
80 for(int i=0; i<6; i++) {
81 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, cube_size, cube_size,
82 0, GL_RGB, GL_FLOAT, 0);
83 }
86 // create fbo
87 glGenFramebuffers(1, &fbo);
89 // tex-gen for cubemap visualization
90 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
91 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
92 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
93 float planes[][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}};
94 glTexGenfv(GL_S, GL_OBJECT_PLANE, planes[0]);
95 glTexGenfv(GL_T, GL_OBJECT_PLANE, planes[1]);
96 glTexGenfv(GL_R, GL_OBJECT_PLANE, planes[2]);
97 return true;
98 }
100 void app_cleanup()
101 {
102 delete pano_mesh;
103 delete pano_tex;
104 }
106 void app_draw()
107 {
108 glClear(GL_COLOR_BUFFER_BIT);
110 Mat4 view_matrix;
111 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
112 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
114 glMatrixMode(GL_MODELVIEW);
115 glLoadMatrixf(view_matrix[0]);
117 if(show_cubemap) {
118 draw_cubemap();
120 glColor3f(0, 0, 0);
121 app_print_text(10, 10, "cubemap");
122 glColor3f(0, 0.8, 1);
123 app_print_text(8, 13, "cubemap");
124 } else {
125 draw_equilateral();
127 glColor3f(0, 0, 0);
128 app_print_text(10, 10, "equilateral");
129 glColor3f(1, 0.8, 0);
130 app_print_text(8, 13, "equilateral");
131 }
132 glColor3f(1, 1, 1);
134 app_swap_buffers();
135 assert(glGetError() == GL_NO_ERROR);
136 }
138 void render_cubemap()
139 {
140 printf("rendering cubemap %dx%d\n", cube_size, cube_size);
142 float *pixels = new float[cube_size * cube_size * 3];
144 glViewport(0, 0, cube_size, cube_size);
146 Mat4 viewmat[6];
147 viewmat[0].rotation_y(deg_to_rad(90)); // +X
148 viewmat[1].rotation_y(deg_to_rad(-90)); // -X
149 viewmat[2].rotation_x(deg_to_rad(90)); // +Y
150 viewmat[2].rotate_y(deg_to_rad(180));
151 viewmat[3].rotation_x(deg_to_rad(-90)); // -Y
152 viewmat[3].rotate_y(deg_to_rad(180));
153 viewmat[4].rotation_y(deg_to_rad(180)); // +Z
155 // this must coincide with the order of GL_TEXTURE_CUBE_MAP_* values
156 static const char *fname_pattern[] = {
157 "cubemap_px%s",
158 "cubemap_nx%s",
159 "cubemap_py%s",
160 "cubemap_ny%s",
161 "cubemap_pz%s",
162 "cubemap_nz%s"
163 };
164 static char fname[64];
166 glMatrixMode(GL_PROJECTION);
167 glPushMatrix();
168 glLoadIdentity();
169 gluPerspective(90, 1.0, 0.5, 500.0);
170 glScalef(-1, -1, 1);
172 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
174 for(int i=0; i<6; i++) {
175 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
176 GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cube_tex, 0);
178 glClear(GL_COLOR_BUFFER_BIT);
180 glMatrixMode(GL_MODELVIEW);
181 glLoadMatrixf(viewmat[i][0]);
183 draw_equilateral();
185 //glReadPixels(0, 0, cube_size, cube_size, GL_RGB, GL_FLOAT, pixels);
186 glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, GL_FLOAT, pixels);
187 //flip_image(pixels, cube_size, cube_size);
189 sprintf(fname, fname_pattern[i], img_suffix);
190 if(img_save_pixels(fname, pixels, cube_size, cube_size, IMG_FMT_RGBF) == -1) {
191 fprintf(stderr, "failed to save %dx%d image: %s\n", cube_size, cube_size, fname);
192 }
193 }
195 glBindFramebuffer(GL_FRAMEBUFFER, 0);
196 glViewport(0, 0, win_width, win_height);
198 glMatrixMode(GL_PROJECTION);
199 glPopMatrix();
201 delete [] pixels;
203 glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
204 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
205 }
207 static void draw_equilateral()
208 {
209 pano_tex->bind();
210 glEnable(GL_TEXTURE_2D);
211 pano_mesh->draw();
212 glDisable(GL_TEXTURE_2D);
213 }
215 static void draw_cubemap()
216 {
217 glPushAttrib(GL_ENABLE_BIT);
219 glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
220 glEnable(GL_TEXTURE_CUBE_MAP);
221 glEnable(GL_TEXTURE_GEN_S);
222 glEnable(GL_TEXTURE_GEN_T);
223 glEnable(GL_TEXTURE_GEN_R);
225 pano_mesh->draw();
227 glPopAttrib();
228 }
230 void app_reshape(int x, int y)
231 {
232 glViewport(0, 0, x, y);
234 glMatrixMode(GL_PROJECTION);
235 glLoadIdentity();
236 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
238 win_width = x;
239 win_height = y;
240 }
242 void app_keyboard(int key, bool press)
243 {
244 if(press) {
245 switch(key) {
246 case 27:
247 app_quit();
248 break;
250 case ' ':
251 show_cubemap = !show_cubemap;
252 app_redisplay();
253 break;
255 case 'c':
256 render_cubemap();
257 break;
258 }
259 }
260 }
262 static float prev_x, prev_y;
263 static bool bnstate[16];
265 void app_mouse_button(int bn, bool press, int x, int y)
266 {
267 if(bn < (int)(sizeof bnstate / sizeof *bnstate)) {
268 bnstate[bn] = press;
269 }
270 prev_x = x;
271 prev_y = y;
272 }
274 void app_mouse_motion(int x, int y)
275 {
276 float dx = x - prev_x;
277 float dy = y - prev_y;
278 prev_x = x;
279 prev_y = y;
281 if(!dx && !dy) return;
283 if(bnstate[0]) {
284 cam_theta += dx * 0.5;
285 cam_phi += dy * 0.5;
287 if(cam_phi < -90) cam_phi = -90;
288 if(cam_phi > 90) cam_phi = 90;
289 app_redisplay();
290 }
291 }
293 static bool parse_args(int argc, char **argv)
294 {
295 for(int i=1; i<argc; i++) {
296 if(argv[i][0] == '-') {
297 /*
298 } else if(strcmp(argv[i], "-help") == 0) {
299 printf("usage: %s [options]\noptions:\n", argv[0]);
300 printf(" -help: print usage information and exit\n");
301 exit(0);
302 } else {*/
303 fprintf(stderr, "invalid option: %s\n", argv[i]);
304 return false;
305 //}
306 } else {
307 if(img_fname) {
308 fprintf(stderr, "unexpected option: %s\n", argv[i]);
309 return false;
310 }
311 img_fname = argv[i];
312 }
313 }
315 return true;
316 }
318 static void flip_image(float *pixels, int xsz, int ysz)
319 {
320 float *top_ptr = pixels;
321 float *bot_ptr = pixels + xsz * (ysz - 1) * 3;
322 float *line = new float[xsz * 3];
323 int scansz = xsz * 3 * sizeof(float);
325 for(int i=0; i<ysz / 2; i++) {
326 memcpy(line, top_ptr, scansz);
327 memcpy(top_ptr, bot_ptr, scansz);
328 memcpy(bot_ptr, line, scansz);
329 top_ptr += xsz * 3;
330 bot_ptr -= xsz * 3;
331 }
333 delete [] line;
334 }