1 / 24

Material & Lighting

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.

tahir
Download Presentation

Material & Lighting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Material & Lighting Chapter 7

  2. 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.

  3. 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.

  4. 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

  5. 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

  6. 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 );

  7. 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 ) • }

  8. 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 • }

  9. Render State Just add two line of code as the following: • privatevoidRender() • { • . . . . . . . . . . . . . . . • m_device.RenderState.Lighting=true; • SetLights(); • . . . . . . . . . . . . . . . • }

  10. Output When face up When face down

  11. 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

  12. 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();

  13. 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); • }

  14. Output

  15. Teapot & directional Light private bool InitMesh() { meshTeapot = Mesh.Teapot(device); if(meshTeapot==null) return false; return true; } Use Microsoft built-in Teapot mesh

  16. 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); }

  17. 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

  18. Output

  19. 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

  20. 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); }

  21. 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; }

  22. Output

  23. 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

  24. 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); }

More Related