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: aintGL() {
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();
}