From f85b1b3a2952fdc0437d6f7e0dc5c0b322a3de9b Mon Sep 17 00:00:00 2001 From: Jiaming Guo Date: Wed, 25 Jan 2017 16:15:49 -0600 Subject: [PATCH] add proper light --- main.cu | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/main.cu b/main.cu index 6090d6c..28f19ac 100644 --- a/main.cu +++ b/main.cu @@ -76,6 +76,10 @@ float radius = 3; //equals to radius int adjoint_fac = 0; int light_fac = 0; +//light position +stim::vec3 l1(0.0, 0.0, 0.0); +stim::vec3 l2(0.0, 0.0, 0.0); + //sets an OpenGL viewport taking up the entire window void glut_render_single_projection(){ @@ -126,6 +130,50 @@ void glut_render_modelview(){ //draws the network(s) void glut_render(void) { + // light + stim::vec3 eye = cam.getPosition(); //get the camera position (eye point) + if (l1[0] + l1[1] + l1[2] + l2[0] + l2[1] + l2[2] == 0) { + stim::vec3 s = bb.size(); + + l1[0] = eye[0] + s[0] / 2; + l1[1] = eye[1] + s[1] / 2; + l1[2] = eye[2] + s[2] / 2; + l2[0] = eye[0] - s[0] / 2; + l2[1] = eye[1] - s[1] / 2; + l2[2] = eye[2] - s[2] / 2; + } + + GLfloat global_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; + GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 }; + GLfloat diffuse1[] = { 0.6, 0.6, 0.6, 1.0 }; + GLfloat diffuse2[] = { 0.4, 0.4, 0.4, 1.0 }; + GLfloat specular[] = { 0.6, 0.6, 0.6, 1.0 }; + GLfloat position1[] = { l1[0], l1[1], 50, 0.0 }; // upper right light source + GLfloat position2[] = { l2[0], l2[1], 0, 0.0 }; // lower left light source + GLfloat position[] = { eye[0], eye[1], 50, 1.0 }; + + glClearColor(0.0, 0.0, 0.0, 1.0); + glShadeModel(GL_SMOOTH); + + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); + + glLightfv(GL_LIGHT0, GL_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); + glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2); + glLightfv(GL_LIGHT1, GL_SPECULAR, specular); + glLightfv(GL_LIGHT1, GL_POSITION, position2); + + glLightfv(GL_LIGHT2, GL_AMBIENT, ambient); + glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse1); + glLightfv(GL_LIGHT2, GL_SPECULAR, specular); + glLightfv(GL_LIGHT2, GL_POSITION, position); + + glEnable(GL_COLOR_MATERIAL); + //no mapping, just comparing if (ind == 0) { if (num_nets == 1) { //if a single network is loaded @@ -266,6 +314,8 @@ void glut_render(void) { glEnable(GL_LIGHTING); } + glDisable(GL_COLOR_MATERIAL); + glutSwapBuffers(); } @@ -409,12 +459,14 @@ void glut_keyboard(unsigned char key, int x, int y){ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); + glEnable(GL_LIGHT2); } else if (light_fac && !adjoint_fac) { light_fac = 0; glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); glDisable(GL_LIGHT1); + glDisable(GL_LIGHT2); } break; @@ -496,30 +548,6 @@ 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 - GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 }; - 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); - - - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); - - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1); - //glLightfv(GL_LIGHT0, GL_SPECULAR, specular); - glLightfv(GL_LIGHT0, GL_POSITION, position1); - //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_NORMALIZE); } #ifdef __CUDACC__ -- libgit2 0.21.4