Hot Lab #2 - Demo 12 : Bounding Sphere - The Mail Owl Scrolls

Hot Lab #2 - Demo 12 : Bounding Sphere

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DX12BoundingSphere
{
    
public class Form1 : System.Windows.Forms.Form
    {
        
private System.ComponentModel.Container components = null;
        
private Device GraphicDevice;

        
//My object
        
private WorldObject myObject;

        
private float angle = 0;

        
public Form1()
        {
            InitializeComponent();
        }

        
protected override void Dispose( bool disposing )
        {
            
if( disposing )
            {
                
if (components != null
                {
                    components.Dispose();
                }
            }
            
base.Dispose( disposing );
        }

        
#region Windows Form Designer generated code

        private void 
InitializeComponent()
        {
            
// 
            // Form1
            // 
            
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            
this.ClientSize = new System.Drawing.Size(292, 266);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
            
this.Load += new System.EventHandler(this.Form1_Load);

        }
        
#endregion

        
[STAThread]
        
static void Main() 
        {
            Form1 frm = 
new Form1();

            frm.InitializeGraphics();
            frm.Show();

            
while (frm.Created)
            {
                frm.Render();
                Application.DoEvents();
            }

            frm.DisposeGraphics();
        }

        
public void InitializeGraphics()
        {
            PresentParameters pp = 
new PresentParameters();
            
            pp.Windowed = 
true;
            pp.BackBufferFormat = Format.X8R8G8B8;
            pp.SwapEffect = SwapEffect.Discard;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.EnableAutoDepthStencil = 
true;

            GraphicDevice = 
new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,pp);

            GraphicDevice.RenderState.Lighting = 
true;
            
//GraphicDevice.RenderState.Ambient = Color.DarkBlue;
            
GraphicDevice.RenderState.Ambient = Color.Gray;

            
//instantiate cube
            
myObject = new WorldObject(ref GraphicDevice);
        }

        
public void DisposeGraphics()
        {
            GraphicDevice.Dispose();
        }

        
public void Render()
        {
            GraphicDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,Color.CornflowerBlue  ,1,0);

            GraphicDevice.Lights[0].Type = LightType.Directional;
            GraphicDevice.Lights[0].Direction = 
new Vector3(0,0,1);
            GraphicDevice.Lights[0].Attenuation1 = 0.01f;

            GraphicDevice.Lights[0].Enabled = 
true;

            GraphicDevice.Transform.Projection = Matrix.PerspectiveFovLH((
float)Math.PI / 4, this.Width / this.Height,1,1000);
            GraphicDevice.Transform.View = Matrix.LookAtLH(
new Vector3(0,0,-2 * myObject.radius),new Vector3(0,0,0),new Vector3(0,1,0));

            GraphicDevice.BeginScene();
            
            GraphicDevice.Transform.World = Matrix.Multiply(Matrix.RotationX(Geometry.DegreeToRadian(-90)),Matrix.RotationY(Geometry.DegreeToRadian(angle)));
            myObject.Render(
ref GraphicDevice);

            angle += 1f;
            
if (angle == 360.0f)
            {
                angle = 0;
            }
            
            GraphicDevice.EndScene();

            GraphicDevice.Present();
        }

        
private void Form1_Load(object sender, System.EventArgs e)
        {
        
        }

        
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            
if (e.KeyChar.ToString() == "w")
            {
                
if (GraphicDevice.RenderState.FillMode == FillMode.WireFrame)
                {
                    GraphicDevice.RenderState.FillMode = FillMode.Solid;
                    GraphicDevice.RenderState.CullMode = Cull.CounterClockwise;
                }
                
else
                
{
                    GraphicDevice.RenderState.FillMode = FillMode.WireFrame;
                    GraphicDevice.RenderState.CullMode = Cull.None;
                }
            }
        }
    }

    
class WorldObject
    {
        Mesh tiny;
        Material[] tinymaterials;
        Texture[] tinytextures;
        
float _radius;
        Vector3 _centre;

        
public float radius
        {
            
get
            
{
                
return _radius;
            }

        }

        
public Vector3 centre
        {
            
get
            
{
                
return _centre;
            }
        }

        
public WorldObject(ref Device dev)
        {
            ExtendedMaterial[] tinyextendedmaterials;

            tiny = Mesh.FromFile("tiny.x",MeshFlags.Managed,dev,
out tinyextendedmaterials);

            
if ((tinyextendedmaterials != null) && (tinyextendedmaterials.Length > 0))
            {
                tinymaterials = 
new Material[tinyextendedmaterials.Length];
                tinytextures = 
new Texture[tinyextendedmaterials.Length];

                
for (int i = 0; i < tinyextendedmaterials.Length;i++)
                {
                    tinymaterials[i] = tinyextendedmaterials[i].Material3D;

                    
//Check for texture
                    
if ((tinyextendedmaterials[i].TextureFilename != null)  && (tinyextendedmaterials[i].TextureFilename != string.Empty))
                    {
                        tinytextures[i] = TextureLoader.FromFile(dev,tinyextendedmaterials[i].TextureFilename);
                    }
                }                                       
            }

            
//Calculate Bounding Sphere
            
VertexBuffer vb = tiny.VertexBuffer;
            GraphicsStream stm = vb.Lock(0,0,LockFlags.None);
            _radius = Geometry.ComputeBoundingSphere(stm,tiny.NumberVertices,tiny.VertexFormat,
out _centre);
        }

        
public void Render(ref Device dev)
        {
            
for (int i = 0; i < tinymaterials.Length;i++)
            {
                dev.SetTexture(0,tinytextures[i]);
                dev.Material = tinymaterials[i];
                tiny.DrawSubset(i);
            }
        }
    }
}

powered by IMHO 1.2

Published Monday, August 01, 2005 2:35 PM by mailowl

Comments

# Direct X Hot Lab #2 - Conclusion

Monday, August 01, 2005 3:09 PM by TrackBack

# re: Hot Lab #2 - Demo 12 : Bounding Sphere

I searched for Bounding Sphere on google and found this only useful part is at the bottom:
//Calculate Bounding Sphere
VertexBuffer vb = tiny.VertexBuffer;
GraphicsStream stm = vb.Lock(0,0,LockFlags.None);
_radius = Geometry.ComputeBoundingSphere(stm,tiny.NumberVertices,tiny.VertexFormat,out _centre);

but I'm not sure what I do with it or what its for ????

Monday, October 10, 2005 3:25 AM by Richard

# Moms work at home.

Work at home moms. Wahm com the online magazine for work at home moms. Moms work from home. Work for stay at home moms. Amazon com work at home moms. Work at home ideas for moms wahmoms net.

Thursday, October 30, 2008 4:24 AM by Moms work at home.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: