如何为光线投射生成相机光线

How to generate camera rays for ray casting

我正在尝试使用 OpenGL 和 C++ 制作一个简单的体素引擎。我的第一步是从相机发出光线并检测光线是否与某物相交(出于测试目的它只有两个平面)。通过创建一个全屏四边形并对片段着色器进行编程,以便为每个片段发送一条光线(现在我只是假设一个片段是一个像素),我已经在没有相机旋转的情况下工作了 texCoord.x、texCoord.y、-1。现在我正在尝试实现相机旋转。

我试图在 cpu 中生成一个旋转矩阵并将其发送到着色器,着色器会将其与每条光线相乘。然而,当我旋转相机时,飞机开始以一种我只能用这个视频描述的方式伸展。 https://www.youtube.com/watch?v=6NScMwnPe8c

这是创建矩阵的代码,每帧 运行:

float pi = 3.141592;
// camRotX and Y are defined elsewhere and can be controlled from the keyboard during runtime.
glm::vec3 camEulerAngles = glm::vec3(camRotX, camRotY, 0);

std::cout << "X: " << camEulerAngles.x << " Y: " << camEulerAngles.y << "\n";

// Convert to radians
camEulerAngles.x = camEulerAngles.x * pi / 180;
camEulerAngles.y = camEulerAngles.y * pi / 180;
camEulerAngles.z = camEulerAngles.z * pi / 180;

// Generate Quaternian
glm::quat camRotation;
camRotation = glm::quat(camEulerAngles);

// Generate rotation matrix from quaternian
glm::mat4 camToWorldMatrix = glm::toMat4(camRotation);
// No transformation matrix is created because the rays should be relative to 0,0,0

// Send the rotation matrix to the shader
int camTransformMatrixID = glGetUniformLocation(shader, "cameraTransformationMatrix");
glUniformMatrix4fv(camTransformMatrixID, 1, GL_FALSE, glm::value_ptr(camToWorldMatrix));

和片段着色器:

#version 330 core
in vec4 texCoord;

layout(location = 0) out vec4 color;
uniform vec3 cameraPosition;
uniform vec3 cameraTR;
uniform vec3 cameraTL;
uniform vec3 cameraBR;
uniform vec3 cameraBL;
uniform mat4 cameraTransformationMatrix;
uniform float fov;
uniform float aspectRatio;
float pi = 3.141592;

int RayHitCell(vec3 origin, vec3 direction, vec3 cellPosition, float cellSize)
{
    if(direction.z != 0)
    {
        float multiplicationFactorFront = cellPosition.z - origin.z;
        if(multiplicationFactorFront > 0){
            vec2 interceptFront = vec2(direction.x * multiplicationFactorFront + origin.x,
                                       direction.y * multiplicationFactorFront + origin.y);
            if(interceptFront.x > cellPosition.x && interceptFront.x < cellPosition.x + cellSize &&
               interceptFront.y > cellPosition.y && interceptFront.y < cellPosition.y + cellSize)
            {
                return 1;
            }
        }
        float multiplicationFactorBack = cellPosition.z + cellSize - origin.z;
        if(multiplicationFactorBack > 0){
            vec2 interceptBack = vec2(direction.x * multiplicationFactorBack + origin.x,
                                      direction.y * multiplicationFactorBack + origin.y);
            if(interceptBack.x > cellPosition.x && interceptBack.x < cellPosition.x + cellSize &&
               interceptBack.y > cellPosition.y && interceptBack.y < cellPosition.y + cellSize)
            {
                return 2;
            }
        }
    }
    return 0;
}

void main()
{
    // For now I'm not accounting for FOV and aspect ratio because I want to get the rotation working first
    vec4 beforeRotateRayDirection = vec4(texCoord.x,texCoord.y,-1,0);
    // Apply the rotation matrix that was generated on the cpu
    vec3 rayDirection = vec3(cameraTransformationMatrix *  beforeRotateRayDirection);

    int t = RayHitCell(cameraPosition, rayDirection, vec3(0,0,5), 1);
    if(t == 1)
    {
        // Hit front plane
        color = vec4(0, 0, 1, 0);
    }else if(t == 2)
    {
        // Hit back plane
        color = vec4(0, 0, 0.5, 0);
    }else{
        // background color
        color = vec4(0, 1, 0, 0);
    }
}

