240 likes | 427 Views
Material & Lighting. Chapter 7. What is Material?. When using lighting for the 3D program, the surface color is determined by Material . Therefore we can get different colorful solids if we use many different Materials in a 3D program. The following are properties of class Material.
E N D
Material & Lighting Chapter 7
What is Material? When using lighting for the 3D program, the surface color is determined by Material. Therefore we can get different colorful solids if we use many different Materials in a 3D program. The following are properties of class Material.
How to use Material The code is the following: Material mtrl = new Material(); mtrl.Diffuse = Color1; mtrl.Ambient = Color2; m_device.Material = mtrl; Then every object will have color of Color1 if it has lights, and color of Color2 id without lights.
Lights There are different type of 3D lights 1. Directional, means parallel light like sun light Need to set:1. Type , which is LightType.Directional ,2. Direction, which is a 3D vector3. Diffuse, which is the light color4. Set LingtingEnable = true
Set Normal direction for vertex When a light is emitting to on a surface, the reflection of the light will be depended on the normal direction of each point on the surface. For a planar surface, the normal direction is a constant vector. However, for other surface, we have to define normal direction for each vertex of all triangles, the 3D program can automatically calculate the normal direction of the triangles Normal directions
Add Normal to Vertices • private void SetVertex() • { • Vertices = new CustomVertex.PositionNormalTextured[8]; Vertices[0].Position = new Vector3(-0.7f, -1.0f, 1.0f );Vertices[1].Position = new Vector3( 0.7f, -1.0f, 1.0f ); Vertices[2].Position = new Vector3(-0.7f, -1.0f, -1.0f ); Vertices[3].Position = new Vector3( 0.7f, -1.0f, -1.0f ); • // two faces have different directionsVertices[4].Position = new Vector3( 0.7f, -1.0f, 1.0f );Vertices[5].Position = new Vector3(-0.7f, -1.0f, 1.0f );Vertices[6].Position = new Vector3( 0.7f, -1.0f, -1.0f );Vertices[7].Position = new Vector3(-0.7f, -1.0f, -1.0f );
Vertices[0].Tu = 0.0f; Vertices[0].Tv = 0.0f;Vertices[1].Tu = 0.5f; Vertices[1].Tv = 0.0f; Vertices[2].Tu= 0.0f; Vertices[2].Tv = 1.0f;Vertices[3].Tu = 0.5f; Vertices[3].Tv = 1.0f; • Vertices[4].Tu = 0.5f; Vertices[4].Tv = 0.0f;Vertices[5].Tu = 1.0f; Vertices[5].Tv = 0.0f;Vertices[6].Tu = 0.5f; Vertices[6].Tv = 1.0f;Vertices[7].Tu = 1.0f; Vertices[7].Tv = 1.0f; • // now add normal to vertices • Vertices[0].Normal=Vertices[1].Normal= Vertices[2].Normal=Vertices[3].Normal= new Vector3( 0.0f, 1.0f, 0.0f ); • Vertices[4].Normal=Vertices[5].Normal = Vertices[6].Normal=Vertices[7].Normal= new Vector3( 0.0f, -1.0f, 0.0f ) • }
Setup Lights • privatevoidSetLights() • { • Material mtrl = new D3D.Material(); mtrl.Diffuse = Color.White ; mtrl.Ambient = Color.White ; m_device.Material = mtrl; m_device.Lights[0].Type = LightType.Directional; // typem_device.Lights[0].Direction = new Vector3(0.0f, -1.0f, 0.0f) ;// directionm_device.Lights[0].Enabled = true; //turn it onm_device.RenderState.Ambient =Color.FromArgb(0x404040); • // if no light, what is the color • }
Render State Just add two line of code as the following: • privatevoidRender() • { • . . . . . . . . . . . . . . . • m_device.RenderState.Lighting=true; • SetLights(); • . . . . . . . . . . . . . . . • }
Output When face up When face down
Make a cylinder surface Divide each boundary circles evenly into 60 parts. Connect points vertically to get 60 rectangles. Then split each rectangle into two triangles. Draw all those triangles will get the cylinder. The normal direction is a direction that perpendicular to surface
Make VertexBuffer of 122 points • CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vb.Lock(0,0); for (int i = 0; i < 61; i++){ • float theta = (float)(2 * Math.PI * i) / 60; • verts[2* i].Position= new Vector3 ( (float)Math.Cos(theta), -1, (float)Math.Sin(theta));verts[2* i].Normal=new Vector3( (float)Math.Cos(theta), 0, (float)Math.Sin(theta));verts[2* i +1].Position=new Vector3( (float)Math.Cos(theta), 1, (float)Math.Sin(theta));verts[2* i +1].Normal=new Vector3( (float)Math.Cos(theta), 0, (float)Math.Sin(theta)); • }vb.Unlock();
Setup Lights • privatevoidSetLights() • { • Material mtrl = new Material(); mtrl.Diffuse = Color.White ; mtrl.Ambient = Color.White ; m_device.Material = mtrl; m_device.Lights[0].Type = LightType.Directional; // typem_device.Lights[0].Diffuse = Color.Yellow ; // light color float alpha = Environment.TickCount / 250.0f;m_device.Lights[0].Direction = new Vector3((float)Math.Cos(alpha), 1.0f, (float)Math.Sin(alpha));m_device.Lights[0].Enabled = true ; //turn it onm_device.RenderState.Ambient =Color.FromArgb(0x404040); • }
Teapot & directional Light private bool InitMesh() { meshTeapot = Mesh.Teapot(device); if(meshTeapot==null) return false; return true; } Use Microsoft built-in Teapot mesh
SetLights float x=1.0f, y=-1.0f; // changing light position private void SetLights() { Material mtrl = new Material(); mtrl.Diffuse = Color.Green; mtrl.Ambient = Color.Green; device.Material = mtrl; device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White device.Lights[0].Direction = new Vector3(x, y, 1.0f); device.Lights[0].Update(); device.Lights[0].Enabled = true; device.RenderState.Ambient = Color.FromArgb(0x404040); }
On Key Down protected override void OnKeyDown( KeyEventArgs e) { Keys k = e.KeyData; if(k==Keys.Left) x +=0.1f; if(k==Keys.Right ) x -=0.1f; if(k==Keys.Up) y -=0.1f; if(k==Keys.Down ) y +=0.1f; } Use arrow keys to control location of the lights
2. Point light, means the light feels like a candle light Need to set:1. Type , which is LightType.Point ,2. Position, which is the location of the light3. Attenuation1, which is how the light intensity decreasing over the distance. 4. Range5. Set Enable = true
Teapot & Point Light float x=1.0f, y=-1.0f; // changing light position private void SetLights() { Material mtrl = new Material(); mtrl.Diffuse = Color.Green; mtrl.Ambient = Color.Green; device.Material = mtrl; device.Lights[0].LightType = Point; device.Lights[0].Position = new Vector3(x, 1.3f, -1.7f); device.Lights[0].Attenuation1 =0.5f;device.Lights[0].Range = 100.0f;device.Lights[0].Direction.Normalize(); device.Lights[0].Update(); device.Lights[0].Enabled = true; device.RenderState.Ambient = Color.FromArgb(0x404040); }
On Key Down protected override void OnKeyDown( KeyEventArgs e) { Keys k = e.KeyData; if(k==Keys.Left) // rotation to left { float t = x*(float)Math.Cos(0.1f) -z*(float)Math.Sin(0.1f) ; z = x*(float)Math.Sin(0.1f) +z*(float)Math.Cos(0.1f) ; x=t; } if(k==Keys.Right ) // rotation to right { float t = x*(float)Math.Cos(-0.1f) -z*(float)Math.Sin(-0.1f) ; z = x*(float)Math.Sin(-0.1f) +z*(float)Math.Cos(-0.1f) ; x=t; } if(k==Keys.Up) y -=0.1f; if(k==Keys.Down ) y +=0.1f; }
3. Spot light, means the light feels like a spot flash light Need to set:1. Type , which is LightType.Spot .2. Position, which is the location of the light3. Direction, Range4. Attenuation1, which is how the light intensity decreasing over the distance. 5. Inner angle and outer angle4. Set Enable = true
Spot Light float x=2.0f, y=2.0f, z=-4.0f; // changing light position private void SetLights() { device.Lights[0].Type = LightType.Spot ; device.Lights[0].Diffuse = Color.White;device.Lights[0].Position = new Vector3(x, y, z); device.Lights[0].Attenuation1 =0.45f; device.Lights[0].InnerConeAngle = (float)(Math.PI / 40); device.Lights[0].OuterConeAngle = (float)(Math.PI / 4); device.Lights[0].Range = 100.0f; device.Lights[0].Direction = new Vector3((-1)*x, (-1)*y, (-1)*z); device.Lights[0].Direction.Normalize(); device.Lights[0].Update();device.RenderState.Ambient = Color.FromArgb(0x404040); }