Jump to content
  • Advertisement
Sign in to follow this  
Salatinjsh

OpenGL GL lighting problem

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 )

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!