Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3429134
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:02:41+00:00 2026-05-18T07:02:41+00:00

I am trying to mix 2d and 3d in opengl in pyglet, i.e. draw

  • 0

I am trying to mix 2d and 3d in opengl in pyglet, i.e. draw a 3d scene then switch to orthographic projection and draw stuff over the top. I draw the 3d stuff, push the projection matrix to the
stack, do a glOrtho projection matrix, draw the 2d stuff, then pop the previous matrix off the stack.
The 3d stuff draws fine but for some reason the 2d part isn’t drawing at all, even on its own.
Here’s the code:

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:02:42+00:00Added an answer on May 18, 2026 at 7:02 am

    I would recommend that you fully reset the modelview and projection matrices on each render, and then don’t use push/pop when you go from 3d to 2d.

    However, I suspect that you are using bad coordinates so the scene is drawing outside the clip planes. In partciular I am a tad suspicious of putting the near clipping plane at zero. Normally 2d elements are drawn with z=0.

    Try putting the near clip-plane at -1.

    I’m also a bit unsure why you’re calling glTranslatef(0, 0.0, -z) in draw2d, I wouldn’t bother.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to iterate over an Enumerable collection of mix of nullable types However
This is all in OpenGL ES 2.0... I'm trying to mix a 3D perspective
I am trying to mix two mp3 files in my Android application.Is there any
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I'm trying to mix Objective-C with C++. When I compile the code, I get
Possible Duplicate: Mix Razor and Javascript code Here is the code I'm trying to
I'm trying to figure out how to validate a form element with a mix
I'm trying to mix php and javascript, because my javascript needs to access some
I'm trying to mix the accordion and the autocomplete plugins into one single solution.
I'm trying to fetch a web page that is a mix of English and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.