
-----------------------------------
Insectoid
Sun Apr 29, 2012 2:14 pm

Python and openGL and fractals, oh my!
-----------------------------------
I built this basic fractal heightmap generator/renderer in Python with openGL using the Pyglet library. It works, but very slowly. 

As it happens, I know very little about Python, openGL, or fractals. Consequently, the code for it is not very python-like, and the openGL draws are very, very slow, and the generator is pretty slow too. In order to speed up the draws, I tried to move all my vertices into a Pyglet VertexList but couldn't quite get that to work. At the moment, I generate a 2D list of color data and where each list element is one pixel. I then iterate over this list and draw each point. This is my bottleneck, I believe. A VertexList is supposed to run a lot faster since it sits in video RAM and that's my objective (unless there's a better way, like drawing everything to a buffer initially and then flipping that buffer to the screen, then never updating the buffer). 

You'll need Pyglet installed to run this. Also, Pyglet on Python 2.6+ is broken on OS X 10.6, so mac users will need to run with Python2.5.


#!/usr/bin/python2.5
import random
import pyglet
from pyglet.gl import *

#This function generates a 3D heightmap
def fractal3D (_initial_range, passes, _roughness, size):
	heightMap = 

Any little tweaks to turn this into Python code (rather than C code with Python syntax, which is pretty much what this is) or to improve efficiency would be appreciated.

-----------------------------------
DemonWasp
Mon Apr 30, 2012 1:37 pm

RE:Python and openGL and fractals, oh my!
-----------------------------------
The simplest way to improve efficiency is to batch your GL calls: right now you make two GL call per vertex, which is 2 * 256 * 256 = 131K calls per frame. There are calls where you can just pass an array (I'm not familiar with pyglet, so I don't know them offhand), which should let you make 2 calls per frame (ignoring glClear, glBegin, glEnd), assuming you set up your arrays correctly.
