• Advertisement
Sign in to follow this  

[PyGame + PyOpenGL] Setting Target Surface

This topic is 4293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just started playing around with Pygame w/ PyOpenGL. I'm making good progress, but I'm having trouble figuring out how to have PyOpenGL calls render to a surface other than the one returned by pygame.display.set_mode(...). By default, everything I draw with the gl***() methods is drawn directly onto the back buffer. Calling pygame.display.flip() then flips the new image to the screen. What I'd like to do is render multiple scenes to seperate Surface objects, then render all the surfaces together onto the back buffer, then flipping the final image to the screen. This will be used for fade in/out transitions between game screens. - Mike

Share this post


Link to post
Share on other sites
Advertisement
Here's my current code, if anyone is curious:


import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

width = 800
height = 600

color = Color("black")
screen = pygame.display.set_mode( (width, height), DOUBLEBUF | OPENGL )
glViewport(0, 0, width, height)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective( 45.0, float(width) / float(height), 0.1, 100.0 )

angle = 0

while True:

for e in pygame.event.get():
if e.type == QUIT:
break;

glClearColor(color[0], color[1], color[2], color[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

angle += 0.5

glMatrixMode(GL_MODELVIEW)

glLoadIdentity()
glTranslatef(0, 0.0, -6.0)
glRotatef(angle, 0.0, 1.0, 0.0)

glBegin(GL_POLYGON)
glColor4f(1.0, 1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, 0.0)
glVertex3f(1.0, 1.0, 0.0)
glVertex3f(1.0, -1.0, 0.0)
glVertex3f(1.0, -1.0, 0.0)
glVertex3f(-1.0, -1.0, 0.0)
glVertex3f(-1.0, 1.0, 0.0)
glEnd()

pygame.display.flip()


Share this post


Link to post
Share on other sites
As far as I know, rendering to anything other than the back buffer involves using the render-to-texture extension (WGL_ARB_render_texture?). Unfortunately you might find that this is not covered by PyOpenGL, and even if it is, you might then find it's not covered by PyGame surfaces.

Anyway, I believe you've already been advised of this on the PyGame list, which isn't surprising, as that list is entirely dedicated to PyGame users while there are few of them here. GameDev.net is more general purpose and can't cover niches like PyGame quite so well.

Share this post


Link to post
Share on other sites
Thanks. The lack of responses here led to me submit my question to the PyGame mailing list. I was planning on posting my resolution in here when I've got the render-to-texture thing figured out.

Share this post


Link to post
Share on other sites
The solution ended up being a little different than what I was looking for, but far more flexible. The helpful guys on the PyGame mailing list recommended using OpenGL's "accumulation buffer", or rendering directly from the back buffer to a texture in video memory using glCopyTexImage2D(). I chose the later, since that gives me the flexibility of stretching, rotating, translating, and blending the final texture, where the accumulation buffer would limit me to blending.

Share this post


Link to post
Share on other sites
Is that pretty much bypassing PyGame surfaces at that stage then?

It would be nice to have this and other OpenGL functionality wrapped up in easy to use wrappers for Python. It seems odd that Python doesn't have better graphics libraries.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
Is that pretty much bypassing PyGame surfaces at that stage then?


Pretty much. I'm still flipping the back buffer to the screen using pygame.display.flip(), but otherwise I'm using all gl*() calls for all my rendering.

Quote:
Original post by Kylotan
It would be nice to have this and other OpenGL functionality wrapped up in easy to use wrappers for Python. It seems odd that Python doesn't have better graphics libraries.


Yes, it would. Unless I'm missing something, PyGame and PyOpenGL are the definitive graphics libraries for Python.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement