From e1700dd03360641a7dce685886ffdfa85e44f4c3 Mon Sep 17 00:00:00 2001 From: Jiaming Guo Date: Sat, 21 Jan 2017 12:16:49 -0600 Subject: [PATCH] fixed bugs when introducing lighting --- main.cu | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------- 1 file changed, 74 insertions(+), 47 deletions(-) diff --git a/main.cu b/main.cu index 14bda36..6090d6c 100644 --- a/main.cu +++ b/main.cu @@ -74,6 +74,7 @@ float delta; float sigma = 3; //default sigma float radius = 3; //equals to radius int adjoint_fac = 0; +int light_fac = 0; //sets an OpenGL viewport taking up the entire window void glut_render_single_projection(){ @@ -142,18 +143,19 @@ void glut_render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the screen glEnable(GL_TEXTURE_1D); //enable texture mapping - if(adjoint_fac == 1) + if(light_fac == 0) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //texture map will be used as the network color - else if (adjoint_fac == 0) + else 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 (adjoint_fac == 1) { - glDisable(GL_TEXTURE_1D); + glDisable(GL_TEXTURE_1D); //disable texture in order to render in other color 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 + glDisable(GL_DEPTH_TEST); //should disable depth to render transparancy glColor4f(0.0f, 0.3f, 0.0f, 0.2f); T.glAdjointCylinder(sigma, radius); glDisable(GL_BLEND); @@ -185,9 +187,9 @@ void glut_render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the screen glEnable(GL_TEXTURE_1D); //enable texture mapping - if (adjoint_fac == 1) + if (light_fac == 0) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //texture map will be used as the network color - else if (adjoint_fac == 0) + else 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 @@ -229,34 +231,41 @@ void glut_render(void) { } } } - 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(); - //} + + 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); + if (light_fac == 1) + glDisable(GL_LIGHTING); + 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.8, 0.0, 0.0); // using red 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(); + glColor3f(1.0, 1.0, 1.0); //clear red color + if (light_fac == 1) + glEnable(GL_LIGHTING); + } + glutSwapBuffers(); } @@ -393,11 +402,27 @@ void glut_keyboard(unsigned char key, int x, int y){ radius = 0.2; break; + // turn on/off the light + case 'l': // if keyboard 'l' is pressed, then change the light + if (!light_fac && !adjoint_fac) { + light_fac = 1; + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_LIGHT1); + } + else if (light_fac && !adjoint_fac) { + light_fac = 0; + glDisable(GL_LIGHTING); + glDisable(GL_LIGHT0); + glDisable(GL_LIGHT1); + } + break; + // 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) + if (!adjoint_fac && compareMode && !light_fac) adjoint_fac = 1; - else if(adjoint_fac && compareMode) + else if(adjoint_fac && compareMode && !light_fac) adjoint_fac = 0; break; @@ -473,26 +498,28 @@ void glut_initialize(){ 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 + GLfloat diffuse1[] = { 0.8, 0.8, 0.8, 1.0 }; + GLfloat diffuse2[] = { 0.4, 0.4, 0.4, 1.0 }; + //GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; + GLfloat position1[] = { 2 * c[0], 4 * c[1], 1 * c[0], 1.0 }; // upper right light source + GLfloat position2[] = { 0.0, -2 * c[1], 1 * c[0], 1.0 }; // lower left light source glClearColor(0.0, 0.0, 0.0, 1.0); glShadeModel(GL_SMOOTH); - glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); + + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1); + //glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_POSITION, position1); - glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); + //glMaterialfv(GL_FRONT, GL_SPECULAR, specular); + //glMaterialf(GL_FRONT, GL_SHININESS, 60.0); + glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2); glLightfv(GL_LIGHT1, GL_POSITION, position2); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); + glEnable(GL_NORMALIZE); } #ifdef __CUDACC__ -- libgit2 0.21.4