Lighting with GU, specular and diffuse?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
rasmus
Posts: 17
Joined: Wed Jul 21, 2004 9:30 am
Location: Göteborg, Sweden

Lighting with GU, specular and diffuse?

Post by rasmus »

First of all, big thanks to chip for the outstanding work on libGU.

I've been experimenting a bit with lighting, but I can't seem to get a light to be specular and diffuse at the same time. Is it possible?

Basically, this code gives nice diffuse lighting:

Code: Select all

ScePspFVector3 lpos = { 5, 0, -20 };
sceGuEnable(GU_STATE_LIGHTING);
sceGuEnable(GU_STATE_LIGHT0);
sceGuLight(0, 0, 0, &lpos);
sceGuLightColor(0, 2, 0xffffff);
and this code gives a specular highlight (but no diffuse shading at all)

Code: Select all

ScePspFVector3 lpos = { 5, 0, -20 };
sceGuEnable(GU_STATE_LIGHTING);
sceGuEnable(GU_STATE_LIGHT0);
sceGuLight(0, 0, 8, &lpos);
sceGuLightColor(0, 2, 0xffffff);
sceGuSpecular(12.0f);
Does anyone know if there is a way to make a diffuse+specular lighting model?

Cheers,
Rasmus
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Re: Lighting with GU, specular and diffuse?

Post by chp »

rasmus wrote:First of all, big thanks to chip for the outstanding work on libGU.

I've been experimenting a bit with lighting, but I can't seem to get a light to be specular and diffuse at the same time. Is it possible?

Basically, this code gives nice diffuse lighting:

Code: Select all

ScePspFVector3 lpos = { 5, 0, -20 };
sceGuEnable(GU_STATE_LIGHTING);
sceGuEnable(GU_STATE_LIGHT0);
sceGuLight(0, 0, 0, &lpos);
sceGuLightColor(0, 2, 0xffffff);
and this code gives a specular highlight (but no diffuse shading at all)

Code: Select all

ScePspFVector3 lpos = { 5, 0, -20 };
sceGuEnable(GU_STATE_LIGHTING);
sceGuEnable(GU_STATE_LIGHT0);
sceGuLight(0, 0, 8, &lpos);
sceGuLightColor(0, 2, 0xffffff);
sceGuSpecular(12.0f);
Does anyone know if there is a way to make a diffuse+specular lighting model?

Cheers,
Rasmus
If you look at the source for sceGuLightColor(), you'll notice that passing 1, 2 or 4 sets the different components for a light, and then index 3 & 6 are used to set two components to the same value using just one call... Perhaps you should experiment with the following:

Code: Select all

sceGuSendCommandi(0x8f,0xffffff);
sceGuSendCommandi(0x90,0xffffff);
sceGuSendCommandi(0x91,0xffffff);
Those would set all color components for light 0 two pure white. Then I would tamper with register 0x5f, which seems to be passed the kind of light used in bits 8-11. I have not spent much time with lights, but I guess I'll have to dig into it soon. :)
GE Dominator
rasmus
Posts: 17
Joined: Wed Jul 21, 2004 9:30 am
Location: Göteborg, Sweden

Post by rasmus »

I've done a bit of experimenting and here are my results:

On the light type register (0x5f on light 0) it seems bit 1 switches between specular and diffuse lighting (1 is specular, 0 is diffuse). Bit 8-9 changes the light direction somewhat, so I guess it changes point / directional / spot light or something like that. It does not affect specular/diffuse though. Bit 0 is really interesting though. It turns on some kind of component that increases when the light hit the surface near grazing angle. It is also raised to the power of sceSpecular() register. Does anyone know what this lighting model is called?

The three color components for each light source (0x8f..0x91 on light 0) are in order amibent, diffuse/specular and grazing angle color.

It seems I'm not getting any closer to a combined diffuse specular light though :(
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

rasmus wrote:I've done a bit of experimenting and here are my results:

On the light type register (0x5f on light 0) it seems bit 1 switches between specular and diffuse lighting (1 is specular, 0 is diffuse). Bit 8-9 changes the light direction somewhat, so I guess it changes point / directional / spot light or something like that. It does not affect specular/diffuse though. Bit 0 is really interesting though. It turns on some kind of component that increases when the light hit the surface near grazing angle. It is also raised to the power of sceSpecular() register. Does anyone know what this lighting model is called?

The three color components for each light source (0x8f..0x91 on light 0) are in order amibent, diffuse/specular and grazing angle color.

It seems I'm not getting any closer to a combined diffuse specular light though :(
I did some experimenting of my own last night, and I've commited my results to PSPSDK. This should help you and others get lighting working. I'll commit a new sample as soon as I've cleaned it up and verified my results, but you should be able to use the new defines in pspgu to your advantage before this. Do note however that this new version of pspgu has a lot of other changes when it comes to defines, which WILL break your app, but it will be easy to fix.

There are, as you say, three different kind of lights available: directional, pointlights and spotlights.

From what I can see, you can combine lights to enable parts of the equation. Remember sceGuLightColor() and how it sets its registers? Well it turns out this is how you enable lights aswell, so for example to setup a diffuse light this:

Code: Select all

ScePspFVector3 lpos = { 5, 0, -20 };
sceGuEnable(GU_LIGHTING);
sceGuEnable(GU_LIGHT0);
sceGuLight(0, GU_DIRECTIONAL, GU_DIFFUSE, &lpos);
sceGuLightColor(0, GU_DIFFUSE, 0xffffff); 
To get diffuse AND specular, you can do this:

Code: Select all

ScePspFVector3 lpos = { 5, 0, -20 };
sceGuEnable(GU_LIGHTING);
sceGuEnable(GU_LIGHT0);
sceGuLight(0, GU_DIRECTIONAL, GU_DIFFUSE_AND_SPECULAR, &lpos);
sceGuLightColor(0, GU_DIFFUSE, 0xffffff);
sceGuLightColor(0, GU_SPECULAR, 0xff00ff);
and GU_DIFFUSE_AND_SPECULAR is simply the value 6, or (GU_DIFFUSE|GU_SPECULAR). To set them both in one go you do:

Code: Select all

sceGuLightColor(0,GU_DIFFUSE_AND_SPECULAR,0xffffff);
From what I understood in sceGuLight(), lights can contain either ambient & diffuse (giving you a virtual 5th light if you want to have global changes) or diffuse & specular. Components are filtered, so that you can only either pass in GU_AMBIENT_AND_DIFFUSE or GU_DIFFUSE_AND_SPECULAR to this function to switch between light type 0 and 1.

ALSO, this exposed that you can apparently pass in the value '8' as a component which enables light type 2, and this might be some other kind of lighting-model. I have not yet figured it out, so it is still undocumented.
GE Dominator
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

Ok, I've submitted a light-sample after cleaning the code up and enabling a few more lights. You'll clearly see both diffuse & specular in that sample, or you're probably blind. :)

Then again, one of the beers last night might have blinded you... ;)
GE Dominator
Arwin
Posts: 426
Joined: Tue Jul 12, 2005 7:00 pm

Post by Arwin »

chp wrote:Ok, I've submitted a light-sample after cleaning the code up and enabling a few more lights. You'll clearly see both diffuse & specular in that sample, or you're probably blind. :)

Then again, one of the beers last night might have blinded you... ;)
Thanks for that, I was looking forward to seeing the GPU/VPU in action ...
Post Reply