analitics

Pages

Showing posts with label teapot. Show all posts
Showing posts with label teapot. Show all posts

Thursday, August 11, 2011

GLUT - teapot

Using GLUT library is pretty simple if you know the operating mechanism.
GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so you can write a single OpenGL program that works across all PC and workstation OS platforms.
To work you need to install Python (I used version 2.7) and how PyOpenGL. You'll find it here.
The python module PyOpenGL provides bindings to OpenGL, GLUT, and GLE.
The necessary steps are:
1. initialize the GLUT library :glutInit function
2. sets the initial display mode with parameters set GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH
3. initialization of the future windows with a given width/heightat a given width/height
4. Then the creation of four functions required to display, resizing, keyboard and display.These functions will be called in order to: glutCreateWindow, glutReshapeFunc, glutKeyboardFunc, glutDisplayFunc.
5. calling these functions.
6. calling glutMainLoop to enter the GLUT event processing loop.
Here's source code.
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *


def display():
    glEnable( GL_LIGHTING )
    glEnable( GL_LIGHT0 )
    glEnable( GL_DEPTH_TEST )

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
    glPushMatrix()
    gluLookAt( 0,2.5,-7, 0,0,0, 0,1,0 )
    glutSolidTeapot( 2.5 )
    glPopMatrix()
    glFlush()

def reshape(w,h):
    glViewport( 0, 0, w, h )
    glMatrixMode( GL_PROJECTION )
    glLoadIdentity()
    gluPerspective( 45.0, 1.0*w/h, 0.1, 100.0 )
    glMatrixMode( GL_MODELVIEW )
    glLoadIdentity()

def keyboard(key,x,y):
    if key==chr(27): sys.exit(0)

glutInit( sys.argv )
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH )
glutInitWindowSize( 500, 400)
glutCreateWindow( 'teapot' )
glutReshapeFunc( reshape )
glutKeyboardFunc( keyboard )
glutDisplayFunc( display )
glutMainLoop()
Here's the result.
You can read more in the documentation.