我正在使用pyglet制作一个带python的3D游戏引擎。如果我的顶点有1作为Z轴,那么一切都可以渲染,但其他任何东西都会消失。
我的Obj加载代码:
class Mesh:
def __init__(self, name):
self.vbo = GLuint()
self.size = 0
self.name = name
self.verts = []
self.norms = []
self.indis = []
self.type = GL_TRIANGLES
def Process(self,data):
words = []
for item in data: #For every line contained in data
words = Get_Words(item, " ")
if words[0] == "#": #Skip comments
continue
elif words[0] == "v": #Process Vertex Data
self.verts.append(float(words[1])) #Vertex X
self.verts.append(float(words[2])) #Vertex Y
self.verts.append(float(words[3])) #Vertex Z
elif words[0] == "vn": #Process Normal Data
self.norms.append(float(words[1])) #Normal X
self.norms.append(float(words[2])) #Normal Y
self.norms.append(float(words[3])) #Normal Z
elif words[0] == "f": #Process Face Data
faces = []
if len(words) == 5: #If mesh has quads instead of triangles
self.type = GL_QUADS #Set mesh type to quads
for x in range(1,len(words)):
faces = Get_Words(words[x], "//") #Obj Face data example: 5//2
self.indis.append(GLuint(int(faces[0]))) #We only want the 5, 2 is the face number
def Send(self):
data = (GLfloat*len(self.verts))(*self.verts)
self.size = sizeof(data)
glGenBuffers(1, pointer(self.vbo))
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(GL_ARRAY_BUFFER, self.size, 0, GL_STATIC_DRAW)
glBufferSubData(GL_ARRAY_BUFFER, 0, self.size, data)
del self.verts
return data
def Draw(self, *args):
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glVertexPointer(3, GL_FLOAT, 0, 0)
if len(args) == 0:
glDrawArrays(GL_POINTS, 0, self.size)
else:
glDrawArrays(args[0], 0, self.size)
现在我只渲染顶点数据 这是我的主要代码:
window = New()
winset = Window()
winset.title = "Test"
winset.resizable = True
winset.icon = "icon.png"
winset.clear_color = vec4(0.2,0.2,0.2,1)
glEnableClientState(GL_VERTEX_ARRAY)
glEnable(GL_DEPTH_TEST)
data = (GLfloat*6)(*[100,100, 200,200, 100,200])
vbo = GLuint()
glGenBuffers(1, pointer(vbo))
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(data), 0, GL_STATIC_DRAW)
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)
d = []
d.append("v 300 300 1") #1 1 1
d.append("v 200 300 1") #-1 1 1
d.append("v 300 200 1") #1 -1 1
d.append("v 200 300 1")
d.append("v 300 200 1")
d.append("v 200 200 1")
d.append("v 200 300 -2")
d.append("v 300 200 -2")
d.append("v 200 200 -2")
cube = Mesh("Cube")
cube.Process(d)
cube.Send()
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#glColor3f(1,1,1)
#glBindBuffer(GL_ARRAY_BUFFER, vbo)
#glVertexPointer(2, GL_FLOAT, 0, 0)
#glDrawArrays(GL_TRIANGLES, 0, sizeof(data))
glColor3f(1,1,0)
cube.Draw(GL_TRIANGLES) #GL_TRIANGLES | GL_LINES
winset.Run(window)
窗口是正在运行的窗口,而winset只是更改窗口的设置
答案 0 :(得分:0)
我发现了我做错了什么。我忘了使用glFrustum