简体   繁体   English

Pyglet在绘制时会使用整个纹理

[英]Pyglet use entire texture when drawing

I am attempting to draw a basic cube in pyglet, and apply a texture to it. 我试图在pyglet中绘制一个基本的立方体,并对其应用纹理。 I can draw the cube, but the texture is only one pixel from the file i loaded. 我可以绘制立方体,但纹理仅是我加载的文件的一个像素。 Here is how i am loading the texture and setting it. 这是我加载纹理并进行设置的方式。

self.texture = pyglet.image.load("texture.png").get_texture()
self.batch.add(
            len(vertices) / 3, gl_QUADS, self,
            ("v3f" , vertices)
        )

self.batch.draw()

this will successfully draw my cube where I want it, but the texture is not what I want. 这将成功地将我的多维数据集绘制到所需的位置,但纹理不是我想要的。 only the lowest left most pixel of the texture file ("texture.png") is shown. 仅显示纹理文件(“ texture.png”)的最低的最左像素。 I need each face of the cube to use the entire file 我需要立方体的每个面都可以使用整个文件

The GL does not know how to map the texture to your primitives. GL不知道如何将纹理映射到您的图元。 You need to define a mapping between your primitives anfd the texture space. 您需要在元和纹理空间之间定义一个映射 One way of doing that is by specifying texture coordinates for each of your vertices. 一种方法是为每个顶点指定纹理坐标

As GL is desgined a state machine, when you don't set the value of some attribute in a per-vertex manner, it will use the last value set for all of the vertices, so it will sample the texture at the very same point across the whole primitive - only one pixel of the texture will be used. 因为GL被指定为状态机,所以当您不以每个顶点的方式设置某些属性的值时,它将使用为所有顶点设置的最后一个值,因此它将在同一点采样纹理在整个图元上-将仅使用纹理的一个像素。

From the question, it is not clear if you use loegacy GL or modern shader-based GL. 从这个问题来看,不清楚您使用的是传统GL还是基于现代着色器的GL。 I guess that you use the old fixed-function pipeline wihtout self-written shaders (otherwise, adding the texture would probably have no effect at all). 猜想您使用的是带有自定义着色器的旧的固定功能管线(否则,添加纹理可能根本没有效果)。 Have a look into the chapter on texture mapping in the OpenGL red book in that case. 在这种情况下,请阅读OpenGL红皮书中有关纹理映射的章节 It should explain the basics needed to get this to work. 它应该解释使它起作用所需的基本知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM