[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Re: pygame + pyopengl



On Sun, Apr 08, 2001 at 12:19:40AM -0700, Kevin Turner wrote:
> What happens though is you just get a blank screen, no matter how many
> glBegin/End/Vertex/etc things you do.  None of the GL calls give you
> error messages, and I believe the GL hardware is kicking in (my screen
> does some funky things when any GL app starts), but there's a distinct
> lack of pretty pictures.

Woohoo!

You prompted me into trying this out for myself, and I have managed to
get a simple OpenGL demo working within pygame.  The tutorial in question
is on the SDL web site:

http://www.libsdl.org/opengl/intro.html

It starts with the simple display of a flat triangle and square using OpenGL
(lesson 2).  This is the one I've got working.  I made a simple conversion
of the C code to Python.  Find the python file attached.

I've attached the Python source code.  To get it working you have to add
one simple function to pygame.  This is very easy to do, so don't be scared
even if you don't know C.

The function has to go in src/display.c in pygame.  Put it somewhere near
the gltest1/gltest2 functions:

static PyObject* gl_swap_buffers(PyObject* self, PyObject* arg)
{
    if(!PyArg_ParseTuple(arg, ""))
        return NULL;

    VIDEO_INIT_CHECK();

    SDL_GL_SwapBuffers();

    RETURN_NONE
}

And add the following to the table of functions in the same file.
Again, search for testgl1 and testgl2 to find a place to put it:

{ "gl_swap_buffers", gl_swap_buffers, 1, NULL },

Looks like all that needs doing is to go through and add all the SDL_GL
functions to pygame.  SDL_GL_UpdateRects, SDL_GL_Lock and SDL_GL_Unlock look
like useful alternatives to SDL_GL_SwapBuffers, and the testgl1 and testgl2
need renaming to more permanent names.  And that's about all.

Francis 

-- 
Home: francis@flourish.org  Web: www.flourish.org
#!/usr/bin/python2

# Conversion of lesson 2 from the SDL OpenGL Tutorial to pygame.
# http://www.libsdl.org/opengl/intro.html

import pygame, pygame.image

# These are from pyopengl
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

pygame.init()

Width=640
Height=480
screen = pygame.display.set_mode((Width, Height), pygame.OPENGL)
pygame.display.set_caption("Jeff Molofee's GL Code Tutorial ... NeHe '99")

# This proves we've made an OpenGL surface OK
pygame.display.testgl2()

glViewport(0, 0, Width, Height)
glClearColor(0.0, 0.0, 0.0, 0.0)     # This Will Clear The Background Color To Black
glClearDepth(1.0)                # Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS)             # The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST)          # Enables Depth Testing
glShadeModel(GL_SMOOTH)          # Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION)
glLoadIdentity()             # Reset The Projection Matrix

gluPerspective(45.0,float(Width)/float(Height),0.1,100.0)

glMatrixMode(GL_MODELVIEW)

while 1 == 1:
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear The Screen And The Depth Buffer
	glLoadIdentity()             # Reset The View

	glTranslatef(-1.5,0.0,-6.0)       # Move Left 1.5 Units And Into The Screen 6.0

	# draw a triangle
	glBegin(GL_POLYGON)              # start drawing a polygon
	glVertex3f( 0.0, 1.0, 0.0)        # Top
	glVertex3f( 1.0,-1.0, 0.0)        # Bottom Right
	glVertex3f(-1.0,-1.0, 0.0)        # Bottom Left  
	glEnd()                  # we're done with the polygon

	glTranslatef(3.0,0.0,0.0)             # Move Right 3 Units

	# draw a square (quadrilateral)
	glBegin(GL_QUADS)                # start drawing a polygon (4 sided)
	glVertex3f(-1.0, 1.0, 0.0)        # Top Left
	glVertex3f( 1.0, 1.0, 0.0)        # Top Right
	glVertex3f( 1.0,-1.0, 0.0)        # Bottom Right
	glVertex3f(-1.0,-1.0, 0.0)        # Bottom Left  
	glEnd()                  # done with the polygon

	# swap buffers to display, since we're double buffered.
	# In C, this is SDL_GL_SwapBuffers()
	pygame.display.gl_swap_buffers()