三角形纹理旋转并水平翻转

时间:2019-04-04 02:23:43

标签: c++ raytracing

编写一个上课的光线追踪器,我遇到了一个奇怪的问题,我似乎无法确定问题的根源。我有纹理,由于某种原因,它顺时针旋转90度,然后水平翻转。我正在使用重心坐标来导航我的uv空间坐标。

我已经尝试过如何生成u,v,w。但这似乎会导致相同的问题。

In program issue visible my actual test texture

//how i'm generating my barycentric coordinates:
Ph = Pe + Npe*Th; //Ph is the point in space that is being tested, I'm generating u,v,w while testing inside/outside triangle (Pe = Point of eye, Npe = vector from eye to point on triangle, Th = time hit)

        A = glm::cross(P1 - P0, P2 - P0);
        //glm::vec3 A0 = glm::cross(Ph - P1, Ph - P2);
        //glm::vec3 A1 = glm::cross(Ph - P2, Ph - P0);
        //glm::vec3 A2 = glm::cross(Ph - P0, Ph - P1);
        glm::vec3 A0 = glm::cross(P1-Ph, P2-Ph);
        glm::vec3 A1 = glm::cross(P2-Ph, P0-Ph);
        glm::vec3 A2 = glm::cross(P0-Ph, P1-Ph);

        if (glm::dot(n0, glm::normalize(A0)) < 0 || glm::dot(n0, glm::normalize(A1)) < 0 || glm::dot(n0, glm::normalize(A2)) < 0)
        {
            //point is outside triangle
            return -1;
        }


        //normalize and chec k dot products to detrmine if they are facing the right way.

        u = glm::length(A0) / glm::length(A);
        v = glm::length(A1) / glm::length(A);
        w = 1 - u - v;

然后是用于计算纹理坐标的部分。

//portion of code calculating texture coordinates
//calculate new location of texture coordinate, assume z position is 0
        glm::vec3 textureCo = P0TexCo*this->u + P1TexCo*this->v + P2TexCo*this->w;
        u = textureCo[0];
        v = textureCo[1];

1 个答案:

答案 0 :(得分:0)

发现了问题,这与opengl解释坐标的方式以及显示数组像素的起点有关。解决方案是在加载图像后反转图像。