2016-03-25 173 views
0

我正在用python使用pyglet製作3d遊戲引擎。如果我的頂點有1作爲Z軸,那麼一切都呈現好,但其他任何東西都會消失。Python 3d渲染不工作

我的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) 

窗口運行窗口,萬迪諾只是設置,改窗口

回答

0

我發現我做錯了什麼。我忘了使用glFrustum