pyopengl: Could it replace c++? [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
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.

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.

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

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

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.

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?

Related

Making a 2d game in Python - which library should be used? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking to make a 2d side scrolling game in Python, however I'm not sure what library to use. I know of PyGame (Hasn't been updated in 3 years), Pyglet, and PyOpenGL. My problem is I can't find any actually shipped games that were made with Python, let alone these libraries - so I don't know how well they perform in real world situations, or even if any of them are suitable for use in an actual game and not a competition.
Can anyone please shed some light on these libraries? Is PyGame still used effectively? Is Pyglet worth while? Have either of them been used to make a game? Are there any other libraries I'm forgetting?
Honestly I'm not even sure I want to use Python, it seems too slow, unproven (For games written solely in Python), etc.... I have not found any game that was made primarily in Python, for sure, that has been sold. If I don't end up going with Python, what would a second/better choice be? Java? LUA?
Pygame is just a wrapper for SDL, so technically a lot of games have been made using it, most of the work is done C-side anyways. It's a little slow, though if you are doing a 2D game, it should be sufficient. Frets-on-Fire was written in pure python with PyGame, and FoFiX was written in python with C extensions, and those games are both pretty great, altough on slower computers FoFiX can have trouble running 4 player.
Pyglet is also pretty nice, though I've only used it for emulator development, drawing single layers, so I don't know how well it actually works for game development. I've found that it's much easier to use direct OpenGL with Pyglet than it is with SDL/PyGame, so if you are doing a 3D game, it's probably better than using SDL. Also, since it's written in python, it's very easy to extend classes to change how a function is handled. For instance, I was trying to load data into an openGL texture through the pyglet Texture class, but the class couldn't load the data in a format I wanted, so I extended that class and changed the function that got the texture type (comes from a string) to include the texture I wanted, quick and painless.
One library I don't see mentioned enough is SFML. The 2.0 API (currently in beta) is pretty stellar and simple to use, and, if I remember correctly, runs a little faster than SDL. It is also more "object oriented" than SDL, which is really nice if you are working in an OO language like Python. It has some nice Python bindings written by Bastien Leonard in Cython that are pretty easy to use (or you can use the old C bindings if you don't want to use the most up to date version). I used this a while back and it was quite an enjoyable experience.
The reason why there aren't a lot of shipped games using Python is pretty simple: You can't close Python source. Even if you "compile" python into an executable using Py2Exe or something, it really just packs up the interpreter and python source into an exe file, and the source can be got just by opening up the exe in a hex editor.
While I'm a supporter of the "write it in Python, write time critical parts in C", if you feel python is going to be too slow, I would probably suggest C or, preferably, C++ as the way to go, simply because you don't get that much of a usability advantage for writing in Java, and the C languages can be quite a bit faster running than Java, and Lua will just take away some of the usability of Python without giving you any noticeable performance enhancement. SFML and SDL can both be used straight from C(++).
Advice if you do decide to use python: Use it as it is supposed to be used, as a scripting language. Do not try to bit twiddle or load images in pure python, either use the interfaces available in the library you choose (they all have very good interfaces for those kinds of things) or write an extension module to do that for you. Use python for logic control and fetching data from your libraries. If you follow that simple rule, you really shouldn't run into any performance issues.
In my experience, the things that are really going to kill you aren't generally related to graphics at all. Sure, it's a challenge to get all your frame rendering done in less than 16 ms without techniques like dirty-rectangle updating, but that's not going to be the big thing you'll lose sleep over. The biggest performance considerations are going to come from much more elementary things - collision detection, spatial searching, and pathfinding are three of the big ones - that can easily send your logic frame rate into the floor if done inefficiently over even a few hundred objects. For problems like these, pure Python may not be the best choice, but it's not hard to offload these to C/C++ while retaining Python for higher-level logic. This is essentially what games like Civilization 4 and EVE Online (client and server!) do - offload performance-critical code to C++ while retaining the flexibility and simplicity of Python for 'everyday' tasks.
pygame is actively developed (they haven't had a stable release in a while, but the Mercurial repository is quite lively) and fairly popular. It also uses the well-regarded (C-based!) SDL library under the hood. pyglet is also actively developed and uses OpenGL for all of its rendering, making moving to 3D development easier if you're planning to go in that direction (since you'll already have most of the basics).
If you decide to go the not-Python route, you can use SDL directly with C/C++, along with addon libraries like SDL_gfx (for primitives), SDL_ttf (for text), and SDL_net (for basic networking). Another excellent and widely used alternative for C/C++ is Allegro. I've used both, and I will claim that it's largely a matter of taste which one you go with.
Pygame should suffice for what you want to do. Pygame is stable and if you look around the websites you will find games which have been coded in pygame. What type of game are you looking to implement?

Coming from C, how should I learn Python? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I've got a good grasp on C, my first programming language. I know a reasonable number of tricks and techniques and have written quite a few programs, mostly for scientific stuff. Now I'd like to branch out and understand OOP, and Python seems like a good direction to go.
I've seen several questions on how to learn Python, but most of them were from folks who were looking to start programming for the first time. I don't need a tutorial that will tell me what a string is, but I do need one that can tell me how to make a string in Python. Any help on some good sources to look through? Bonus points if the source is free :)
I knew C before I knew Python. No offence intended, but I don't think that your C knowledge is that big a deal. Unless you read very, very slowly, just set out to learn Python. It won't take that long to skim through the material you're familiar with, and it's not as if a Python tutorial aimed at C programmers will make you a better Python programmer - it might teach you things in a different order, is all, and raise some specific things that you would do in C but that you should not do in Python.
Strings in Python actually are somewhat different from strings in C, and they're used differently. I strongly recommend learning them "from scratch", rather than thinking about them in terms of their differences from C strings. For one thing, in Python 2 it's best not to use Python's "string" class to represent strings: there's a separate unicode string class and for practical Python apps (pretty much anything involving user data), you need that. (Python 3 fixes this, making the str class a unicode string). You need to establish a good working practice for unicode/byte data and decode/encode.
A common mistake when learning a second programming language, is to think "I know how to program, I just need to translate what I do in C into Python". No, you don't. While it's true that an algorithm can be basically the same in different languages, the natural way to do a particular thing can be completely different in different languages. You will write better Python code if you learn to use Python idiomatically, than if you try to write Python like a C programmer. Many of the "tricks" you know that make sense in C will be either pointless or counter-productive in Python. Conversely many things that you should do happily in a typical Python program, like allocating and freeing a lot of memory, are things that in C you've probably learned to think twice about. Partly because the typical C program has different restrictions from the typical Python program, and partly because you just have to write more code and think harder to get that kind of thing right in C than you do in Python.
If you're learning the language because you urgently need to program a system/platform which has Python but doesn't have C, then writing Python programs that work like C programs is a reasonable interim measure. But that probably doesn't apply to you, and even if it did it's not the ultimate goal.
One thing you might be interested to look at because of your C experience, is the Python/C API. Python is great for many things, but it doesn't result in the fastest possible computational core of scientific apps [neither does C, probably, but let's not go into FORTRAN for now ;-)]. So if you're aiming to continue with scientific programming through your move in Python, and your programs are typically memory-bus- and CPU-bound doing immense amounts of number-crunching (billions of ops), then you might like to know how to escape into C if you ever need to. Consider it a last resort, though.
You do need to understand Python reasonably well before the Python/C API makes much sense, though.
Oh yes, and if you want to understand OOP in general, remember later on to take a look at something like Java, Objective-C, C++, or D. Python isn't just an OO language, it's a dynamic OO language. You might not realise it from comparing just C with Python, but dynamic vs static types is a completely independent issue from the OOP-ness of Python. Python objects are like hashtables that allow you to attach new fields willy-nilly, but objects in many other OO languages store data in ways which are much more like a C struct.
I learned everything I know about Python from the official documentation: http://docs.python.org/
And it's free.
dive into python is a good place to start
fire up an interpreter, IPython is even better than the plain Python interpreter
use dir() and help() to poke around
and don't forget to read through the official docs at least once
I'd recommend the book How to Think Like a Computer Scientist in Python. It really helped me get going in Python (now my favorite language) coming from Java, C and C++.
Diveintopython, official docs, "Learning python" by Mark Lutz(4th edition) is one of the best books.
As someone who has worked with Java for over 12 years, I found that picking up a problem and solving it in a new language is the best way to learn. I don't believe in reading - it wastes a huge amount of time, and you can easily end up reading for too long.
My advice is to find a problem and set off to solve it with Python. You will learn alot in the process.
Good luck
If you have a programming background, Python is pretty straightforward to pick up. The most onerous task is learning the libraries and idioms. The documentation at python.org is quite good and free. If you're doing number crunching you'll almost certainly want to become familiar with the numpy extension.

