analitics

Pages

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.


Sunday, August 7, 2011

Python errors: numpy vs. Numeric

Today I decided to clarify some of the error that we met and I found a solution.

I created a new series called: Python errors

The first error occurs in trying to run scripts that require Numeric module.

You can run python 2.7 with numpy. But the Numeric module is not available in python 2.7 .

Such a script will try to import module:

import Numeric

and the next step will be something like:


s = Numeric.zeros((N,N),Numeric.Float32)
...
s = Numeric.zeros((N,N),Numeric.Int32)

The first error is :

    import Numeric
ImportError: No module named Numeric

Now , if you change from Numeric in numpy then you got this error:

    ... = numpy.zeros( (N,N),numpy.Float32 )
AttributeError: 'module' object has no attribute 'Float32'
To working well, just use this :
numpy.zeros( (N,N),float)
numpy.zeros( (N,N), int)

Actually, should be replaced :

numpy.Float32

with

float

The same applies for Int32 .

Friday, August 5, 2011

Installing and using pygame module in Windows XP.

You must have one of these versions of python installed:

2.6 , 2.7 , 3.1 or 3.2

... 32 bits or 64 bits.

Take the version you need from here.

Just run the executable and it will automatically install the python.

You can check if it runs:

python
Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> from pygame import *

On the same site you can find other modules required. You can try them.

Sunday, June 5, 2011

An alternative for parsing - the pyparsing module

First download the package from here, unzip and run the installation command:
C:\pyparsing-1.5.5>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
copying pyparsing.py -> build\lib
running install_lib
copying build\lib\pyparsing.py -> C:\Python27\Lib\site-packages
byte-compiling C:\Python27\Lib\site-packages\pyparsing.py to pyparsing.pyc
running install_egg_info
Writing C:\Python27\Lib\site-packages\pyparsing-1.5.5-py2.7.egg-info
From the official site we find that:
The pyparsing module is an alternative approach to creating and executing simple grammars, vs. the traditional lex/yacc approach, or the use of regular expressions. The pyparsing module provides a library of classes that client code uses to construct the grammar directly in Python code.
The package comes with a number of examples that illustrate the flexibility of this module.
This file format is used by ICal on the Macintosh and in the Mozilla Calendar project. I think that can be imported into google calendar.

They can be found here.
Of these I particularly liked the example for parsing files with extension ics.

Thursday, April 21, 2011

Python and Merlin , using win32com module.

I remembered my days when I played with visual script.
Later I discovered python on linux and I realized that it's as good if not better.
Then I made ​​a script that display the character Merlin said something.
Today I'll try the same thing with python.
First you have to install python module called win32com.
For this we use installer from here.
It automatically installs python 2.6 folder, see picture below...
To begin to build script.
For this we need to read the MSDN documentation least, see here.
The first step required is to import module module ...
import win32com
from win32com import client
from win32com.client import constants
Access file .acs and declare a variable named vrajitor to use it.
agent = win32com.client.Dispatch("Agent.Control.2")
agent.Connected = -1
agent.Characters.Load("Merlin", "Merlin.acs")
vrajitor= agent.Characters("Merlin")
Now we can use its methods for animation and not only ...
vrajitor .LanguageID = 0x409
vrajitor .Show()
vrajitor .Play("Announce")
vrajitor .Speak("I like to use Python programming language!")
vrajitor .Speak("... and I'm really good. See!?")
vrajitor .Play("Congratulate")
print "Type some commands! (max 99 comands)"
print "Congratulate, Acknowledge, Blink, Confused, Decline, Explain"
for i in range(100):
 cmd=raw_input()
 print cmd
 vrajitor .Play(cmd)
Here's the result ...

merlin python from Catalin Festila on Vimeo.