# Problem with specular reflection

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

## Recommended Posts

Hello, I've got problems with the specular reflection when I draw the surface with drawPrimitivUP. The problem is there is no light effect on the surface at all. If I paint it black, it stays black like a black hole, :-) no reflections. If I do the same with some standard meshes like a sphere everything works proper. Do somebody has a wise advice? thankfully Armin [Edited by - arri on July 18, 2005 3:37:23 PM]

##### Share on other sites
- Do you have lighting enabled? (D3DRS_LIGHTING)
- Have you setup your light structure correctly, and enabled it? (IDirect3DDevice9::SetLight, LightEnable)

If you can, please post the relevant bits of code.

##### Share on other sites
that's my relevant code:

hr = g_dev.TestCooperativeLevel
If hr = D3DERR_DEVICENOTRESET Then
g_dev.Reset g_d3dpp
RestoreDeviceObjects
ElseIf hr <> 0 Then
Exit Sub
End If

m_graphroot.UpdateFrames

D3DUtil_ClearAll dColor(fX.bkRnd).xP
g_dev.SetRenderState D3DRS_AMBIENT, ambientColor

g_dev.SetRenderState D3DRS_LIGHTING, 1
g_dev.SetRenderState D3DRS_ZENABLE, 1
g_dev.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
g_dev.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
g_dev.SetRenderState D3DRS_SPECULARENABLE, 1
g_dev.SetRenderState D3DRS_ZFUNC, D3DCMP_LESS

g_dev.SetRenderState D3DRS_ALPHABLENDENABLE, 1
g_dev.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
g_dev.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
g_dev.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_ALWAYS
g_dev.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE

g_dev.SetRenderState D3DRS_EDGEANTIALIAS, 1

g_dev.SetRenderState D3DRS_ZBIAS, 0
g_dev.SetRenderState D3DRS_CLIPPING, 1
g_dev.SetRenderState D3DRS_STENCILENABLE, 1
g_dev.SetRenderState D3DRS_LOCALVIEWER, 0
g_dev.SetRenderState D3DRS_DITHERENABLE, 0

g_dev.BeginScene

g_dev.SetTransform D3DTS_WORLD, m_graphroot.GetMatrix
g_dev.SetRenderState D3DRS_ALPHABLENDENABLE, 1

g_dev.SetRenderState D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL
g_dev.SetRenderState D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL

g_dev.SetRenderState D3DRS_ZENABLE, 0
g_dev.SetTexture 0, Nothing

g_dev.SetRenderState D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL

mat1.emissive = ColorValue4(0, 0, 0, 0)
mat1.diffuse = colB: mat1.Ambient = colB: mat1.specular = colB
mat1.Power = 0.1
g_dev.SetMaterial mat1

g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_CCW 'D3DCULL_NONE
g_dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, d3P(i).vArea(0).ctV - 1, d3P(i).vArea(n).pTs(0), Len(d3P(i).vArea(n).pTs(0))

g_dev.SetTransform D3DTS_WORLD, m_graphroot.GetMatrix
g_dev.SetRenderState D3DRS_ZENABLE, 1

g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_CCW
g_dev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, d3P(i).vArea(0).ctV - 1, d3P(i).vArea(n).pTs(0), Len(d3P(i).vArea(n).pTs(0))

g_dev.EndScene

D3DUtil_PresentAll m_hwnd

I think, my light-setuo is ok because with the same setup it works proper with the standard meshes! And light in general is enabled!

##### Share on other sites
It can be because the surface is too large. You use gouraud, which samples light in vertices and interpolates it over the entire poly. If all vertices are dark, the whole poly will be dark.

##### Share on other sites
I don't use the vertice color! For every color type like ambient diffuse etc. did I use the material as source. So this can't be the reason. As saw, I draw every surface twice. At first without z-enabled and after that with z-enabled. That's for the correct dispaly of semi transparent surfaces. But This can't be the problem either, I already tested the code without switching the z-enabled-function off. Do you have another idea. maybe another renderstate setting?

##### Share on other sites
do nobody have any suggestions?

##### Share on other sites
What is colB set to? My theory is that your alpha is not set up properly.

I would turn off blending for now, and also turn off MODULATE, and change it to SELECTARG1.

Also, why are you setting just the alpha op, and not the color op?

##### Share on other sites
colB is a color that could be set to any r g b a -values. What do you mean with that my alpha is not properly. Can't I set it to any values besides 0 for sure?

The default value for the color op is MODULATE, I didn't try to modify it yet. Also I have too admit that I'm not really common with all this color and blending options!

What do you think is a good setting for the color op?

##### Share on other sites
I just tested the code with settings you seggested: alphaop = selectarg1, colorop = selectarg1, alphablending =0. I also switched all alpha values to 1.

But I think this will not fix the problem, because I don't want to use the color from a texture. I want to use my color from my material in order that the surface appears in one color. And the real problem is that the surface actually stays in defined color colB without any reflection of the light!

In my vertices I use a diffuse color value, but in this case, when I want to use the color from material, by setting the color source options, the vertice color won't have any effects, right?

##### Share on other sites
Quote:
 Original post by arriIf I do the same with some standard meshes like a sphere everything works proper. Do somebody has a wise advice?

The fact that other meshes work as expected, but your mesh doesn't indicates a problem with your mesh or its vertices.

Obvious question: Do the vertices in your mesh have normals? Are they normalised?

No normals == no lighting.

Additionally, what is the order of the components in your vertex structure?

With the traditional fixed function pipeline, the order of the components of each vertex is important; for example the normal in your vertex MUST be before the vertex colour, and both of those MUST be before your texture coordinates.

i.e.

This WILL NOT work:
struct MYVERTEX{   D3DXVECTOR3 position;   D3DCOLOR    colour;   D3DXVECTOR3 normal;   D3DXVECTOR2 texture;};

But this WILL work:
struct MYVERTEX{   D3DXVECTOR3 position;   D3DXVECTOR3 normal;   D3DCOLOR    colour;   D3DXVECTOR2 texture;};

##### Share on other sites
It's the way gouraud shading works: DirectX computes light in vertices and then interpolates the light over entire poly. It doesn't depend on how you pass color to DirectX. Or how would you expect DX to act? It wouldn't compute color in every pixel (this is called Phong shading, currently unsupported). You can use vertex/pixel shaders to achieve this effect (there are so many examples).

##### Share on other sites

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