好的。真的很难知道哪里出了问题,不过我会试试的。

这里有一些提示和注意事项:

1) 您可以通过将它们映射到 RGB 颜色来调试方向。请记住,您应该规范化向量并从 (-1,1) 映射到 (0,1)。只需做 dir*0.5+1.0 类型的事情。示例:

color = vec4(normalize(rayDirection) * 0.5, 0) + vec4(1);

2) 可以更直接的得到旋转矩阵。四元数是从正向初始化的,它会先绕Y轴旋转(水平看)然后然后才绕X轴旋转(垂直看)。请记住,如果您从欧拉角进行初始化,则旋转顺序取决于实现。尽可能使用 mat4_cast 来避免实验性 glm 扩展 (gtx)。示例:

// Define rotation quaternion starting from look rotation
glm::quat camRotation = glm::vec3(0, 0, 0);
camRotation = glm::rotate(camRotation, glm::radians(camRotY), glm::vec3(0, 1, 0));
camRotation = glm::rotate(camRotation, glm::radians(camRotX), glm::vec3(1, 0, 0));
glm::mat4 camToWorldMatrix = glm::mat4_cast(camRotation);

3) 您的 beforeRotateRayDirection 是一个向量,(可能)从 (-1,-1,-1) 一直指向 (1,1,-1)。 未归一化,(1,1,1) 的长度为 √3 ≈ 1.7320508075688772...请确保您已在碰撞数学中考虑到这一点或仅对矢量进行归一化.

到目前为止我的部分回答...

您的碰撞测试有点奇怪...您似乎想将光线投射到给定单元格位置的 Z 平面中(但两次,一次用于正面,一次用于背面)。我已经审查了您的代码逻辑,它有一定的道理,但是没有顶点程序,因此不知道 texCoord 范围值是什么,因此无法确定。您可能想重新考虑您的逻辑,如下所示:

int RayHitCell(vec3 origin, vec3 direction, vec3 cellPosition, float cellSize)
{
    //Get triangle side vectors
    vec3 tu = vec3(cellSize,0,0);   //Triangle U component
    vec3 tv = vec3(0,cellSize,0);   //Triangle V component

    //Determinant for inverse matrix
    vec3 q = cross(direction, tv);
    float det = dot(tu, q);
    //if(abs(det) < 0.0000001) //If too close to zero
    //  return;
    float invdet = 1.0/det;

    //Solve component parameters
    vec3 s = origin - cellPosition;
    float u = dot(s, q) * invdet;
    if(u < 0.0 || u > 1.0)
        return 0;

    vec3 r = cross(s, tu);
    float v = dot(direction, r) * invdet;
    if(v < 0.0 || v > 1.0)
        return 0;

    float t = dot(tv, r) * invdet;
    if(t <= 0.0)
        return 0;

    return 1;
}

void main()
{
    // For now I'm not accounting for FOV and aspect ratio because I want to get the 
    // rotation working first
    vec4 beforeRotateRayDirection = vec4(texCoord.x, texCoord.y, -1, 0);
    // Apply the rotation matrix that was generated on the cpu
    vec3 rayDirection = vec3(cameraTransformationMatrix * beforeRotateRayDirection);

    int t = RayHitCell(cameraPosition, normalize(rayDirection), vec3(0,0,5), 1);
    if (t == 1)
    {
        // Hit front plane
        color = vec4(0, 0, 1, 0);
    }
    else
    {
        // background color
        color = vec4(0, 1, 0, 0);
    }
}

这应该会给你一架飞机,如果可行请告诉我。立方体会很容易做。

PS.: uv 可用于纹理映射。