pyopengl: Could it replace c++?

2019-02-12 03:08发布

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.

5条回答
啃猪蹄的小仙女
2楼-- · 2019-02-12 03:13

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.)

查看更多
乱世女痞
3楼-- · 2019-02-12 03:14

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?

查看更多
Animai°情兽
4楼-- · 2019-02-12 03:17

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

查看更多
别忘想泡老子
5楼-- · 2019-02-12 03:21

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.

查看更多
闹够了就滚
6楼-- · 2019-02-12 03:34

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.

查看更多
登录 后发表回答