ich lese mich gerade durch "OpenGL Programming Guide" und probiere dabei einige Dinge aus. Was partout nicht funktionieren will, ist die Darstellung eines Objektes in einer Farbe. Daher wollte ich hier mal gefragt haben in der Hoffnung, dass ich Hilfe finde.
InitializeGL und resizeGL funktionieren soweit, d. h. meine Objekte werden brav gezeichnet. Was nicht funktioniert ist dieser Teil (GLWidget ist von QGLWidget abgeleitet):
Code: Alles auswählen
==============
gllight.cpp
==============
/** Turns on the light. */
void GLLight::switchOn(){
GLColorRGBA color;
GLfloat pos[4];
bool lightingIsOn = glIsEnabled(GL_LIGHTING);
pos[0] = _Position.x(); pos[1] = _Position.y(); pos[2] = _Position.z(); //copy position
if(isDirectional())
pos[3] = 0.0; // 0 is a flag for directional light (as the sun)
else pos[3] =1.0; // 1 is a positional light (as a lamp)
glLightfv(GL_LIGHT0 + _LightNumber, GL_POSITION, pos);
color = _DiffuseColor * _Brightness;
glLightfv(GL_LIGHT0 + _LightNumber, GL_DIFFUSE, color.fv());
color = _SpecularColor * _Brightness;
glLightfv(GL_LIGHT0 + _LightNumber, GL_SPECULAR, color.fv());
color = _AmbientColor * _Brightness;
glLightfv(GL_LIGHT0 + _LightNumber, GL_AMBIENT, color.fv());
glEnable(GL_LIGHT0 + _LightNumber);
if(_ShowLightSource){
if(lightingIsOn)
glDisable(GL_LIGHTING); //make shure that a red line is shown
glPushMatrix();
glColor3fv(cl_Red.fv());
glBegin(GL_LINES); // draw a ray
glVertex3d(0.0,0.0,0.0);
glVertex3d(pos[0], pos[1],pos[2]);
glEnd();
glTranslated(pos[0], pos[1],pos[2]);
glutWireSphere(0.05, 4,4); //draw a light source
glPopMatrix();
if(lightingIsOn)
glEnable(GL_LIGHTING); //re-enable lighting, if it was on
}
On = true;
}
==============
glwidget.cpp
==============
void GLWidget::paintGL()
{
// Start with a plain white background prior to doing anything else
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Save current modelview matrix
glPushMatrix();
// Set up light
glEnable( GL_LIGHTING );
// Set up the light source (verwendet Methoden einer bereitgestellten Klasse )
_lamp->setAmbientColor( cl_White * 0.2 );
_lamp->setDiffuseColor( cl_White );
_lamp->setSpecularColor( cl_White );
_lamp->setPosition( GLVector( 5.0, 6.0, 7.0 ));
_lamp->switchOn();
glEnable( GL_COLOR_MATERIAL );
// Set object's material color
glMaterialfv(GL_FRONT_AND_BACK,
GL_AMBIENT_AND_DIFFUSE,
cl_Blue.fv());
// Set specular reflectance
glMaterialfv(GL_FRONT_AND_BACK,
GL_SPECULAR,
cl_White.fv());
// Set shininess
glMaterialf(GL_FRONT_AND_BACK,
GL_SHININESS,
50);
/*
Hier werden jetzt nur noch Vertexe gezeichnet, die am Ende eine Pyramide ergeben.
*/
}
Ich bin der Meinung weißes Licht bei _lamp und blaue Reflectionen beim Material eingestellt zu haben. Meine Pyramide sollte meiner Meinung nach daher blau erscheinen. Dem ist aber nicht so. Sie ist rot. Vermutlich weil in der Methode "switchOn()" rot via "glColor3f()" gesetzt wird. Aber selbst wenn ich diesen Teil auskommentiere ist meine Pyramide nicht blau. Ich versteh's nicht.The OpenGL lighting model makes the approximation that a material's color depends on the percentages of the incoming red, green, and blue light it reflects. For example, a perfectly red ball reflects all the incoming red light and absorbs all the green and blue light that strikes it. If you view such a ball in white light (composed of equal amounts of red, green, and blue light), all the red is reflected, and you see a red ball. If the ball is viewed in pure red light, it also appears to be red. If, however, the red ball is viewed in pure green light, it appears black (all the green is absorbed, and there's no incoming red, so no light is reflected).
Gruß,
gelignite