Lighting Spotlights

Started by
14 comments, last by EnigmaX 21 years, 2 months ago
Ok I have a question. I''ve got a tile based terrain where each tile is like 2 triangles with a texture put on top of it. I use this as my texture setup.

d3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
d3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3dDevice->SetTextureStageState(0,D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
d3dDevice->SetTextureStageState(0,D3DTSS_MINFILTER, D3DTEXF_LINEAR);

So now when I enable lighting, even ambient light doesn''t show up. I''ve got colored vertices underneath even though they don''t show and I''ve got normals. I''m enabling my lighting like this:

//setup lighting
d3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE);
d3dDevice->SetRenderState(D3DRS_COLORVERTEX, TRUE);
d3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_COLORVALUE(1.0, 1.0, 0.0, 0.0));

// Set up a material
D3DMATERIAL8 mtrl;
ZeroMemory(&mtrl, sizeof(D3DMATERIAL8));
mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f;
mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
d3dDevice->SetMaterial(&mtrl);


So my question is, why isn''t the lighting showing up? Is it because of the way I''m using the textures? I''ve loaded a .x file in there, too and the lighting seems to work on it. Also, if I comment out the texture code I posted above, the lighting seems to work. Any help?
Advertisement
AP, the output of the lighting stage is diffuse and specular, fed into the texture stages as D3DTA_DIFFUSE and D3DTA_SPECULAR. You''re not using either. Try this:

d3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
d3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);
d3dDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_TEXTURE);
Donavon KeithleyNo, Inky Death Vole!
Also, by default the lighting stage pulls material diffuse from the vertices and material ambient from the current material. You might want them both to come from the vertex colors, so:

d3dDevice->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
Donavon KeithleyNo, Inky Death Vole!
quote:
Here''s how you calculate the normal:
vector a = vertices[2] - vertices[0];
vector b = vertices[1] - vertices[0];
normal = crossproduct(a, b);


Doesn''t that method only find the surface normal? How do you know how many normals a vertex needs? I have a tiled terrain with hills, etc. but am calculating my normals like you posted and it''s not working properly.


Donavon Keithley - thanks for your help. That explained my problem.
Ok, I found a tutorial that explained that finding the vertex normal is as easy as normalising the sum of the face vertices surrounding it.

I haven''t implemented this yet but I''m afraid I''m going to have a problem because I scale my terrain on the up axis before rendering. Will this cause a problem? Do you know what I can do to get around it?
so I''m calculating my vertex normals like this:



  for(int j=0;j<=mapDepth;j++)	{for(int i=0;i<=mapWidth;i++){	D3DXVECTOR3 sum;			//get left face normalif(i > 0)  sum+=myVertices[j*(mapWidth+1)+i-1].normal;//get right face normalif(i < mapWidth)  sum+=myVertices[j*(mapWidth+1)+i+1].normal;//get top face normalif(j < mapDepth)  sum+=myVertices[(j+1)*(mapWidth+1)+i].normal;//get bottom face normalif(j > 0)  sum+=myVertices[(j-1)*(mapWidth+1)+i].normal;D3DXVec3Normalize((D3DXVECTOR3*)&myVertices[j*(mapWidth+1)+i].normal, &sum);//copy vertex into vertex buffervertices[j*(mapWidth+1)+i] = myVertices[j*(mapWidth+1)+i];}}  


And the original normal is taken like this:


  vertex[0] = vertices[0].position;vertex[1] = vertices[1].position;vertex[2] = vertices[2].position;CalculateNormal(vertex, &vertices[0].normal);D3DXVec3Normalize((D3DXVECTOR3*)&vertices[0].normal, vertex);  


where my CalculateNormal function is this:

  void CalculateNormal(D3DVECTOR v[3], D3DVECTOR *normal){	D3DVECTOR a, b;	static const int x = 0;	static const int y = 1;	static const int z = 2;	a.x = v[0].x - v[1].x;	a.y = v[0].y - v[1].y;	a.z = v[0].z - v[1].z;	b.x = v[1].x - v[2].x;	b.y = v[1].y - v[2].y;	b.z = v[1].z - v[2].z;	//find cross product to get the normal	normal->x = (a.y*b.z) - (a.z*b.y);	normal->y = (a.z*b.x) - (a.x*b.z);	normal->z = (a.x*b.y) - (a.y*b.x);}  


Now I''m scaling my terrain after I caluclate normals so that may be my main problem but whats happening is, that depending on where the camera is looking, its like the light goes on and off. Sometimes its bright, sometimes its dark. Can anyone help? Or at least tell me if I''m calculating the normals correctly?

This topic is closed to new replies.

Advertisement