Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Light Shader


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.

  • You cannot reply to this topic
40 replies to this topic

#21 belfegor   Crossbones+   -  Reputation: 2692

Like
0Likes
Like

Posted 25 May 2013 - 02:38 PM

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, 25 May 2013 - 02:41 PM.


Sponsor:

#22 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 25 May 2013 - 04:35 PM

No, still getting Z-Fighting.



#23 belfegor   Crossbones+   -  Reputation: 2692

Like
0Likes
Like

Posted 26 May 2013 - 04:45 AM

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.

Attached Files

  • Attached File  test.rar   155.12KB   42 downloads

Edited by belfegor, 26 May 2013 - 04:46 AM.


#24 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 26 May 2013 - 08:00 AM

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


#25 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 27 May 2013 - 11:27 PM

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

 

Any idea what could be causing the problem?


Edited by Medo3337, 27 May 2013 - 11:58 PM.


#26 belfegor   Crossbones+   -  Reputation: 2692

Like
0Likes
Like

Posted 28 May 2013 - 08:41 AM

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.



#27 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 31 May 2013 - 08:29 PM

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?



#28 belfegor   Crossbones+   -  Reputation: 2692

Like
0Likes
Like

Posted 01 June 2013 - 05:37 AM

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.



#29 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 01 June 2013 - 06:00 AM

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


#30 belfegor   Crossbones+   -  Reputation: 2692

Like
0Likes
Like

Posted 01 June 2013 - 06:43 AM

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?



#31 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 01 June 2013 - 07:18 PM

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?



#32 lwm   Members   -  Reputation: 1451

Like
2Likes
Like

Posted 02 June 2013 - 05:26 AM

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?


current project: Roa


#33 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 02 June 2013 - 11:34 AM

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.



#34 belfegor   Crossbones+   -  Reputation: 2692

Like
0Likes
Like

Posted 02 June 2013 - 12:05 PM

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



#35 Migi0027 =A=   Crossbones+   -  Reputation: 1988

Like
1Likes
Like

Posted 05 June 2013 - 04:22 PM

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, 05 June 2013 - 04:23 PM.

Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!


#36 metsfan   Members   -  Reputation: 654

Like
1Likes
Like

Posted 05 June 2013 - 05:21 PM

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, 05 June 2013 - 05:23 PM.


#37 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 05 June 2013 - 06:44 PM

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



#38 metsfan   Members   -  Reputation: 654

Like
0Likes
Like

Posted 05 June 2013 - 07:39 PM

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



#39 Medo3337   Members   -  Reputation: 675

Like
0Likes
Like

Posted 05 June 2013 - 07:44 PM

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



#40 metsfan   Members   -  Reputation: 654

Like
0Likes
Like

Posted 06 June 2013 - 06:46 AM

@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, 06 June 2013 - 06:47 AM.





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.



PARTNERS