2013-04-28 152 views
0

最近,我已經開始從這個網站學習openGL =>http://3dgep.com/?p=2365
我遇到了一個問題。這是我沒有得到現場顯示的場景。三維立方體沒有正確顯示pyglet

我張貼在這個網站我的代碼:

import pyglet 
from pyglet.gl import * 
from pyglet import clock, window 

''' 
    http://www.learnersdictionary.com/search/aspect 
    a dictionary site 

    http://www.opengl.org/sdk/docs/man2/ 
    opengl api reference 

''' 

def vector(type, *args): 
    ''' 
     return a ctype array 
     GLfloat 
     GLuint 
     ... 
    ''' 
    return (type*len(args))(*args) 



class model: 
    def __init__(self, vertices, colorMatrix, indice): 
     self.vertices = vertices 
     self.colorMatrix = colorMatrix 
     self.indice = indice 
     self.angle = 0 

    def update(self): 
     self.angle += 1 
     self.angle %= 360 

    def draw(self): 
     glMatrixMode(GL_MODELVIEW) 
     glLoadIdentity() 

     glRotatef(self.angle, 1, 1, 0) 


     glEnableClientState(GL_VERTEX_ARRAY) 
     glEnableClientState(GL_COLOR_ARRAY) 

     glColorPointer(3, GL_FLOAT, 0, vector(GLfloat, *self.colorMatrix)) 
     glVertexPointer(3, GL_FLOAT, 0, vector(GLfloat, *self.vertices)) 

     glDrawElements(GL_QUADS, len(self.indice), GL_UNSIGNED_INT, vector(GLuint, *self.indice)) 


     glDisableClientState(GL_COLOR_ARRAY) 
     glDisableClientState(GL_VERTEX_ARRAY) 



class world: 
    def __init__(self): 
     self.element = [] 

    def update(self, dt): 
     for obj in self.element: 
      obj.update() 

    def addModel(self, model): 
     self.element.append(model) 

    def draw(self): 
     for obj in self.element: 
      obj.draw() 


def setup(): 
    # look for GL_DEPTH_BUFFER_BIT 
    glEnable(GL_DEPTH_TEST) 







win = window.Window(fullscreen=False, vsync=True, resizable=True, height=600, width=600) 
mWorld = world() 

cube = (
    1, 1, 1, #0 
    -1, 1, 1, #1 
    -1, -1, 1, #2 
    1, -1, 1, #3 
    1, 1, -1, #4 
    -1, 1, -1, #5 
    -1, -1, -1, #6 
    1, -1, -1 #7 
) 


color = (
    1, 0, 0, 
    1, 0, 0, 
    1, 0, 0, 
    1, 0, 0, 
    0, 1, 0, 
    0, 1, 0, 
    0, 0, 1, 
    0, 0, 1 
) 

indice = (
    0, 1, 2, 3, # front face 
    0, 4, 5, 1, # top face 
    4, 0, 3, 7, # right face 
    1, 5, 6, 2, # left face 
    3, 2, 6, 7 # bottom face 
    #4, 7, 6, 5 #back face 
) 

obj = model(cube, color, indice) 
mWorld.addModel(obj) 


@win.event 
def on_resize(width, height): 
    glViewport(0, 0, width, height) 
    glMatrixMode(GL_PROJECTION) 
    glLoadIdentity() 
    glOrtho(-10, 10, -10, 10, -10, 10) 
    glMatrixMode(GL_MODELVIEW) 
    return pyglet.event.EVENT_HANDLED 

@win.event 
def on_draw(): 
    glClearColor(0.2, 0.2, 0.2, 0.8) 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 
    mWorld.draw() 


pyglet.clock.schedule(mWorld.update) 
clock.set_fps_limit(30) 
setup() 
pyglet.app.run() 

我想也許我錯過了一些重要的概念,所以我不能得到正確的結果。任何人都可以教我我犯了什麼錯誤? :(

此外,還有一些奇怪的事情。

indice = (
    0, 1, 2, 3, # front face 
    0, 4, 5, 1, # top face 
    4, 0, 3, 7, # right face 
    1, 5, 6, 2, # left face 
    3, 2, 6, 7 # bottom face 
    #4, 7, 6, 5 #back face 
) 

如果我取消這條線從 到

#4, 7, 6, 5 #back face
 4, 7, 6, 5 #back face
屏幕上會顯示什麼...

回答

1

0.0好,這是不可思議。我我試過把這段代碼翻譯成C++,並且顯示正確。 我使用opengl,glut和C++。所以,我想也許這就是pyglet的問題吧。無論如何, 我可以繼續關於openGL的學習:)

最後,我找到了如何讓這段代碼正確運行的方法! 改變這裏的代碼

self.vertices = vector(GLfloat, *vertices) 
self.colorMatrix = vector(GLfloat, *colorMatrix) 
self.indice = vector(GLuint, *indice) 

glColorPointer(3, GL_FLOAT, 0, self.colorMatrix) 
glVertexPointer(3, GL_FLOAT, 0, self.vertices) 
glDrawElements(GL_QUADS, len(self.indice), GL_UNSIGNED_INT, self.indice) 

很好,很關鍵的一點垃圾收集? 我認爲

self.vertices = vector(GLfloat, *vertices) 

這種方式使得有引用該矢量的對象,所以當我稱爲其需要的c型陣列glDrawElements(...)和其他功能也不會被釋放