Monday, January 26, 2015

OpenGl in Python

Info:
Everyone knows OpenGl lib. It's often in use with C. But you can also use it with python. Of course it's not the fastest possiblity, but you can do a lot of funny things together with python.

You need PyOpengl for doing this.
This package is free aviable under:https://pypi.python.org/pypi/PyOpenGL/3.0.2


A Little Example:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
window = 0
rtri = 0.0
rquad = 2.0
speed = 0.1
Wireframe = True

def InitGL(Width, Height):                
    glClearColor(0.3, 0.3, 0.3, 0.0)    
    glClearDepth(1.0)                   
    glDepthFunc(GL_LESS)                
    glEnable(GL_DEPTH_TEST)
    glPolygonMode(GL_FRONT, GL_LINE)    
    glPolygonMode(GL_BACK, GL_LINE)     
    glShadeModel(GL_SMOOTH)                

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()                    
                                        
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)

    glMatrixMode(GL_MODELVIEW)


def ReSizeGLScene(Width, Height):
    if Height == 0:                       
        Height = 1

    glViewport(0, 0, Width, Height)        
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)

def DrawGLScene():
    global rtri, rquad ,speed

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)    
    glLoadIdentity()
    glTranslatef(0.0,0.0,-5.0)        
    glRotatef(rquad,speed,speed,speed)
    glBegin(GL_QUADS)                 

    glColor3f(0.0,1.0,0.0)            
    glVertex3f( 1.0, 1.0,-1.0)        
    glVertex3f(-1.0, 1.0,-1.0)        
    glVertex3f(-1.0, 1.0, 1.0)        
    glVertex3f( 1.0, 1.0, 1.0)       

    glColor3f(1.0,0.5,0.0)           
    glVertex3f( 1.0,-1.0, 1.0)        
    glVertex3f(-1.0,-1.0, 1.0)        
    glVertex3f(-1.0,-1.0,-1.0)        
    glVertex3f( 1.0,-1.0,-1.0)        

    glColor3f(1.0,0.0,0.0)           
    glVertex3f( 1.0, 1.0, 1.0)        
    glVertex3f(-1.0, 1.0, 1.0)       
    glVertex3f(-1.0,-1.0, 1.0)       
    glVertex3f( 1.0,-1.0, 1.0)        

    glColor3f(1.0,1.0,0.0)            
    glVertex3f( 1.0,-1.0,-1.0)        
    glVertex3f(-1.0,-1.0,-1.0)        
    glVertex3f(-1.0, 1.0,-1.0)        
    glVertex3f( 1.0, 1.0,-1.0)       

    glColor3f(0.0,0.0,1.0)            
    glVertex3f(-1.0, 1.0, 1.0)        
    glVertex3f(-1.0, 1.0,-1.0)        
    glVertex3f(-1.0,-1.0,-1.0)       
    glVertex3f(-1.0,-1.0, 1.0)        

    glColor3f(1.0,1.0,1.0)            
    glVertex3f( 1.0, 1.0,-1.0)       
    glVertex3f( 1.0, 1.0, 1.0)        
    glVertex3f( 1.0,-1.0, 1.0)        
    glVertex3f( 1.0,-1.0,-1.0)        
    glEnd()                           
    rtri  = rtri + 0.2                  
    rquad = rquad - 0.15                 
    glutSwapBuffers()
def keyPressed(*args):
    global rquad
    if args[0]==b"x":
        global Wireframe
        if Wireframe==False:
            glPolygonMode(GL_FRONT, GL_LINE)    
            glPolygonMode(GL_BACK, GL_LINE)
            Wireframe=True
        elif Wireframe ==True:
            glPolygonMode(GL_FRONT, GL_FILL)
            glPolygonMode(GL_BACK, GL_FILL)
            Wireframe=False
        else:pass
    elif args[0]==b"\x1b":
        exit()
    elif args[0]==b"v":
        rquad=2
        print(rquad)

    print(args[0])

def main():
    global window
    glutInit()
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
    glutInitWindowSize(640, 480)
    glutInitWindowPosition(0, 0)
    window = glutCreateWindow(b"Cube")
    glutDisplayFunc(DrawGLScene)
    glutIdleFunc(DrawGLScene)
    glutReshapeFunc(ReSizeGLScene)
    glutKeyboardFunc(keyPressed)
    InitGL(640, 480)
    glutMainLoop()
print("Press any key to exit")
main()


No comments:

Post a Comment