Python Shadows

I’m working on a flight simulator and I want the planes to have shadows. How does you do that? Also, I’m using Python, so Python code is preferable.

Search google for “stencil shadows” and “shadow mapping”. There are plenty of resources about this topic.


I couldn’t find anything that was in Python, works, and gives the actual functions I need.

You won’t find CODE, especially not python-code, since that is a very uncommon language to use with OpenGL (most people use C++, or C#).

What you will find, is articles that describe HOW to implement it. That is language-independent. You will need to program it yourself! And be warned, shadows are not easy to do right.


So I noticed.
I’ve already tried implementing the articles, but I’m unable to get them to work. Even the C++ tutorials (NeHe #27) fail to run. That’s why I came here, to see if there is anyone who can help.

I hate C languages because they are overly complex and have stupid incomprehensible syntax.

I’m wondering if anyone can give me just the few functions that are necessary to implement it, and where to put them. It can’t be that hard.

Oh, yes it can. Shadows are the #1 research-topic in real-time computer-graphics.

Sorry to disappoint, but it is just not possible to give you a short answer, how to “enable” shadows. It is quite a complex task and often it makes up a good piece of engine-code.

Ever wondered, why Doom 3 was considered a graphical break-through?

You know the keywords to search for, you have some articles, that explain how it is done, now you are on your own.

All you have to do is assign glShadows to TRUE.

just kidding…

Since you are implementing a flight simulator, I would guess that your shadows do not have to be terribly accurate and they would only appear when the plane is near the ground and the ground is fairly flat.

If that is so, then you could create a “shadow image” in Photoshop and draw a textured quad just above the ground and adjust it’s transparency based on how close the plane is to the ground. This wouldn’t work for hilly situations and it wouldn’t cast an accurate shadow since the orientation of the plane is fixed in the image.

For anything more complicated, it would be time to read the red book.

Also, why the hatred towards C languages?

C++ is the swiss army knife of programming languages.
It can be as object oriented or as “close to the metal” as you want it to be.

OpenGL is accessible in many languages.
Which is cool.
However, when it comes to speed and feature-richness, OpenGL shines best in C/C++.

@CRasterImage: Let us not start another discussion about programming languages (and C :slight_smile: ). There are plenty of people like me that hate C++ for good reasons.

@Geometrian: Still, Python is not very popular with OpenGL. So you will most likely have to port code from C to Python yourself. If you have substantial questions, we will gladly help.

P.S. Please post questions liek this in a beginners forum. If you can’t get Nehe’s samples to run, you are hardly a advanced user (whatever it may be).

P.P.S. Won’t it be better to replace “beginners” and “advanced” forums with some different categories (like “quick questions” and “lengthy discussions”?)

Nah, better not. That would confuse people even more. I mean, how many discussions have you seen, that started out as a “quick” question and turned into a hot and lengthy discussion (maybe about something else entirely) ?

And even “beginners” questions can become quite lengthy to explain.

Also when did you start a discussion, that was intended to become long?


Given the topic of ‘shadows’ and the comments as to the difficulty involved in implementing them maybe he was correct to post here. :stuck_out_tongue:

In actuality, Geometrian is really just getting started here:

It looks like his terrain engine is a start for his flight simulator.

Geometrian, did you ever get the vertex array version working?

Oh, yes. The vertex array version has been working for months. I need the VBO version. Anyway, on the flight simulator, I built a nice map, textured it with a 4096x4096 texture, and used a 256x256 height-map to do the heights. I made 1 qaud = 1 pixel on the height-map so collision detection becomes really easy; just get the (average) height at the quad the plane is over.

So, about shadows.

Yes, I posted this here because I thought it was an advanced topic. I’ll leave any discretion to the moderators.

Anyway, I’m trying to copy a game (, which used shadow textures. In my case, the quads are fairly large, so the shadow texture wouldn’t really work, and I’m also going for higher quality. In the future, maybe the plane could cast shadows on itself (think biplane: top wing on bottom wing), but right now, I’m concerned with just projecting the shadow on the ground.

The solution doesn’t have to be amazing, just good. NeHe 27 would be fine… it just doesn’t work. It uses something called the stencil buffer?

Oh, yes it can. Shadows are the #1 research-topic in real-time computer-graphics.[/QUOTE]A basic solution. For instance, the shadow can be of a single darkness; also, there’s only one light source; also the shadow has only to be projected on the ground.

Oh, yes it can. Shadows are the #1 research-topic in real-time computer-graphics.[/QUOTE]A basic solution. For instance, the shadow can be of a single darkness; also, there’s only one light source; also the shadow has only to be projected on the ground.[/QUOTE]Yeah right… You’re on the edge of sounding insulting here.

Shadows are not simple, shadows are not easy, shadow code can not be given to you because we’re not coding your engine.
Shadow rendering is such deep-merged with the core of a 3d engine no one except you can implement it in your code.

You’ve been given hints about what to look for, now it’s your job as a programmer to do the work.

I don’t mean to be. Just seems like the problem is being overstated. I believe you that shadows are a big problem. After that, I said why my problem is simpler than most cases. Shouldn’t be a big problem, just one light, one shadow. Programmers do things, not tell people they can’t be done.

So everyone has been saying. I get that.

That makes sense.

All right.

Still tricky due to my language.


After that, I said why my problem is simpler than most cases. Shouldn’t be a big problem, just one light, one shadow.

I don’t think you understood.

Shadowing is always a tradeoff, and no matter how much you limit it, it is never simple. Shadowing, in scan-line algorithms, is a hack, no matter how you do it. Because it’s a hack, the correlation between “light” and “shadow” is entirely up to you. That is, the light position you use to compute the color of the model has no direct relationship with the position of the shadow. Properly built programs put them in the same place, but at the level of OpenGL, they are two different things that happen from two different causes.

For a flight-simulator, if all you truly care about is having flying objects create shadows on the ground, a simple projected shadow image of your plane should be sufficient. But even that is non-trivial, as you have to do a render-to-texture to create the shadow image (well, you don’t have to, but you’d get accurate results), then compute a texture matrix for performing the projection and use it when rendering the ground.

Still tricky due to my language.

Nonsense. In two ways.

One, as a programmer, you should be able to implement an algorithm in the language of your choice (so long as the language is capable). So if you read about, say, shadow mapping, even if the example is in C++, the actual text of the article/tutorial should be perfectly convertable into your preferred language.

Two, Python is not so different from C/C++ that a Python programmer who has never seen those languages would be utterly confused by their code. Sure, it’ll take you a bit longer, and you can’t just copy-and-paste code into your application, but you weren’t planning on doing that anyway.

The problem with shadows is, that the complexity of implementation compared to the quality of the result does not scale very well.

Even simple “blob” shadows, that some engines use as a fallback, can be very difficult to implement. They don’t waste much performance, but the implementation can be a real challenge, depending on how your engine works.

Now much better quality shadows, like stencil shadows or shadow-mapping, are difficult to implement, but the “basic” implementation is not so much more work than a “blob”-shadow implementation, because even that one is so much work!

So, the moment you start adding shadows, no matter which method you use, you have a huge amount of work to do, even for “basic” things.

And to achieve higher quality, you have to invest exponentially more work (and processor power).

You were right, that this is an advanced question, so the forum is the correct one. However, it seems like you yourself are still very much of a beginner. Otherwise you wouldn’t complain, that there is no python-code available. Also, you would have read the papers, that google gives you, in more detail and would now ask us the details about how to implement this and that, and would not insist on wanting a “basic” solution right now.

Therefore, i would say you should work on other parts of your engine first and come back to the shadows later, when you have a more thorough understanding of computer graphics and programming in general. But that is your decision. Just accept, that we cannot help you more and it is not, because we wouldn’t want to.


Yep, shadows are a bitch to get right. For an outdoor scene like your flight sim, you should be looking at parallel split shadow maps (PSSM).

I said that:

But it is simpler. Thus, a full-blown pro solution is unnecessary.


Whoa. I don’t remember anyone saying anything about that. That sounds interesting.

Didn’t mention that either…

I hate C for lots of reasons. I’m entirely self-taught, (with no books either), so I only really learned Python. I realize that’s rather limiting.

To shadows. Like I said, I’m entirely self-taught, without any books, so I thought I’d come to the experts who are current on shadows.

I just hate C.

I’m doing that right now.

The problem with that is that the engine is already done. The models have been built, the lighting’s in place, the collision detection, 3D explosions, sky, land, sea, ai, other game logic, menus, levels, etc. all done. It took me all of last summer. I’m done now, except for some finishing touches like the shadows. I’m often crashing into the ground because I can’t see how far I am from it. Shadows are important because we don’t have the benefit of stereoscopic focusing for depth perception. That’s the primary reason I want this.

I can advice you to look at shadow maps.

I just hate C.

Hate it all you want, that’s where you’ll find the examples. Assuming you can read it and understand it you can learn from the examples. No one is telling you to convert to C. Just don’t complain about lack of Python examples.

People on this forum could certainly help you with shadows once you choose your algorithm. Obviously you’ve hit the first obstacle: How to decide what’s best for your situation? Only you can figure that part out. There’s a lot of techniques out there to sort amongst.

Possibly with more information on your engine design someone could offer a more informed opinion about how they would proceed with shadows, but still, you may not be comfortable with how someone else would prefer to do things. And forum participants have no incentive to make decisions for you for your engine, much as they enjoying helping with more general issues.

Ok… C and Python are just 2 languages. There’re not so many differences between the two.
Doing such a distinction is definitely not a professional way. What, braces vs tabs+column? Please! As a programmer you’re supposed to see those languages as tools, not as goal.