Pyglet use entire texture when drawing

573 views Asked by At

I am attempting to draw a basic cube in pyglet, and apply a texture to it. 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. I need each face of the cube to use the entire file

1

There are 1 answers

0
derhass On

The GL does not know how to map the texture to your primitives. 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.

From the question, it is not clear if you use loegacy GL or modern shader-based 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. It should explain the basics needed to get this to work.