Is PyOpenGL a good place to start learning opengl programming?

I want to start learning OpenGL but I don't really want to have to learn another language to do it. I already am pretty proficient in python and enjoy the language. I just want to know how close it is to the regular api? Will I be able to pretty easily follow tutorials and books without too much trouble?
I know C++ gives better performance, but for just learning can I go wrong with PyOpenGL?
With the caveat that I have done very little OpenGL programming myself, I believe that for the purposes of learning, PyOpenGL is a good choice. The main reason is that PyOpenGL, like most other OpenGL wrappers, is just that: a thin wrapper around the OpenGL API.
One large benefit of PyOpenGL is that while in C you have to worry about calling the proper glVertex3{dfiX} command, Python allows you to just write glVertex3(x,y,z) without worrying about telling Python what type of argument you passed in. That might not sound like a big deal, but it's often much simpler to use Python's duck-typing instead of being overly concerned with static typing.
The OpenGL methods are almost completely all wrapped into Python methods, so while you'll be writing in Python, the algorithms and method calls you'll use are identical to writing OpenGL in any other language. But since you're writing in Python, you'll have many fewer opportunities to make "silly" mistakes with proper pointer usage, memory management, etc. that would just eat up your time if you were to study the API in C or C++, for instance.
PyOpenGL
I don't think it is a good choice. In my opinon in C/C++ it is easier to play around around with your OpenGL code - start with simple app, then add shader, then add some geometry functions, make a texture/geometry generator, build scene via CSG, etc. You know - to have fun, play around with code, experiment and learn something in process. I honestly just don't see myself doing this in python. Surely it is possible to do OpenGL programming in Python, but I see no reason to actually do it. Plus several OpenGL functions take memory pointers as arguments, and although there probably a class (or dozen of alternatives) for that case, I don't see a reason to use them when a traditional way of doing things is available in C/C++, especially when I think about amount of wrappers python code uses to pass vector or array of those into OpenGL function. It just looks like making things more complicated without a real reason to do that. Plus there is a noticeable performance drop, especially when you use "RAW" OpenGL.
Besides, if you're going to make games, it is very likely that you'll have to use C++ or some other "non-python" language.
P.S. I've done enough OpenGL programming, a lot of DirectX programming, but I specialize on C++, and use python only for certain algorithmic tests, tools and scripts.

