• Create Account

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

40 replies to this topic

### #1Medo3337  Members

891
Like
-2Likes
Like

Posted 22 May 2013 - 10:07 AM

I'm looking for a shader example that I can use to setup any type of light in any position in the scene.

### #2Migi0027 (肉コーダ)  Members

4386
Like
0Likes
Like

Posted 22 May 2013 - 10:50 AM

That's quite something to ask for.

Do you have any problems in developing the shader?

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #3Medo3337  Members

891
Like
-2Likes
Like

Posted 22 May 2013 - 11:14 AM

I don't even know how to write shader for lights, so I'm looking for any example out that can be used with FFP to create any type of light anywhere in the scene.

### #4Migi0027 (肉コーダ)  Members

4386
Like
2Likes
Like

Posted 22 May 2013 - 11:42 AM

Assuming that you know the basic concepts, such as: Normals and Vectors

One simple type of lighting you could try to implement is directional lighting, such as the sun (Assuming that the rays are directional):

• Multiply the mesh's normal by its rotation mesh to get the world normal (Position and Scaling does not change the normal, at least not in the simple cases).
• In the 2nd line we calculate how visible this normal is in comparison to the light direction/vector, by using the dot function with the light vector and the world normal. The saturate function makes sure that the value is in between 0 and 1.
• Now send the color to the pixel shader, the variable "color" is defined in the input layout for the pixel shader, which is returned by the vertex shader, and that has to be done by you.

Code (VS):

float4 n = normalize(mul(rotationMatrix, normal));
float diffusebrightness = saturate(dot(n, lDir));
output.color = lColor * diffusebrightness;

• Well if you don't have anything other to do, just return the color, as directional lighting only affects faces.

Code (PS):

return input.color;

Input Layout:

An example of an input layout:

struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};


Spoiler

This should get you going.

Edited by Migi0027, 22 May 2013 - 12:11 PM.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #5Auskennfuchs  Members

907
Like
0Likes
Like

Posted 22 May 2013 - 02:08 PM

If you want to use FFP, have a look at

Everything is described for DX8, but there're not much changes to DX9. When you want to use many lights than I would prefer to have a look at deferred rendering and use real self-made shaders.

### #6Medo3337  Members

891
Like
0Likes
Like

Posted 22 May 2013 - 05:40 PM

@Auskennfuchs: I actually want to use FFP for lights but I can't get it to work because I'm rendering the meshes with shaders.

@Migi0027: How about point light and spot light? Do I have to use a shader file for the whole game light? Sometimes I'm using shaders file to render certain meshes, how do I use both the current shader file and the light shader file?

### #7Tom KQT  Members

1698
Like
0Likes
Like

Posted 23 May 2013 - 12:40 AM

EDIT: As Steve_Segreto pointed out, it is possible to combine FFP and vertex shaders when rendering in multiple passes. I didn't know it. And I still cannot imagine how would that work if you want to "use FFP for lights" and "render meshes with shaders", it's probably rather for "render some lights using FFP and some using shaders".

And there are problems with FFP+shader combinations anyway, so it does not invalidate my post too much ;)

You want to use the fixed-function pipeline for lights (device->SetLight(), device->LightEnable()), while rendering your meshes with shaders? Sorry but that won't be possible. If you want to use FFP for lighting, you have to use the quite simple FFP materials.

You cannot really divide the process into "rendering a mesh" and "lighting the mesh" and use FFP for one of those and shaders for the second. How a mesh sufrace looks when rendered is given by how the light interacts with the material, there's a tight connection. The output of your shader must represent the final look of the mesh, you cannot really say to the FFP something like "here's my material shader, please light it up for me with your lights".

In fact you need shaders with all possible combinations of lights and material. There are many ways how to achieve it, from the most flexible (branching using if conditions in the shader) to really having separate shader files (or effect techniques) for every combination. Btw, now I'm talking only about TYPES of lights (directional, point etc.) and materials (textured with diffuse lighting, normal-mapped, light-mapped etc.), details like material and light colors (ambient, diffuse, specular), material texture etc. can be set via shader variables and thus the shader file will of course be the same for a red material and a blue material.

Edited by Tom KQT, 23 May 2013 - 01:21 AM.

### #8Steve_Segreto  Members

2007
Like
0Likes
Like

Posted 23 May 2013 - 12:47 AM

