2010-11-24 51 views
5

私はpygletでopenglで2dと3dを混ぜようとしています。つまり、3dシーンを描画して正投影に切り替え、私は3Dのものを描画し、投影行列を スタックにプッシュし、glOrth投影行列を行い、2Dのものを描画して、前の行列をスタックからポップします。 3Dのものはうまく描けますが、何らかの理由で2dの部分が全く描画されません。 ここにコードがあります:draw2d()OpenGLで2dと3dを混ぜて(pygletを使って)

class Window(pyglet.window.Window): 

    # resolution 
    width, height = 1024, 786 

    def __init__(self, width, height): 

     # initialise window 
     super(Window, self).__init__(width, height) 

     # set title 
     self.set_caption("OpenGL Doss") 

     # call update() at 30fps 
     pyglet.clock.schedule_interval(self.update, 1/30.0) 

     glEnable(GL_TEXTURE_2D)   # enable textures 
     glShadeModel(GL_SMOOTH)   # smooth shading of polygons 
     glClearColor(0.0, 0.0, 0.0, 0.0) 

     glClearDepth(1.0) 

     glDepthFunc(GL_LEQUAL)   
     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # make stuff look nice 

     self.world = World()   # initialise world 

     self.label = pyglet.text.Label('Hello, world', 
          font_name='Times New Roman', 
          font_size=20, 
          width=10, height=10) 

    def on_resize(self, width, height): 
     print 'on resize' 
     if height == 0: 
      height = 1 
     glViewport(0, 0, width, height) # specify viewport 

     # load perspective projection matrix 
     glMatrixMode(GL_PROJECTION) 
     glLoadIdentity() 
     gluPerspective(45, 1.0 * width/height, 0.1, 100.0) 
     #glLoadIdentity() 

    def on_draw(self): 
     self.set3d() 

     # draw 3d stuff 
     self.world.draw() 

     self.set2d() 

     # draw 2d stuff 
     self.draw2d() 

     self.unSet2d() 

    def update(self, dt): 
     "called at set interval during runtime" 
     #maze = self.world.maze 
     maze_platform = self.world.maze_platform 

     pacman = maze_platform.maze.pacman 

     maze_platform.update() 

     # send it world pointer 
     pacman.update(self.world) 



    def on_key_press(self, symbol, modifiers): 
     control.press(symbol, modifiers) 

    def on_key_release(self, symbol, modifiers): 
     control.release(symbol, modifiers) 

    def set3d(self): 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 
     glEnable(GL_DEPTH_TEST)   # enable depth testing 
     # reset modelview matrix 
     glMatrixMode(GL_MODELVIEW) 
     glLoadIdentity() 

    def set2d(self): 

     glDisable(GL_DEPTH_TEST) 
     # store the projection matrix to restore later 
     glMatrixMode(GL_PROJECTION) 
     glPushMatrix() 

     # load orthographic projection matrix 
     glLoadIdentity() 
     #glOrtho(0, float(self.width),0, float(self.height), 0, 1) 
     far = 8192 
     glOrtho(-self.width/2., self.width/2., -self.height/2., self.height/2., 0, far) 

     # reset modelview 
     glMatrixMode(GL_MODELVIEW) 
     glLoadIdentity() 

     #glClear(GL_COLOR_BUFFER_BIT) 



    def unSet2d(self): 

     # load back the projection matrix saved before 
     glMatrixMode(GL_PROJECTION) 
     glPopMatrix() 

    def draw2d(self): 
     z=-6 
     n=100 
     glTranslatef(0, 0.0, -z) 


     glBegin(GL_TRIANGLES) 
     glVertex3f(0.0, n, 0.0) 
     glVertex3f(-n, -n, 0) 
     glVertex3f(n, -n, 0) 
     glEnd() 


def main(): 
    window = Window(Window.width, Window.height) 
    pyglet.app.run() 
    print 'framerate:', pyglet.clock.get_fps(), '(error checking = %s)' % pyglet.options['debug_gl'] 

if __name__ == '__main__': main() 
    #command = 'main()' 
    #cProfile.run(command) 

答えて

2

各レンダリングでモデルビューと投影行列を完全にリセットし、3dから2dに移動するときにプッシュ/ポップを使用しないことをお勧めします。

しかし、シーンがクリッププレーンの外側に描画されるように、不正な座標を使用していると思われます。 Partciularでは、ニアクリッピングプレーンをゼロにすることに疑念を感じています。通常、2次元要素はz = 0で描画されます。

ニアクリッププレーンを-1にしてみてください。

なぜ私はdraw2dでglTranslatef(0、0.0、-z)を呼び出すのか分からないので、気にしません。

+0

私はクリッピングプレーンの間違った側でそれを描いていることに気付きました。そして、翻訳は全く無意味です。 – eggbert

0

あなたの三角形を描画する前にglDisable(GL_TEXTURE_2D)glColor3ub(255,255,255)を試してみてください。

テクスチャジオメトリを使用している場合は、world.draw()を再度呼び出す前に、glEnable(GL_TEXTURE_2D)を必ず再送信してください。

関連する問題