In my opengl program, all i have is a single quad with a light above it. When i call the code to set the information for the light before i call gluLookAt, the light shows, but not in the right place. When i call the code after i call gluLookAt, the light doesnt show up at all. I would like to know why it doesnt work the way i want it to.

Here are the source files. The code is in c, but thats ok because this isnt a langauge problem.

main.c
#include <stdlib.h>

#include <GL/glut.h>
#include "main.h"

/* app settings */
int appPaused = 0;
void (*appPolygonMode)() = 0;

/* light 0 */
GLfloat light0Ambient [4] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat light0Diffuse [4] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light0Position [4] = { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat light0Specular [4] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light0SpotCutOff [1] = { 180.0f };
GLfloat light0SpotDirection [3] = { 0.0f, 1.0f, 0.0f };
GLfloat light0SpotExponent [1] = { 0.0f };

/* floor */
GLuint floorList;
GLfloat floorAmbient [4] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat floorDiffuse [4] = { 1.0f, 0.0f, 0.0f, 1.0f };
GLfloat floorEmission [4] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat floorShininess [1] = { 128.0f };
GLfloat floorSpecular [4] = { 1.0f, 0.0f, 0.0f, 1.0f };

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(320, 200);
glutCreateWindow(argv[0]);
init();
glutMainLoop();
return(0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* with setLight0 here, the light shows, but in the wrong place */
gluLookAt(0.0f, 5.0f, 15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
/* here, the light just doesnt show */
setLight0();

glCallList(floorList);

glutSwapBuffers();
}

void idle(void)
{
if(!appPaused)
{
glutPostRedisplay();
}
}

void init(void)
{
/* color */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);

/* depth testing */
glEnable(GL_DEPTH_TEST);

/* culling */
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

/* lighting */
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

/* glut */
atexit(quit);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);

/* defaults */
setPolygonModeFill();

/* floor */
floorList = glGenLists(1);
glNewList(floorList, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT, floorAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, floorDiffuse);
glMaterialfv(GL_FRONT, GL_EMISSION, floorEmission);
glMaterialfv(GL_FRONT, GL_SHININESS, floorShininess);
glMaterialfv(GL_FRONT, GL_SPECULAR, floorSpecular);
glBegin(GL_TRIANGLE_STRIP);
glNormal3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-10.0f, 0.0f, -10.0f);
glVertex3f( 10.0f, 0.0f, -10.0f);
glVertex3f(-10.0f, 0.0f, 10.0f);
glVertex3f( 10.0f, 0.0f, 10.0f);
glEnd();
glEndList();
}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'q': exit(0); break;
case 'p': appPaused = appPaused ^ 1; break;
case 'm': appPolygonMode(); break;
}

glutPostRedisplay();
}

void quit(void)
{
glDeleteLists(floorList, 1);
}

void reshape(int w, int h)
{
if(h == 0)
{
h = 1;
}

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (GLfloat)w/(GLfloat)h, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW);
}

void setLight0(void)
{
glLightfv(GL_LIGHT0, GL_AMBIENT, light0Ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Specular);
glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, light0SpotCutOff);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0SpotDirection);
glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, light0SpotExponent);
}

void setPolygonModeFill(void)
{
glPolygonMode(GL_FRONT, GL_FILL);
appPolygonMode = setPolygonModeLine;
}

void setPolygonModeLine(void)
{
glPolygonMode(GL_FRONT, GL_LINE);
appPolygonMode = setPolygonModePoint;
}

void setPolygonModePoint(void)
{
glPolygonMode(GL_FRONT, GL_POINT);
appPolygonMode = setPolygonModeFill;
}


main.h
#ifndef _MAIN_H

#define _MAIN_H

void display(void);
void idle(void);
void init(void);
void keyboard(unsigned char, int, int);
void quit(void);
void reshape(int, int);
void setLight0(void);
void setPolygonModeFill(void);
void setPolygonModeLine(void);
void setPolygonModePoint(void);

#endif /* _MAIN_H */


And just in case, heres the Makefile
APP		:= plane

OBJS := main.o
DEPS := $(OBJS:.o=.d)

CC := gcc
CFLAGS := -pipe
CPPFLAGS := -Wall
TARGET_ARCH :=
RM := rm -f

ifdef RELEASE
CFLAGS += -s -O3 -ffast-math
TARGET_ARCH += -march=i686
else
CFLAGS += -g
endif

LD := $(CC)
LDFLAGS := $(CFLAGS)
LDLIBS := -lGL -lGLU -lglut

%.d : %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -MM -o $@ $<

$(APP) : $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
-include $(DEPS)

clean:
$(RM) $(APP)
$(RM) $(OBJS)
$(RM) $(DEPS)
Posted on 2002-06-08 23:55:08 by ChimpFace9000
Might it be in the spot light direction, should it be 0.0f, -1.0f, 0.0f instead.

Or perhaps the position should have a negative Y component
Posted on 2002-06-11 12:45:10 by Eóin
I'm not sure, but perhaps if you set the normal for the polygon, the light would display correctly?

Dig
Posted on 2002-06-12 03:17:47 by dig
i have a problem with lights in opengl.

1. i set the light position, type, materials, etc
2. i render object

as for now - everything is ok, but:

3. i use glRotatef()
4. ...render object..

and here's my problem

light moves along with the object!!!

when object rotates, light does so.

what do i need to do if i want my object to rotate and light to stay in position?

ps: current matrix is modelview when i use glRotatef on this object, if i use projection matrix, my whole scene moves (but it's normal).

any ideas?
Posted on 2002-06-25 13:02:35 by ti_mo_n
Light position is transformed with the current modelview matrix. So:

1.Set light position
2.glRotate, glTranslate
3.Draw

is different from

1.glRotate, glTranslate
2.Set light position
3.Draw

In first case, light position is fixed, in the second case light will rotate (translate)...
Posted on 2002-06-25 17:32:53 by minimoog
thanx, it really works!
Posted on 2002-06-29 12:22:33 by ti_mo_n