PDA

View Full Version : Python Shadows



Geometrian
10-28-2007, 09:54 AM
Hi,
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.
G

Jan
10-28-2007, 10:11 AM
Search google for "stencil shadows" and "shadow mapping". There are plenty of resources about this topic.

Jan.

Geometrian
10-28-2007, 10:42 AM
I couldn't find anything that was in Python, works, and gives the actual functions I need.

Jan
10-28-2007, 11:30 AM
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.

Jan.

Geometrian
10-28-2007, 12:18 PM
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.

Jan
10-28-2007, 04:43 PM
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.
Jan.

CRasterImage
10-29-2007, 01:03 AM
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++.

Zengar
10-29-2007, 01:35 AM
@CRasterImage: Let us not start another discussion about programming languages (and C :) ). 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"?)

Jan
10-29-2007, 02:19 AM
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?

Jan.

pudman
10-29-2007, 01:54 PM
Given the topic of 'shadows' and the comments as to the difficulty involved in implementing them maybe he was correct to post here. :P

In actuality, Geometrian is really just getting started here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=469366

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

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

Geometrian
10-29-2007, 05:13 PM
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.
Given the topic of 'shadows' and the comments as to the difficulty involved in implementing them maybe he was correct to post here. :PYes, 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 (smallrockets.com), 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?



It can't be that hard.
Oh, yes it can. Shadows are the #1 research-topic in real-time computer-graphics.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.

tfpsly
10-31-2007, 08:43 PM
It can't be that hard. Oh, yes it can. Shadows are the #1 research-topic in real-time computer-graphics.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.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.

Geometrian
10-31-2007, 09:11 PM
Yeah right... You're on the edge of sounding insulting here.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.
Shadows are not simple, shadows are not easy,So everyone has been saying. I get that.
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.That makes sense.
You've been given hints about what to look for, now it's your job as a programmer to do the work.All right.

Still tricky due to my language.

G

Korval
10-31-2007, 10:40 PM
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.

Jan
11-01-2007, 04:41 AM
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.


Jan.

knackered
11-01-2007, 02:20 PM
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).

Geometrian
11-01-2007, 08:58 PM
Shadowing is always a tradeoff, and no matter how much you limit it, it is never simple.I said that:
I believe you that shadows are a big problem. After that, I said why my problem is simpler than most cases.But it is simpler. Thus, a full-blown pro solution is unnecessary.
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.OK.
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[...]Whoa. I don't remember anyone saying anything about that. That sounds interesting.
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).Didn't mention that either...
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.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.
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.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.
Otherwise you wouldn't complain, that there is no python-code available.I just hate C.
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,I'm doing that right now.
Therefore, i would say you should work on other parts of your engine first and come back to the shadows later,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.

Zengar
11-02-2007, 12:20 AM
I can advice you to look at shadow maps.

pudman
11-02-2007, 08:44 AM
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.

tfpsly
11-02-2007, 10:21 AM
I just hate COk... 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.

Geometrian
11-02-2007, 10:57 AM
I can advice you to look at shadow maps. OK, so that's one way to do it. Any others?

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.I know, it just adds another level of complexity to an already difficult problem. If there was a tutorial, and I couldn't get it to work, or my grfx card doesn't support it, I wouldn't know that until I translated it to Python.

I just hate COk... 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. Yep. You're right. I like Python b/c it is simpler to use and more intuitive. Python is based on C++, though, so of course they're similar.
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.Like I've said, you're the experts, so you would probably be able to tell what's appropriate and not overkill. What exactly do you mean by engine info.?

pudman
11-02-2007, 02:33 PM
What exactly do you mean by engine info.?

Well, to start with, what exactly are you trying to shadow? You said earlier "but right now, I'm concerned with just projecting the shadow on the ground." I take it that means the the shadow the plane casts due to the sun? Nehe's tutorial should cover a version of that using the stencil buffer.

You mentioned that you couldn't get that tutorial to work. This forum probably has people that can help you figure out why! (Even if you port it to Python)

Like I said, choose a technique (I'd suggest the stencil one since there's a tutorial) and plug away at it for a while. Come back with questions.

