Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Terrain light problem


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
20 replies to this topic

#1 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 03 September 2012 - 01:58 AM

Hello,

I have problem with lights. I want to do dark atmospher with a dark light


my vertex is that;
[source lang="cpp"]struct VertexType{ float x,y,z; float Tu1, Tv1; float Tu2, Tv2; D3DVECTOR NORMAL;};[/source]
I dont know what i should do with NORMAL ? examples in lights they write it handle like -1,0,1 -1,1,0.


here my settings;

[source lang="cpp"] pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE); pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));[/source]

how i should do it ? for dark light on terrain ?


-Thanks.

Edited by lyzerk, 03 September 2012 - 01:59 AM.


Ad:

#2 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 03 September 2012 - 08:46 AM

Normal is a vector perpendicular for each triangle/surface. Since one vertex typically is part of several triangles, the normal of a vertex is the average of the normals of the triangles which share the same vertex.

http://en.wikipedia.org/wiki/Surface_normal check here for more information which may help you to visualize the problem.

Otherwise, to calculate vertex normals, you'll need to calculate the triangle normals first.

for each triangle - calculate surface normal using vector cross product.

for each vertex - add together all the normals of the triangles which share the vertex, normalize the result.

Using correct vertex normals is essential for having correct lighting effects.

Cheers!

#3 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 03 September 2012 - 10:26 AM

thanks for answer im done it without normal vertex
here code;

[source lang="cpp"] pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); pd3dDevice->SetRenderState(D3DRS_LIGHTING, true); pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0x313236);[/source]

but now i have another problem with light borders

http://prntscr.com/ewzcm

i dont want it like that i want it like circle how can i do that ?

edit :
[source lang="cpp"] ZeroMemory( &light, sizeof(D3DLIGHT9) ); light.Type = D3DLIGHT_POINT; D3DXCOLOR color(255, 255, 255, 0); light.Ambient = color * 0.4f; light.Diffuse = color; light.Specular = color * 0.6f; light.Range = 20.0f; light.Theta=1.0f; light.Phi=1.0f; light.Falloff=1.0f; light.Attenuation0= 1.0f; //light.Attenuation1= 0.0f; //light.Attenuation2= 0.5f;[/source]

here light codes

Edited by lyzerk, 03 September 2012 - 10:58 AM.


#4 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 03 September 2012 - 11:33 AM

Hi, you'll need to use per pixel lighting instead of per vertex lighting to get a circle.

I can't remember if per pixel lighting can be enable with fixed function pipeline. You may need to do some vertex and pixel shader programming.

Cheers!

#5 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 03 September 2012 - 11:53 AM

Hi, you'll need to use per pixel lighting instead of per vertex lighting to get a circle.

I can't remember if per pixel lighting can be enable with fixed function pipeline. You may need to do some vertex and pixel shader programming.

Cheers!


Thanks for answer. Anybody can give me basic sample ?

#6 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 03 September 2012 - 12:26 PM

Well internet is full of per pixel lighting samples.

Such as http://www.gamasutra.com/view/feature/131275/implementing_lighting_models_with_.php

http://will-sherif.appspot.com/html/D3D9-FX-files.html

Mind you that, stepping outside of the fixed function pipeline will bring you lots of challenges, such as implementing lighting from multiple light sources etc. Even things like fog need to be programmed by yourself.

Of course, it is a step that you may need to make eventually.

Cheers!

#7 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 05:06 AM

ok i research all links, but everybody has 200 line effect file(.fx) how can i understand that ? im trying basic light with fx file i did that for now;

