Working with Lighting and Shadows on the DC

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Working with Lighting and Shadows on the DC

Post by PH3NOM » Sat Jul 13, 2013 2:54 pm

Since KGL is completely absent from any lighting code, I decided to take a swing at it.

I have made an implementation of Dynamic Vertex Lighting based on some resources provided by nVidia, although it applies to GL.
http://http.developer.nvidia.com/CgTuto ... ter05.html
To accelerate things, I have used the SH4s fast math functions when possible.

Here is a basic scene I wrote for testing; some cylindrical and rectangular columns.

In this image, the scene is lit with one diffuse light, and a low level of ambient light:
Image

This image is the same scene with the same diffuse light, except now the specular term is also being calculated:
Image

Same scene, now with a high level of ambient light:
Image

So, I have the basic lighting model up and running, but now it leaves the desire for shadows to interact with the lighting.

My question is, how do we use the PVR to achieve shadows?
I know there is a modifier volume that can be set to "PVR_MODIFIER_CHEAP_SHADOW" but I really dont know how to use modifier volumes.
Any info appreciated!

BTW this guy seems to have achieved some very nice effects with PVR shadows:
http://yam.20to4.net/dreamcast/index_old.html

Small update, added attenuation calculations, and posted a short clip on youtube:
User avatar
Christuserloeser
Moderator
Moderator
Posts: 5936
Joined: Thu Aug 28, 2003 12:16 am
Location: DCEvolution.net
Has liked: 0
Been liked: 0
Contact:

Re: Working with Lighting and Shadows on the DC

Post by Christuserloeser » Mon Jul 22, 2013 11:51 am

Impressive! and yeah, I remember that yam.20to4.net website. Nice to see he updated it as recent as 2010
Insane homebrew collector.
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by RyoDC » Mon Jul 22, 2013 3:36 pm

>My question is, how do we use the PVR to achieve shadows?

