我正在尝试为我所拥有的聚光灯的方向找到合适的矢量(我正在尝试使其像手电筒一样)。我希望它面对的方向与相机始终面对的方向相同,就像您将手电筒放在自己面前一样。
但是,我似乎找不到正确的方向向量。

此刻,我跟随摄像机的前后移动,但没有转向角度。此外,实际位置要指向上方,而不是像我想要的那样直接指向前方。

我知道各个对象的所有法线都在起作用;我已经测试了普通照明。

我将只发布与该问题相关的代码。如果您确实需要我的整个代码,请这样说。
灯光代码:

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,因此可以轻松计算出减去中心和眼睛的向量:

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


您可以在计算向量后对其进行归一化。

希望能帮助到你。

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

10-11 21:58