首页 > 解决方案 > 三角形纹理水平旋转和翻转

问题描述

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

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

在程序问题中可见 我的实际测试纹理

//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];

标签: c++raytracing

解决方案


发现了这个问题,它与opengl如何解释坐标以及它从哪个角开始显示数组中的像素有关。解决方案是在加载图像后反转图像。


推荐阅读