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, 21 mai 2012

Python , OpenGL and cube map OpenGL ARB

Today I will show a simple script that uses the OpenGL ARB "Architecture Review Board". The result is show in the next image.
I use just one image file named 111.JPG for all cube images. Let's see the python script.

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GL.ARB.texture_cube_map import *
import Image                
import sys
#after I import all module , I set some vars
angle    = 0.1
texture  = 0
#this var set the type of reflection
reflMode = GL_REFLECTION_MAP_ARB
#this load the texture 
def loadTexture ( fileName ):
    image  = Image.open ( fileName )
    width  = image.size [0]
    height = image.size [1]
    image  = image.tostring ( "raw", "RGBX", 0, -1 )
    
    texture = glGenTextures ( 1 )
    glBindTexture     ( GL_TEXTURE_2D, texture )   
    glPixelStorei     ( GL_UNPACK_ALIGNMENT,1 )
    glTexParameterf   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
    glTexParameterf   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
    glTexParameteri   ( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR )
    glTexParameteri   ( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR )
    gluBuild2DMipmaps ( GL_TEXTURE_2D, 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image )
    
    return texture
#this load a cube map of six images
def loadCubemap ( faces, path = ""  ):
    texture = glGenTextures ( 1 )
    target_map  = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
    glBindTexture     ( GL_TEXTURE_CUBE_MAP_ARB, texture )  
    glPixelStorei     ( GL_PACK_ALIGNMENT,1 )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,     GL_REPEAT )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,     GL_REPEAT )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
    glTexParameteri   ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
    glEnable          ( GL_TEXTURE_CUBE_MAP_ARB )
       
    for face in faces:
        if path != "":
            file = path + "/" + f
        else:
            file = face
        image  = Image.open ( file )
        width  = image.size [0]
        height = image.size [1]
        image  = image.tostring ( "raw", "RGBX", 0, -1 )
        gluBuild2DMipmaps ( target_map, 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image )
        target_map = target_map + 1
    return texture
#this check if is working OpenGL ARB extension 
def extensionInit ():
    if not glInitTextureCubeMapARB ():
        print "ARB_texture_cubemap not working !"
        sys.exit ( 1 )
#this is default init of opengl 
def init ():
    glClearColor ( 1.0, 1.0, 1.0, 0.0 )
    glClearDepth ( 1.0 )                
    glDepthFunc  ( GL_LEQUAL )
    glEnable     ( GL_DEPTH_TEST )
    glHint       ( GL_POLYGON_SMOOTH_HINT,         GL_NICEST )
    glHint       ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST )
#this help us when resize the window
def reshape ( width, height ):
    glViewport     ( 0, 0, width, height )
    glMatrixMode   ( GL_PROJECTION )
    glLoadIdentity ()
    gluPerspective ( 55.0, float(width)/float (height), 1.0, 60.0 )
    glMatrixMode   ( GL_MODELVIEW )
    glLoadIdentity ()
    gluLookAt      ( 0.0, 6.0, 0.0, 4.0, -4.0, 4.0, 0.0, 0.5, 0.0 )

def display ():
    global texture, reflMode
    
    glClear   ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
    glEnable  ( GL_TEXTURE_CUBE_MAP_ARB )
    glEnable  ( GL_TEXTURE_GEN_S )
    glEnable  ( GL_TEXTURE_GEN_T )
    glEnable  ( GL_TEXTURE_GEN_R )
    glTexGeni ( GL_S, GL_TEXTURE_GEN_MODE, reflMode )
    glTexGeni ( GL_T, GL_TEXTURE_GEN_MODE, reflMode )
    glTexGeni ( GL_R, GL_TEXTURE_GEN_MODE, reflMode )
    
    glBindTexture   ( GL_TEXTURE_CUBE_MAP_ARB, texture )
    glPushMatrix    ()
    glTranslatef    ( 2, 2, 2 )
    glRotatef       ( angle,  0, 10, 0 )
    
    glutSolidTeapot ( 1.5 )
    
    glPopMatrix     ()
    glutSwapBuffers ()
#this test the keyboard
def keyPressed ( *args ):
    global reflMode
    
    key = args [0]
    if key == '\033':
        sys.exit ()
    elif key == 'n' or key == 'N':
        reflMode = GL_NORMAL_MAP_ARB
    elif key == 'r' or key == 'R':
        reflMode = GL_REFLECTION_MAP_ARB
#this will animate , is a idle function
def animate ():
    global angle
    
    angle  = 0.01 * glutGet ( GLUT_ELAPSED_TIME )
   
    glutPostRedisplay ()
#... and the main function
def main ():
    global texture
    
    glutInit               ( sys.argv )
    glutInitDisplayMode    ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH )
    glutInitWindowSize     ( 600, 400 )
    glutInitWindowPosition ( 0, 0 )
    
    glutCreateWindow ( "ARB_texture_cubemap demo" )
    glutDisplayFunc  ( display )
    glutIdleFunc     ( animate )
    glutReshapeFunc  ( reshape )
    glutKeyboardFunc ( keyPressed )

    init          ()
    extensionInit ();
    
    texture = loadCubemap ( ( "111.JPG", "111.JPG", "111.JPG", "111.JPG", "111.JPG", "111.JPG" ), "" )
    print texture
    glutMainLoop()

print "Hit ESC key to quit."
main()

vineri, 13 august 2010

OpenGL and Python - GLSL example

Today I played a bit with GLSL.
Here is the final result in the image below:

The source code used by me:

vertex shader
varying vec3 normal;
void main() {
  normal = gl_NormalMatrix * gl_Normal;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 }
fragment shader
varying vec3 normal;
void main() {
 float scale = 1.0 / 10.0;
 float frx = fract(gl_FragCoord.x * scale);
 float fry = fract(gl_FragCoord.y * scale);
 gl_FragColor = vec4(frx,fry,0.0,1.0);
 }

joi, 12 august 2010

PyOpenGL - first lines of code.

What is the best way to write the first line of code on pyopengl ?
Please see below :
Python 2.6.4 (r264:75706, Jun  4 2010, 18:20:16) 
[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from OpenGL import platform
>>> dir(platform)
['CurrentContextIsValid', 'GL', 'GLE', 'GLU', 'GLUT', 'GLUT_GUARD_CALLBACKS', 'GetCurrentContext',
 'OpenGL', 'PLATFORM', 'PlatformPlugin', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
  '__path__', '_load', 'baseplatform', 'copyBaseFunction', 'createBaseFunction', 'createExtensionFunction',
   'ctypesloader', 'getGLUTFontPointer', 'glx', 'os', 'safeGetError', 'sys']
>>> gl=platform.OpenGL
>>> print gl

>>> glCreateShaderObjectARB = gl.glCreateShaderObjectARB
>>> glShaderSourceARB = gl.glShaderSourceARB
As we can see, is simple to start it.
Just use gl.function_opengl.
You can see more examples here.

marți, 16 martie 2010

OpenGL and Python - Motion blur

What is glAccum? The OpenGL function - glAccum operate on the accumulation buffer.
This function provides support for many special effects.
Today I simulated the effect of motion blur with this function.
See the picture below:

duminică, 14 martie 2010

OpenGL and Python - Fog

Today I spent my time with something new - the OpenGL fog effect.
I made one simple fog effect using the last code source.
The result is this image:

marți, 9 martie 2010

OpenGL and Python - Glut

The pyopengl python module showing now the true power of OpenGL.
The python code is simple and this allows us to see the OpenGL teapot object.
The image below, it's a screenshot with this source code.

That is one of my projects about pyopengl.
The python code I used it is simple and it has about 150 rows with some features.