views:

339

answers:

4

Hi everyone.

I'm starting a computer graphics course, and I have to choose a language.

Choices are between C++ and Python. I have no problem with C++, python is a work in progress. So i was thinking to go down the python road, using pyopengl for graphics part.

I have heard though, that performance is an issue.

Is python / pyopengl mature enough to challenge C++ on performance?

I realize its a long shot, but I'd like to hear your thoughts, experiences on uses of pyopengl.

Thanks in advance.

A: 

Python is a dynamic language that get interpreted and compiled on runtime and as such cannot have better performance then C++ - take a look at this post for comparison between several programming languages.

Another good reason to prefer C++ is parallel execution. Many tasks in CG can be optimized by splitting them to multiple threads tgat run in parallel - ever tried to start a new thread using Python?

Dror Helper
Could you make precise what you imply with "ever tried to start a new thread using Python"? Threads are very easy to start in Python. :) Did you mean that CPython's threads suffer from the Global Interpreter Lock?
EOL
Actually I never did multi-threaded development in Python but I saw a lot of posts against it
Dror Helper
+1  A: 

Python is an awesome language, but it's not the right tool for graphics. And if you want to do anything remotely advanced you'll have to use unpythonic libraries and will end up with ugly C code written in Python.

Max Shawabkeh
Thats exactly what a friend told me, same words, different language.
Tom
That's why you write your own libraries when it comes to doubt.
Xavier Ho
+7  A: 

It depends a LOT on the contents of your computer graphics course. If you are doing anything like the introductory course I've taught in the past, it's basically spinning cubes and spheres, some texture mapping and some vertex animation, and that's about it. In this case, Python would be perfectly adequate, assuming you can get around the Unpythonic (and, lets be honest, un-C++) OpenGL state-machine paradigm.

For things like doing your matrix maths you can use Numpy, the core of which is written in C and is really quite quick. You'll be up and running faster, iterate faster and most likely have more fun.

If, however, you are doing some hardcore, cutting edge, millions-of-triangles-per-scene-skinned-animated-everything computer graphics course, stick with C++.

If your class has given you the choice it's probably a safe bet that Python will be ok.

If you want to leverage your knowledge into a real job in computer graphics though, pretty much every game and graphics engine is written in C or C++, while Python (or Lua) is left as a scripting language.

kibibu
Thanks, i appreciate the real world computer graphics paragraph
Tom
Can't explain how precisely correct I think this answer is.
phkahler
@phkahler - what do you mean?
kibibu
@kibibu: I think phkahler means you're so very, very correct, that he couldn't explain it in words. ;]
Xavier Ho
phkahler
+2  A: 

Here's my personal experience:

When I first heard about PyOpenGL, I was absolutely thrilled. OpenGL in my favourite language? Deal! So I started learning 3D graphics programming by myself.

I went through several tutorials and books like NeHe and the OpenGL SuperBible. Because PyOpenGL's functions are identical to that of OpenGL itself's (with very minor differences), it wasn't hard to replicate most of the examples. Besides, NeHe has many source code in Python that others made.

It wasn't too long after (about 2 weeks) I read up on Quaternions and implemented in Python myself. Now I have a GLSL-enabled environment with full 3D camera interaction options. I made a simple Phong shader, and used Quaternions to drive my camera rotations. I haven't got a single performance hit, yet.

Months later, I came back to this code.

I attempted a Python Octree implementation, and when I went to 8 levels (256x256x256 voxels), it took more than 2G of RAM to compute and minutes after, it still isn't done. I realised when you store many objects in Python, it's not just a simple struct like in C++. That's where I realised I need to factor this out, write this in C++, and then glue it back with a Python call.

Once I'm done with this, if I remember, I will update you. ;]

(To answer your question, no, Python will never replace C++. Those two lanaguages have different purposes, and different strengths.)

Xavier Ho
As computer performance increases, a dynamic language can potentially become useful eventually even for high performance games, computer power should always be viewed as less valuable than programmers, when computers advance enough it would make sense to use Python for things that now are only able to be done in c++, of course it won't be faster in terms of running on the machine but the development time would be.. there would be a paradigm shift so only things that not even possible now would be done in c++ and everything done now could be done in a language like Python
Rick