[英]3d cube didn't show correctly writen by pyglet
Recently, I have started to learn openGL from this site => http://3dgep.com/?p=2365 最近,我开始从此站点学习openGL => http://3dgep.com/?p=2365
and I encounter a problem. 我遇到了一个问题。 That's I didn't get the scene as the site shows. 那是我没有得到现场展示的现场。
I post my code on this site: 我在此网站上发布了我的代码:
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()
I think maybe I miss some important concepts so I can't get the correct result. 我认为也许我错过了一些重要的概念,所以我无法获得正确的结果。 Can anyone teach me what mistake I make? 谁能教我我犯了什么错误? :( :(
Furthermore, there is something strange. 此外,还有一些奇怪的事情。
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
)
If I uncomment this line from 如果我取消注释此行
#4, 7, 6, 5 #back faceto 至
4, 7, 6, 5 #back face 4,7,6,5#背面the screen will show nothing... 屏幕上什么也不会显示...
0.0 well, that's weird. 0.0好,那很奇怪。 I have tried to translate this code into C++ and it shows correctly. 我试图将此代码转换为C ++,并且显示正确。 I use opengl, glut, and c++. 我使用opengl,glut和c ++。 So, I think maybe that's the issue on pyglet. 因此,我认为这可能是pyglet的问题。 Whatever, I can go on my studying about openGL :) 无论如何,我都可以继续学习openGL :)
Finally, I find the way how to make this code run correctly!! 最后,我找到了使此代码正确运行的方法! change the code here 在这里更改代码
self.vertices = vector(GLfloat, *vertices)
self.colorMatrix = vector(GLfloat, *colorMatrix)
self.indice = vector(GLuint, *indice)
and 和
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)
well, Is the key point garbage collection? 好吧,关键点是垃圾回收吗? I think 我认为
self.vertices = vector(GLfloat, *vertices)
this way makes there is an object to reference the vector so it won't be freed when I called glDrawElements(...) and others functions which need the c-type array 这种方式使得有一个对象可以引用向量,因此当我调用glDrawElements(...)和其他需要c型数组的函数时,它将不会被释放
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 )
You missed the last semicolon at 3,2,6,7[,] # bottom face
. 您错过了3,2,6,7[,] # bottom face
的最后一个分号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.