effect file :
[source lang="cpp"]float4x4 matWorldViewProj; float4x4 matWorld; float4 vecLightDir;struct VS_OUTPUT{ float4 Pos : POSITION; float3 Light : TEXCOORD0; float3 Norm : TEXCOORD1;};VS_OUTPUT VS(float4 Pos : POSITION, float3 Normal : NORMAL){ VS_OUTPUT Out = (VS_OUTPUT)0; Out.Pos = mul(Pos, matWorldViewProj); // transform Position Out.Light = vecLightDir; // output light vector Out.Norm = normalize(mul(Normal, matWorld)); // transform Normal and normalize it return Out;}float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1) : COLOR{ float4 diffuse = { 255.0f, 255.0f, 255.0f, 0.0f}; float4 ambient = {255.0f, 255.0f, 255.0f, 0.0f}; return ambient + diffuse * saturate(dot(Light, Norm));}technique myLight{ pass P0 { vertexShader = compile vs_2_0 VS(); pixelShader = compile ps_2_0 PS(); }} [/source]


here is code;

[source lang="cpp"] id3dxEffect->SetMatrix(id3dxEffect->GetParameterByName(NULL, "matWorldViewProj"), &matView ); id3dxEffect->SetMatrix(id3dxEffect->GetParameterByName(NULL, "matWorld"), &matWorld ); D3DXCOLOR color(1,1,1,1); id3dxEffect->SetValue(id3dxEffect->GetParameterByName(NULL, "vecLightDir"), &color, sizeof(color) ); id3dxEffect->SetTechnique(id3dxEffect->GetParameterByName(NULL, "myLight") ) ; UINT numPasses = 0; id3dxEffect->Begin( &numPasses, 0 ); for( int i = 0; i < numPasses; i++ ) { id3dxEffect->BeginPass( i ); //terrain.Render(pd3dDevice); id3dxEffect->EndPass(); } id3dxEffect->End(); id3dxEffect->Begin( &numPasses, 0 ) ; id3dxEffect->BeginPass(0); terrain.Render(pd3dDevice); id3dxEffect->EndPass(); id3dxEffect->End() ;[/source]

and terrain is gonna just black. im calling terrain.render wrong place ? Really i need to understand that but at internet all so complicated

sorry for my english.

#8 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 06:05 AM

Hi,

You should call terrain.render between beginpass and endpass inside the loop. Although, your rendering code should be drawing.

In shader the color values should be in range of 0.0-1.0 not 0.0-255.0f, but that doesn't explain black output.

Your light direction vector isn't normalized, but that either doesn't explain the black output.

What is the output if your pixel shader is simply:

float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1) : COLOR
{
return float4(1.0f,1.0f,1.0f,1.0f);
}

?

Cheers!

#9 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 06:51 AM

ok i did terrain.render inside the loop. and changed PS

result is same just http://prntscr.com/eyqqt.
maybe vecLightDir,matWorld have problem ?

matView is
[source lang="cpp"] D3DXVECTOR3 vEyePt( 0, 100, 0 ); D3DXVECTOR3 vLookatPt( 0 , 0, camera.vec ); D3DXVECTOR3 vUpVec( 0, 1, 1 ); //up D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );[/source]
matWorld;

[source lang="cpp"] D3DXMATRIXA16 matWorld; D3DXMatrixTranslation( &matWorld, camera.camera.x, camera.camera.y, camera.camera.z);[/source]

Edited by lyzerk, 04 September 2012 - 06:57 AM.


#10 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 07:05 AM

Ah, you don't get any rendering on the screen. There could be million reasons for it.

I could take a guess and say that you could try to use SetMatrixTranspose method instead of SetMatrix.

Cheers!

#11 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 07:25 AM

http://prntscr.com/eysh8 here is result for Transpose its seems like just line. here is the ps

[source lang="cpp"]float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1) : COLOR{ float4 diffuse = { 1.0f, 1.0f, 1.0f, 1.0f}; float4 ambient = {1.0f, 1.0f, 1.0f, 0.0f}; return ambient + diffuse * saturate(dot(Light, Norm)); //return float4(1.0f,1.0f,1.0f,1.0f);}[/source]

#12 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 07:34 AM

Hi,

id3dxEffect->SetMatrix(id3dxEffect->GetParameterByName(NULL, "matWorldViewProj"), &matView );

I think that there is problem with this line too. "MatWorldViewProj" isn't the view matrix, but world-view-projection matrix. You'll need to multiply those 3 matrices to get the correct worldviewproj matrix.

Cheers!

Edited by kauna, 04 September 2012 - 07:38 AM.


#13 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 08:13 AM

ok i think i got one spot light but there is still some problem with alpha
http://prntscr.com/eyv1c

its just white i cant see terrain texture, u have any idea about that ?

#14 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 08:21 AM

It is really hard to say what's going on in the screen shot. I think that there might still be problems with the matrices.

Otherwise, the pixel shader you presented earlier doesn't do any texture lookups, so you don't get any texture on the mesh/mess on the screen.

When using shaders, you'll need to be rather precise what you want it to do. Binding textures doesn't have any effect if you don't read the texture in the pixel shader and write it out.

Cheers!

#15 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 08:50 AM

so its mean, vertex texture in must be with effect file ?

#16 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 08:56 AM

Not really, you may bind your textures as earlier. You'll need to use tex2D command in the shader to read the texture.

But I'd fix the transform part first. Maybe SetMatrix is correct in this case :)

Cheers!

#17 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 11:10 AM

I dont think so its about matrix, we got one result with that but all terrain is black, first we need to see terrain after we can understand its matrix problem :P

can u give me any basic code for tex2D with c++ side ?

#18 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 11:25 AM

You can't see the terrain unless you get your matrices right. If your model-view-projection matrix is messed up, you get just garbage on the screen.


There is plenty of examples how to use tex2D on the internet. Even the links I provided earlier have some examples how to use them. Also, DirectX SDK has many basic examples on getting started with shaders.

Pratically:

float4 Color = tex2D(s,t);

where t is a float2 texture coordinate and s is a sampler state describing how to read filter the texture, such as:

sampler s = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

Cheers!

Edited by kauna, 04 September 2012 - 11:30 AM.


#19 lyzerk   Members   -  Reputation: 120

Like
0Likes
Like

Posted 04 September 2012 - 11:45 AM

hmm i understand that
i did it like this

[source lang="cpp"] D3DXVECTOR2 v(camera.camera.x, camera.camera.z); id3dxEffect->SetValue(id3dxEffect->GetParameterByName(NULL, "textureT"), &v, sizeof(v) );[/source]

[source lang="cpp"]float2 textureT;sampler Texture2 = sampler_state{ magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR; AddressU = mirror; AddressV = mirror;};float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1 ) : COLOR{ float4 diffuse = { 1.0f, 1.0f, 1.0f, 1.0f}; float4 ambient = {1.0f, 1.0f, 1.0f, 1.0f}; return tex2D(Texture2, textureT) * (ambient + diffuse * saturate(dot(Light, Norm)));}[/source]
its just taking one color, am i doing it wrong ? (my texture is earth)
result : http://prntscr.com/ez6x7

#20 kauna   Members   -  Reputation: 1127

Like
0Likes
Like

Posted 04 September 2012 - 11:57 AM

You'll need to use the texture coordinates that you have generated earlier. At this moment, your pixel shader is reading texture at one and same location defined by your camera position.

That is, you'll need to add the pixel shader to accept texture coordinates :

float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1, float2 Tex : TEXCOORD2 ) : COLOR

and you'll need to make your vertex shader to output them. Your vertex structure should already have a texture coordinates, since you calculated them at the beginning of this message chain.

Otherwise, the screenshot doesn't give clue if your matrices are correct or not.

Best regards!




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