Hi there, I’m very new to shading and I was trying to create some terrain. I calculate all the positions and then I get the normals, but then when I render them, it all appears like a flat and shiny surface.
Edit: Gif doesn’t show sometimes, if it doesn’t, click on it.
Here is my drawing code:
protected void DrawPrimitivesUserDefined(VertexPositionColorNormal[] vertices)
{
Utils.PrimitivesEffect.CurrentTechnique = Utils.PrimitivesEffect.Techniques["BasicEffect_VertexLighting_VertexColor"];
Utils.PrimitivesEffect.World = SmartGardenCamera.worldMatrix;
Utils.PrimitivesEffect.View = SmartGardenCamera.viewMatrix;
Utils.PrimitivesEffect.Projection = SmartGardenCamera.projectionMatrix;
Utils.PrimitivesEffect.EnableDefaultLighting();
Utils.PrimitivesEffect.LightingEnabled = true; // turn on the lighting subsystem
Utils.PrimitivesEffect.VertexColorEnabled = true;
Utils.PrimitivesEffect.DiffuseColor = new Vector3(1, 0, 0);
Utils.PrimitivesEffect.AmbientLightColor = new Vector3(0, .25f, .75f);
Utils.PrimitivesEffect.EmissiveColor = new Vector3(0, 0, 1);
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.CullClockwiseFace;
rasterizerState.FillMode = FillMode.Solid;
GraphicsDevice.RasterizerState = rasterizerState;
foreach (EffectPass pass in Utils.PrimitivesEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip,vertices, 0, (Consts.TerrainWidth * 2) - 2);
}
GraphicsDevice.RasterizerState = SolidState; //SolidState is set to FillMode.Solid; Resetting the rasterizer state.
}
And more importantly, here I calculate my normals:
for (int i = 0; i < Terrain.Count; i++)
{
for (int j = 0; j < Terrain[i].Count - 2; j += 3)
{
Vector3 p1 = Terrain[i][j].Position;
Vector3 p2 = Terrain[i][j + 1].Position;
Vector3 p3 = Terrain[i][j + 2].Position;
Vector3 v1 = p2 - p1;
Vector3 v2 = p3 - p1;
Vector3 normal = Vector3.Cross(v1, v2);
normal = new Vector3(1, 1, 1);
normal.Normalize();
VertexPositionColorNormal vpcn1 = Terrain[i][j];
VertexPositionColorNormal vpcn2 = Terrain[i][j + 1];
VertexPositionColorNormal vpcn3 = Terrain[i][j + 2];
vpcn1.Normal = normal;
vpcn2.Normal = normal;
vpcn3.Normal = normal;
Terrain[i][j] = vpcn1;
Terrain[i][j + 1] = vpcn2;
Terrain[i][j + 2] = vpcn3;
}
}
//Terrain is a List<List<VertexPositionColorNormal>>.
//Each List<VertexPositionColorNormal> is a strip of terrain
Mainly taken from here.
Any guidance is very much appreciated.
I apologize for my shallow understanding of shaders and normals.