volmetrics

diff src/main.cc @ 17:0f4fff558737

transfer function
author Eleni Maria Stea <elene.mst@gmail.com>
date Mon, 03 Mar 2014 23:12:13 +0200
parents add30e2d5253
children 4e02e18e70ef
line diff
     1.1 --- a/src/main.cc	Mon Feb 03 02:52:46 2014 +0200
     1.2 +++ b/src/main.cc	Mon Mar 03 23:12:13 2014 +0200
     1.3 @@ -2,6 +2,7 @@
     1.4  #include <GL/glut.h>
     1.5  #include <GL/glui.h>
     1.6  
     1.7 +#include <math.h>
     1.8  #include <stdio.h>
     1.9  #include <assert.h>
    1.10  
    1.11 @@ -22,7 +23,9 @@
    1.12  static bool init_xfer(void);
    1.13  static void display_xfer(void);
    1.14  static void reshape_xfer(int x, int y);
    1.15 +static void mouse_xfer(int button, int state, int x, int y);
    1.16  static void motion_xfer(int x, int y);
    1.17 +//todo keyb esc
    1.18  
    1.19  static int mainwin_id, xferwin_id;
    1.20  
    1.21 @@ -320,6 +323,7 @@
    1.22  	xferwin_id = glutCreateWindow("Transfer Function");
    1.23  	glutDisplayFunc(display_xfer);
    1.24  	glutReshapeFunc(reshape_xfer);
    1.25 +	glutMouseFunc(mouse_xfer);
    1.26  	glutMotionFunc(motion_xfer);
    1.27  
    1.28  	return true;
    1.29 @@ -342,7 +346,34 @@
    1.30  		high = thres;
    1.31  	}
    1.32  
    1.33 -	glBegin(GL_QUADS);
    1.34 +	//drawing the transfer function curve
    1.35 +
    1.36 +	glLineWidth(3);
    1.37 +	glBegin(GL_LINE_STRIP);
    1.38 +	glColor3f(0.8, 0.3, 0.8);
    1.39 +	for(int i=0; i<num_val; i++) {
    1.40 +		float val = transfer_function(x, low, high);
    1.41 +		glVertex2f(x, val);
    1.42 +		x += dx;
    1.43 +	}
    1.44 +	glEnd();
    1.45 +
    1.46 +	//threshold bars
    1.47 +
    1.48 +	glLineWidth(2);
    1.49 +	glBegin(GL_LINES);
    1.50 +	glColor3f(0.4, 0.4, 0.8);
    1.51 +	glVertex2f(low, 0);
    1.52 +	glVertex2f(low, 1);
    1.53 +	glColor3f(0.4, 0.8, 0.4);
    1.54 +	glVertex2f(high, 0);
    1.55 +	glVertex2f(high, 1);
    1.56 +	glEnd();
    1.57 +
    1.58 +/*
    1.59 + * gradient
    1.60 + */
    1.61 +/*  glBegin(GL_QUADS);
    1.62  	for(int i=0; i<num_val; i++) {
    1.63  		float val = transfer_function(x, low, high);
    1.64  		glColor3f(val, val, val);
    1.65 @@ -355,7 +386,7 @@
    1.66  		glVertex3f(x + dx, 1.0, 0.0);
    1.67  		x += dx;
    1.68  	}
    1.69 -	glEnd();
    1.70 +	glEnd(); */
    1.71  
    1.72  	glutSwapBuffers();
    1.73  	assert(glGetError() == GL_NO_ERROR);
    1.74 @@ -369,8 +400,43 @@
    1.75  	glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    1.76  }
    1.77  
    1.78 +static float *select_thres;
    1.79 +static float prev_select_thres;
    1.80 +static void mouse_xfer(int button, int state, int x, int y)
    1.81 +{
    1.82 +	if(button == GLUT_LEFT_BUTTON) {
    1.83 +		if(state == GLUT_DOWN) {
    1.84 +			int width = glutGet(GLUT_WINDOW_WIDTH);
    1.85 +			float xpos = (float)x / (float)width;
    1.86 +			if(fabs(xpos - thres) <= fabs(xpos - thres2)) {
    1.87 +				select_thres = &thres;
    1.88 +			}
    1.89 +			else {
    1.90 +				select_thres = &thres2;
    1.91 +			}
    1.92 +			prev_select_thres = *select_thres;
    1.93 +		}
    1.94 +		else {
    1.95 +			if(fabs(*select_thres - prev_select_thres) > 0.001) {
    1.96 +				mesh->clear();
    1.97 +				ui->sync_live();
    1.98 +			}
    1.99 +			select_thres = 0;
   1.100 +		}
   1.101 +	}
   1.102 +}
   1.103 +
   1.104  static void motion_xfer(int x, int y)
   1.105  {
   1.106 -	//to pontiki sto kontinotero akro
   1.107 -	//glutPostRedisplay k sta 2 win
   1.108 +	if(!select_thres)
   1.109 +		return;
   1.110 +
   1.111 +	int width = glutGet(GLUT_WINDOW_WIDTH);
   1.112 +	float xpos = (float)x / (float)width;
   1.113 +
   1.114 +	*select_thres = xpos;
   1.115 +
   1.116 +	glutPostRedisplay();
   1.117 +	glutSetWindow(mainwin_id);
   1.118 +	glutPostRedisplay();
   1.119  }