• Trouble with OpenGL and GLUT lighting
    5 replies, posted
I've been attempting to teach myself how to make 3D programs with OpenGL and GLUT, and have run into a rather odd issue. When I apply lighting to a 3D object it appears that two of the sides (on a cube, with other objects it gets more complex) are semi-transparent, despite the alpha value of all the sides being uniform. Does anyone know what might be causing this? Also, I'm using Fedora 17 (KDE build) as my OS. [AN]: This issue has been solved. Scroll down to the bottom to see the solution. Video capture of the problem (NOTE: specular and diffusion lighting is disabled in this, you can do so by hitting the s and d keys respectively) [url]https://docs.google.com/file/d/0BzQ5_qeAF6JuWkRXZm1OTi05QWc/edit[/url] Code ======== main.cpp ======== [code] #define EXTERN #include "main.h" GLfloat redDiffuseMaterial[] = {1.0, 0.0, 0.0}; //set the material to red GLfloat whiteSpecularMaterial[] = {1.0, 1.0, 1.0}; //set the material to white GLfloat greenEmissiveMaterial[] = {0.0, 1.0, 0.0}; //set the material to green GLfloat whiteSpecularLight[] = {1.0, 1.0, 1.0}; //set the light specular to white GLfloat blackAmbientLight[] = {0.0, 0.0, 0.0}; //set the light ambient to black GLfloat whiteDiffuseLight[] = {1.0, 1.0, 1.0}; //set the diffuse light to white GLfloat blankMaterial[] = {0.0, 0.0, 0.0}; //set the diffuse light to white GLfloat mShininess[] = {128}; //set the shininess of the material bool diffuse = false; bool emissive = false; bool specular = false; void renderbasic(void); void display(void); void reshape(int width, int height); void keyPressed(unsigned char key, int x, int y); void keyReleased(unsigned char key, int x, int y); void keyOperations(void); void keySpecial(int key, int x, int y); void keySpecialReleased(int key, int x, int y); void keySpecialOperations(void); void cube(void); void light(void); bool movingUp = false; // Whether or not we are moving up or down bool* keyStates = new bool[256];// Key buffers bool* keySpecialStates = new bool[246]; float yLocation = 0.0f; // Keep track of our position on the y axis. float trans_z=0.f; float yRotationAngle = 0.0f; // The angle of rotation for our object int main (int argc, char **argv) { glutInit(&argc, argv); // Initialize GLUT // Create the main window glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // Set up a basic display buffer glutInitWindowSize(800, 600); // Set the width and height of the window glutInitWindowPosition(100, 100); // Set the position of the window glutCreateWindow("Helena"); // Set the title for the window glutDisplayFunc(display); glutIdleFunc(display); // Tell GLUT to use the method "display" as our idle method as well glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for rendering glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses glutKeyboardUpFunc(keyReleased); // Tell GLUT to use the method "keyReleased" for key released events glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keySpecial" for special key presses glutSpecialUpFunc(keySpecialReleased); // Tell GLUT to use the method "keySpecialReleased" for special released key events // Enable Z-buffer read and write glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); // Setup a perspective projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.f, 1.f, 1.f, 500.f); // Set color and depth clear value glClearDepth(1.f); glClearColor(0.85f, 1.f, 1.f, 1.f); // Clear the background of our window //Enter GLUT main loop glutMainLoop(); return EXIT_SUCCESS; } void display(void) { keyOperations(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the colour buffer glMatrixMode(GL_MODELVIEW);//Set stuff up to be drawn glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations glEnable(GL_BLEND);//allow blending gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); light(); cube(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glutSwapBuffers();// Swap drawing buffer with window buffer } void reshape(int width, int height) { glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up) gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly } void keyPressed(unsigned char key, int x, int y) { keyStates[key]=true; } void keyReleased(unsigned char key, int x, int y) { keyStates[key]=false; } void keyOperations(void) { if(keyStates['+']) trans_z+=0.5f;//for zooming if(keyStates['-']) trans_z-=0.5f; //light-stuff if (keyStates['s']) { if (specular==false) { specular=true; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, whiteSpecularMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess); } else if (specular==true) { specular=false; glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blankMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, blankMaterial); } } if (keyStates['d']) { if (diffuse==false) { diffuse=true; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redDiffuseMaterial); } else if (diffuse==true) { diffuse = false; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blankMaterial); } } if (keyStates['e']) { if (emissive==false) { emissive = true; glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, greenEmissiveMaterial); } else if (emissive==true) { emissive=false; glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, blankMaterial); } } } void keySpecial(int key, int x, int y) { keyStates[key]=true; } void keySpecialReleased(int key, int x, int y) { keyStates[key]=false; } void keySpecialOperations(void) { } void cube(void) { if (movingUp) // If we are moving up yLocation -= 0.005f; // Move up along our yLocation else // Otherwise yLocation += 0.005f; // Move down along our yLocation if (yLocation < -3.0f) // If we have gone up too far movingUp = false; // Reverse our direction so we are moving down else if (yLocation > 3.0f) // Else if we have gone down too far movingUp = true; // Reverse our direction so we are moving up yRotationAngle += 0.5f; // Increment our rotation value if (yRotationAngle > 360.0f) // If we have rotated beyond 360 degrees (a full rotation) yRotationAngle -= 360.0f; // Subtract 360 degrees off of our rotation glTranslatef(0.0f, yLocation, 0.0f); // Translate our object along the y axis glTranslatef(0.f, 0.f, trans_z);//movement controls glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate our object around the y axis glColor3f(0.33f, 1.f, 0.33f); glutSolidCube(1.f); // glScalef( 2.0, 0.5, 1.0 );//Practice scaling // glutSolidCube(2.f); } void light(void) { glLightfv(GL_LIGHT0, GL_SPECULAR, whiteSpecularLight); glLightfv(GL_LIGHT0, GL_AMBIENT, blackAmbientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteDiffuseLight); } void renderbasic(void) { glBegin(GL_POLYGON); // Start drawing a quad "basic" glColor3f(1.f, 0.f, 0.f);//Red glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner glColor3f(0.f, 1.f, 0.f);//Green glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner glColor3f(0.f, 0.f, 1.f);//Blue glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner glColor3f(1.f, 0.f, 1.f);//Magenta glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner glColor3f(1.f, 1.f, 1.f);//White //other verticies glVertex3f(1.0f, -1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 1.0f); glEnd(); } [/code] ======== main.h ======== [code] #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glew.h> #include <GL/glut.h> #ifndef EXTERN//So that all files borrow declarations from main.cpp (EXTERN declared in main.cpp) #define EXTERN extern #endif [/code]
Did you try without the blending ? [editline]22nd January 2013[/editline] Did you try without the blending ?
Yes, sadly, no luck. :/ The next thing is running it on windows(after I get the dependencies installed) to see if the error is in the code its self.
You should disable blending if you don't use it. What does it do if you disable the Lightning is there any transparency ?
I'm learning to use openGL, so having blending enabled is needed eventually. More data: -if i put a cube inside the current one, the issue still happens, but you can't see the inside cube. -program without lighting: [url]https://docs.google.com/file/d/0BzQ5_qeAF6JuVFBxM05wb1ZDdzg/edit[/url] -the issue starts as soon as I put then line "glEnable(GL_LIGHT0);" into the code...
-Found the problem- the line: [code]glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // Set up a basic display buffer[/code] needs to be changed to: [code]glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); // Set up a basic display buffer[/code] in order to enable a depth in glut. -Case closed-
Sorry, you need to Log In to post a reply to this thread.