2017-04-18 72 views
0

我試圖在pygame中將numpy數組設置爲OpenGl紋理。問題是隻有第一個像素被作爲紋理數據,而不是隨機模式(例如),我每次只能得到一個隨機顏色。PyOpenGL numpy紋理顯示爲純色

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) 
+3

哪裏是你的紋理座標?現在你只能沿管道發送頂點。 – genpfault

+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