dbf-udg

diff src/udg.cc @ 5:e09cbb2e9d4f

metaballs
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Feb 2013 03:46:52 +0200
parents 5fb21401b7c8
children 57ea4988a9f2
line diff
     1.1 --- a/src/udg.cc	Thu Feb 07 18:52:28 2013 +0200
     1.2 +++ b/src/udg.cc	Mon Feb 18 03:46:52 2013 +0200
     1.3 @@ -6,6 +6,7 @@
     1.4  #include "sdr.h"
     1.5  #include "dither_matrix.h"
     1.6  #include "scroller.h"
     1.7 +#include "mballs.h"
     1.8  
     1.9  #define DITHER_SZ		8
    1.10  #define DITHER_LEVELS	16
    1.11 @@ -35,11 +36,14 @@
    1.12  void destroy_rtarg(struct render_target *rt);
    1.13  
    1.14  int xsz, ysz;
    1.15 -float cam_theta, cam_phi = 25, cam_dist = 8;
    1.16 +float cam_theta, cam_phi = 25, cam_dist = 9;
    1.17  unsigned int dither_tex;
    1.18  struct render_target *rtarg;
    1.19  unsigned int prog;
    1.20  
    1.21 +int opt_highres, opt_regular_render;
    1.22 +
    1.23 +
    1.24  int main(int argc, char **argv)
    1.25  {
    1.26  	glutInit(&argc, argv);
    1.27 @@ -96,10 +100,15 @@
    1.28  		return false;
    1.29  	}
    1.30  
    1.31 +	if(!mball_init()) {
    1.32 +		return false;
    1.33 +	}
    1.34 +
    1.35  	glEnable(GL_CULL_FACE);
    1.36  	glEnable(GL_DEPTH_TEST);
    1.37  	glEnable(GL_LIGHTING);
    1.38  	glEnable(GL_LIGHT0);
    1.39 +	glEnable(GL_NORMALIZE);
    1.40  
    1.41  	return true;
    1.42  }
    1.43 @@ -137,11 +146,15 @@
    1.44  void disp()
    1.45  {
    1.46  	float ldir[] = {-1, 1, 2, 0};
    1.47 +	int xres, yres;
    1.48  
    1.49 -	int xres = xsz / DITHER_SZ;
    1.50 -	int yres = ysz / DITHER_SZ;
    1.51 -	/*int xres = xsz;
    1.52 -	int yres = ysz;*/
    1.53 +	if(opt_highres) {
    1.54 +		xres = xsz;
    1.55 +		yres = ysz;
    1.56 +	} else {
    1.57 +		xres = xsz / DITHER_SZ;
    1.58 +		yres = ysz / DITHER_SZ;
    1.59 +	}
    1.60  
    1.61  	if(!rtarg) {
    1.62  		printf("(re)creating render target (%dx%d)\n", xres, yres);
    1.63 @@ -150,7 +163,9 @@
    1.64  		}
    1.65  	}
    1.66  
    1.67 -	glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
    1.68 +	if(!opt_regular_render) {
    1.69 +		glBindFramebufferEXT(GL_FRAMEBUFFER, rtarg->fbo);
    1.70 +	}
    1.71  	glViewport(0, 0, xres, yres);
    1.72  
    1.73  	glClearColor(1, 1, 1, 1);
    1.74 @@ -161,72 +176,72 @@
    1.75  	glMatrixMode(GL_MODELVIEW);
    1.76  	glLoadIdentity();
    1.77  
    1.78 -	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    1.79 -
    1.80  	glTranslatef(0, 0, -cam_dist);
    1.81  	glRotatef(cam_phi, 1, 0, 0);
    1.82  	glRotatef(cam_theta, 0, 1, 0);
    1.83  
    1.84 +	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    1.85 +
    1.86  	const float blue[] = {0.4, 0.45, 1.0, 1};
    1.87  	const float white[] = {1, 1, 1, 1};
    1.88  	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
    1.89  	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
    1.90  	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 80.0);
    1.91  
    1.92 -	glFrontFace(GL_CW);
    1.93 -	glutSolidTeapot(1.0);
    1.94 -	glFrontFace(GL_CCW);
    1.95 +	mball_render();
    1.96  
    1.97  
    1.98 -	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
    1.99 -	glViewport(0, 0, xsz, ysz);
   1.100 +	if(!opt_regular_render) {
   1.101 +		glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
   1.102 +		glViewport(0, 0, xsz, ysz);
   1.103  
   1.104 -	glClear(GL_COLOR_BUFFER_BIT);
   1.105 +		glClear(GL_COLOR_BUFFER_BIT);
   1.106  
   1.107 -	glMatrixMode(GL_PROJECTION);
   1.108 -	glPushMatrix();
   1.109 -	glLoadIdentity();
   1.110 -	glMatrixMode(GL_MODELVIEW);
   1.111 -	glLoadIdentity();
   1.112 -	glPushMatrix();
   1.113 +		glMatrixMode(GL_PROJECTION);
   1.114 +		glPushMatrix();
   1.115 +		glLoadIdentity();
   1.116 +		glMatrixMode(GL_MODELVIEW);
   1.117 +		glLoadIdentity();
   1.118 +		glPushMatrix();
   1.119  
   1.120 -	glPushAttrib(GL_ENABLE_BIT);
   1.121 -	glDisable(GL_DEPTH_TEST);
   1.122 +		glPushAttrib(GL_ENABLE_BIT);
   1.123 +		glDisable(GL_DEPTH_TEST);
   1.124  
   1.125 -	bind_program(prog);
   1.126 -	set_uniform_int(prog, "framebuf", 0);
   1.127 -	set_uniform_int(prog, "dither_tex", 1);
   1.128 -	set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
   1.129 -	set_uniform_int(prog, "dither_size", DITHER_SZ);
   1.130 +		bind_program(prog);
   1.131 +		set_uniform_int(prog, "framebuf", 0);
   1.132 +		set_uniform_int(prog, "dither_tex", 1);
   1.133 +		set_uniform_int(prog, "dither_levels", DITHER_LEVELS);
   1.134 +		set_uniform_int(prog, "dither_size", DITHER_SZ);
   1.135  
   1.136 -	glActiveTextureARB(GL_TEXTURE0);
   1.137 -	glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
   1.138 -	glEnable(GL_TEXTURE_2D);
   1.139 -	glActiveTextureARB(GL_TEXTURE1);
   1.140 -	glBindTexture(GL_TEXTURE_2D, dither_tex);
   1.141 -	glEnable(GL_TEXTURE_2D);
   1.142 +		glActiveTextureARB(GL_TEXTURE0);
   1.143 +		glBindTexture(GL_TEXTURE_2D, rtarg->color_tex);
   1.144 +		glEnable(GL_TEXTURE_2D);
   1.145 +		glActiveTextureARB(GL_TEXTURE1);
   1.146 +		glBindTexture(GL_TEXTURE_2D, dither_tex);
   1.147 +		glEnable(GL_TEXTURE_2D);
   1.148  
   1.149 -	glBegin(GL_QUADS);
   1.150 -	glColor3f(0, 1, 0);
   1.151 -	glTexCoord2f(0, 0); glVertex2f(-1, -1);
   1.152 -	glTexCoord2f(1, 0); glVertex2f(1, -1);
   1.153 -	glTexCoord2f(1, 1); glVertex2f(1, 1);
   1.154 -	glTexCoord2f(0, 1); glVertex2f(-1, 1);
   1.155 -	glEnd();
   1.156 +		glBegin(GL_QUADS);
   1.157 +		glColor3f(0, 1, 0);
   1.158 +		glTexCoord2f(0, 0); glVertex2f(-1, -1);
   1.159 +		glTexCoord2f(1, 0); glVertex2f(1, -1);
   1.160 +		glTexCoord2f(1, 1); glVertex2f(1, 1);
   1.161 +		glTexCoord2f(0, 1); glVertex2f(-1, 1);
   1.162 +		glEnd();
   1.163  
   1.164 -	glActiveTextureARB(GL_TEXTURE1);
   1.165 -	glDisable(GL_TEXTURE_2D);
   1.166 -	glActiveTextureARB(GL_TEXTURE0);
   1.167 -	glDisable(GL_TEXTURE_2D);
   1.168 +		glActiveTextureARB(GL_TEXTURE1);
   1.169 +		glDisable(GL_TEXTURE_2D);
   1.170 +		glActiveTextureARB(GL_TEXTURE0);
   1.171 +		glDisable(GL_TEXTURE_2D);
   1.172  
   1.173 -	bind_program(0);
   1.174 +		bind_program(0);
   1.175  
   1.176 -	glPopAttrib();
   1.177 +		glPopAttrib();
   1.178  
   1.179 -	glMatrixMode(GL_PROJECTION);
   1.180 -	glPopMatrix();
   1.181 -	glMatrixMode(GL_MODELVIEW);
   1.182 -	glPopMatrix();
   1.183 +		glMatrixMode(GL_PROJECTION);
   1.184 +		glPopMatrix();
   1.185 +		glMatrixMode(GL_MODELVIEW);
   1.186 +		glPopMatrix();
   1.187 +	}
   1.188  
   1.189  	glutSwapBuffers();
   1.190  	assert(glGetError() == GL_NO_ERROR);
   1.191 @@ -274,6 +289,18 @@
   1.192  			}
   1.193  		}
   1.194  		break;
   1.195 +
   1.196 +	case 'r':
   1.197 +		opt_regular_render = !opt_regular_render;
   1.198 +		break;
   1.199 +
   1.200 +	case 'h':
   1.201 +		opt_highres = !opt_highres;
   1.202 +		if(rtarg) {
   1.203 +			destroy_rtarg(rtarg);
   1.204 +			rtarg = 0;
   1.205 +		}
   1.206 +		break;
   1.207  	}
   1.208  }
   1.209