knackered
11-03-2007, 06:57 AM
I wouldn't imagine you could get away with planar shadows, not unless your ground is just a flat plane - if it is, you're not going to sell your sim/game to anyone. If it's a heightmap, and you only care about shadows on the ground, not on trees or buildings, then you could easily get away with a simple projected texture. That projected texture could be a simple blob that you knock up in photoshop, or it could be dynamically generated by rendering your aircraft into a texture using an orthographic projection matrix.
Anyway, here's an nice overview to give you some terminology to help your search:
http://www.devmaster.net/articles/shadows/
http://www.devmaster.net/articles/shadow_techniques/

Geometrian
11-03-2007, 10:50 AM
http://www.devmaster.net/articles/shadows/
http://www.devmaster.net/articles/shadow_techniques/From this, I think I want to do "Depth Shadow Mapping" because it is fast and meets all my other requirements. Also, since the shadow of the plane won't be very large, the texture size drawback doesn't apply. All the links were broken though...

Brolingstanz
11-03-2007, 12:17 PM
That's a fine choice you've made there.

Links work fine for me.

pudman
11-03-2007, 12:46 PM
(Working) Links:

http://www.cs.ualberta.ca/~ghali/courses/texts/redbook/html/09-10-shadowmap.c.html
http://developer.nvidia.com/object/robust_shadow_volumes.html

Another tutorial on shadow mapping:
http://www.paulsprojects.net/tutorials/smt/smt.html

pudman
11-03-2007, 12:48 PM
Links work fine for me.

I believe he was talking about the secondary links from the main article which are no longer valid. :-)

Brolingstanz
11-03-2007, 12:55 PM
My bad.

Brolingstanz
11-03-2007, 12:58 PM
The nvidia link I saw seemed to work alright so I cut my clicking adventure short with that.

Brolingstanz
11-03-2007, 01:17 PM
P.S. Anyone played with VSMs?

Brolingstanz
11-03-2007, 01:41 PM
I think a PSSM-VSM combo would be pretty sweet, but I haven't gotten around to trying it yet...

Seems like if you can get past the light bleeding in VSMs you'd be home free.

Anyways, back to my biscuits...

Geometrian
11-03-2007, 09:25 PM
I converted http://www.cs.ualberta.ca/~ghali/courses/texts/redbook/html/09-10-shadowmap.c.html
to Python, in two versions. One, which I converted to not use GLUT does not seem to draw shadows- I don't think I did it right. At least GLUT works. The other, which I'm trying to convert now doesn't run because I can't figure out what kind of argument glutInit() wants. It crashes.

pudman
11-03-2007, 10:48 PM
I can't figure out what kind of argument glutInit() wants.

The parameters to glutInit() aren't important, as passing is perfectly valid and not critical to a functioning program.
(http://www.opengl.org/resources/libraries/glut/spec3/node10.html)

You crash is elsewhere.

Geometrian
11-03-2007, 10:59 PM
It crashes when there's any keyboard input.
I think I'm getting some sort of shadow, but the framerate is really slow, like .33fps. Also, it's not rotating.

Commenting out this line:
glDrawPixels(SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT, GL_LUMINANCE, GL_FLOAT, depthImage)

makes it fast, but then the shadows are not displayed.

Brolingstanz
11-04-2007, 06:21 AM
Yet another choice link from the OpenGL gallery...

http://www.opengl.org/resources/libraries/

Brolingstanz
11-04-2007, 06:21 AM
Try using a texture to display/debug your depthmaps.

Brolingstanz
11-04-2007, 06:21 AM
DrawPixels can be a bit on the slow side at times...

ZbuffeR
11-04-2007, 07:10 AM
"Copyright (c) 1993-2003" You are porting what looks like an old example :)
I guess DrawPixels called from python code can be even slower, as it transfers quite a lot of data from GPU to CPU.

Maybe try to port this sample instead, as it seems to be more modern :
http://http.download.nvidia.com/developer/SDK/Individual_Samples/samples.html#hw_shadowmaps_simple

Geometrian
11-04-2007, 09:21 AM
Seems odd. The "Simple" tutorial-example-thingie is many pages long and has many errors. Really frustrating.

I don't want to give up on the old tutorial yet. Now that it's in Python, I can really manipulate it. Perhaps the slowness problem is due to depthImage being in a Numpy style array? What format is it supposed to be in?

I find it hard to believe that such a tutorial could run on the old hardware it was designed for, but barely move on my new laptop.