What kind of applications are built using Python? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I wanted to know
Python is suited for what kind of applications?
I am new to Python world but I know it's a scripting language like Perl but I was not sure about the kind of applications which one would build using Python and would certainly appreciate if someone can provide some useful information.
It's hard to think of kinds of general applications where Python would be unsuitable, but there are several kinds where, like just about all higher-level languages akin to it, it might be considered a peculiar and probably inferior choice.
In "hard real time" applications, all dynamic memory allocation and freeing, and especially garbage collection, are quite understandably frowned upon; this rules out almost all modern languages (including Python, but also Java, C#, etc, etc), since almost all of them rely on dynamic memory handling and garbage collection of some kind or other.
If you're programming for an "embedded device" which you expect to be produced and sold in huge numbers, every bit of ROM may add measurably to the overall costs, so you want a language focused on squeezing the application down to the last possible bit -- any language that relies on a rich supporting runtime environment or operating system (including Python, and, again, also Java, C#, etc, etc) would no doubt force you to spend extra on many more bits of ROM (consider threaded-interpretive languages like good old Forth: they can make a substantial application's code be measurably more compact than straightforward machine code would!).
There many be other niches that share similar constraints (mostly focused on MEMORY: focus on using as few bits as possible and/or strictly confining execution within precisely predefined limits -- no dynamism, no allocation, no garbage collection, etc, etc), and basically the case would once again incline in similar ways (for example, there are server applications, intended to run on myriads of servers, which can save many megabytes per server if coded in C++ [especially if without "allegedly-smart" pointers;-)] rather than Java, Python, C#, and so on).
Of course there are excellent reasons most modern languages (Python, Java, C#, etc) choose to do dynamic memory allocation, garbage collection, and so forth, despite the importance of application niches where those techniques are a negative aspect: essentially, if you can possibly afford such nice memory handling, writing applications becomes MUCH, MUCH easier, and a whole class of problems and bugs connected with the need to carefully manage memory if you lack such support can go away -- programmer productivity really soars... IF garbage collection and the like can be afforded at all, that is. For example, if an application was going to run on a few hundreds or thousands of servers, I probably wouldn't bother coding it in C++ with manual memory management in order to save memory; it's only at tens and hundreds of thousands of servers, that the economics of all those extra megabytes really kicks in.
Note that, despite the common misconception that "interpreted languages" (ones with a rich underlying runtime or VM, like Java, C#, Python, etc) "are slow", in fact for most CPU-intensive applications (such as scientific computation), Python is perfectly suitable, as long as the "rich supporting runtime environment" (e.g. numpy) is factored in. So, that is not really a factor -- though memory consumption and garbage collection CAN be, in some niches.
http://www.python.org/about/apps/
http://wiki.python.org/moin/Applications
Recap:
Web Applications ( Django, Pylons )
Games ( Eve Online - MMORPG )
Software Development ( Trac for Project Management )
Object Databases ( ZODB / Durus )
Network Programming ( Bittorent )
Mobile applications
And far more...
You say:
I am new to Python world but I know it's an scripting language.
I think the distinction between "scripting languages" and "programming languages" is quite arbitrary. Nearly every language developed in the last 10-20 years has some kind of runtime support, usually in the form of a bytecode interpreter or virtual machine. Python is no different: it gets compiled to bytecode and the bytecode is executed by the Python runtime. The point is, I would say there are very few things you can do in Java, C#, Ruby, etc., that you couldn't do in Python.
That said, however, different languages have different strengths. So there are certainly some kinds of programs that would be better suited to being written in Python. It really depends on what you want the programming language to do for you, and what you want to do yourself. The right answer depends on what kinds of problems you're interested in solving.
I know its a bit late, but if it helps.
Civilization IV
OpenStack
Bazaar
Mercurial
Blender 3D
TwistedMatrix
Trac
Allura (source project for SourceForge.net)
BitTorrent(<5.3)
Gwibber
Ubuntu Software Center
YUM
OpenERP
journyx
Please note that I have avoided the entire race of web-frame works, IDEs (Eric Python IDE, Ninja-ide, PIDA -ide,Wing IDE,Stani's Python Editor and tools ( Pygame, PyGTK, wxPython, mod python, IPython) and webservices ( youtube.com, reddit.com, quora.com, dropbox.com)
Well, the short answer is, since you mentioned Perl, anything you could build in Perl you could build in Python. You can build anything in any language, and if the language has easy C bindings, you could even do it efficiently.
Now, this being the case, the question becomes somewhat philosophical. Python has as a key tenet "There should only be one way to do it". Perl is exactly the opposite. The key tenet of Perl is "There Is More Than One Way To Do It" (TIMTOWTDI) or ( Tim Toady, to his frineds ;) ) How do you like to do things? One clear and shining path, agreed upon by most? Or perhaps you value the almost infinite number of solution paths that any task has in Perl?
So, assuming that your task is I/O bound ( like most things ) rather than CPU bound ( real time programming or games , or nipple crinkling number crunching ) then Python would be suitable. Whether its philosophy suits you is the key question.
Most of the 3d packages these days, such as Maya, SoftImage, Houdini, RealFlow, Blender, etc. all use Python as an embedded scripting and plugin language.
It's computer programming language, and as such any computer program could theoretically could be built with it. See here for an example
Bittorrent was built on Python.
http://en.wikipedia.org/wiki/List_of_Python_software
follow the link and You will see a lot of things. Actually I am also willing to learn Python thats why I have been searching such answers like you and i got this link. Good luck buddy.

What are some good projects to make for a newbie Python (but not new to programming) developer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm downloading Python 3.1.1 and that comes with the IDLE correct?
I'm also downloading QT for Windows which I'm told is a good GUI framework to work with Python.
What projects should I try to make in order to grasp some of the goodies Python brings to the table?
Thanks a bunch SO.
I highly recommend
http://www.diveintopython3.net
It assumes you already understand programming, and walks you through examples that demonstrate the unique abilities of Python.
Do the next project you intended to program with your prefered language with Python.
If you are new to python, why not start with some simpler command line projects? I know you said you are not new to development, but maybe you should spend some time with the core python stuff before tacking on a GUI framework. Just a suggestion.
Also, I would point out that Python 3+ code looks a bit different than a lot of the python 2.x code samples you will see around the internet. I have found Python 3 to be not the best in terms of backward compatibility. You might want to start out with a 2.x version of Python to get the most out of the plethora of Python tutorials on the internet, then move to Python 3 if you need it.
Write a simple Text Editor.
That was one of the projects i started when i first learned python. It gets you used to the GUI framework, file IO, many types, OOP, lots... It's something that you can grow over time as your confidence builds and it's cross platform so it's handy.
If python is your first dynamic lanugage you might want to play with some of it's dynamic aspects.
For example, using the getattr and setattr methods on objects, you could write a class that provides a fluent way of accessing elements from an XML document. Rather calling methods on an object with parameters like 'xml.getnode("a").getnode("b")' you could dynamically lookup the nodes as attributes and allow 'xml.a.b' instead. I thought this was very cool having come from static languages.
Note that this won't neccessarily give you a great feel for python in general (although you'll pick up the language as you go) but it will give you a taste of what is possible in dynamic languages.
PythonChallenge
Code Golf
Google Code Jam
These are good ways to practice learning Python.
Might I also suggest that you consider using a different IDE.
If you are interested in GUI programming, I would suggest looking into wxPython, PyWin32, easyGUI, TkInter (which is bundled with the Python distribution)
Python Challenge This is fun and interesting to learn Python programming.
While it is a matter of personal preference, I certainly wouldn't want to play around with a GUI framework when starting out -- I would want to get a feel for the language first by playing around with smaller snippets, such as those suggested on Code Golf. While getting your code to fit into the smallest number of bytes perhaps isn't the best way to learn good design, I think it's a good way to learn parts of the language. Certainly, just doing the tasks without necessarily trying to compact them down excessively could be helpful.
A project I wish someone would write: a friendly GUI that wraps around the scanner library and the PDF library, and lets the user easily scan and file documents.
It would have a toolbar with big buttons: "scan letter", "scan brochure", "scan photo". These would respectively choose high-resolution black-and-white, medium-resolution color, and high-resolution color.
The user would plop down the document and hit one of those buttons. Python would wake up the scanner and have it scan, and then would use Python Image Library or something to auto-detect the size of the actual scanned document and auto-crop down to minimal size.
For "scan photo" you would get a JPEG. For the others, you would get a PDF. And it would have an option where you could scan several pages and then select the scanned pages, and say "group" and it would make a single PDF out of them.
Other useful toolbar buttons would be: "Copy Letter", "Copy Brochure", "Copy Photo". These would scan and then immediately print on an appropriate output device (or just on the default output device for your first version).
If you want to go crazy, you could add an OCR function to try to recover searchable text from the scanned images, and put that in the PDF as tags or something.
Someday I will write this if nobody else does...

Categories