GL lighting problem

Started by
3 comments, last by Salatinjsh 12 years, 11 months ago
Hi!

I'm learning OpenGL using GLUT window. So far i have made a colorful cube rotating on y and x asis and now i have added lighting but it doesnt work. It only makes it all in shadow but it should make nice perspectively shaded cube.

There is the code and i hope i'll have help :


#include <GL/glew.h>
#include <GL/glut.h>

GLfloat rotquad;

GLfloat LightPosition[] = { 0, 0, 0, 1 };
GLfloat LightAmbient[] = { 1, 1, 1, 1 };
GLfloat LightDiffuse[] = { 1, 1, 1, 1 };

bool light = false;
bool diffuse = false;
bool emissive = false;
bool specular = false;

void Keyboard (unsigned char key, int x, int y)
{
if (key=='l')
{
light=!light;
if (!light)
{
glDisable(GL_LIGHTING);
} else
{
glEnable(GL_LIGHTING);
}
}
}

void KeyboardSpec (int key, int x, int y)
{

}


void Init()
{
glEnable (GL_BLEND);
glEnable (GL_DEPTH_TEST);
glEnable (GL_COLOR_MATERIAL);
glEnable (GL_LIGHT1);
}

void SetUpLight ()
{
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
}

void Display()
{
glClearColor(0.2, 0.2, 0.2, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();

glTranslatef(0, 0, -6);

glRotatef(rotquad, 1, 0, 0);
glRotatef(rotquad, 0, 1, 0);

glBegin(GL_QUADS); // Start Drawing The Cube

glColor4f(1, 0, 0, 1);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

glColor4f(0, 1, 1, 1);
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)

glColor4f(1, 0, 1, 1);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)

glColor4f(1, 1, 0, 1);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back)

glColor4f(0, 0, 1, 1);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)

glColor4f(0, 1, 0, 1);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();

rotquad -= 1.5f;

glutSwapBuffers();
}

void OnReshape(int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}

int main(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGBA); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("First GLUT window"); // Set the title for the window

Init();

glutDisplayFunc (Display);
glutIdleFunc (Display);
glutKeyboardFunc (Keyboard);
glutSpecialFunc (KeyboardSpec);
glutReshapeFunc (OnReshape);

glutMainLoop(); // Enter GLUT's main loop

return 0;
}


few functions may be quite strange, thats because i have combined header and files asociated to it ;) (im not sure that im saing it right because not english :D )
Advertisement
Looks like you need to add normals in. Lighting calculations require the normal of the surface being lit to determine the final lighting coefficients.
A glNormal3f(x, y, z); call is required for each normal you are using, e.g.:

[color="#000000"]glColor4f[color="#666600"]([color="#006666"]1[color="#666600"], [color="#006666"]0[color="#666600"], [color="#006666"]0[color="#666600"], [color="#006666"]1[color="#666600"]);[color="#666600"]
[color="#666600"]glNormal3f( 0.0f, 1.0f, 0.0f); // normal facing upwards
glVertex3f[color="#666600"]( [color="#006666"]1.0f[color="#666600"], [color="#006666"]1.0f[color="#666600"],-[color="#006666"]1.0f[color="#666600"]); [color="#880000"]// Top Right Of The Quad (Top)[color="#000000"]
glVertex3f[color="#666600"](-[color="#006666"]1.0f[color="#666600"], [color="#006666"]1.0f[color="#666600"],-[color="#006666"]1.0f[color="#666600"]); [color="#880000"]// Top Left Of The Quad (Top)[color="#000000"]
glVertex3f[color="#666600"](-[color="#006666"]1.0f[color="#666600"], [color="#006666"]1.0f[color="#666600"], [color="#006666"]1.0f[color="#666600"]); [color="#880000"]// Bottom Left Of The Quad (Top)[color="#000000"]
glVertex3f[color="#666600"]( [color="#006666"]1.0f[color="#666600"], [color="#006666"]1.0f[color="#666600"], [color="#006666"]1.0f[color="#666600"]); [color="#880000"]// Bottom Right Of The Quad (Top)
This will set the normal used for those four vertices (any any following, until the next glNormal3f call or glEnd.
Hope this helps.

/edit Wow this text editor is bad.
may work but i dont know how it works and cant get it working, i'm reading about normals now in opengl website but anyway can't make it working :(

may work but i dont know how it works and cant get it working, i'm reading about normals now in opengl website but anyway can't make it working :(


Try glEnable(GL_LIGHTING);
Check this out:

http://www.sjbaker.o...l_lighting.html


Google translated to latvian, shame it doesn't do the image aswell.

Try glEnable(GL_LIGHTING);
Check this out:

http://www.sjbaker.o...l_lighting.html


Google translated to latvian, shame it doesn't do the image aswell.



Thx nice page, made my mind clear but i still can't get light working :@ :@




EDIT: I GOT THE LIGHT THANK U GUYS!!!!!!


I found that the problem realy was with normals :D

This topic is closed to new replies.

Advertisement