我正在尝试为我拥有的聚光灯的方向找到合适的矢量(我正在尝试使它像手电筒一样)。我希望它面向与相机始终面向的方向相同的方向,就像您将手电筒放在您面前一样。但是,我似乎找不到正确的方向向量。
目前,我有轻微的跟随相机前后移动的感觉,但不是转向角度。此外,实际位置是向上的,而不是像我想要的那样直接指向前方。
我知道各个对象的所有法线都在工作;我已经测试了普通照明。
我只会发布与问题相关的代码。如果你真的需要我的整个代码,请说出来。灯光代码:
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();
}
谢谢您的帮助。