cubemapper

changeset 5:614295b72341 tip

minor fixes, and redundant code removal
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 30 Jul 2017 16:16:57 +0300
parents 2bfafdced01a
children
files src/app.cc
diffstat 1 files changed, 20 insertions(+), 47 deletions(-) [+]
line diff
     1.1 --- a/src/app.cc	Sun Jul 30 16:11:19 2017 +0300
     1.2 +++ b/src/app.cc	Sun Jul 30 16:16:57 2017 +0300
     1.3 @@ -31,13 +31,11 @@
     1.4  static void draw_cubemap();
     1.5  static bool parse_args(int argc, char **argv);
     1.6  
     1.7 -static void flip_image(float *pixels, int xsz, int ysz);
     1.8 -
     1.9  static const char *img_fname, *img_suffix;
    1.10  static float cam_theta, cam_phi;
    1.11  
    1.12 -static Texture *pano_tex;
    1.13 -static Mesh *pano_mesh;
    1.14 +static Texture *tex;
    1.15 +static Mesh *mesh;
    1.16  
    1.17  static int win_width, win_height;
    1.18  static int show_cubemap;
    1.19 @@ -64,28 +62,28 @@
    1.20  	glEnable(GL_MULTISAMPLE);
    1.21  
    1.22  	Mesh::use_custom_sdr_attr = false;
    1.23 -	pano_mesh = new Mesh;
    1.24 -	gen_sphere(pano_mesh, 1.0, 80, 40);
    1.25 -	pano_mesh->flip();
    1.26 +	mesh = new Mesh;
    1.27 +	gen_sphere(mesh, 1.0, 80, 40);
    1.28 +	mesh->flip();
    1.29  	Mat4 xform;
    1.30  	xform.rotation_y(-M_PI / 2.0);	// rotate the sphere to face the "front" part of the image
    1.31 -	pano_mesh->apply_xform(xform, xform);
    1.32 +	mesh->apply_xform(xform, xform);
    1.33  
    1.34  	xform.scaling(-1, 1, 1);		// flip horizontal texcoord since we're inside the sphere
    1.35 -	pano_mesh->texcoord_apply_xform(xform);
    1.36 +	mesh->texcoord_apply_xform(xform);
    1.37  
    1.38 -	pano_tex = new Texture;
    1.39 -	if(!pano_tex->load(img_fname)) {
    1.40 +	tex = new Texture;
    1.41 +	if(!tex->load(img_fname)) {
    1.42  		return false;
    1.43  	}
    1.44 -	printf("loaded image: %dx%d\n", pano_tex->get_width(), pano_tex->get_height());
    1.45 +	printf("loaded image: %dx%d\n", tex->get_width(), tex->get_height());
    1.46  
    1.47  	if(!(img_suffix = strrchr(img_fname, '.'))) {
    1.48  		img_suffix = ".jpg";
    1.49  	}
    1.50  
    1.51  	// create cubemap
    1.52 -	cube_size = pano_tex->get_height();
    1.53 +	cube_size = tex->get_height();
    1.54  	glGenTextures(1, &cube_tex);
    1.55  	glBindTexture(GL_TEXTURE_CUBE_MAP, cube_tex);
    1.56  	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    1.57 @@ -116,8 +114,8 @@
    1.58  
    1.59  void app_cleanup()
    1.60  {
    1.61 -	delete pano_mesh;
    1.62 -	delete pano_tex;
    1.63 +	delete mesh;
    1.64 +	delete tex;
    1.65  }
    1.66  
    1.67  void app_draw()
    1.68 @@ -199,9 +197,7 @@
    1.69  
    1.70  		draw_equilateral();
    1.71  
    1.72 -		//glReadPixels(0, 0, cube_size, cube_size, GL_RGB, GL_FLOAT, pixels);
    1.73  		glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, GL_FLOAT, pixels);
    1.74 -		//flip_image(pixels, cube_size, cube_size);
    1.75  
    1.76  		sprintf(fname, fname_pattern[i], img_suffix);
    1.77  		if(img_save_pixels(fname, pixels, cube_size, cube_size, IMG_FMT_RGBF) == -1) {
    1.78 @@ -223,9 +219,9 @@
    1.79  
    1.80  static void draw_equilateral()
    1.81  {
    1.82 -	pano_tex->bind();
    1.83 +	tex->bind();
    1.84  	glEnable(GL_TEXTURE_2D);
    1.85 -	pano_mesh->draw();
    1.86 +	mesh->draw();
    1.87  	glDisable(GL_TEXTURE_2D);
    1.88  }
    1.89  
    1.90 @@ -239,7 +235,7 @@
    1.91  	glEnable(GL_TEXTURE_GEN_T);
    1.92  	glEnable(GL_TEXTURE_GEN_R);
    1.93  
    1.94 -	pano_mesh->draw();
    1.95 +	mesh->draw();
    1.96  
    1.97  	glPopAttrib();
    1.98  }
    1.99 @@ -271,6 +267,8 @@
   1.100  
   1.101  		case 'c':
   1.102  			render_cubemap();
   1.103 +			show_cubemap = 1;
   1.104 +			app_redisplay();
   1.105  			break;
   1.106  		}
   1.107  	}
   1.108 @@ -311,15 +309,8 @@
   1.109  {
   1.110  	for(int i=1; i<argc; i++) {
   1.111  		if(argv[i][0] == '-') {
   1.112 -			/*
   1.113 -			} else if(strcmp(argv[i], "-help") == 0) {
   1.114 -				printf("usage: %s [options]\noptions:\n", argv[0]);
   1.115 -				printf(" -help: print usage information and exit\n");
   1.116 -				exit(0);
   1.117 -			} else {*/
   1.118 -				fprintf(stderr, "invalid option: %s\n", argv[i]);
   1.119 -				return false;
   1.120 -			//}
   1.121 +			fprintf(stderr, "invalid option: %s\n", argv[i]);
   1.122 +			return false;
   1.123  		} else {
   1.124  			if(img_fname) {
   1.125  				fprintf(stderr, "unexpected option: %s\n", argv[i]);
   1.126 @@ -331,21 +322,3 @@
   1.127  
   1.128  	return true;
   1.129  }
   1.130 -
   1.131 -static void flip_image(float *pixels, int xsz, int ysz)
   1.132 -{
   1.133 -	float *top_ptr = pixels;
   1.134 -	float *bot_ptr = pixels + xsz * (ysz - 1) * 3;
   1.135 -	float *line = new float[xsz * 3];
   1.136 -	int scansz = xsz * 3 * sizeof(float);
   1.137 -
   1.138 -	for(int i=0; i<ysz / 2; i++) {
   1.139 -		memcpy(line, top_ptr, scansz);
   1.140 -		memcpy(top_ptr, bot_ptr, scansz);
   1.141 -		memcpy(bot_ptr, line, scansz);
   1.142 -		top_ptr += xsz * 3;
   1.143 -		bot_ptr -= xsz * 3;
   1.144 -	}
   1.145 -
   1.146 -	delete [] line;
   1.147 -}