Jump to content
  • Advertisement
Sign in to follow this  
ReaVeR-Andrey

No diffuse lighting!

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My engine fails to apply diffuse lighting to terrain. The material has proper values (D3DCOLORVALUE(1,1,1,1)), same for light. I've just displayed mesh (tiny.x) which had proper lighting. It seems that the terrain doesn't get light at all. My normals have correct values. Here is my drawing code:
   pos += Vector3D ( -(float) width / 2, 0, (float) height / 2 );

   D3D::SetFVF ( TVertex::FVF );
   D3D::lpDev->SetStreamSource ( 0, pVB, 0, sizeof ( TVertex ) );
   D3D::lpDev->SetIndices ( pIB );

   TVertex* verts;
   pVB->Lock ( 0, 0, (void**) &verts, D3DLOCK_DISCARD );

   DWORD num = 0;

   WORD lastMat = cells [0].matIndex;
   D3D::SetMaterial ( mats [lastMat].mat );
   D3D::SetTexture ( 0, mats [lastMat].tex );
   D3D::SetNullTexture ( 1 );
  
   for ( int y = 0; y < height-1; y++ )
   {
      int base = y * width;
      for ( int x = 0; x < width-1; x++ )
      {
         if ( num && cells [x+base].matIndex != lastMat )
         {
            pVB->Unlock ();
            HRESULT res = D3D::lpDev->DrawIndexedPrimitive ( D3DPT_TRIANGLELIST, 0, 0, num * 4, 0, num * 2 );
            pVB->Lock ( 0, 0, (void**) &verts, D3DLOCK_DISCARD );
            WORD lastMat = cells [x+base].matIndex;
            D3D::SetMaterial ( mats [lastMat].mat );
            D3D::SetTexture ( 0, mats [lastMat].tex );
            num = 0;
         }
         verts [num*4+0].v = Vector3D ( (float) x, (float) cells [x+base].height * step, -(float) y ) + pos;
         verts [num*4+0].n = normals [cells [x+base].normal];
         verts [num*4+0].t = Vector2D ( 0, 0 );
         verts [num*4+1].v = Vector3D ( (float) x+1, (float) cells [x+1+base].height * step, -(float) y ) + pos;
         verts [num*4+1].n = normals [cells [x+1+base].normal];
         verts [num*4+1].t = Vector2D ( 1, 0 );
         verts [num*4+2].v = Vector3D ( (float) x, (float) cells [x+width+base].height * step, -(float) y-1 ) + pos;
         verts [num*4+2].n = normals [cells [x+width+base].normal];
         verts [num*4+2].t = Vector2D ( 0, 1 );
         verts [num*4+3].v = Vector3D ( (float) x+1, (float) cells [x+width+1+base].height * step, -(float) y-1 ) + pos;
         verts [num*4+3].n = normals [cells [x+width+1+base].normal];
         verts [num*4+3].t = Vector2D ( 1, 1 );
         num++;
         if ( num >= maxVBItems )
         {
            pVB->Unlock ();
            HRESULT res = D3D::lpDev->DrawIndexedPrimitive ( D3DPT_TRIANGLELIST, 0, 0, num * 4, 0, num * 2 );
            pVB->Lock ( 0, 0, (void**) &verts, D3DLOCK_DISCARD );
            num = 0;
         }
      }
   }
   pVB->Unlock ();
   if ( num )
      D3D::lpDev->DrawIndexedPrimitive ( D3DPT_TRIANGLELIST, 0, 0, num * 4, 0, num * 2 );

Share this post


Link to post
Share on other sites
Advertisement
It fails to light it... but what exactly do you get? all white, all black?

A couple of possibilities:

- Your normals could be wrong, have you checked them

- Your light properties might be wrong
- check the range and direction in particular

hth
Jack

Share this post


Link to post
Share on other sites
My normals are all right (they are facing almost up).
Light properties are:
light.type = D3DLIGHT_DIRECTIONAL;
light.dir = Vector3D ( 0, -1, 0 ).normalize ();
light.diffuse = Color ( 1, 1, 1, 1 );
light.range = 1000;

Share this post


Link to post
Share on other sites
How many tiles are you rendering (i.e. how many vertices?) Also, double check your materials and that you have the light enabled before you draw the terrain. Posting the actual render loop would help.

As Jack said, what's it doing wrong?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!