Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw using pygame, while also drawing with pyopengl?

Tags:

I have a program that is written in python and uses pygame along with pyopengl. The only problem is, I can't draw anything on the screen with pygame.draw which is what I was going to use for interfacing with my program. I would like to know if there is a way to draw using pygame's system while also drawing 3D using pyopengl behind it.

like image 474
Zachary Milner Avatar asked Mar 09 '21 18:03

Zachary Milner


People also ask

Can you draw in Pygame?

In this PyGame and Python programming tutorial video, we cover how to draw shapes with PyGame's built in drawing functionality. We can do things like draw specific pixels, lines, circles, rectangles, and any polygon we want by simply specifying the points to draw between. Let's get started!


1 Answers

You can't do that directly.

However you can draw on a pygame.Surface object with the pygame.draw module or pygame.Surface.blit. Use pygame.PixelArray to access the pixels on the surface directly. Use the pixels to generate an OpenGL Texture object. This texture can be used in OpenGL.

In the other direction you can render into a OpenGL Renderbuffer or Texture object (see Framebuffers). Load the texture onto the GPU with glReadPixels or glGetTexImage and create a pygame.Surface with pygame.image.frombuffer.


See also Does PyGame do 3d?, PyGame and ModernGL library, PyGame and OpenGL 4 or PyGame and OpenGL immediate mode (Legacy OpenGL).

like image 181
Rabbid76 Avatar answered Nov 12 '22 16:11

Rabbid76