From 19ec4731eaea24ff9dac28540bf413a40ce04528 Mon Sep 17 00:00:00 2001 From: Jiaming Guo Date: Fri, 20 Jan 2017 16:31:52 -0600 Subject: [PATCH] add light --- main.cu | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------- 1 file changed, 88 insertions(+), 63 deletions(-) diff --git a/main.cu b/main.cu index 1f7eb6c..14bda36 100644 --- a/main.cu +++ b/main.cu @@ -67,17 +67,13 @@ std::vector colormap; // special key indicator int mods; -// create display lists -GLuint dlist1; -GLuint dlist2; - //OpenGL objects GLuint cmap_tex = 0; //texture name for the color map float delta; float sigma = 3; //default sigma float radius = 3; //equals to radius -int transparancy_fac = 0; +int adjoint_fac = 0; //sets an OpenGL viewport taking up the entire window void glut_render_single_projection(){ @@ -146,23 +142,31 @@ void glut_render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the screen glEnable(GL_TEXTURE_1D); //enable texture mapping - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //texture map will be used as the network color + if(adjoint_fac == 1) + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //texture map will be used as the network color + else if (adjoint_fac == 0) + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_1D, cmap_tex); //bind the Brewer texture map GT.glCylinder(sigma, radius); //render the GT network - if (transparancy_fac == 1) { + if (adjoint_fac == 1) { + glDisable(GL_TEXTURE_1D); glEnable(GL_BLEND); //enable color blend glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set blend function glDisable(GL_DEPTH_TEST); //should disable depth - glColor4f(1.0f, 1.0f, 1.0f, 0.2f); - T.glCylinder(sigma, radius); + glColor4f(0.0f, 0.3f, 0.0f, 0.2f); + T.glAdjointCylinder(sigma, radius); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_1D); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } glut_render_right_projection(); //set up a projection for the right half of the window glut_render_modelview(); //set up the modelview matrix using camera details + T.glCylinder(sigma, radius); //render the T network + sigma = radius; // set sigma equal to radius } } @@ -181,23 +185,31 @@ void glut_render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the screen glEnable(GL_TEXTURE_1D); //enable texture mapping - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //texture map will be used as the network color + if (adjoint_fac == 1) + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //texture map will be used as the network color + else if (adjoint_fac == 0) + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//map light to texture glBindTexture(GL_TEXTURE_1D, cmap_tex); //bind the Brewer texture map _GT.glCylinder(sigma, radius); //render the GT network - if (transparancy_fac == 1) { + if (adjoint_fac == 1) { + glDisable(GL_TEXTURE_1D); //temporarily disable texture glEnable(GL_BLEND); //enable color blend glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set blend function glDisable(GL_DEPTH_TEST); //should disable depth - glColor4f(1.0f, 1.0f, 1.0f, 0.2f); - _T.glCylinder(sigma, radius); + glColor4f(0.0f, 0.3f, 0.0f, 0.2f); + _T.glAdjointCylinder(sigma, radius); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_1D); //re-enable texture + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } glut_render_right_projection(); //set up a projection for the right half of the window glut_render_modelview(); //set up the modelview matrix using camera details + _T.glCylinder(sigma, radius); //render the T network + sigma = radius; //set sigma equal to radius } else { @@ -206,54 +218,45 @@ void glut_render(void) { glut_render_modelview(); //set up the modelview matrix using camera details glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the screen - _GT.glRandColorCylinder1(dlist1, _gt_t, colormap, sigma, radius); - if (transparancy_fac == 1) { - glEnable(GL_BLEND); //enable color blend - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set blend function - glDisable(GL_DEPTH_TEST); //should disable depth - glColor4f(1.0f, 1.0f, 1.0f, 0.2f); - _T.glRandColorCylinder2(dlist2, _t_gt, colormap, sigma, radius); - glDisable(GL_BLEND); - glEnable(GL_DEPTH_TEST); - } + _GT.glRandColorCylinder(0, _gt_t, colormap, sigma, radius); glut_render_right_projection(); //set up a projection for the right half of the window glut_render_modelview(); //set up the modelview matrix using camera details - _T.glRandColorCylinder2(dlist2, _t_gt, colormap, sigma, radius); + _T.glRandColorCylinder(1, _t_gt, colormap, sigma, radius); + sigma = radius; //set sigma equal to radius } } } - - if (num_nets == 2) { // works only with two networks - std::ostringstream ss; - if (mappingMode) // if it is in mapping mode - ss << "Mapping Mode"; - else - ss << "Compare Mode"; // default mode is compare mode - - glDisable(GL_TEXTURE_1D); - glMatrixMode(GL_PROJECTION); // set up the 2d viewport for mode text printing - glPushMatrix(); - glLoadIdentity(); - int X = glutGet(GLUT_WINDOW_WIDTH); // get the current window width - int Y = glutGet(GLUT_WINDOW_HEIGHT); // get the current window height - glViewport(0, 0, X / 2, Y); // locate to left bottom corner - gluOrtho2D(0, X, 0, Y); // define othogonal aspect - glColor3f(0.0, 1.0, 0.0); // using green to show mode - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - - glRasterPos2f(0, 5); //print text in the left bottom corner - glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)(ss.str().c_str())); - - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - } + glDisable(GL_TEXTURE_1D); //disable texture + + //if (num_nets == 2) { // works only with two networks + // std::ostringstream ss; + // if (mappingMode) // if it is in mapping mode + // ss << "Mapping Mode"; + // else + // ss << "Compare Mode"; // default mode is compare mode + // glMatrixMode(GL_PROJECTION); // set up the 2d viewport for mode text printing + // glPushMatrix(); + // glLoadIdentity(); + // int X = glutGet(GLUT_WINDOW_WIDTH); // get the current window width + // int Y = glutGet(GLUT_WINDOW_HEIGHT); // get the current window height + // glViewport(0, 0, X / 2, Y); // locate to left bottom corner + // gluOrtho2D(0, X, 0, Y); // define othogonal aspect + // glColor3f(0.0, 1.0, 0.0); // using green to show mode + + // glMatrixMode(GL_MODELVIEW); + // glPushMatrix(); + // glLoadIdentity(); + + // glRasterPos2f(0, 5); //print text in the left bottom corner + // glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)(ss.str().c_str())); + + // glPopMatrix(); + // glMatrixMode(GL_PROJECTION); + // glPopMatrix(); + //} glutSwapBuffers(); } @@ -358,12 +361,12 @@ void glut_keyboard(unsigned char key, int x, int y){ switch (key) { // change render mode - case 'm': // if keyboard 'm' is pressed, then change render mode - if (compareMode && !mappingMode && ind) { // if current mode is comparing mode + case 'm': // if keyboard 'm' is pressed, then change render mode + if (compareMode && !mappingMode && ind && !adjoint_fac) { // if current mode is comparing mode compareMode = false; mappingMode = true; } - else if (!compareMode && mappingMode && ind) { // if current mode is mapping mode + else if (!compareMode && mappingMode && ind && !adjoint_fac) { // if current mode is mapping mode compareMode = true; mappingMode = false; } @@ -389,13 +392,13 @@ void glut_keyboard(unsigned char key, int x, int y){ if (radius < 0.001f) radius = 0.2; break; - - // render a transparant T very close to GT - case 32: // if keyboard 'SPACE' is pressed, then change the transparancy_fac - if (compareMode && !mappingMode && !transparancy_fac) - transparancy_fac = 1; - else - transparancy_fac = 0; + + // render a transparant T very close to GT in compare mode + case 32: // if keyboard 'SPACE' is pressed, then change the adjoint_fac + if (!adjoint_fac && compareMode) + adjoint_fac = 1; + else if(adjoint_fac && compareMode) + adjoint_fac = 0; break; // close window and exit application @@ -468,6 +471,28 @@ void glut_initialize(){ //place the camera along the z-axis at a distance determined by the network size along x and y cam.setPosition(c + stim::vec(0, 0, camera_factor * std::max(bb.size()[0], bb.size()[1]))); cam.LookAt(c[0], c[1], c[2]); //look at the center of the network + + // light + stim::vec3 eye = cam.getPosition(); //get the camera position point + stim::vec3 focus = cam.getLookAt(); //get the camera focal point + + GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 }; + GLfloat diffuse1[] = { 1.0, 1.0, 1.0, 1.0 }; + GLfloat diffuse2[] = { 0.2, 0.2, 0.2, 1.0 }; + GLfloat position1[] = { 3*focus[0], 3*focus[1], -3*eye[2], 1.0 }; // upper right light source + GLfloat position2[] = { -2*focus[0], -2*focus[1], -3*eye[2], 0.0 }; // lower left light source + glClearColor(0.0, 0.0, 0.0, 1.0); + glShadeModel(GL_SMOOTH); + + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1); + glLightfv(GL_LIGHT0, GL_POSITION, position1); + glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); + glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2); + glLightfv(GL_LIGHT1, GL_POSITION, position2); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); } #ifdef __CUDACC__ -- libgit2 0.21.4