• 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

Damn it i lost my crystal ball. biggrin.png

Could you attach minimal project files that reproduces the problem so we can spot what is wrong.

0

Share this post


Link to post
Share on other sites

Sorry for the delay.

 

Since the project is too large, I'm unable to upload any real source code.

 

However, I'm still doing some work, I tried to do the following on a separate mesh:

device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); // If FALSE, the mesh will appear normally, if TRUE I will see undesired transparent mesh
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
// Draw
boxMesh->DrawSubset(0);

device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
// Draw again
boxMesh->DrawSubset(0);

 

1. I notice that the entire mesh is transparent (which is undesired), when I set D3DRS_ALPHABLENDENABLE to FALSE in the first draw, I get normal mesh, however I will need D3DRS_ALPHABLENDENABLE to be TRUE in some meshes, why the mesh is transparent in the above code? I want to enable alpha blending but not make the mesh transparent.

 

2. How do I give the shader multiples lights? sometimes I will have 500 different light in the scene, how do I give 500 light to the shader and specify the light type and other properties as well in each light?

0

Share this post


Link to post
Share on other sites

You cant make new minimal project that reproduces the same problem?

How do you expect someone to help you if you are lazy?

 

1.

I want to enable alpha blending but not make the mesh transparent.

Why do you need alpha blending in the first pass enabled then?

 

2. I gave you the link with examples for multiple lights in previous page. Do you even try to read them?

You need to manage lights that are not visible or very far away and which contribution is minimal so that you can skip some work.

If you need that many lights in scene then you probably want to switch to other rendering techniques, so search forum for "deferred rendering" and "light pre pass".

But i think that that will be pretty hard-core for you right now to understand.

0

Share this post


Link to post
Share on other sites
> Why do you need alpha blending in the first pass enabled then?
For example, If I have a glass mesh, I would need alpha blending to be enabled in the first pass.
 
> If you need that many lights in scene then you probably want to switch to other rendering techniques, so search forum for "deferred rendering" and "light pre pass".
But i think that that will be pretty hard-core for you right now to understand.
 
So, what you think I should do right now to get multiple lights working? BTW, I still have many things to do so I don't want to spend long time just for lights.
0

Share this post


Link to post
Share on other sites

1.

You should partition your list of objects into opaque and transparent. Draw transparent after opaque.

 

2.

...what you think I should do right now to get multiple lights working?

Read about common lightning techniques and then decide which fits you the most.

Its all about compromise, each have its own pros and cons.

 

I still have many things to do so I don't want to spend long time just for lights.

If you are not ready to spend lot of time on this then you should consider using some graphic library to do job for you?

0

Share this post


Link to post
Share on other sites

You need to manage lights that are not visible or very far away and which contribution is minimal so that you can skip some work.

 

I checked the code sample "Direct3D 9 Normal Mapping Demo" which is a good example, however it's using one single light in the entire scene, sometimes I will have a mesh which is getting affected by multiple lights at the same time.

 

Does that means that the project is useless and I must look for other technique such as "deferred rendering" and "light pre pass"? If yes, any deferred rendering shader out there?

0

Share this post


Link to post
Share on other sites

Does that means that the project is useless and I must look for other technique such as "deferred rendering" and "light pre pass"? If yes, any deferred rendering shader out there?

 

It's not useless. You can use normal mapping with almost any lighting technique you want. I would advise against using the deferred techniques for now. They are a lot harder to implement and bring their own problems with them (transparent objects for example). You can't just throw a "deferred rendering shader" on there and expect it to work.

 

Choosing a lighting technique is an important decision and will fundamentally change the way your rendering pipeline works and performs.

 

Your choices:

- Multi-pass: Render the object multiple times and apply one light source with each pass.

- "Uber-shader": Multiple lights in one pass plus anything else you might need and enable/disable the parts you need.

- Combination of the above

- Deferred: Render scene to a G-Buffer and calculate lighting independently

 

What does your typical scene look like? How many lights are we talking? How large are they? How many objects do they affect?

2

Share this post


Link to post
Share on other sites

First of all I want to mention that it's FPS game.

 

I don't know how many light I will be having in the scene, it depends on the game mission, but I expect many different lights.

 

For example: I could have 30-40 light in a scene as well as multiple lights affecting many meshes, rendering each mesh 30-40 times is just a total waste of performance.

0

Share this post


Link to post
Share on other sites

If you want to listen to me, you should go with forward renderer and multi-pass lights, since you are accustomed with it and it is easiest way.

If you have 30-40 lights in the scene then be smart where do you place them, have 3 or 4 as max light count affecting some object, choose by some priority for example for outdoor scene sun would always be included, player flashlight is priority, then choose rest by distance, cull occluded lights, for point lights test light bounding sphere against objects bounding volume for intersection to see if it should contribute...

0

Share this post


Link to post
Share on other sites

From seeing your comments, I can easily see that you aren't happy about the performance happy.png

 

As other people recommended, you can check whether the object is affected by the light, here's how with a Point Light!

 

Bounding Spheres

 

Bounding Spheres is if you were to collect all the vertices in the mesh and make a sphere encapsulate them, like this:

530px-Tighter_bounding_sphere.pngRABBITS!biggrin.png

 

