问题描述
|
OpenGL:简单的glut立方体不亮
嗨,我正在尝试研究opengl和glut中的灯光,但是我很早就遇到了问题。我有以下代码,可以通过自定义相机设置在自定义位置制作发光立方体:
void testApp::draw(){
static float amb[] = {0.4,0.4,0.0};
static float dif[] = {1.0,1.0,0.0};
float light_diffuse[] = {1.0,0.0,1.0};
float light_position[] = {-1.0,0.0}; // i tried a lot of positions here!
// set camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,200,1200,1,0);
// set lights
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT,GL_AMBIENT,amb);
glMaterialfv(GL_FRONT,dif);
// draw scene
ofScale(50,50,50);
glPushmatrix();
glutSolidCube(2);
// close everything
glPopMatrix();
gldisable(GL_LIGHT0);
gldisable(GL_LIGHT1);
gldisable(GL_LIGHTING); }
我只看到一个黑色的立方体,没有光,我认为问题是光的位置,我尝试了很多位置,但是不起作用:黑色的立方体。
解决方法
您真的离立方体很远。使其更小并靠近。或者,按以下方式更改照明:
float light_diffuse[] = {100.0,0.0,100.0,100.0};
float light_position[] = {-100.0,1.0,0.0};
您几乎看不到立方体上的任何灯光效果。考虑使用茶壶。