通过glBindTexture获取错误的纹理

时间:2016-09-09 16:13:37

标签: python opengl pygame pyopengl

我有.png格式的两个纹理(地板和墙壁),我在初始化期间加载它们,并在glBindTexture渲染之前选择,但我得到了: screen_shot_1 而不是这个: screen_shot_2 初始化:

def __init__(self, screen_info, wall_number):
    self.screen = screen_info
    self.inscription = font.Font(get_path([RES_PATH[0], 'font', 'main.ttf']), 12)
    self.path = get_path([RES_PATH[0], RES_PATH[1], ''])
    self.sprite_list = {}
    self.tex_list = {}
    self.generate_list()
    self.wall_coordinates = []
    self.wall_number = wall_number
    self.wall_counter = 0

    glClearColor(*BACKGROUND_COLOR)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(FOV, self.screen.current_w / self.screen.current_h, DISTANCE_NEAR, DISTANCE_FAR)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glEnable(GL_DEPTH_TEST)
    glEnable(GL_TEXTURE_2D)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

主循环:

gi = interaction.GI(screen_info, 11)

while True:
    timer.tick(60)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()

    camera.update_view()
    camera.update_move(forward, backward, left, right, gi.get_wall_coordinates())

    gi.draw_ground(0, 0)
    gi.draw_square(-2, 1.5, -4)
    gi.draw_square(0, 1.5, -4)
    gi.draw_square(2, 1.5, -4)
    gi.draw_square(4, 1.5, -4, rot=True)
    gi.draw_square(4, 1.5, -2, rot=True)
    gi.draw_square(4, 1.5, 0, rot=True)
    gi.draw_square(2, 1.5, 2)
    gi.draw_square(0, 1.5, 2)
    gi.draw_square(-2, 1.5, 2)
    gi.draw_square(-2, 1.5, 0, rot=True)
    gi.draw_square(-2, 1.5, -4, rot=True)

    pygame.display.flip()

纹理加载器:

def load_tex(self, filename, rotate=False, text_render=False, text=''):
    if not text_render:
        surface = image.load(filename)

        if rotate:
            surface = transform.rotate(surface, 180)

    else:
        surface = self.inscription.render(str(text), 1, (255, 255, 255), (0, 0, 0))
        surface = transform.scale(surface, (64, 64))
        surface = transform.rotate(surface, 180)

    size = surface.get_size()
    surface = image.tostring(surface, 'RGB', True)
    texture = glGenTextures(1)

    glBindTexture(GL_TEXTURE_2D, texture)
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, size[0], size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, surface)

    return texture

在初始化期间加载纹理:

def generate_list(self):
    for filename in listdir(self.path):
        self.tex_list[filename] = self.load_tex(get_path([RES_PATH[0], RES_PATH[1], filename]))

绘图场地:

def draw_ground(self, x, z, length=50):
    glBindTexture(GL_TEXTURE_2D, self.tex_list['floor.png'])
    # glBindTexture(GL_TEXTURE_2D, self.load_tex(get_path([RES_PATH[0], RES_PATH[1], 'floor.png'])))
    glBegin(GL_QUADS)

    glTexCoord2f(0, length)
    glVertex3f(x - length, -0.5, z + length)
    glTexCoord2f(length, length)
    glVertex3f(x + length, -0.5, z + length)
    glTexCoord2f(length, 0)
    glVertex3f(x + length, -0.5, z - length)
    glTexCoord2f(0, 0)
    glVertex3f(x - length, -0.5, z - length)

    glEnd()

绘图墙:

def draw_square(self, x, y, z, length=2, rot=False):
    glBindTexture(GL_TEXTURE_2D, self.tex_list['wall.png'])
    # glBindTexture(GL_TEXTURE_2D, self.load_tex(get_path([RES_PATH[0], RES_PATH[1], 'wall.png'])))
    glBegin(GL_QUADS)

    glTexCoord2f(0, 1)
    glVertex3f(x, y, z)
    glTexCoord2f(1, 1)

    if not rot:
        glVertex3f(x + length, y, z)
        glTexCoord2f(1, 0)
        glVertex3f(x + length, y - length, z)

    else:
        glVertex3f(x, y, z + length)
        glTexCoord2f(1, 0)
        glVertex3f(x, y - length, z + length)

    glTexCoord2f(0, 0)
    glVertex3f(x, y - length, z)

    glEnd()