ZbuffeR
11-04-2007, 11:44 AM
We told you shadows are hard.


Perhaps the slowness problem is due to depthImage being in a Numpy style array?
I don't know the relative performance of python arrays
Python has a crappy slow virtual machine. Too bad Jython seem almost abandonned. But anyway, the readpixel/drawpixels pair is not useful for having shadows, just to debug the depthmap rendering.


About perf on the old example, I tried compiling it in C, it is blazingly fast.

Geometrian
11-04-2007, 12:02 PM
Yeah, I tried it. I couldn't compile it though. How would one modify the Python version. Something like:
if shadowRender:
#What do I put here?

ZbuffeR
11-04-2007, 12:07 PM
I edited my previous post. No need for drawpixels.

Geometrian
11-04-2007, 12:12 PM
So I just get rid of it? Also, why does it crash when I press any button?

Geometrian
11-04-2007, 12:39 PM
In the Pygame (not GLUT) version, I can get the shadows to work by disabling the textures. I see a blue cube and a red torus at the bottom left of the screen. The torus is rotating, not around the cube, but in front of it. The light appears to be coming from the viewpoint, but the shadows are projected on a white quad on the bottom right of the screen.
http://img253.imageshack.us/img253/165/shadowsui1.jpg (http://imageshack.us)
Here's the Python source for this this screenshot:
http://www.geometrian.com/Shadows3D_3_1.py

Geometrian
11-04-2007, 07:06 PM
So, the shadows are being rendered incorrectly. First, where did the white quad come from, and second, why is the light, shining from the right casting a shadow on the right? As you can see from the marks on the torus and cube, the light is coming from right about where the shadow is- from the right.

ZbuffeR
11-04-2007, 07:17 PM
Well, if you disabled texture mapping, no shadow mapping !
Actually what you are seeing there are :
left: scene as seen from the light
right: associated depthmap

What is missing the scene from the camera point of view, whith the depth compare between the shadow map and the scene.

Geometrian
11-04-2007, 07:49 PM
I didn't follow that, sorry.

pudman
11-04-2007, 10:17 PM
The white quad, and it's contents, are the depthmap. If you were to visualize what was rendered to the depth buffer that's what you'd see. That's why the quad is white, because it's uniform depth (1.0). The contents of the white quad are not the torus and cube's shadow.

The shadow, as you can plainly see in the image on the left, is now correctly applied to the torus (red, with shadow) and cube (blue, with shadow).

Basically, you got the demo to work (minus the texturing). Congrats.

Geometrian
11-04-2007, 11:09 PM
It's shadowing?! YAY! Now I need to figure out how it works...

I still don't see how it's shadowing though--I zoomed in, but the torus doesn't cast a shadow on itself or the cube at any point--I thought it was the normals causing the light/dark areas...

Thanks,
G

ZbuffeR
11-05-2007, 04:18 AM
The shadow, as you can plainly see in the image on the left, is now correctly applied to the torus (red, with shadow) and cube (blue, with shadow).

Alas no in fact, it is not working, Geometrian is right. The are no cast shadows on the colored image. And the point of view is the one from the light.
You are still missing the main view. The only texture on this sample is the shadow map, so you need to make it work.

I found a strange (but modern) example using shadow maps with GLSL.
It uses an external framework apparently, but the GL code seem here.
http://wiki.vrmedia.it/index.php?title=Shadow_Mapping

Geometrian
11-06-2007, 09:02 PM
This, from the link at the bottom of the page, looks promising for the theory. http://www.cs.cmu.edu/afs/cs/academic/class/15462/web.06s/asst/project3/shadowmap/index.html

How should I modify the source I provided to make the shadows work?

Geometrian
11-10-2007, 11:42 PM
So, like, it doesn't crash. When I enable textures, it runs really slowly, but the shadow still looks the same. What, then is the point of them? In the course of another problem, I found that glReadPixels() in PyOpenGL (current version) returns the wrong type of data as opposed to the right one in the previous version. Is there an alternate function?

Geometrian
11-11-2007, 07:03 PM
I think I'm onto something with this other tutorial:
http://www.paulsprojects.net/tutorials/smt/smt.html
I've translated it from the C++ it's in to Python. I think that these lines don't work:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL)
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)

I see the torus and the spheres, but no shadow.