To get this result, you need two things:

  1. The center of the mesh
  2. The radius of the mesh from the center

Calculating the center:

  • Declare the variable ohmy.png
  • Loop over all your vertices and add them to the center
  • Divide the center by the number of vertices.
  • THAT'S IT!
D3DXVECTOR3 center = D3DXVECTOR3(0, 0, 0);

for(int v = 0; v < mVertices.size(); v++ )
{
    center += mVertices[v];
}

center /= mVertices.size();

 

Calculating the radius: (With the center)

  • Declare the variable laugh.png
  • Loop over all the vertices
  • Get the distance between the vertices and the center
  • Find the greatest length
float radius = 0.0f;
for (int v = 0; v < mVertices.size(); v ++)
{
    D3DXVECTOR3 diff = mVertices[v] - center;
		
    float length = sqrtf(D3DXVec3Dot(&diff, &diff));

    if (length > radius)
        radius = length;
}

 

To visualize the result, you just need to translate a sphere (or stuff) to this location: sphere->Translate(mesh->Position + mesh->Center), and also scale it by the meshes radius.

 

Testing for collision between Sphere-Sphere

 

As you know the point light has a radius, and so does, hopefully, the mesh with these calculations. To see if they collide, just do the following

  • Find the vector between the mesh center and the point light's position.
  • Get the length of that vector
  • Check whether that length is smaller than both the point light's radius and the mesh's radius.
  • If true, they collide!!!
D3DXVECTOR3 mDistance = mesh->center + pointLight.Position;
float d = sqrtf( D3DXVec3Dot( &mDistance, &mDistance) );

if (d < (mesh->BoundingSphere.radius + pointLight.Range) )
    // They collide!!!

 

In other words, if they collide, the light will affect the mesh.

 

Mini Tutorials! laugh.png

Edited by Migi0027
1

Share this post


Link to post
Share on other sites

First of all I want to mention that it's FPS game.

I don't know how many light I will be having in the scene, it depends on the game mission, but I expect many different lights.

For example: I could have 30-40 light in a scene as well as multiple lights affecting many meshes, rendering each mesh 30-40 times is just a total waste of performance.


Lights are additive, so you loop through all lights in the shader and add the outgoing irradiance values for each light. You should read up on lighting and BRDFs if you are serious about having 30-40 lights in a scene.

On a separate note, if you are serious about trying to build a first person shooter game, you are going about it the completely wrong way. Gamedev.net is a great place to ask questions when you already have some idea of what you want, but if you just come in here and ask for code without really a question, you come off as lazy. Then you come here with every single error you receive for code you didn't write, instead of taking some initiative and trying to figure it out on your own. That is why you have received so many downvotes for this thread. There are many resources for learning how to do proper lighting, and shader programming.

If you are serious about learning graphics programming, I highly recommend this book:
http://www.amazon.com/Real-Time-Rendering-Third-Edition-Akenine-Moller/dp/1568814240/ref=sr_1_1_bnp_1_har?ie=UTF8&qid=1370474265&sr=8-1&keywords=real+time+rendering

That's just one of many books you will need if you want to be a graphics programmer.

Best of luck, I hope you soon come to realize that this is the absolute wrong way to ask for help.

Edit - On another note, have you asked yourself whether you even want to be doing the graphics yourself? Consider using an engine that already has done the lighting stuff for you. Edited by metsfan
1

Share this post


Link to post
Share on other sites

@metsfan: Maybe you misunderstood me, I have already done a lot of things other than lights, so I don't really need to start graphics programming from the beginning.

 

The thing that I don't have experience with is shader, I thought I could use FFP light but I couldn't since I can't use it while using shaders.

 

I will not use another game engine since I have already programmed almost 60%-70% of the game engine, so it's not wise to just throw what I have done so far away.

 

BTW, when I ask for code samples, it doesn't mean that I'm lazy, it means that I want to get the idea on how it should be done.

 

@Migi0027: I have heard I could pass light array to the shader, why would I test for collision? why not just pass the light array and calculate the final light accordingly?

0

Share this post


Link to post
Share on other sites

@metsfan: Maybe you misunderstood me, I have already done a lot of things other than lights, so I don't really need to start graphics programming from the beginning.

 

The thing that I don't have experience with is shader, I thought I could use FFP light but I couldn't since I can't use it while using shaders.

 

I will not use another game engine since I have already programmed almost 60%-70% of the game engine, so it's not wise to just throw what I have done so far away.

 

BTW, when I ask for code samples, it doesn't mean that I'm lazy, it means that I want to get the idea on how it should be done.

 

@Migi0027: I have heard I could pass light array to the shader, why would I test for collision? why not just pass the light array and calculate the final light accordingly?

 

What he is saying is this:  Each point light has an area of influence.  Additionally, each light adds complexity to the final shading equation, since the shading equation must be performed for each light.  You can cull away lights which do not affect your object, thus increasing performance.  This can be done, as he showed, using Sphere-Sphere  intersection.  One sphere would be the area of influence for the light, the 2nd sphere would be a bounding sphere approximation for your 3D model.  

0

Share this post


Link to post
Share on other sites

@metsfan: Any examples for passing and processing different kind of LIGHT ARRAY to pixel shader?


You can pass your light data to HLSL using a constant buffer. Edited by metsfan
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