• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Light Shader

40 posts in this topic

That's quite something to ask for. huh.png

 

Do you have any problems in developing the shader?

 

You know we might be able to help you. wink.png

0

Share this post


Link to post
Share on other sites

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.

-2

Share this post


Link to post
Share on other sites

Assuming that you know the basic concepts, such as: Normals and Vectorshuh.png

 

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

 

Vertex Shader:

  • 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;

 

Pixel Shader:

  • 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;
};

 

Shader If Needed, click below...

[spoiler]

   This shader is for DirectX 11, but the principles should be the same, I think! wacko.png

cbuffer ConstantObjectBuffer : register(c1)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;

    matrix rotationMatrix;
}

cbuffer ConstantFrameBuffer : register(c0)
{
    float4 lDir;
    float4 lColor;
}

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

VOut VShader(float4 position : POSITION, float4 normal : NORMAL)
{
    VOut output;
    output.position = mul(position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);
	
    float4 n = normalize(mul(rotationMatrix, normal));
    float diffusebrightness = saturate(dot(n, lDir));
    output.color = lColor * diffusebrightness;

    return output;
}

float4 PShader(VOut input) : SV_TARGET
{
    return input.color;
} 

 

PS. That shader has NOT been tested, so some few errors may be present, but I can't spot some right now.

[/spoiler]

 

This should get you going. wink.png

 

If any further issues, please reply.

Edited by Migi0027
2

Share this post


Link to post
Share on other sites

@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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

@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?

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

First you draw your objects once with one shader:

 

// activates shader 1
device->SetPixelShader(PixelShader1); // one that apply diffuse texture
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->SetPixelShader(PixelShader2); // one that does lightning
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
0

Share this post


Link to post
Share on other sites

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

-1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

 

Please read here for more info:

 

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
0

Share this post


Link to post
Share on other sites

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...
0

Share this post


Link to post
Share on other sites

I notice when I do that:

 

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

I get ZFighting.

0

Share this post


Link to post
Share on other sites

I think in second pass you don't need to write to zbuffer, try adding this:

 

...
device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); // disable
Object.Draw();
device->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); // reenable after done with "lightning objects"

 

Does that solve z-fighting?

Edited by belfegor
0

Share this post


Link to post
Share on other sites

I don't see z-fighting even with ridiculous near/far values and with low quality D16 DS buffer, and even left zwriteeneble on.

There is a source in attachment with point light. Hold left mouse and move to rotate and w/s/a/d to move camera.

Edited by belfegor
0

Share this post


Link to post
Share on other sites

Hmm... I guess the Z-Fighting is actually expected, since I'm drawing two meshes on each others, the first one sometimes will appear and the second one other times will appear.

 

I don't know why you don't have Z-Fighting in your project, but drawing two meshes on each others will normally cause Z-Fighting.

 

Btw, I'm not using shaders yet, I'm just trying to blend two meshes on each others.

 

Here is what I'm doing:

device->SetTexture(...);
device->SetMaterial(...);

device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
mesh->DrawSubset(0);

device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );

mesh->DrawSubset(0);
0

Share this post


Link to post
Share on other sites

Up, tried different ways on my project and still getting z-fighting.

 

Any idea what could be causing the problem?

Edited by Medo3337
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0