Pages

Se afișează postările cu eticheta glut. Afișați toate postările
Se afișează postările cu eticheta glut. 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()

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.