Wednesday, June 29, 2011

Pushing and Poppin'

In OpenGL when you apply any type of transformation or apply any type of texture or property, it is applied globally to everything else below it.

So if I were to do this:

for(int i = 0; i < 5; i++)
     glColor3f(1.0,0.0,0.0) ;
     glutSolidTetrahedron (); // TETRAHEDRONS!!

We would expect to see 5 evenly spaced tetrahedrons(think pyramids) right? Of course since I'm asking you would expect its wrong, so I know you wouldn't whip out your visa and bet with me.

See the translation is applied to the entire world and it persists. Therefore instead of this:

^   ^   ^   ^   ^

You end up with this

^   ^      ^         ^            ^

How do we solve this? Simple you push and then pop before you make the translation! By pushing into a sort of matrix buffer you tell the compiler : Only apply the changes to things inside this matrix, nothing else. So if you do this instead:

for(int i = 0; i < 5; i++)

     glColor3f(1.0,0.0,0.0) ;

     glutSolidTetrahedron (); // TETRAHEDRONS!!

and with that you will get the expected result.

Wednesday, June 22, 2011



So I've been working on OpenGL on the iPhone, but I'm still kinda learning Objective C and the iOS libraries so its a bit slow going. This week I started up messing around on OpenGL on a PC. The first pictures is from a projection view and the second is from an orthographic view. Gonna use open GL to work on a small project of mine in my spare time.

I also know DirectX 10, but I just have more fun in OpenGL. I'm thinking of taking it to the next level and buying the OpenGL SuperBible( The Blue Book) and learning shaders a bit better. (Shaders are hard!)

No code samples today as I feel its rather basic and just a bunch of setup stuff unless you want to watch lots of arrays initialized like this:

GLfloat cube[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,-1.0,1.0},

I thought so.

Wednesday, June 15, 2011

Sleep Sort

Every so often a certain notorious image board (which shall not be named, lest it is summoned) pops out a gold nugget. I preset sleep sort:

function f() {
while [ -n "$1" ]
"$1" &

It works like this: Imagine you had a room full of people and you each hand them a number between let say 1 and 100. Whatever number that person gets, they wait for that amount of time in minutes and then they write their number down on a white board.

People with the highest numbers will wait for much longer times and people with small numbers will wait less. So the person who got 1, will only wait for one minute and write down their number, the person who got 2 has to wait longer than the person who got 1 to write their number and so on. There you have it sleep sort, instead of minutes it works in seconds.

It's an interesting and new idea on sorting. The problem with it is that it has a terrible worst case and best case. It takes as long as the highest number to sort. So if you wanted to sort some numbers such as 1, 2, 3, 78943893 it would take 78943893 seconds to sort 4 numbers.

This could be fixed by calling some kind of notify whenever a number was printed. This actually seems like a perfect application for a parallel processing. Have each thread sleep, but upon printing out a sorted value, notify all the threads which are still sleeping that they can proceed to process the next one in line with the least amount of sleep time remaining.

What is more fascinating about this is the simplicity of it all. I doubt we'll see this taking over as the default sorting procedure but it is a very humorous and elegant piece of code.

I've whipped up some quick Java code which should re-create this bash script but haven't tested it. Later on I plan to see if I can write it up as a parallel program with the fixes I've suggested.

public void f(int s)
public void main()
    String sortMe = "9 0 4 56 5 9 1 2 3 64 7 9";
    StringTokenizer t = new StringTokenizer(sortMe, " ");
    while (t. hasMoreTokens())


Wednesday, June 8, 2011

SideNote: Starcraft

Same as previous post I've been play starcraft too. More facts.
This graphic was designed by Lorena O'Neal, you can check out more of
her work here (

(Don't worry next post I go back to OpenGL...been procrastinating on it since its been so hot, and with no AC in my apartment its easier to play video games than it is to program...terrible excuse I know)

Side Note: WOW

So I've recently been play world of warcraft for the first time and its kinda of fun, but I think I'll be done with it once the 30 day trial is up.
Here are some interesting stats about it.
This graphic was designed by Lorena O'Neal, you can
check out more of her work here (

Wednesday, June 1, 2011

iPhone OpenGL ES

So I already know how to program on the iPhone for the most part since I took on a small project for work for a few months.

Today I decided to explore the functionality of the device a bit more by checking out OpenGL ES.

About 2 years ago I did a course on OpenGL, but I quickly realized that I wouldn't be able to just copy paste my old code and expect it to work. Things are a bit different in the iPhone setup (and the fact that my old code is written in C, the iPhone uses C++[oops I meant Objective-C])

After a bit (read: a lot) of googling, I was able to get a basic setup done. Its a shame that the default one apple provides starts the application off with using shaders, which even for someone who took a class on this 2 years ago was a bit tough. In the end though I was able to get a triangle drawn and rotate it. Pictures? I do think so!

Some of the code, there is more but these two functions are the heart of it all

The multicolored triangle

It's rotating*
*Note that  though in the last screenshot the triangle should have been rotating it rather looks like its just moving up. I'm not 100% sure why this is, but I think it might have something to do with my view projections. I'll be working on this again tomorrow and trying to get some better results.

Also 100 + followers. Yay!