### #9Medo3337  Members

891
Like
0Likes
Like

Posted 23 May 2013 - 04:16 PM

@Tom KQT: Sometimes I will be using a shader file to terrain texture splatting, do I have to modify it for light? If I already made 500 shader files for different effects, do I have to modify the 500 shader file just to make light work on all meshes?

Can I use a separate shader file for light?

### #10Migi0027 (肉コーダ)  Members

4386
Like
0Likes
Like

Posted 24 May 2013 - 11:35 AM

You can do multiple passes if you wan't to, and then blend the results together carefully.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #11Medo3337  Members

891
Like
0Likes
Like

Posted 24 May 2013 - 01:35 PM

Does that mean that I have to create light shader in every shader file to make the mesh get light effect? I'm confused.

### #12Migi0027 (肉コーダ)  Members

4386
Like
0Likes
Like

Posted 24 May 2013 - 01:42 PM

For each single object:

• Render Diffuse with texture (special shader of the mesh)
• Render lighting (could be the same shader all over)
• Blend the results together

So basically you would have two shaders for one mesh, but one of these shaders would be the same all over. I guess.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #13Medo3337  Members

891
Like
0Likes
Like

Posted 24 May 2013 - 03:41 PM

### #14belfegor  Members

2833
Like
0Likes
Like

Posted 24 May 2013 - 04:08 PM

// activates shader 1
Object.Draw();


Then you draw it again with other pixel shadr but enable alpha blending, for example it could be ADD mode:

// activates shader 2 ( 1 one is now "disabled")
device->SetRenderState( d3drs_alphablendenable, true)
device->SetRenderState( d3drs_srcblend, one)
device->SetRenderState( d3drs_destblend, one)
Object.Draw();


So it blends over what is rendered before it. Get it?

Or you could do all in one shader (diffuse + lighning), but that is your choice.

Have you ever worked in Photoshop with different layers and blend them?

If so, it is the similar thing.

Edited by belfegor, 24 May 2013 - 04:10 PM.

### #15Medo3337  Members

891
Like
-1Likes
Like

Posted 24 May 2013 - 11:01 PM

@belfegor: That means I will have to draw most meshes twice? That will not be a good idea for performance, drawing everything twice?

### #16belfegor  Members

2833
Like
0Likes
Like

Posted 25 May 2013 - 01:00 AM

Yes you have to draw twice and maybe even more for other effects. Note that this isn't necessary the bad thing at all, most games do it this way.

There is also some other techniques used to minimize draw call count but have other disadvantages. Search forum for "Light pre-pass" and "Deferred rendering".

You have to choose what best suit your game/scene.

### #17Steve_Segreto  Members

2007
Like
0Likes
Like

Posted 25 May 2013 - 03:18 AM

I actually pointed out:

"You can mix FVF for vertex structure in D3D9 C++ and also use an effect file that provides a vertex shader."

Once you select a vertex shader/pixel shader, you have fewer options for setting device states than if you remained strictly with the FFP.

http://msdn.microsoft.com/en-us/library/windows/desktop/jj658611(v=vs.85).aspx

Declaring your vertex structures using FVF does not imply that you are strictly FFP. You can still set a vertex shader/pixel shader (either directly or through effect file usage). Once you've set a vertex shader/pixel shader, you've opted out of FFP for that part of the pipeline.

Edited by Steve_Segreto, 25 May 2013 - 03:21 AM.

### #18Medo3337  Members

891
Like
0Likes
Like

Posted 25 May 2013 - 09:09 AM

Any examples for point and spot lights using shader?

I want to be able to set the light by giving its position and color to the Shader, I mean (example):

effect->SetPosition(...); // Light position
effect->SetColor(...);   // Light color
// etc...


### #19belfegor  Members

2833
Like
0Likes
Like

Posted 25 May 2013 - 09:28 AM

Look here for DX9 demos

This one has all 3 light types.

Edited by belfegor, 25 May 2013 - 09:32 AM.

### #20Medo3337  Members

891
Like
0Likes
Like

Posted 25 May 2013 - 02:19 PM

I notice when I do that:

// activates shader 2 ( 1 one is now "disabled")
device->SetRenderState( d3drs_alphablendenable, true)
device->SetRenderState( d3drs_srcblend, one)
device->SetRenderState( d3drs_destblend, one)
Object.Draw();


I get ZFighting.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.