OpenGL Spinning Triangle With GLFW
From IronPython Cookbook
This example demonstrates how to use Tao with the GLFW binding to initialize an OpenGL context and render a spinning triangle. This example has been adapted from the Tao example which lies under a MIT license.
This has been tested with:
- IronPython 2.0Alpha8 (the original distribution)
- mono 1.2.6 under Ubuntu
- tao 2.0.0 (binary distribution deployed in the gac)
- GLFW 2.6
# -*- coding: utf-8 -*-
import clr
clr.AddReference('Tao.Glfw')
clr.AddReference('Tao.OpenGl')
from Tao.Glfw import Glfw
from Tao.OpenGl import Gl, Glu
def run():
Glfw.glfwInit()
if not Glfw.glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, Glfw.GLFW_WINDOW):
Glfw.glfwTerminate()
return 2
Glfw.glfwEnable(Glfw.GLFW_STICKY_KEYS)
Glfw.glfwSwapInterval(0)
running = True
frame_count = 0
start_time = Glfw.glfwGetTime()
while running:
current_time = Glfw.glfwGetTime()
coordinates = Glfw.glfwGetMousePos()
#Calculate and display FPS (frames per second)
if (current_time - start_time) > 1 or frame_count == 0:
frame_rate = frame_count / (current_time - start_time);
Glfw.glfwSetWindowTitle("Spinning Triangle (%d FPS)" % frame_rate)
start_time = current_time
frame_count = 0
frame_count = frame_count + 1
window_dim = Glfw.glfwGetWindowSize()
if window_dim[1] > 0:
height = window_dim[1]
else:
height = 1
# Set viewport
Gl.glViewport(0, 0, window_dim[0], height)
# Clear color buffer
Gl.glClearColor(0, 0, 0, 0)
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT)
# Select and setup the projection matrix
Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()
Glu.gluPerspective(65, window_dim[0]/height, 1, 100)
# Select and setup the modelview matrix
Gl.glMatrixMode(Gl.GL_MODELVIEW)
Gl.glLoadIdentity()
Glu.gluLookAt(0, 1, 0, 0, 20, 0, 0, 0, 1)
# Draw a rotating colorful triangle
Gl.glTranslatef(0, 14, 0)
Gl.glRotatef(1/3 * float(coordinates[0]) + float(current_time) * 100,
0, 0, 1)
Gl.glBegin(Gl.GL_TRIANGLES)
Gl.glColor3f(1, 0, 0)
Gl.glVertex3f(-5, 0, -4)
Gl.glColor3f(0, 1, 0)
Gl.glVertex3f(5, 0, -4)
Gl.glColor3f(0, 0, 1)
Gl.glVertex3f(0, 0, 6)
Gl.glEnd();
Glfw.glfwSwapBuffers()
running = ((Glfw.glfwGetKey(Glfw.GLFW_KEY_ESC) == Glfw.GLFW_RELEASE) and
Glfw.glfwGetWindowParam(Glfw.GLFW_OPENED) == Gl.GL_TRUE)
Glfw.glfwTerminate()
if __name__ == '__main__':
run()
Back to Contents.