Though I really not that competent on the topic (I don't know exactly is there really exist some hardware on pvr dreamcast chip to deal with shadows), I think there's nothing much can be done.
Because as all we know, pvr only does culling, sorting, and rasterization of polygons, it doesn't do any geometry transformations, so I don't think much can be done on pvr in sense of shadows.
I mean, the problem is, Pvr don't know anything about your 3d world, it receives just a screen coordinates of polygons and then just renders them, using texture information and depth buffer (overlap test).
But what you can do on sh4 is to calculate the projection of an object to the surface, from the point of light source (or multiple light sources, if you want to count them all), and then use that geometry information (triangle strips) and pass it to pvr directly, to blend it with targeted surface color pixels, and to receive a shadow. After that you can also add some program code to blur shadows edge or something like that.
How do I try to build a Dreamcast toolchain:
Image
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Tue Jul 23, 2013 9:55 pm

RyoDC wrote:>My question is, how do we use the PVR to achieve shadows?

Though I really not that competent on the topic (I don't know exactly is there really exist some hardware on pvr dreamcast chip to deal with shadows), I think there's nothing much can be done.
Because as all we know, pvr only does culling, sorting, and rasterization of polygons, it doesn't do any geometry transformations, so I don't think much can be done on pvr in sense of shadows.
I mean, the problem is, Pvr don't know anything about your 3d world, it receives just a screen coordinates of polygons and then just renders them, using texture information and depth buffer (overlap test).
Although not quite a "stencil" buffer, the PVR supports modifier volumes that can determine per-pixel weather or not a point is inside a defined volume.

Check that page I linked to in my first post.

Also, this is a post I just found regarding MDK 2 on the Dreamcast:
The Dreamcast shadows aren't stencil shadows. They are a PowerVR2 exclusive feature called "modifier volumes", which are very different:

A stencil shadow volume is drawn after the scene. It interacts with the Z-buffer to "mark" pixels which are inside the volume in the stencil buffer. Then, the render can do blending only against the marked (or unmarked) pixels.

The modifier volume relied on the PowerVR2 unique tiled deferred rendering. It didn't use a z-buffer nor stencil buffer: it could determine, per pixel, if a polygon was inside or outside a modifier volume (via ray-triangle tests) and use a different material if so. This "material" could be darker, brighter or even use a different texture (but this was rarely used).
http://www.neogaf.com/forum/showpost.ph ... stcount=99

What I am asking is basic info on how to use modifier volumes with kos...
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by RyoDC » Wed Jul 24, 2013 6:53 am

They are a PowerVR2 exclusive feature called "modifier volumes", which are very different
Huh, that's why I didn't find anything in google on this :)
Can you advice any sources where I can read on how pvr2 works internally?
How do I try to build a Dreamcast toolchain:
Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5389
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: Working with Lighting and Shadows on the DC

Post by BlueCrab » Wed Jul 24, 2013 9:13 am

PH3NOM wrote:What I am asking is basic info on how to use modifier volumes with kos...
There are two examples that are in the KOS tree, assuming you're using a relatively recent enough version (they were added in 2009, IIRC). Here they are:
examples/dreamcast/pvr/modifier_volume
examples/dreamcast/pvr/modifier_volume_tex

They're kinda silly little examples, but they should at least demonstrate basically how you can use modifiers to affect polygons...
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Sat Jul 27, 2013 12:45 am

BlueCrab wrote:
PH3NOM wrote:What I am asking is basic info on how to use modifier volumes with kos...
There are two examples that are in the KOS tree, assuming you're using a relatively recent enough version (they were added in 2009, IIRC). Here they are:
examples/dreamcast/pvr/modifier_volume
examples/dreamcast/pvr/modifier_volume_tex

They're kinda silly little examples, but they should at least demonstrate basically how you can use modifiers to affect polygons...
Thank you for that!

As I am using the Dev Iso R3 Build of KOS, modifier volume examples were not apart of KOS. I went ahead and updated the pvr code in the kernel to the latest KOS repo.
With that, I updated the way I compile polys for the PVR with my implementation of OpenGL.
I have not yet had time to look at your example code, but are your modifier volumes a quad(2d) or a qube(3d)?

At any rate, here are some screens from my most recent test of my build of OpenGL with Dynamic Vertex Lighting with 3 light sources ( 1 ambient, 2 diffuse with attenuation ).
Dont consider the frame rate bad(20fps), as things are still not final, but not bad considering KGL will crash trying to render this sonic model alone :0

(Very low ambient lighting)
Image

(Medium ambient lighting)
Image

(High ambient lighting)
Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5389
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: Working with Lighting and Shadows on the DC

Post by BlueCrab » Sat Jul 27, 2013 9:49 am

PH3NOM wrote:As I am using the Dev Iso R3 Build of KOS, modifier volume examples were not apart of KOS. I went ahead and updated the pvr code in the kernel to the latest KOS repo.
With that, I updated the way I compile polys for the PVR with my implementation of OpenGL.
I do highly recommend that if you're working on building stuff for others to use that you look into updating the entirety of your toolchain to work with a more current version of KOS. I know that it can be a pain, but it'll be worth it in the end, I'd think.
I have not yet had time to look at your example code, but are your modifier volumes a quad(2d) or a qube(3d)?
Just a single quad.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Wed Jul 01, 2015 7:35 pm

BlueCrab-
Wow, cant believe this is a 2 year bump.
Thank you again for adding the cheap_shadow example to KOS.
I am considering again the idea of using modifier volumes for simple shadows on DC.

My idea is to tessellate and cache a Cone as 2 Triangle Strips ( base and wall ) once, then when rendering simply set the the matrix scale, translation, and rotation matrices as need to render the mesh as a modifier volume at any given position / rotation / size.
Image

Anything inside that volume will be effectively shadowed.

One thing that bothers me is that the modifier volumes in KOS seem to be more like "modifier sprites", in a 2D context.
The pvr_modifier_vol_t seems very similar to pvr_sprite_col_t, but I dont think that is what I need...
Does the PVR support another type of "modifier volume" vertex type? Or can this effect effect be achieved?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5389
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: Working with Lighting and Shadows on the DC

Post by BlueCrab » Wed Jul 01, 2015 11:26 pm

The hardware only supports one polygon type for specifying modifier volumes -- a basic triangle (there is no shortcut way of doing strips with modifiers, you have to do every triangle separately). You can build whatever 3D volumes you want out of those triangles, though.

So, if you're looking at the modifier_volume example, you could add as many more triangles as you want in the volume between lines 154 (pvr_prim(&mod, sizeof(mod));) and 156 (pvr_prim(&mhdr2, sizeof(mhdr2));).

In short, the example only shows 2D, but it should work fine in 3D as well as far as I know. :wink:
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Thu Jul 02, 2015 7:42 pm

Ahh, thanks that clears things up a bit.

Just to be clear, I am looking at shadow.c.

At first look I was thinking each polygon was a quad, and that the 4th vertex was derived from polygon triangulation, kind of like how the pvr sprites work.
But now I see each polygon is actually a triangle, which is why you submit 2 of them to create the quad seen.

I also notice you are submitting two headers.
Can you explain a bit about how the headers must be submitted?
Does a header need to be submitted for each modifier polygon, or 2 headers per volume; i.e. 1 to start volume and 1 to end volume?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5389
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: Working with Lighting and Shadows on the DC

Post by BlueCrab » Thu Jul 02, 2015 11:38 pm

There's a bit in the header to say what the last triangle is in the volume (versus any other triangle in the volume). That's why you have to submit two different headers. As I said in the last reply, you can submit as many triangles as you want between the first header and the second header.

Note in the shadow.c example (lines 41-43):

Code: Select all

    pvr_mod_compile(&mhdr, list + 1, PVR_MODIFIER_OTHER_POLY, PVR_CULLING_NONE);
    pvr_mod_compile(&mhdr2, list + 1, PVR_MODIFIER_INCLUDE_LAST_POLY,
                    PVR_CULLING_NONE);
The second header there has the PVR_MODIFIER_INCLUDE_LAST_POLY flag set, where as the first one has the PVR_MODIFIER_OTHER_POLY parameter there (which is 0, by the way). If you're building an exclusion volume (for whatever reason), then you'll want to use the PVR_MODIFIER_EXCLUDE_LAST_POLY, of course.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Fri Jul 03, 2015 10:41 pm

Thanks BlueCrab, thats what I thought.

I think things get tricky with Z Clipping 3D Modifier Volumes. I still need to investigate more...
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Sun Jul 05, 2015 4:00 pm

This is not yet using modifier volumes, but today I have implemented a sort of stencil-based shadows for models, and I am quite happy with the results!

Image

Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5389
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 12 times
Contact:

Re: Working with Lighting and Shadows on the DC

Post by BlueCrab » Sun Jul 05, 2015 10:33 pm

That's pretty impressive, if I must say so myself. :o
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by PH3NOM » Sun Jul 05, 2015 11:08 pm

Thank you 8-)

