PDA

View Full Version : Lighting



cHaOsBrInGeR
11-22-2000, 01:26 AM
Hi, there. I have probs with the damn lighting. I made a cube spinning around, but the lighning, I think, moves with the cube. The result is that the lighting isn't realistic. The cube gets nearly black at one point of his movement, and then 180 Degrees later, he gets totally lighted. What's wrong?
Here's the code:
#include <qgl.h>
#include <qimage.h>
#include <qapplication.h>

GLfloat r;
GLuint texture[1];
GLfloat LightAmbient[] = {1.0, 1.0, 1.0, 1.0};
GLfloat LightDiffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat LightPosition[] = {5.0, 5.0, 10.0, 1.0};

class GWidget : public QGLWidget {
public:
GWidget(QWidget *parent=0, const char *name=0);
protected:
void initializeGL();
void resizeGL(int, int);
void paintGL();
void timerEvent(QTimerEvent *);
};

GWidget::GWidget(QWidget *parent, const char *name) : QGLWidget(parent, name) {
startTimer(10);
}

void GWidget::initializeGL() {
QImage tex, buf;
buf.load("kat.bmp");
tex=QGLWidget::convertToGLFormat(buf);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
}


void GWidget::resizeGL(int width, int height) {
glViewport( 0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, width/height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void GWidget: http://www.opengl.org/discussion_boards/ubb/tongue.gifaintGL() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -6.0);
glRotatef(r, 1.0, 1.0, 1.0);
//Quad
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0,1.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0,-1.0,1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,1.0);

glTexCoord2f(1.0, 0.0); glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,1.0,-1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0,1.0,-1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0,-1.0,-1.0);

glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,-1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,1.0,1.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0,1.0,1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,-1.0);

glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0,-1.0,-1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0,-1.0,1.0);
glTexCoord2f(1.0, 0.0); glVertex3f(-1.0,-1.0,1.0);

glTexCoord2f(1.0, 0.0); glVertex3f(1.0,-1.0,-1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,-1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0,1.0,1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0,-1.0,1.0);

glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0,-1.0);
glTexCoord2f(1.0, 0.0); glVertex3f(-1.0,-1.0,1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,1.0,1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,-1.0);

glEnd();
r+=0.4;
}

void GWidget::timerEvent(QTimerEvent*) {
updateGL();
}

int main(int argc, char* argv[]) {
QApplication app(argc,argv);
GWidget w;
app.setMainWidget(&w);
w.show();
return app.exec();
}

dans
11-22-2000, 02:47 AM
Subdividing your cube should improve vertex lighting calculations. Lighting is calculated on a per-vertex basis, add more mesh detail to your cube and the vertex lighting results will improve.

cHaOsBrInGeR
11-23-2000, 12:11 AM
Sorry, could you me more precisely?
Thanx

Bob
11-23-2000, 12:48 AM
You are not passing any normals to OpenGL, so it's using the default normal for all vertices, and therefore all faces on the whole cube gets lit in the same way. Pass a normal either per triangle or per vertex in order to be able to use lighting at all.

And if you don't want the light to follow the objects transformations, you need to set lightposition AFTER you have performed the transformations, in other words, not in the initialisation function, but rather after glRotatef in PaintGL().

Moz
11-23-2000, 12:52 AM
For example your cube is made of 6 faces, each face is defined by 4 vertices.
If you want the lighting to be more accurate add vertices for each face : instead of using only one square for each face, use a grid of 9 squares (or more).
An even better way to light your cube would be to use a lightmap, the result could be as good as if you were using one vertex per pixel with lighting and it would be faster.

Moz

Bob
11-23-2000, 01:52 AM
Higher meshresolution won't help if you're not passing normals http://www.opengl.org/discussion_boards/ubb/rolleyes.gif

cHaOsBrInGeR
11-24-2000, 12:27 AM
Thanx a lot!!!!!!!!! :-)

cHaOsBrInGeR
11-24-2000, 01:12 AM
Ahhhhh.... The lighting is improved now, but it sill follows the cube, so if I rotate the cube just vertikal, not all vertexes got lighten in the same way. How come?
THX

Bob
11-24-2000, 05:06 AM
Read my first post in this thread. There I explain how to solve the problem with light following the transformation of the object.