对象上的OpenGL纹理对齐

发布于 2025-01-26 16:02:52 字数 3611 浏览 2 评论 0原文

我对对象的纹理对齐有一个问题。 这就是图像的样子。

我一直在关注此

self.texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, self.texture)
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

    image = pg.image.load(filepath).convert()
    image_width,image_height = image.get_rect().size
    img_data = pg.image.tostring(image,'RGBA')
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image_width,image_height,0,GL_RGBA,GL_UNSIGNED_BYTE,img_data)

“ 是我加载OBJ文件的方式:

v = []
    vt = []
    vn = []
    
    #final, assembled and packed result
    vertices = []

    #open the obj file and read the data
    with open(filename,'r') as f:
        line = f.readline()
        while line:
            firstSpace = line.find(" ")
            flag = line[0:firstSpace]
            if flag=="v":
                #vertex
                line = line.replace("v ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                v.append(l)
            elif flag=="vt":
                #texture coordinate
                line = line.replace("vt ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                vt.append(l)
            elif flag=="vn":
                #normal
                line = line.replace("vn ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                vn.append(l)
            elif flag=="f":
                #face, three or more vertices in v/vt/vn form
                line = line.replace("f ","")
                line = line.replace("\n","")
                #get the individual vertices for each line
                line = line.split(" ")
                faceVertices = []
                faceTextures = []
                faceNormals = []
                for vertex in line:
                    #break out into [v,vt,vn],
                    #correct for 0 based indexing.
                    l = vertex.split("/")
                    position = int(l[0]) - 1
                    faceVertices.append(v[position])
                    texture = int(l[1]) - 1
                    faceTextures.append(vt[texture])
                    normal = int(l[2]) - 1
                    faceNormals.append(vn[normal])
                # obj file uses triangle fan format for each face individually.
                # unpack each face
                triangles_in_face = len(line) - 2

                vertex_order = []
                """
                    eg. 0,1,2,3 unpacks to vertices: [0,1,2,0,2,3]
                """
                for i in range(triangles_in_face):
                    vertex_order.append(0)
                    vertex_order.append(i+1)
                    vertex_order.append(i+2)
                for i in vertex_order:
                    for x in faceVertices[i]:
                        vertices.append(x)
                    for x in faceTextures[i]:
                        vertices.append(x)
                    for x in faceNormals[i]:
                        vertices.append(x)
            line = f.readline()
    return vertices

我不确定为什么它不能正确加载。我唯一的想法是,在阅读OBJ文件时,纹理坐标的加载方式存在一些问题,但是纹理似乎并没有伸展仅仅是不对。

我尝试在搅拌机中打开对象,检查紫外线,甚至重新效仿模型。我还尝试将图像旋转围绕思考,也许换了一些轴,但这也无济于事。

I have an issue with texture alignment on objects.
This is what the image looks like.

I've been kind of following this tutorial on PyOpengl:

This is the way I load textures:

self.texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, self.texture)
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

    image = pg.image.load(filepath).convert()
    image_width,image_height = image.get_rect().size
    img_data = pg.image.tostring(image,'RGBA')
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image_width,image_height,0,GL_RGBA,GL_UNSIGNED_BYTE,img_data)

And this is the way I load the OBJ file:

v = []
    vt = []
    vn = []
    
    #final, assembled and packed result
    vertices = []

    #open the obj file and read the data
    with open(filename,'r') as f:
        line = f.readline()
        while line:
            firstSpace = line.find(" ")
            flag = line[0:firstSpace]
            if flag=="v":
                #vertex
                line = line.replace("v ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                v.append(l)
            elif flag=="vt":
                #texture coordinate
                line = line.replace("vt ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                vt.append(l)
            elif flag=="vn":
                #normal
                line = line.replace("vn ","")
                line = line.split(" ")
                l = [float(x) for x in line]
                vn.append(l)
            elif flag=="f":
                #face, three or more vertices in v/vt/vn form
                line = line.replace("f ","")
                line = line.replace("\n","")
                #get the individual vertices for each line
                line = line.split(" ")
                faceVertices = []
                faceTextures = []
                faceNormals = []
                for vertex in line:
                    #break out into [v,vt,vn],
                    #correct for 0 based indexing.
                    l = vertex.split("/")
                    position = int(l[0]) - 1
                    faceVertices.append(v[position])
                    texture = int(l[1]) - 1
                    faceTextures.append(vt[texture])
                    normal = int(l[2]) - 1
                    faceNormals.append(vn[normal])
                # obj file uses triangle fan format for each face individually.
                # unpack each face
                triangles_in_face = len(line) - 2

                vertex_order = []
                """
                    eg. 0,1,2,3 unpacks to vertices: [0,1,2,0,2,3]
                """
                for i in range(triangles_in_face):
                    vertex_order.append(0)
                    vertex_order.append(i+1)
                    vertex_order.append(i+2)
                for i in vertex_order:
                    for x in faceVertices[i]:
                        vertices.append(x)
                    for x in faceTextures[i]:
                        vertices.append(x)
                    for x in faceNormals[i]:
                        vertices.append(x)
            line = f.readline()
    return vertices

I am not sure why it's not loading correctly. The only idea I have is that there is some issue with the way texture coordinates are loaded when reading the OBJ file, but the texture doesn't seem to be stretching just being misaligned.

I tried opening the object in blender and checking the UVs and even reexporting the model. I also tried rotating the image around thinking maybe some axis was swapped but that also didn't help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2025-02-02 16:02:52

大多数图像格式都考虑以栅格顺序,上下写入数据。但是GL的纹理功能期望底行首先出现在内存中。看起来您的图像加载程序似乎并不是在垂直翻转图像,并且您的OBJ进口商也不会将纹理沿Y轴坐标。

Most image formats consider the data to be written in raster order, top to bottom. But GL's texture functions expect the bottom row to appear first in memory. It doesn't look like your image load routine is flipping the images vertically, and your OBJ importer isn't inverting the texture coordinates along the y axis either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文