Here is a clip of the current build, using the shadow algorithm in the last screen I posted with 2 players.
Shadows still need a bit of tweaking, but not bad for a first pass.

park
DCEmu Junior
DCEmu Junior
Posts: 42
Joined: Tue Jun 24, 2008 10:46 am
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by park » Mon Jul 06, 2015 8:00 am

[quote="PH3NOM"]Thank you 8-)

Here is a clip of the current build, using the shadow algorithm in the last screen I posted with 2 players.
Shadows still need a bit of tweaking, but not bad for a first pass.


Wow thats looking pretty cool. Hopefully you consider some sort of demo or maybe even creating \ selling a game in the future.I remember very few dreamcast (two off the top of my head) used the real time shadowmaping shadows(i guess somewhat like modern games) ,derby tsuku 2 and Matt hoffman bmx.Just thought Id mention in case you would be interested in cool effects.

Image
Image
User avatar
Bouz
DCEmu Junior
DCEmu Junior
Posts: 46
Joined: Mon May 10, 2010 3:42 pm
Location: St. Bauzille de Putois (France)
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by Bouz » Thu Jul 09, 2015 2:48 pm

What about Crazy Taxi and Jet Set Radio?
User avatar
SiZiOUS
DC Developer
DC Developer
Posts: 386
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has liked: 13 times
Been liked: 10 times
Contact:

Re: Working with Lighting and Shadows on the DC

Post by SiZiOUS » Fri Jul 10, 2015 3:53 am

PH3NOM, this is really impressive. Keep up the great work! :o
User avatar
bogglez
Moderator
Moderator
Posts: 576
Joined: Sun Apr 20, 2014 9:45 am
Has liked: 0
Been liked: 0

Re: Working with Lighting and Shadows on the DC

Post by bogglez » Wed Aug 19, 2015 1:53 am

The shadows in Matt hoffman BMX and Phantasy Star Online are so pixelated they're probably using shadow maps, aren't they?
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Post Reply