PyOpenGL numpy纹理显示为纯色

时间:2017-04-18 21:42:27

标签: python numpy opengl pygame pyopengl

我正在尝试在pygame中将numpy数组设置为OpenGl纹理。问题是只有第一个像素被视为纹理数据,而不是随机模式(例如)我每次只获得一个随机颜色。

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

import numpy as np

#Settings
width = 200
height = 300
resolution = (800,600)
texture_data = np.random.randint(256,size=(height, width, 3))

#pygame init
pygame.init()
gameDisplay = pygame.display.set_mode(resolution,OPENGL)

#OpenGL init
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,resolution[0],0,resolution[1],-1,1)
glMatrixMode(GL_MODELVIEW)
glDisable(GL_DEPTH_TEST)
glClearColor(0.0,0.0,0.0,0.0)
glEnable(GL_TEXTURE_2D)

#Set texture
texture = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data)
glGenerateMipmap(GL_TEXTURE_2D)

glActiveTexture(GL_TEXTURE0)

#Clean start
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()

#draw rectangle
glTranslatef(300,200,0)
glBegin(GL_QUADS)
glVertex2f(0,0)
glVertex2f(0,height)
glVertex2f(width,height)
glVertex2f(width,0)
glEnd()
glFlush()

#game loop until exit
gameExit = False
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True

    #throttle
    pygame.time.wait(100)

1 个答案:

答案 0 :(得分:0)

正如genpfault指出的那样,我缺少纹理坐标。我需要将它们添加到矩形绘图部分。

#draw rectangle
glTranslatef(300,200,0)
glBegin(GL_QUADS)

glTexCoord(0,0)
glVertex2f(0,0)

glTexCoord(0,1)
glVertex2f(0,height)

glTexCoord(1,1)
glVertex2f(width,height)

glTexCoord(1,0)
glVertex2f(width,0)

glEnd()
glFlush()

如果遗失了纹理贴图的工作方式,请阅读此内容:

OpenGL Programming Guide - Chapter 9 Texture Mapping