简体   繁体   English

将纹理映射到Quad OpenGL和pyglet

[英]Mapping a texture to a quad opengl and pyglet

I am trying to map a texture to a quad in pyglet using the opengl functions but the quad is just showing up as white. 我正在尝试使用opengl函数将纹理映射到pyglet中的四边形,但是四边形只是显示为白色。

My code looks like this: 我的代码如下所示:

import pyglet
from pyglet.gl import *

glEnable(GL_TEXTURE_2D)
image = pyglet.image.load("redbrick.png")
texture = image.get_texture()

and my draw function: 和我的绘画功能:

def on_draw():
    window.clear()

    glBindTexture (GL_TEXTURE_2D, 13)
    glBegin (GL_QUADS);
    glTexCoord2i (0, 0)
    glVertex2i (0, 0)
    glTexCoord2i (1, 0)
    glVertex2i (100, 0)
    glTexCoord2i (1, 1)
    glVertex2i (100, 100)
    glTexCoord2i (0, 1)
    glVertex2i (0, 100)
    glEnd ()

Is there something I am doing wrong or missing out for it to be drawn as a white quad? 我在做错什么还是错过了要绘制为白色四边形的东西吗?

I see a few things that may be wrong. 我看到一些可能是错误的事情。 It would be nice to have the full code in like a pastebin but I can't comment to ask so... 像pastebin一样包含完整的代码会很好,但是我不能这么问...

your texture needs to be made into an opengl texture. 您的纹理需要制成一个opengl纹理。 You need to first convert your image into a raw data format. 您需要首先将图像转换为原始数据格式。 When you load the image do image.get_data(), compressed into a single line below. 加载图像时,请执行image.get_data(),压缩到下面的一行中。 This isn't the most efficient way but a simple example. 这不是最有效的方法,而是一个简单的示例。

After Binding the texture set drawing parameters. 绑定纹理后,设置绘图参数。

Then hand the data to the video card in glTexImage2D. 然后将数据交给glTexImage2D中的视频卡。

data=pyglet.image.load("redbrick.png").get_data()
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (image_width), (image_height), 0, GL_RGBA, GL_UNSIGNED_BYTE, data)

After that you should be able to use this texture if you bind with texture_id. 之后,如果与texture_id绑定,则应该可以使用此纹理。 13 could be nothing for all I can see. 13对我所看到的来说可能什么都不是。 Provide all the code and I could probably revise. 提供所有代码,我可能会修改。

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

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