gpt4 book ai didi

c - 在 openGL 中寻找聚光灯 vector

转载 作者:行者123 更新时间:2023-11-30 15:56:26 30 4
gpt4 key购买 nike

我正在尝试为我拥有的聚光灯的方向找到正确的 vector (我正在尝试使其像手电筒一样)。我希望它面向与相机始终面向相同的方向,就像您在面前举着手电筒一样。然而,我似乎找不到正确的方向 vector 。

目前,我的光线有点跟随相机的前/后移动,但不跟随转动角度。另外,实际的点是向上的,而不是像我想要的那样正前方。

我知道各个对象的所有法线都在工作;我已经测试了一般照明。

我只会发布与问题相关的代码。如果您确实需要我的完整代码,请说出来。灯光代码:

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

感谢您的帮助。

最佳答案

由于您使用的是 gluLookAt,您可以轻松计算减去中心和眼睛的 vector :

(...)
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
(...)
spotlightVecX = centerX - eyeX;
spotlightVecY = centerY - eyeY;
spotlightVecZ = centerZ - eyeZ;

计算 vector 后,您可以将其归一化。

希望有帮助。

关于c - 在 openGL 中寻找聚光灯 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11279470/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com