numpy数组的glDrawPixels未显示

时间:2018-11-30 16:28:33

标签: python opengl pygame pyopengl

我正在尝试用opengl绘制conway的生活游戏。它在常规pygame中工作正常,但是我已经读过glTexImage2D是快速绘制数组中已有内容的方法。我已经检查了the examples and docs provided,但不仅有很多示例链接失效,而且它们是为python 2编写的,因此即使不翻译它们,我什至无法运行它们。我已经注意到,与大多数现代GUI软件包不同,opengl不会真正返回任何内容,因此我认为我没有正确地应用纹理。 (例如,在pygame中,您将生成表面,然后应用返回的表面)。 conways代码的工作原理是:获取有效值和无效值以及dtype,然后仅根据构造函数参数进行所有必要的检查,这意味着我可以在眨眼之间将其从ubyte更改为float。问题很棒。

目前,这只是一个黑屏。删除pygame.display.flip()时,它只会保持白色,因此理论上会在某处绘制某些内容以将其更改为黑色。我感觉问题出在glbindtexture方法周围,但是老实说,我不知道解决方案是什么。

我将放置conways代码,以防万一有人想要运行它。在pygame模式下,我通过scale变量缩放图像,但是对于opengl,我只希望它首先运行,因此conways数组的大小现在是窗口大小,因此是400。这意味着要花一点时间,但是一旦窗口标题更新,就表示更新已完成。

opengl抽屉:

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *
from random import random,seed
import numpy as np
from conways3 import Conways
from time import time

size = 400;
scale = 1;

conways = Conways(size,dead=0.0,alive=1.0,dtype=np.ubyte)

pygame.init()
flags = OPENGL|HWSURFACE|DOUBLEBUF
display = pygame.display.set_mode((size*scale, size*scale),flags)
########OPTIMIZATIONS##########
pygame.event.set_allowed([pygame.QUIT]);
###############################

running = True

clock = pygame.time.Clock()
t1 = t2 = t3 = 0
glEnable(GL_TEXTURE_2D)
try:
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        clock.tick()
        t1 = time()
        Z = conways.update()
        t2 = time()
        tid = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, tid)
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE, size,size,0,GL_LUMINANCE, GL_UNSIGNED_BYTE, Z)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) 

    ##    surf = pygame.surfarray.make_surface(Z)
    ##    display.blit(pygame.transform.scale(surf,(size*scale, size*scale)), (0, 0))
    ##    pygame.display.update()
        pygame.display.flip()
        t3 = time()
        pygame.time.wait(10)
        pygame.display.set_caption("fps: {:.4f} calc: {:.4f} draw: {:.4f} tot: {:.4f}".format(clock.get_fps(), t2-t1, t3-t2,t3-t1))
##        print(t2-t1)
except Exception as e:
    print('-'*20)
    print(e)

pygame.quit()

通道:

from random import random, seed
import numpy as np
from time import time

class Conways:
    def __init__(self,size,dead=False,alive=True,dtype = np.bool8):
        seed(1)

        self.using1 = True;

        self.size = size;
        self.dead = dead;
        self.alive = alive;
        self.dtype = dtype;

        self.arr1 = np.zeros((self.size,self.size),dtype=self.dtype);
        self.arr2 = np.zeros((self.size,self.size),dtype=self.dtype);

        for i in range(self.size):
                for j in range(self.size):
                        self.arr1[i][j] = self.alive*(random() < 0.5);
    def calcNeighbors(self,arr, i, j):
            count = -1*arr[i][j];    
            for x in range(-1, 2):
                    for y in range(-1, 2):
                            count += (arr[(x+i)%self.size][(y+j)%self.size] == self.alive);
            return count;
    def calcEffi(self,arr, i, j):
            count = 0
            maxi = self.size - 1
            if i > 0:
                    count     += arr[i - 1][j] == self.alive
            if i < maxi:
                    count     += arr[i + 1][j] == self.alive
            if j > 0:
                    count     += arr[i][j - 1] == self.alive
                    if i > 0:
                            count += arr[i - 1][j - 1] == self.alive
                    if i < maxi:
                            count += arr[i + 1][j - 1] == self.alive
            if j < maxi:
                    count     += arr[i][j + 1] == self.alive
                    if i > 0:
                            count += arr[i - 1][j + 1] == self.alive
                    if i < maxi:
                            count += arr[i + 1][j + 1] == self.alive
            return count;

    def calc(self,arr1, arr2):
            for i in range(self.size):
                    for j in range(self.size):
                            neighbors = self.calcEffi(arr1, i, j);
                            if neighbors < 2 or neighbors > 3:
                                    arr2[i][j] = self.dead;
                            elif neighbors == 3:
                                    arr2[i][j] = self.alive;
                            else:
                                    arr2[i][j] = arr1[i][j];
    def update(self):
            if self.using1:
                    self.calc(self.arr1,self.arr2);
            else:
                    self.calc(self.arr2,self.arr1);
            self.using1 = not self.using1;
            return self.arr2 if self.using1 else self.arr1;

1 个答案:

答案 0 :(得分:1)

由于数组值在[0,1]范围内,因此您必须分别使用float32 GL_FLOAT

conways = Conways(size,dead=0.0,alive=1.0,dtype=np.float32)
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, size, size, 0, GL_LUMINANCE, GL_FLOAT, Z)

但是主要的问题是您什么都不画。您必须在整个视口上绘制一个四边形,并在其上包裹纹理:

glBindTexture(GL_TEXTURE_2D, tid)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, size,size, 0, GL_LUMINANCE, GL_FLOAT, Z)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) 

glBegin(GL_TRIANGLE_FAN)
glTexCoord2f(0, 1)
glVertex2f(-1, -1) 
glTexCoord2f(1, 1)
glVertex2f(1, -1) 
glTexCoord2f(1, 0)
glVertex2f(1, 1) 
glTexCoord2f(0, 0)
glVertex2f(-1, 1) 
glEnd()

或使用glEnableClientStateglVertexPointerglTexCoordPointerglDrawArrays绘制到视口上:

vertices   = np.array([-1, -1, 1, -1, 1, 1, -1, 1], dtype=np.float32)
tex_coords = np.array([0, 1, 1, 1, 1, 0, 0, 0], dtype=np.float32)

glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(2, GL_FLOAT, 0, vertices)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(2, GL_FLOAT, 0, tex_coords)

glDrawArrays(GL_TRIANGLE_FAN, 0, 4)

glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)

但是请注意,数十年来,不赞成使用glBegin / glEnd序列进行绘制,也不推荐使用OpenGL固定功能管线的客户端功能。 阅读有关Fixed Function Pipeline的信息,并参阅Vertex SpecificationShader了解最新的渲染方式。