Pages

Se afișează postările cu eticheta pyopengl. Afișați toate postările
Se afișează postările cu eticheta pyopengl. Afișați toate postările

luni, 2 septembrie 2013

PyGame : First interface - part 5.

Today I worked with pygame and OpenGL python modules.
I want to make a simple script to test and load pygame , OpenGL modules.
This is the script and give me also in vars some OpenGL features.
If modules and features can be loaded then return True or False in some vars.
import sys

try:
    import OpenGL
    from OpenGL.GL import *
    bOpenGL=True
except:
    bOpenGL=False
   
try:
    from OpenGL.GLU import *
    bGLU=True
except:
    bGLU=False

try:
    from OpenGL.GLUT import *
    bGLUT=True
except:
    bGLUT=False

try:
    from OpenGL.GL.EXT import *
    bEXT=True  
except:
    bEXT=False

try:
    from OpenGL.GL.ARB.shader_objects import *
    bshader_objects=True
except:
    bshader_objects=False
try:
    from OpenGL.GL.ARB.vertex_shader import *
    bvertex_shader=True
except:
    bvertex_shader=False
try:
    from OpenGL.GL.ARB.fragment_shader import *
    bfragment_shader=True
except:
    bfragment_shader=False
try:    
    from OpenGL.GL.ARB.multitexture import *
    bmultitexture=True
except:
    bmultitexture=False

if sys.version_info.major < 3:
    print "Python vers=",sys.version_info
    print "bOpenGL=", bOpenGL
    print "OpenGL vers=",OpenGL.__version__
    print "bGLU=", bGLU
    print "bGLUT=", bGLUT
    print "bEXT=", bEXT
    print "bshader_objects=", bshader_objects
    print "bvertex_shader=", bvertex_shader
    print "bfragment_shader=", bfragment_shader
    print "bmultitexture=", bmultitexture
else:
    print ("Python vers=",sys.version_info)
    print ("bOpenGL=", bOpenGL)
    print ("OpenGL vers=",OpenGL.__version__)
    print ("bGLU=", bGLU)
    print ("bGLUT=", bGLUT)
    print ("bEXT=", bEXT)
    print ("bshader_objects=", bshader_objects)
    print ("bvertex_shader=", bvertex_shader)
    print ("bfragment_shader=", bfragment_shader)
    print ("bmultitexture=", bmultitexture)
The output result is this :
$ python test.py 
Python vers= sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
bOpenGL= True
OpenGL vers= 3.0.1
bGLU= True
bGLUT= True
bEXT= True
bshader_objects= True
bvertex_shader= True
bfragment_shader= True
bmultitexture= True

marți, 12 octombrie 2010

PyGame : First interface - part 2

One thing necessary to create an interface and a game is using a "sprite system".
To illustrate this, I'll show you a sequence of source code:

import pygame
def must_quit():
    event = pygame.event.poll()
    return event.type == pygame.QUIT
screen = pygame.display.set_mode((640, 480))
SpriteImage = pygame.image.load('image.jpg')
while not must_quit():
    screen.blit(SpriteImage, (0, 0))
    pygame.display.flip()

It sounds simple but is not.
Why? Because when you work with multiple images when source code is more complicated.
For this we need a system and use the "classes".
Let's see:

class SpriteImage:
    def __init__(self, image_filename):
        self.image = pygame.image.load(image_filename)
    def paint(self):
        screen.blit(self.image, (0, 0))
screen = pygame.display.set_mode((640, 480))
sprite = SpriteImage('image.jpg')

This is a more simple way to use it.
Try learning more about pygame.
Good luck.

joi, 11 martie 2010

PyGame : OpenGL - part 2.

Today I will show you how we find stuff about graphics hardware.
We will use two modules: pyopengl and pygame.
Modules can import other modules.
Each module is only imported once per interpreter session.
>>> import pygame 
>>> import OpenGL 

The imports are taken in the local symbol table, so we need to do this:
>>> from pygame import *
>>> from OpenGL import *
>>> from OpenGL.GL import * 
>>> from OpenGL.GLUT import * 

The next thing you must do is call the function glutInit.
>>> glutInit()
['foo']

Use pygame init function to initialize the window:
>>> pygame.init()
(6, 0)
>>> pygame.display.set_mode((10,10),OPENGL|DOUBLEBUF)


Note:If we not create the window the next commands will not work.
About the beautiful glGetString function, we know.
This return a string describing the current GL connection.
You can use dir() and help() functions to see more.
But we see bellow, some example:
>>> dir(glGetString)
['__call__', '__class__', '__ctypes_from_outparam__', '__delattr__',
 '__dict__','__doc__', '__format__', '__getattribute__', '__hash__',
 '__init__', '__module__',
 '__name__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', 
'__subclasshook__',
 '__weakref__', '_b_base_', '_b_needsfree_', '_flags_', '_objects', 
'_restype_', 'argtypes', 'errcheck', 'restype']
>>> dir(glGetString())
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', 
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Now, the parameters of this function are :
glGetString(GL_VERSION)
glGetString(GL_VENDOR)
glGetString(GL_RENDERER)
glGetStringi(GL_EXTENSIONS, i) 

Bellow, we see how we use these parameters:
>>> glGetString(GL_VERSION) 
'2.1.2 NVIDIA 173.14.22'
>>> glGetString(GL_VENDOR) 
'NVIDIA Corporation'
>>> glGetString(GL_RENDERER) 
'GeForce FX 5200/AGP/SSE/3DNOW!'

Because the GL_EXTENSIONS is a space-separated list of supported extensions to GL, we can use these commands:
>>> print glGetString(GL_EXTENSIONS)

Or you can format this result, see below
>>> print glGetString(GL_EXTENSIONS).split()
['GL_ARB_depth_texture', 'GL_ARB_fragment_program', ...

This is all.

sâmbătă, 28 martie 2009

PyGame : OpenGL - part 1

OpenGL is an application programming interface.
The PyOpenGL modules have many functions and several matrices for working with 3D graphics. This is one example of how to use PyOpenGL.
I've successfully tested it under Linux.

import os
import sys
import pygame
from pygame import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

def init_display(w, h):
 pygame.display.set_mode( (w,h) , pygame.OPENGL | pygame.DOUBLEBUF )
 glViewport(0, 0, w, h)
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

 glEnable(GL_LIGHTING)

 glEnable(GL_LIGHT0)        

 glLight(GL_LIGHT0, GL_POSITION,  (0, 0, 1, 0))    
 glMatrixMode(GL_PROJECTION);

 glLoadIdentity();

 gluOrtho2D(-1, 1, -1, 1);

 glMatrixMode(GL_MODELVIEW);

def draw():
 glClearColor(0.0, 0.0, 0.0, 1.0)
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
 glColor(0,1,0)
 glLight(GL_LIGHT0, GL_POSITION,  (1, 1, 1, 0))
 glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0))    

 glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))
 glutSolidTorus(0.1,0.5,24,24)
 pygame.display.flip()

def main():
 pygame.init()
 glutInit()
 init_display(800, 600)
 while 1:
  event=pygame.event.poll ()
  draw()
  if event.type is KEYDOWN:
   if event.key is K_ESCAPE:
    sys.exit(0)
main()