那么问题是什么?

1 个答案:

答案 0 :(得分:1)

我在您的代码中注意到的一些事情可能与您的纹理问题有关,也可能没有:

  • 对于你的glTexParameterf调用,我看到你使用了GL_NEAREST。这将导致像素化纹理。如果您不使用像素艺术,您可能需要考虑GL_LINEAR作为最终参数,因为这会执行双线性插值,从而消除显着的像素化。
  • 我认为你不需要glTexEnvf和glPixelStorei电话。
  • 作为一个好习惯,一旦你完成使用纹理,你应该通过调用glBindTexture(GL_TEXTURE_0,0)解除绑定。 OpenGL完全是关于状态的,因此最好将事情归结为完成后的事情,以免后来因奇怪的行为而感到惊讶。我的猜测是你没有正确绑定第二个纹理(检查纹理ID),导致第一个纹理用于​​所有调用。尝试重新排序绘制调用,并查看纹理是否更改。这是猜测,因为我无法运行您的示例代码。
  • glTexImage2D的参数看起来很奇怪。您可能需要查看opengl docs及其python等效内容。我个人喜欢使用像GL_RGBA这样的枚举格式来使代码更清晰。

这是我制作的一个使用pyopengl和pygame的简单演示。它呈现两个2d正方形,每个正方形具有不同的纹理。主while循环和load_texture函数是重要的部分:

import pygame
from pygame.locals import *
from OpenGL.GL import *
import sys

def init_gl():
    window_size = width, height = (550, 400)
    pygame.init()
    pygame.display.set_mode(window_size, OPENGL | DOUBLEBUF)
    glEnable(GL_TEXTURE_2D)
    glMatrixMode(GL_PROJECTION)
    glOrtho(0, width, height, 0, -1, 1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

def load_texture(texture_url):
    tex_id = glGenTextures(1)
    tex = pygame.image.load(texture_url)
    tex_surface = pygame.image.tostring(tex, 'RGBA')
    tex_width, tex_height = tex.get_size()
    glBindTexture(GL_TEXTURE_2D, tex_id)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_surface)
    glBindTexture(GL_TEXTURE_2D, 0)
    return tex_id

if __name__ == "__main__":
    init_gl()
    texture1 = load_texture("texture1.png")
    texture2 = load_texture("texture2.png")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        glClear(GL_COLOR_BUFFER_BIT)
        glBindTexture(GL_TEXTURE_2D, texture1)
        glBegin(GL_QUADS)
        glTexCoord(0, 0)
        glVertex(50, 50, 0)
        glTexCoord(0, 1)
        glVertex(50, 100, 0)
        glTexCoord(1, 1)
        glVertex(100, 100, 0)
        glTexCoord(1, 0)
        glVertex(100, 50, 0)
        glEnd()
        glBindTexture(GL_TEXTURE_2D, 0)

        glBindTexture(GL_TEXTURE_2D, texture2)
        glBegin(GL_QUADS)
        glTexCoord(0, 0)
        glVertex(450, 300, 0)
        glTexCoord(0, 1)
        glVertex(450, 350, 0)
        glTexCoord(1, 1)
        glVertex(500, 350, 0)
        glTexCoord(1, 0)
        glVertex(500, 300, 0)
        glEnd()
        glBindTexture(GL_TEXTURE_2D, 0)

        pygame.display.flip()

作为旁注,我发布的代码和演示代码中的大多数都不推荐使用现代OpenGL,它不支持慢速立即模式调用,如glBegin和glEnd。如果您稍后遇到性能问题,可能需要考虑重构以使用更多现代技术,例如VBO(顶点缓冲区对象)。但对于简单的应用程序,这不应该是一个太大的问题。如果这有帮助,请告诉我!