我正在尝试为我所拥有的聚光灯方向找到合适的矢量(我试图让它像手电筒一样)。我希望它能像摄像机一直面向同一个方向,就像你手持一个手电筒一样。 然而,我无法找到正确的方向向量。
目前,我有点跟随相机的前/后移动,但不是转弯角度。另外,实际的点是向上,而不是像我想要的那样直接向前。
我知道各个物体的所有法线都在工作;我已经测试了普通照明。
我只会发布与问题相关的代码。如果你真的需要我的全部代码,请说出来。 光代码:
float sco=20; // Spot cutoff angle
float Exp=0; // Spot exponent
float Ambient[] = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
float Diffuse[] = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
float Specular[] = {0.01*specular,0.01*specular,0.01*specular,1.0};
// Light direction
float Position[] = {Ox+3, 2, Oz,1};
float Direction[] = {Ox+lx, here, Oz+lz, 0};
glLightfv(GL_LIGHT0,GL_AMBIENT ,Ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE ,Diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
glLightfv(GL_LIGHT0,GL_POSITION,Position);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,Direction);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,sco);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,Exp);
相机代码:
// Camera Values
double Ox=0, Oz=0;
float Oy=1.0;
float angle = 0.0;
float lx=0.0,lz=-1.0;
float deltaAngle = 0.0;
float deltaMove = 0;
double here;
void computePos(float deltaMove) {
Ox += deltaMove * lx * 0.1f;
Oz += deltaMove * lz * 0.1f;
}
void computeDir(float deltaAngle) {
angle += deltaAngle;
lx = sin(angle);
lz = -cos(angle);
}
void display() {
here = 2.0f*Oy;
if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle);
gluLookAt(Ox,2,Oz, Ox+lx, here, Oz+lz, 0,1,0);
}
void key(unsigned char ch,int x,int y) {
// Exit on ESC
if (ch == 27)
exit(0);
// WASD controls
else if (ch == 'a' || ch == 'A')
deltaAngle = -0.01;
else if (ch == 'd' || ch == 'D')
deltaAngle = 0.01;
else if (ch == 'w' || ch == 'W') {
collidefront=collision(1);
collidextra=collision(3);
if (collideback == 2 && collidextra == 3) { deltaMove = 0; collideback = 0; }
else if (collideback == 2) { deltaMove = 0.1; collidefront = 0;}
else if (collidefront == 1) deltaMove = 0;
else
deltaMove = 0.1;
}
else if (ch == 's' || ch == 'S') {
collideback=collision(2);
if (collidefront == 1) { deltaMove = -0.3; collidefront = 0; }
else if (collideback == 2) deltaMove = 0;
else
deltaMove = -0.1;
}
else if ((ch == 'e' || ch == 'E') && here < 4)
Oy += 0.01;
else if ((ch == 'c' || ch == 'C') && here > .5)
Oy -= 0.01;
Project(fov,asp,dim);
glutPostRedisplay();
}
感谢您的帮助。
答案 0 :(得分:1)
由于您使用的是gluLookAt
,因此您可以轻松计算减去中心和眼睛的矢量:
(...)
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
(...)
spotlightVecX = centerX - eyeX;
spotlightVecY = centerY - eyeY;
spotlightVecZ = centerZ - eyeZ;
您可以在计算向量后对其进行标准化。
希望它有所帮助。