All pastes #616769 Raw Edit

Something

public text v1 · immutable
·published 2007-07-12 20:54 UTC
rendered paste body
// Comment this line out to have two triangles
//#define SINGLETRIANGLE  


// Comment this line out for default culling
#define CULLNONE        
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 Craig.Direct3D
{
    public class Game : System.Windows.Forms.Form
    {
        private Device device;
        private VertexBuffer vertices;


        private Texture texture;


        static void Main()
        {
            Game app = new Game();
            app.InitializeGraphics();
            app.Show();
            while (app.Created)
            {
                app.Render();
                Application.DoEvents();
            }
            app.DisposeGraphics();
        }
        protected bool InitializeGraphics()
        {
            PresentParameters pres = new PresentParameters();
            pres.Windowed = true;
            pres.SwapEffect = SwapEffect.Discard;
            device =
                    new Device(0, DeviceType.Hardware,
                    this, CreateFlags.SoftwareVertexProcessing, pres);
            //change this later
            device.RenderState.CullMode = Cull.None;
            device.RenderState.Lighting = false;
            texture = CreateOverlayTexture(device);
            vertices = CreateVertexBuffer(device);
            return true;
        }
        protected Texture CreateOverlayTexture(Device device)
        {
            Texture t = TextureLoader.FromFile(device, "gg.bmp");
            return t;
        }
        protected void Render()
        {
            // Sets the surface to black everywhere
            device.Clear(ClearFlags.Target, Color.Bisque, 1.0F, 0);
            device.BeginScene();
            device.SetTexture(0, texture);


            // 3D Rendering calls go here
            SetupMatrices();
            device.SetStreamSource(0, vertices, 0);
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);


            device.EndScene();
            device.Present();
        }
        protected VertexBuffer CreateVertexBuffer(Device device)
        {
            device.VertexFormat =
                    CustomVertex.PositionTextured.Format;
            VertexBuffer buf = new VertexBuffer(
                    typeof(CustomVertex.PositionTextured), // What type 
                    6,                                    // How many 
                    device,                               // The device
                    0,                                    // Default usage
                    CustomVertex.PositionTextured.Format,  // Vertex format
                    Pool.Default);                        // Default pooling
            PopulateVertexBuffer(buf);
            return buf;
        }
        protected void PopulateVertexBuffer(VertexBuffer vertices)
        {
            // Note use of TransformedTextured
            CustomVertex.PositionTextured[] verts =
                    (CustomVertex.PositionTextured[])vertices.Lock(0, 0);
            int i = 0;
            verts[i++] = new CustomVertex.PositionTextured(
                    -1, 2, 0, 1, 0); // top-right
            verts[i++] = new CustomVertex.PositionTextured(
                    -1, -1, 0, 1, 1); // bttom-left
            verts[i++] = new CustomVertex.PositionTextured(
                    1, -1, 0, 0, 1); // bottom-right

            verts[i++] = new CustomVertex.PositionTextured(
                    1, 2, 0, -1, 0); // top left
            verts[i++] = new CustomVertex.PositionTextured(
                    1, -1, 0, -1, 1); // bottom left
            verts[i++] = new CustomVertex.PositionTextured(
                    -1, 2, 0, 0, 0); // top right

            vertices.Unlock();
        }
        protected void SetupMatrices()
        {
            float angle = Environment.TickCount / 500.0F;
            device.Transform.World = Matrix.RotationY(angle);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0.5F, -3),
                    new Vector3(0, 0.5F, -0.5F), new Vector3(0, 1, 0));
            device.Transform.Projection =
                    Matrix.PerspectiveFovLH((float)Math.PI / 4.0F, 1.0F, 1.0F, 5.5F);
        }
        protected void DisposeGraphics()
        {
            device.Dispose();
        }
    }
}