analitics

Pages

Friday, August 12, 2011

GLUT - Creating a fullscreen applications.

To create a fullscreen application must use the function:
glutGameModeString()
glutEnterGameMode()

Below is the source commented.
import sys
from OpenGL.GL import *
from OpenGL.GLUT import *

def main():
 
    # Initialize OpenGL
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
 
    # Configure the display output
    glutGameModeString("1024x600:32@60")

    # The application will enter fullscreen
    glutEnterGameMode()
 
    # Setup callbacks for keyboard and display
    glutKeyboardFunc(keyboard)
    glutDisplayFunc(display)
 
    # Enters the GLUT event processing loop
    glutMainLoop()
 
def keyboard(key, x, y):
    if key == 'q':
        sys.exit(0)

def display():
    glClear(GL_COLOR_BUFFER_BIT)

    # Draw a green line
    glBegin(GL_LINES)
    glColor3f(0.0,100.0,0.0)
    glVertex2f(1.0, 1.0)
    glVertex2f(-1.0, -1.0)
    glEnd()
 
    glutSwapBuffers()
 
if __name__ == "__main__":
    main()

Thursday, August 11, 2011

How to make working pygtk with python 2.7

As you know, PyGTK is a module that allows us to use the GTK interface.
A good start to use this module:
Articles
I do not know if I presented this module on the site, but now I will try to present it.
First, as it says on the official site:
"PyGTK lets you easily create applications with a graphical user interface Using the Python programming language."
The second time is easy to use because we can easily create interfaces using Glade.
Glade is a RAD tool.
This development tool to let us create user interfaces for the GTK + toolkit and the GNOME desktop environment.
The output is an xml file which is then used by PyGTK.
Read more on the official site: Glade RAD tool.
The tricky part is connecting with the python source code from the XML components.
You can download it here and install version 2.22 on Windows from here.
I use pygtk-all-in-one-2.22.6.win32-py2.7.msi to test pygtk.
Also, you need GTK 2.22 from here.
How to make working Python 2.7 with GTK?
Just right-click on My Computer, then select Properties.
Click the Advanced tab in the dialog that pops up, and click the Environment Variables button near the bottom.
Under System variables, click the New button and enter GTK_BASEPATH.
Now under variable named Path, and click the Edit button and add GTK_BASEPATH\bin;.
Click the OK buttons restart Windows XP.
Now it is working fine with Python 2.7.
Let's try this script:
#!/usr/bin/env python
import sys

try:
    import pygtk
    pygtk.require("2.0")
    print "pygtk 2.0 ok!"
except:
    print "pygtk 2.0 BAD!"
    pass
    
try:
    import gtk
    print "gtk ok!"
except:
    print "gtk BAD!"
    pass

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.