15

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Tom
  • 43,810
  • 29
  • 138
  • 169
  • 2
    its not whether Python is mature enough, its just an issue of computer performance, computers aren't to the performance level yet to where they can afford to waste the extra cycles that python uses compared to c++, this will change, however, at some point as it inevitably does (with computer speeds increasing year after year).. a language like c++ would always be used, I think, for cutting edge science, etc where they are attempting to use every last bit of computing resource but for most applications higher level languages would eventually take over, as they have throughout history so far – Rick Aug 18 '10 at 06:03

5 Answers5

29

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
  • 6,115
  • 1
  • 35
  • 41
7

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
  • 17,011
  • 9
  • 48
  • 52
  • 1
    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 Aug 18 '10 at 06:00
  • >> of course it won't be faster in terms of running on the machine – Jonathan Hartley Nov 12 '10 at 13:39
  • 2
    I've done a large octtree in Python as a series of Numpy operations. Numpy is crazy insane, I've gotten used to the idea that stuff that makes me cringe at the mere thought of attempting it in C can be done in a couple of lines of Python. Numpy does the same thing for big data crunching. – Gordon Wrigley May 01 '11 at 11:57
7

Python is the way to go. Since all opengl programming is uploading data to the video card RAM, then using opengl to operate on it, the speed limitations in python are moot. Also it makes the hard things in C++ easy ie opening files, images, sounds etc.

As for the person above implementing octrees, there is nothing stopping you from using numpy, which is written in C, from implementing it. (also make sure you are using linear memory like a binary tree, and not pointers to objects in a link like structure)

Blog post on this subject

Bill
  • 31
  • 1
  • 1
2

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
  • 37,799
  • 10
  • 82
  • 91
0

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
  • 30,292
  • 15
  • 80
  • 129
  • 3
    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? – Eric O. Lebigot Mar 22 '10 at 08:37
  • Actually I never did multi-threaded development in Python but I saw a lot of posts against it – Dror Helper Mar 22 '10 at 08:54