[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] how to blit a Surface to screen with OPENGLBLIT arg?



flya flya wrote:
I want use pyopengle to render some 3d effects, but I also need to
blit some normal Surface to screen before redering 3d object.
my code like these:

Welcome to OpenGHell! As far as I know, you can't just draw 2D objects to the screen in OpenGL, even if you're also using Pygame. You have to switch to a 2D OpenGL drawing mode, then create a quadrilateral painted with a texture rather than _just blitting the image_. This is one way to do it:


def CreateSpecialDrawingLists():
"""Create some drawing lists for quick screen-clearing and mode-switching."""
global L_CLEARSCREEN,L_SWITCH_TO_2D,L_SWITCH_TO_3D
L_CLEARSCREEN = glGenLists(3)
L_SWITCH_TO_2D = L_CLEARSCREEN + 1
L_SWITCH_TO_3D = L_CLEARSCREEN + 2


    glNewList(L_CLEARSCREEN,GL_COMPILE)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glEndList()

    glNewList(L_SWITCH_TO_2D,GL_COMPILE)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,SCREEN_SIZE_X,0,SCREEN_SIZE_Y,-1,1)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glEndList()

    glNewList(L_SWITCH_TO_3D,GL_COMPILE)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()

gluPerspective(45.0,float(SCREEN_SIZE_X)/float(SCREEN_SIZE_Y),0.1,100.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glEndList()

def OpenGLSwitchMode(two_d=True):
    """Switch between 2D and 3D drawing modes.
    Currently it assumes the actual screen size/resolution never change,
    so it will go squirrely if the screen is resized."""
    global L_SWITCH_TO_2D, L_SWITCH_TO_3D
    if two_d:
        glCallList(L_SWITCH_TO_2D)
    else:
        glCallList(L_SWITCH_TO_3D)

(Then you switch to 2D mode and back during every run of the drawing loop.)



Kris
Who'd like to be corrected.