Jump to content

  • Log In with Google      Sign In   
  • Create Account

blacken

Member Since 05 Dec 2012
Offline Last Active Feb 28 2013 10:48 PM

Topics I've Started

direct3d9 mesh.toxfile

03 February 2013 - 12:08 AM

Just wondering if someone would be able to help.

 

I am using slimdx january 2012 SDK and i am having some trouble using the .toxfile method.

 

The main problem is i cant find it. I am creating my mesh and everything fine, just need to send it to an xfile and can't find it in visual studio.

 

Thanks in advance.


mesh and vertexbuffer

05 January 2013 - 09:14 PM

Just wondering if someone would be able to help me with a question that i have.

 

I am currently fiddling around with a mesh that i create at runtime.

 

It works when i don't use lighting, however every fourth position in the vertexbuffer is NaN. This doesn't seem to cause any issues in this case.

 

However, when i move to utilising lighting, i compute the normals and the vertexbuffer has seven positions for every vertex, the x,y,z of the position and the x,y,z, of the normal and then NaN in the seventh position.

 

However, when the mesh is rendered it is just garbage.

 

I am wondering if someone would be able to help with this, possibly explaining if i am doing something wrong?

 

Cheers

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D9;
using SlimDX.Windows;
using System.Runtime.InteropServices;
using WindowsFormsControlLibrary1;

namespace WindowsFormsApplication1
{
    public partial class PoleCreation : Form
    {        
        public static RenderForm Window;        // Window used for rendering.
        public static Device D3DDevice;         // Direct3D device.
        public static VertexBuffer Vertices;    // Vertex buffer object used to hold vertices.
        public static short[] index;
        public static IndexBuffer indices;
        public float[] vertexnormals;
        public Mesh mesh, normals;
        private Single rotX = 0, rotY = 0, initX = 0, initY = 0, finX = 0, finY = 0;
        private Boolean booMovingMouse = false;
        private Single WheelScroll = 0;
        public Single ModelAngle, Diameter, PoleHeight, Outer, Inner, x, y, z, Taper;

        public PoleCreation()
        {
            InitializeComponent();           
        }

        public void InitializeD3D()
        {
            vertexnormals = new float[2772];
            // Create a new PresentParameters object and fill in the necessary fields.
            PresentParameters presentParams = new PresentParameters();
            // Below are the required bare mininum, needed to initialize the D3D device.
            presentParams.Windowed = true;  
            presentParams.BackBufferHeight = userControl11.ClientRectangle.Height;
            presentParams.BackBufferWidth = userControl11.ClientRectangle.Width;            
            // Create the device.
            D3DDevice = new Device(new Direct3D(), 0, DeviceType.Hardware, userControl11.Handle, CreateFlags.HardwareVertexProcessing, presentParams);
            //mesh = new Mesh(D3DDevice, 4, 4, MeshFlags.Managed, Vertex.Format);
            mesh = new Mesh(D3DDevice, 924, 396, MeshFlags.Managed, Vertex.Format);
                                             
        }

        //private static Vertex[] BuildVertexData()
        //{
        //    Vertex[] vertexData = new Vertex[4];
        //    vertexData[0].Position = new Vector3(-1.0f, -1.0f, 0.0f);
        //    vertexData[0].Color = Color.Red.ToArgb();
        //    vertexData[1].Position = new Vector3(1.0f, -1.0f, 0.0f);
        //    vertexData[1].Color = Color.Blue.ToArgb();
        //    vertexData[2].Position = new Vector3(0.0f, 1.0f, 0.0f);
        //    vertexData[2].Color = Color.Green.ToArgb();
        //    vertexData[3].Position = new Vector3(0.0f, -1.0f, 1.0f);
        //    vertexData[3].Color = Color.Yellow.ToArgb();
        //    return vertexData;
        //}

        private Vertex[] BuildVertexData()
        {
            Vertex[] vertexData = new Vertex[396];
            int i = 0, j = 0;
            float section = 0;
            for (section = PoleHeight; section >= -0.01; section -= PoleHeight / 10)
            {
                for (ModelAngle = 0; ModelAngle < 360; ModelAngle += 10)
                {
                    y = (float)Math.Round(section,1);
                    x = Outer * (1 + Taper * j * PoleHeight / 10) * (float)Math.Cos(ModelAngle * Math.PI / 180);
                    z = Outer * (1 + Taper * j * PoleHeight / 10) * (float)Math.Sin(ModelAngle * Math.PI / 180);
                    vertexData[i].Position = new Vector3(x, y, z);
                    vertexData[i].Color = Color.LightGray.ToArgb();
                    i++;
                }
                j++;
            }
            return vertexData;
        }

        public void SetupLighting()
        {
            Material material = new Material();
            Light light = new Light();           
            light.Type = LightType.Point;
            light.Range = 100.0f;
            light.Attenuation0 = 0f;
            light.Attenuation1 = 0f;
            light.Attenuation2 = 0f;
            light.Diffuse = Color.ForestGreen;
            light.Ambient = Color.GhostWhite;
            Vector3 LightPosition = new Vector3(0f,50f,-50f);
            Vector3 vecdirection = new Vector3(0f,0f,0f);
            vecdirection = vecdirection - LightPosition;
            light.Direction = vecdirection;    // set the light direction
            light.Position = LightPosition;
            D3DDevice.SetLight(0, light);
            D3DDevice.EnableLight(0, true);
            material.Diffuse = Color.FloralWhite;
            material.Ambient = Color.LightGray;
            D3DDevice.Material = material;
        }

        public void SetupMatrices()
        {            

            // Set up our view matrix. A view matrix can be defined given an eye point,
            // a point to lookat, and a direction for which way is up. Here, we set the
            // eye five units back along the z-axis and up three units, look at the
            // origin, and define "up" to be in the y-direction.
            Vector3 Eye = new Vector3(0.0f, PoleHeight / 2, PoleHeight * 2);
            Vector3 LookAt = new Vector3(0.0f, PoleHeight / 4, 0.0f);
            Vector3 Up = new Vector3(0.0f, 1.0f, 0.0f);
            Matrix view = Matrix.LookAtLH(Eye, LookAt, Up);
            D3DDevice.SetTransform(TransformState.View, view);

            // For the projection matrix, we set up a perspective transform (which
            // transforms geometry from 3D view space to 2D viewport space, with
            // a perspective divide making objects smaller in the distance). To build
            // a perpsective transform, we need the field of view (1/4 pi is common),
            // the aspect ratio, and the near and far clipping planes (which define at
            // what distances geometry should be no longer be rendered).
            float fov = (float)Math.PI / 4;
            float apsectRatio = (float)userControl11.ClientRectangle.Width / userControl11.ClientRectangle.Height;
            float nearPlane = 1.0f;
            float farPlane = PoleHeight * 4;
            Matrix projection = Matrix.PerspectiveFovLH(fov, apsectRatio, nearPlane, farPlane);
            D3DDevice.SetTransform(TransformState.Projection, projection);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeD3D();
            SetupLighting();
            //SetupMatrices();
        }  
 
        private void userControl11_Paint(object sender, PaintEventArgs e)
        {
            if (booMovingMouse == true)
            {
                rotX = (initX - MousePosition.X) + finX;
                rotY = (initY - MousePosition.Y) + finY;
            }

            this.BackColor = SystemColors.Control;
            D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            // Begin the scene.
            D3DDevice.BeginScene();           
            // Setup the world, view and projection matrices.            
            Matrix world = Matrix.RotationYawPitchRoll(rotX / 100, rotY / 100, 0);            
            D3DDevice.SetTransform(TransformState.World, world);
            // Render the vertex buffer.
            D3DDevice.SetStreamSource(0, mesh.VertexBuffer, 0, Vertex.SizeBytes);
            D3DDevice.VertexFormat = Vertex.NormalFormat;
            D3DDevice.Indices = mesh.IndexBuffer;
            D3DDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 396, 0, 924);
            // End the scene.
            D3DDevice.EndScene();
            // Present the backbuffer contents to the screen.
            D3DDevice.Present();           
            userControl11.Invalidate();
        }

        private void ModelCreation()
        {
            //Create the vertex buffer and fill with the triangle vertices.
            mesh.LockVertexBuffer(LockFlags.None).WriteRange(BuildVertexData());
            mesh.UnlockVertexBuffer();
            mesh.LockVertexBuffer(LockFlags.None).ReadRange(vertexnormals, 0, 1188);
            mesh.UnlockVertexBuffer();
            // Turn off culling, so we see the front and back of the triangle
            D3DDevice.SetRenderState(RenderState.CullMode, Cull.None);
            // Turn off D3D lighting, since we are providing our own vertex colors
            D3DDevice.SetRenderState(RenderState.Lighting, true);
            index = new short[]{0, 1, 35, 35, 1, 2, 2, 35, 34, 34, 2, 3, 3, 34, 33, 33, 32, 3, 3, 4, 32, 32, 31, 4, 4, 5, 31, 31, 30, 5, 5, 6, 30, 30, 29, 6, 6, 7, 29, 29, 28, 7, 7, 8, 28, 28, 27, 8, 8, 9, 27, 27, 26, 9, 9, 10, 26, 26, 25, 10, 10, 11, 25, 25, 24, 11, 11, 12, 24, 24, 23, 12, 12, 13, 23, 23, 22, 13, 13, 14, 22, 22, 21, 14, 14, 15, 21, 21, 20, 15, 15, 16, 20, 20, 19, 16, 16, 17, 19, 19, 17, 18, 18, 54, 55, 55, 18, 19, 19, 55, 56, 56, 19, 20, 20, 56, 57, 57, 20, 21, 21, 57, 58, 58, 21, 22, 22, 58, 59, 59, 22, 23, 23, 59, 60, 60, 23, 24, 24, 60, 61, 61, 24, 25, 25, 61, 62, 62, 25, 26, 26, 62, 63, 63, 26, 27, 27, 63, 64, 64, 27, 28, 28, 64, 65, 65, 28, 29, 29, 65, 66, 66, 29, 30, 30, 66, 67, 67, 30, 31, 31, 67, 68, 68, 31, 32, 32, 68, 69, 69, 32, 33, 33, 69, 70, 70, 33, 34, 34, 70, 71, 71, 34, 35, 35, 71, 36, 36, 35, 0, 0, 36, 37, 37, 0, 1, 1, 37, 38, 38, 1, 2, 2, 38, 39, 39, 2, 3, 3, 39, 40, 40, 3, 4, 4, 40, 41, 41, 4, 5, 5, 41, 42, 42, 5, 6, 6, 42, 43, 43, 6, 7, 7, 43, 44, 44, 7, 8, 8, 44, 45, 45, 8, 9, 9, 45, 46, 46, 9, 10, 10, 46, 47, 47, 10, 11, 11, 47, 48, 48, 11, 12, 12, 48, 49, 49, 12, 13, 13, 49, 50, 50, 13, 14, 14, 50, 51, 51, 14, 15, 15, 51, 52, 52, 15, 16, 16, 52, 53, 53, 16, 17, 17, 53, 54, 54, 17, 18, 54, 90, 91, 91, 54, 55, 55, 91, 92, 92, 55, 56, 56, 92, 93, 93, 56, 57, 57, 93, 94, 94, 57, 58, 58, 94, 95, 95, 58, 59, 59, 95, 96, 96, 59, 60, 60, 96, 97, 97, 60, 61, 61, 97, 98, 98, 61, 62, 62, 98, 99, 99, 62, 63, 63, 99, 100, 100, 63, 64, 64, 100, 101, 101, 64, 65, 65, 101, 102, 102, 65, 66, 66, 102, 103, 103, 66, 67, 67, 103, 104, 104, 67, 68, 68, 104, 105, 105, 68, 69, 69, 105, 106, 106, 69, 70, 70, 106, 107, 107, 70, 71, 71, 107, 72, 72, 71, 36, 36, 72, 73, 73, 36, 37, 37, 73, 74, 74, 37, 38, 38, 74, 75, 75, 38, 39, 39, 75, 76, 76, 39, 40, 40, 76, 77, 77, 40, 41, 41, 77, 78, 78, 41, 42, 42, 78, 79, 79, 42, 43, 43, 79, 80, 80, 43, 44, 44, 80, 81, 81, 44, 45, 45, 81, 82, 82, 45, 46, 46, 82, 83, 83, 46, 47, 47, 83, 84, 84, 47, 48, 48, 84, 85, 85, 48, 49, 49, 85, 86, 86, 49, 50, 50, 86, 87, 87, 50, 51, 51, 87, 88, 88, 51, 52, 52, 88, 89, 89, 52, 53, 53, 89, 90, 90, 53, 54, 90, 126, 127, 127, 90, 91, 91, 127, 128, 128, 91, 92, 92, 128, 129, 129, 92, 93, 93, 129, 130, 130, 93, 94, 94, 130, 131, 131, 94, 95, 95, 131, 132, 132, 95, 96, 96, 132, 133, 133, 96, 97, 97, 133, 134, 134, 97, 98, 98, 134, 135, 135, 98, 99, 99, 135, 136, 136, 99, 100, 100, 136, 137, 137, 100, 101, 101, 137, 138, 138, 101, 102, 102, 138, 139, 139, 102, 103, 103, 139, 140, 140, 103, 104, 104, 140, 141, 141, 104, 105, 105, 141, 142, 142, 105, 106, 106, 142, 143, 143, 106, 107, 107, 143, 108, 108, 107, 72, 72, 108, 109, 109, 72, 73, 73, 109, 110, 110, 73, 74, 74, 110, 111, 111, 74, 75, 75, 111, 112, 112, 75, 76, 76, 112, 113, 113, 76, 77, 77, 113, 114, 114, 77, 78, 78, 114, 115, 115, 78, 79, 79, 115, 116, 116, 79, 80, 80, 116, 117, 117, 80, 81, 81, 117, 118, 118, 81, 82, 82, 118, 119, 119, 82, 83, 83, 119, 120, 120, 83, 84, 84, 120, 121, 121, 84, 85, 85, 121, 122, 122, 85, 86, 86, 122, 123, 123, 86, 87, 87, 123, 124, 124, 87, 88, 88, 124, 125, 125, 88, 89, 89, 125, 126, 126, 89, 90, 126, 162, 163, 163, 126, 127, 127, 163, 164, 164, 127, 128, 128, 164, 165, 165, 128, 129, 129, 165, 166, 166, 129, 130, 130, 166, 167, 167, 130, 131, 131, 167, 168, 168, 131, 132, 132, 168, 169, 169, 132, 133, 133, 169, 170, 170, 133, 134, 134, 170, 171, 171, 134, 135, 135, 171, 172, 172, 135, 136, 136, 172, 173, 173, 136, 137, 137, 173, 174, 174, 137, 138, 138, 174, 175, 175, 138, 139, 139, 175, 176, 176, 139, 140, 140, 176, 177, 177, 140, 141, 141, 177, 178, 178, 141, 142, 142, 178, 179, 179, 142, 143, 143, 179, 144, 144, 143, 108, 108, 144, 145, 145, 108, 109, 109, 145, 146, 146, 109, 110, 110, 146, 147, 147, 110, 111, 111, 147, 148, 148, 111, 112, 112, 148, 149, 149, 112, 113, 113, 149, 150, 150, 113, 114, 114, 150, 151, 151, 114, 115, 115, 151, 152, 152, 115, 116, 116, 152, 153, 153, 116, 117, 117, 153, 154, 154, 117, 118, 118, 154, 155, 155, 118, 119, 119, 155, 156, 156, 119, 120, 120, 156, 157, 157, 120, 121, 121, 157, 158, 158, 121, 122, 122, 158, 159, 159, 122, 123, 123, 159, 160, 160, 123, 124, 124, 160, 161, 161, 124, 125, 125, 161, 162, 162, 125, 126, 162, 198, 199, 199, 162, 163, 163, 199, 200, 200, 163, 164, 164, 200, 201, 201, 164, 165, 165, 201, 202, 202, 165, 166, 166, 202, 203, 203, 166, 167, 167, 203, 204, 204, 167, 168, 168, 204, 205, 205, 168, 169, 169, 205, 206, 206, 169, 170, 170, 206, 207, 207, 170, 171, 171, 207, 208, 208, 171, 172, 172, 208, 209, 209, 172, 173, 173, 209, 210, 210, 173, 174, 174, 210, 211, 211, 174, 175, 175, 211, 212, 212, 175, 176, 176, 212, 213, 213, 176, 177, 177, 213, 214, 214, 177, 178, 178, 214, 215, 215, 178, 179, 179, 215, 180, 180, 179, 144, 144, 180, 181, 181, 144, 145, 145, 181, 182, 182, 145, 146, 146, 182, 183, 183, 146, 147, 147, 183, 184, 184, 147, 148, 148, 184, 185, 185, 148, 149, 149, 185, 186, 186, 149, 150, 150, 186, 187, 187, 150, 151, 151, 187, 188, 188, 151, 152, 152, 188, 189, 189, 152, 153, 153, 189, 190, 190, 153, 154, 154, 190, 191, 191, 154, 155, 155, 191, 192, 192, 155, 156, 156, 192, 193, 193, 156, 157, 157, 193, 194, 194, 157, 158, 158, 194, 195, 195, 158, 159, 159, 195, 196, 196, 159, 160, 160, 196, 197, 197, 160, 161, 161, 197, 198, 198, 161, 162, 198, 234, 235, 235, 198, 199, 199, 235, 236, 236, 199, 200, 200, 236, 237, 237, 200, 201, 201, 237, 238, 238, 201, 202, 202, 238, 239, 239, 202, 203, 203, 239, 240, 240, 203, 204, 204, 240, 241, 241, 204, 205, 205, 241, 242, 242, 205, 206, 206, 242, 243, 243, 206, 207, 207, 243, 244, 244, 207, 208, 208, 244, 245, 245, 208, 209, 209, 245, 246, 246, 209, 210, 210, 246, 247, 247, 210, 211, 211, 247, 248, 248, 211, 212, 212, 248, 249, 249, 212, 213, 213, 249, 250, 250, 213, 214, 214, 250, 251, 251, 214, 215, 215, 251, 216, 216, 215, 180, 180, 216, 217, 217, 180, 181, 181, 217, 218, 218, 181, 182, 182, 218, 219, 219, 182, 183, 183, 219, 220, 220, 183, 184, 184, 220, 221, 221, 184, 185, 185, 221, 222, 222, 185, 186, 186, 222, 223, 223, 186, 187, 187, 223, 224, 224, 187, 188, 188, 224, 225, 225, 188, 189, 189, 225, 226, 226, 189, 190, 190, 226, 227, 227, 190, 191, 191, 227, 228, 228, 191, 192, 192, 228, 229, 229, 192, 193, 193, 229, 230, 230, 193, 194, 194, 230, 231, 231, 194, 195, 195, 231, 232, 232, 195, 196, 196, 232, 233, 233, 196, 197, 197, 233, 234, 234, 197, 198, 234, 270, 271, 271, 234, 235, 235, 271, 272, 272, 235, 236, 236, 272, 273, 273, 236, 237, 237, 273, 274, 274, 237, 238, 238, 274, 275, 275, 238, 239, 239, 275, 276, 276, 239, 240, 240, 276, 277, 277, 240, 241, 241, 277, 278, 278, 241, 242, 242, 278, 279, 279, 242, 243, 243, 279, 280, 280, 243, 244, 244, 280, 281, 281, 244, 245, 245, 281, 282, 282, 245, 246, 246, 282, 283, 283, 246, 247, 247, 283, 284, 284, 247, 248, 248, 284, 285, 285, 248, 249, 249, 285, 286, 286, 249, 250, 250, 286, 287, 287, 250, 251, 251, 287, 252, 252, 251, 216, 216, 252, 253, 253, 216, 217, 217, 253, 254, 254, 217, 218, 218, 254, 255, 255, 218, 219, 219, 255, 256, 256, 219, 220, 220, 256, 257, 257, 220, 221, 221, 257, 258, 258, 221, 222, 222, 258, 259, 259, 222, 223, 223, 259, 260, 260, 223, 224, 224, 260, 261, 261, 224, 225, 225, 261, 262, 262, 225, 226, 226, 262, 263, 263, 226, 227, 227, 263, 264, 264, 227, 228, 228, 264, 265, 265, 228, 229, 229, 265, 266, 266, 229, 230, 230, 266, 267, 267, 230, 231, 231, 267, 268, 268, 231, 232, 232, 268, 269, 269, 232, 233, 233, 269, 270, 270, 233, 234, 270, 306, 307, 307, 270, 271, 271, 307, 308, 308, 271, 272, 272, 308, 309, 309, 272, 273, 273, 309, 310, 310, 273, 274, 274, 310, 311, 311, 274, 275, 275, 311, 312, 312, 275, 276, 276, 312, 313, 313, 276, 277, 277, 313, 314, 314, 277, 278, 278, 314, 315, 315, 278, 279, 279, 315, 316, 316, 279, 280, 280, 316, 317, 317, 280, 281, 281, 317, 318, 318, 281, 282, 282, 318, 319, 319, 282, 283, 283, 319, 320, 320, 283, 284, 284, 320, 321, 321, 284, 285, 285, 321, 322, 322, 285, 286, 286, 322, 323, 323, 286, 287, 287, 323, 288, 288, 287, 252, 252, 288, 289, 289, 252, 253, 253, 289, 290, 290, 253, 254, 254, 290, 291, 291, 254, 255, 255, 291, 292, 292, 255, 256, 256, 292, 293, 293, 256, 257, 257, 293, 294, 294, 257, 258, 258, 294, 295, 295, 258, 259, 259, 295, 296, 296, 259, 260, 260, 296, 297, 297, 260, 261, 261, 297, 298, 298, 261, 262, 262, 298, 299, 299, 262, 263, 263, 299, 300, 300, 263, 264, 264, 300, 301, 301, 264, 265, 265, 301, 302, 302, 265, 266, 266, 302, 303, 303, 266, 267, 267, 303, 304, 304, 267, 268, 268, 304, 305, 305, 268, 269, 269, 305, 306, 306, 269, 270, 306, 342, 343, 343, 306, 307, 307, 343, 344, 344, 307, 308, 308, 344, 345, 345, 308, 309, 309, 345, 346, 346, 309, 310, 310, 346, 347, 347, 310, 311, 311, 347, 348, 348, 311, 312, 312, 348, 349, 349, 312, 313, 313, 349, 350, 350, 313, 314, 314, 350, 351, 351, 314, 315, 315, 351, 352, 352, 315, 316, 316, 352, 353, 353, 316, 317, 317, 353, 354, 354, 317, 318, 318, 354, 355, 355, 318, 319, 319, 355, 356, 356, 319, 320, 320, 356, 357, 357, 320, 321, 321, 357, 358, 358, 321, 322, 322, 358, 359, 359, 322, 323, 323, 359, 324, 324, 323, 288, 288, 324, 325, 325, 288, 289, 289, 325, 326, 326, 289, 290, 290, 326, 327, 327, 290, 291, 291, 327, 328, 328, 291, 292, 292, 328, 329, 329, 292, 293, 293, 329, 330, 330, 293, 294, 294, 330, 331, 331, 294, 295, 295, 331, 332, 332, 295, 296, 296, 332, 333, 333, 296, 297, 297, 333, 334, 334, 297, 298, 298, 334, 335, 335, 298, 299, 299, 335, 336, 336, 299, 300, 300, 336, 337, 337, 300, 301, 301, 337, 338, 338, 301, 302, 302, 338, 339, 339, 302, 303, 303, 339, 340, 340, 303, 304, 304, 340, 341, 341, 304, 305, 305, 341, 342, 342, 305, 306, 342, 378, 379, 379, 342, 343, 343, 379, 380, 380, 343, 344, 344, 380, 381, 381, 344, 345, 345, 381, 382, 382, 345, 346, 346, 382, 383, 383, 346, 347, 347, 383, 384, 384, 347, 348, 348, 384, 385, 385, 348, 349, 349, 385, 386, 386, 349, 350, 350, 386, 387, 387, 350, 351, 351, 387, 388, 388, 351, 352, 352, 388, 389, 389, 352, 353, 353, 389, 390, 390, 353, 354, 354, 390, 391, 391, 354, 355, 355, 391, 392, 392, 355, 356, 356, 392, 393, 393, 356, 357, 357, 393, 394, 394, 357, 358, 358, 394, 395, 395, 358, 359, 359, 395, 360, 360, 359, 324, 324, 360, 361, 361, 324, 325, 325, 361, 362, 362, 325, 326, 326, 362, 363, 363, 326, 327, 327, 363, 364, 364, 327, 328, 328, 364, 365, 365, 328, 329, 329, 365, 366, 366, 329, 330, 330, 366, 367, 367, 330, 331, 331, 367, 368, 368, 331, 332, 332, 368, 369, 369, 332, 333, 333, 369, 370, 370, 333, 334, 334, 370, 371, 371, 334, 335, 335, 371, 372, 372, 335, 336, 336, 372, 373, 373, 336, 337, 337, 373, 374, 374, 337, 338, 338, 374, 375, 375, 338, 339, 339, 375, 376, 376, 339, 340, 340, 376, 377, 377, 340, 341, 341, 377, 378, 378, 341, 342, 360, 361, 395, 395, 361, 362, 362, 395, 394, 394, 362, 363, 363, 394, 393, 393, 392, 363, 363, 364, 392, 392, 391, 364, 364, 365, 391, 391, 390, 365, 365, 366, 390, 390, 389, 366, 366, 367, 389, 389, 388, 367, 367, 368, 388, 388, 387, 368, 368, 369, 387, 387, 386, 369, 369, 370, 386, 386, 385, 370, 370, 371, 385, 385, 384, 371, 371, 372, 384, 384, 383, 372, 372, 373, 383, 383, 382, 373, 373, 374, 382, 382, 381, 374, 374, 375, 381, 381, 380, 375, 375, 376, 380, 380, 379, 376, 376, 377, 379, 379, 377, 378};
            mesh.LockIndexBuffer(LockFlags.None).WriteRange(index);
            mesh.UnlockIndexBuffer();
            mesh = mesh.Clone(D3DDevice,MeshFlags.Managed, Vertex.NormalFormat);
            //normals = mesh.Clone(D3DDevice, MeshFlags.Managed, Vertex.NormalFormat);
            mesh.ComputeNormals();
            //normals.ComputeNormals();
            //mesh.LockVertexBuffer(LockFlags.None).ReadRange(vertexnormals, 0, 396);
            mesh.LockVertexBuffer(LockFlags.None).ReadRange(vertexnormals, 0, 2772);
            //normals.LockVertexBuffer(LockFlags.None).ReadRange(vertexnormals,0,396);
            mesh.UnlockVertexBuffer();
            //normals.UnlockVertexBuffer();
        }

        private void userControl11_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            booMovingMouse = true;
            initX = MousePosition.X;
            initY = MousePosition.Y;
        }

        private void userControl11_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            booMovingMouse = false;
            finX += (initX - MousePosition.X);
            finY += (initY - MousePosition.Y);
        }

        private void Form1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            WheelScroll += e.Delta / (float)(Math.Sqrt(Math.Pow(e.Delta, 2)));
            this.Text = WheelScroll.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PoleHeight = (float)Convert.ToDouble(textHeight.Text);
            Outer = (float)Convert.ToDouble(textOuter.Text) / 1000;
            Inner = (float)Convert.ToDouble(textInner.Text) / 1000;
            Taper = (float)Convert.ToDouble(textTaper.Text) / 1000;
            SetupMatrices();
            ModelCreation();
        }

        private void radioConcrete_CheckedChanged(object sender, EventArgs e)
        {
            if (radioConcrete.Checked == true)
            {
                textInner.Enabled = true;
                textTaper.Enabled = true;
            }
            else if (radioConcrete.Checked == false)
            {
                if (radioSteel.Checked == true)
                {
                    textInner.Enabled = true;
                    textTaper.Enabled = true;
                }
                else
                {
                    textInner.Enabled = false;
                    textTaper.Enabled = false;
                }
            }
        }

        private void radioSteel_CheckedChanged(object sender, EventArgs e)
        {
            if (radioSteel.Checked == true)
            {
                textInner.Enabled = true;
                textTaper.Enabled = true;
            }
            else if (radioSteel.Checked == false)
            {
                if (radioConcrete.Checked == true)
                {
                    textInner.Enabled = true;
                    textTaper.Enabled = true;
                }
                else
                {
                    textInner.Enabled = false;
                    textTaper.Enabled = false;
                }
            }
        }

        private void buttonXFile_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {

        }          
    }

    struct Vertex
    {
        public Vector3 Position;
        public int Color;

        public static int SizeBytes
        {
            get { return Marshal.SizeOf(typeof(Vertex)); }
        }

        public static VertexFormat Format
        {
            get { return VertexFormat.Position | VertexFormat.Diffuse; }
        }
        public static VertexFormat NormalFormat
        {
            get { return VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal; }
        }
    }
}

Directx painting in a picturebox/panel

20 December 2012 - 02:26 PM

Hi Guys,

Just wondering if someone would be able to help me out.

I am trying to paint my directx in a picturebox or panel. I have managed to get it working by painting to the form with the protected override void onPaint function and using onPaintBackground to avoid flicker.

However, i am trying to map this over to picturebox or panel however i can't find an onPaintBackground function to prevent flicker in the picturebox/panel.

If there is any information someone would be able to provide to prevent this flicker, that would be great. The main reason i am trying to do this is so that i can put other controls in the form and painting the whole form doesn't allow use of the controls at runtime.

Thanks

Form Flashing when Painting

15 December 2012 - 01:18 AM

Hi Guys,

Just wondering if someone would be able to shed some light on an issue i am having.

I am drawing a simply triangle and getting it to rotate around its axis.

However, when i re paint the screen it flashes as it repaints.

I have attached the code if anyone could shed some light on this problem.

Cheers
[source lang="csharp"]using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using SlimDX;using SlimDX.Direct3D9;using SlimDX.Windows;using System.Runtime.InteropServices;namespace WindowsFormsApplication1{ public partial class Form1 : Form { private Device device; CustomVertex.PositionColored[] vertices; public static RenderForm Window; // Window used for rendering. public static Device D3DDevice; // Direct3D device. public static VertexBuffer Vertices; // Vertex buffer object used to hold vertices. public static int time; // Used for rotation caculations. public static float angle; // Angle of rottaion. public Form1() { InitializeComponent(); // Initialize Direct3D } public void InitializeD3D() { // Create a new PresentParameters object and fill in the necessary fields. PresentParameters presentParams = new PresentParameters(); // Below are the required bare mininum, needed to initialize the D3D device. presentParams.BackBufferHeight = this.ClientRectangle.Height; // BackBufferHeight, set to the Window's height. presentParams.BackBufferWidth = this.ClientRectangle.Width; // BackBufferWidth, set to the Window's width. presentParams.DeviceWindowHandle = this.Handle; // DeviceWindowHandle, set to the Window's handle. // Create the device. D3DDevice = new Device(new Direct3D(), 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, presentParams); //D3DDevice = new Device(0, DeviceType.Hardware, Window.Handle, CreateFlags.HardwareVertexProcessing, presentParams); // Create the vertex buffer and fill with the triangle vertices. Vertices = new VertexBuffer(D3DDevice, 3 * Vertex.SizeBytes, Usage.WriteOnly, VertexFormat.None, Pool.Managed); DataStream stream = Vertices.Lock(0, 0, LockFlags.None); stream.WriteRange(BuildVertexData()); Vertices.Unlock(); // Turn off culling, so we see the front and back of the triangle D3DDevice.SetRenderState(RenderState.CullMode, Cull.None); // Turn off D3D lighting, since we are providing our own vertex colors D3DDevice.SetRenderState(RenderState.Lighting, false); } private static Vertex[] BuildVertexData() { Vertex[] vertexData = new Vertex[3]; vertexData[0].Position = new Vector3(-1.0f, -1.0f, 0.0f); vertexData[0].Color = Color.Red.ToArgb(); vertexData[1].Position = new Vector3(1.0f, -1.0f, 0.0f); vertexData[1].Color = Color.Blue.ToArgb(); vertexData[2].Position = new Vector3(0.0f, 1.0f, 0.0f); vertexData[2].Color = Color.Green.ToArgb(); return vertexData; } public void SetupMatrices() { // For our world matrix, we will just rotate the object about the y-axis. // Set up the rotation matrix to generate 1 full rotation (2*PI radians) // every 1000 ms. To avoid the loss of precision inherent in very high // floating point numbers, the system time is modulated by the rotation // period before conversion to a radian angle. Matrix world = Matrix.RotationY(angle); D3DDevice.SetTransform(TransformState.World, world); // Set up our view matrix. A view matrix can be defined given an eye point, // a point to lookat, and a direction for which way is up. Here, we set the // eye five units back along the z-axis and up three units, look at the // origin, and define "up" to be in the y-direction. Vector3 Eye = new Vector3(0.0f, 3.0f, -5.0f); Vector3 LookAt = new Vector3(0.0f, 0.0f, 0.0f); Vector3 Up = new Vector3(0.0f, 1.0f, 0.0f); Matrix view = Matrix.LookAtLH(Eye, LookAt, Up); D3DDevice.SetTransform(TransformState.View, view); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). float fov = (float)Math.PI / 4; float apsectRatio = (float)this.ClientRectangle.Width / this.ClientRectangle.Height; float nearPlane = 1.0f; float farPlane = 100.0f; Matrix projection = Matrix.PerspectiveFovLH(fov, apsectRatio, nearPlane, farPlane); D3DDevice.SetTransform(TransformState.Projection, projection); } private void Form1_Load(object sender, EventArgs e) { InitializeD3D(); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); // Begin the scene. D3DDevice.BeginScene(); // Update the time and angle. time = Environment.TickCount % 1000; angle = (float)(time * (2.0f * Math.PI) / 1000.0f); // Setup the world, view and projection matrices. SetupMatrices(); // Render the vertex buffer. D3DDevice.SetStreamSource(0, Vertices, 0, Vertex.SizeBytes); D3DDevice.VertexFormat = Vertex.Format; D3DDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); // End the scene. D3DDevice.EndScene(); // Present the backbuffer contents to the screen. D3DDevice.Present(); this.Invalidate(); //this.Update(); } } class CustomVertex { //[StructLayout(LayoutKind.Sequential)] public struct TransformedColored { public Vector4 Position; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.PositionRhw; } //[StructLayout(LayoutKind.Sequential)] public struct PositionColored { public Vector3 Position; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position; } //[StructLayout(LayoutKind.Sequential)] public struct PositionNormalColored { public Vector3 Position; public Vector3 Normal; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal; } } struct Vertex { public Vector3 Position; public int Color; public static int SizeBytes { get { return Marshal.SizeOf(typeof(Vertex)); } } public static VertexFormat Format { get { return VertexFormat.Position | VertexFormat.Diffuse; } } } }[/source]

Can't draw a simple cube

09 December 2012 - 01:09 AM

Hi Guys,

Just wondering if someone out there would be able to help me with what i thought would be an extremely simple thing.

I have been searching the web trying to find some example code on how to draw a simple 3d cube.

I found some here, which i have translated from vb to c# and from managed directx to slimdx.

However, the cube does not want to draw and i am at a wits end as to why. The background is fine but just cant seem to get the cube to show up.

If anyone out there can help me out it would be greatly appreciated.

Cheers
[source lang="csharp"]using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using SlimDX;using SlimDX.Direct3D9;namespace WindowsFormsApplication1{ public partial class Form1 : Form { Device d = null; VertexBuffer vb = null; float rotX = 0, rotY = 0, initX = 0, initY = 0, finX = 0, finY = 0; Boolean booMovingMouse = false; //Next 6 numbers handle the click-drag movement public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { InitializeD3D(); } private void InitializeD3D() { //Create my device and set it up. PresentParameters para = new PresentParameters(); para.Windowed = true; para.SwapEffect = SwapEffect.Discard; d = new Device(new Direct3D(),0,DeviceType.Hardware,this.Handle,CreateFlags.HardwareVertexProcessing,para); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.Opaque, true); //Set up and stock the vertex buffer with the vertexes of my cube. Cube Cube = new Cube(); Cube.New(2, 2, 2, Color.SkyBlue); vb = new VertexBuffer(d, 36, Usage.Dynamic, CustomVertex.PositionColored.format, Pool.Default); vb.Lock(0,36,LockFlags.None).WriteRange(Cube.GetVerts()); vb.Unlock(); } private void SetupCamera() { d.SetTransform(TransformState.Projection, Matrix.PerspectiveFovLH((float)(Math.PI / 4), (float)(this.Width / this.Height), 1f, 100f)); d.SetTransform(TransformState.View, Matrix.LookAtLH(new Vector3(0, 0, 5.0F), new Vector3(), new Vector3(0, 1, 0))); d.SetRenderState(RenderState.Lighting, true); //My single light Light Light = new Light(); Light.Type = LightType.Point; Light.Position = new Vector3(0,0,10); Light.Ambient = Color.White; Light.Attenuation0 = 0.8f; Light.Attenuation1 = 0.02f; Light.Range = 10000.0f; d.SetLight(0,Light); d.EnableLight(0, true); } protected override void OnPaint(PaintEventArgs e) { if (booMovingMouse == true) { rotX = (initX - MousePosition.X) + finX; rotY = (initY - MousePosition.Y) + finY; } //Set up my camera and lighting. Since in most scenes, this changes constantly, //it is here and not in the Initialize. SetupCamera(); //Render the scene. d.Clear(ClearFlags.Target, Color.DarkGray, 1, 0); d.BeginScene(); d.VertexFormat = CustomVertex.PositionNormalColored.format; // draw the cube d.SetStreamSource(0, vb, 0, 0); //Scale the mouse movement by 100 because 1:1 is too sensitive. Y = negative so //the cube moves the way you'd expect on initial movement. A true "grab and move //as expected" routine would be a little too complex for this example but all you'd //need is a routine to determine the orientation of the world to the camera and select //axis of rotation and direction accordingly. d.SetTransform(TransformState.World, Matrix.RotationYawPitchRoll(rotX / 100.0F, -rotY / 100.0F, 0)); //d.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); d.EndScene(); d.Present(); this.Invalidate(); } private void Form1_MouseDown(object sender, EventArgs e) { booMovingMouse = true; initX = MousePosition.X; initY = MousePosition.Y; } private void Form1_MouseUp(object sender, EventArgs e) { booMovingMouse = false; finX += (initX - MousePosition.X); finY += (initY - MousePosition.Y); } } class CustomVertex { //[StructLayout(LayoutKind.Sequential)] public struct TransformedColored { public Vector4 Position; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.PositionRhw; } //[StructLayout(LayoutKind.Sequential)] public struct PositionColored { public Vector3 Position; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position; } //[StructLayout(LayoutKind.Sequential)] public struct PositionNormalColored { public Vector3 Position; public Vector3 Normal; public int Color; public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal; } } class Cube { float X,Y,Z; Color Color; public void New(float Length, float Width, float Height, Color CubeColor) { X = Length; Y = Width; Z = Height; Color = CubeColor; } public CustomVertex.PositionNormalColored[] GetVerts() { //This gets the vertexes of the cube. Each face is composed of 2 triangles. //Each triangle has 3 vertexes, so each face needs 6 vertexes; i.e. some vertexes //are written twice. The order in which you build the vertexes determine which //side gets filled. You plot vertexes counter-clockwise for sides facing the camera //and clockwise for ones facing "away". //Also, in order for each side to be lit, you need to specify the //normal for each vertex. This is the same number for all vertexes on each side. //Setting the normal improperly can give some interesting effects in lighting. float X2 = X/2; float Y2 = Y/2; float Z2 = Z/2; CustomVertex.PositionNormalColored[] verts = new CustomVertex.PositionNormalColored[36]; //Front face. verts[0].Position = new Vector3(-X2,Y2,Z2); verts[0].Normal = new Vector3(0,0,1); verts[0].Color = Color.ToArgb(); verts[1].Position = new Vector3(-X2,-Y2,Z2); verts[1].Normal = new Vector3(0,0,1); verts[1].Color = Color.ToArgb(); verts[2].Position = new Vector3(X2,Y2,Z2); verts[2].Normal = new Vector3(0,0,1); verts[2].Color = Color.ToArgb(); verts[3].Position = new Vector3(-X2,-Y2,Z2); verts[3].Normal = new Vector3(0,0,1); verts[3].Color = Color.ToArgb(); verts[4].Position = new Vector3(X2,-Y2,Z2); verts[4].Normal = new Vector3(0,0,1); verts[4].Color = Color.ToArgb(); verts[5].Position = new Vector3(X2,Y2,Z2); verts[5].Normal = new Vector3(0,0,1); verts[5].Color = Color.ToArgb(); //Back face. This is facing away from the camera, so vertices should be clockwise order. verts[6].Position = new Vector3(-X2,Y2,-Z2); verts[6].Normal = new Vector3(0,0,-1); verts[6].Color = Color.ToArgb(); verts[7].Position = new Vector3(X2,Y2,-Z2); verts[7].Normal = new Vector3(0,0,-1); verts[7].Color = Color.ToArgb(); verts[8].Position = new Vector3(-X2,-Y2,-Z2); verts[8].Normal = new Vector3(0,0,-1); verts[8].Color = Color.ToArgb(); verts[9].Position = new Vector3(-X2,Y2,-Z2); verts[9].Normal = new Vector3(0,0,-1); verts[9].Color = Color.ToArgb(); verts[10].Position = new Vector3(X2,Y2,-Z2); verts[10].Normal = new Vector3(0,0,-1); verts[10].Color = Color.ToArgb(); verts[11].Position = new Vector3(X2,-Y2,-Z2); verts[11].Normal = new Vector3(0,0,-1); verts[11].Color = Color.ToArgb(); //Top face. verts[12].Position = new Vector3(-X2,Y2,Z2); verts[12].Normal = new Vector3(0,1,0); verts[12].Color = Color.ToArgb(); verts[13].Position = new Vector3(X2,Y2,-Z2); verts[13].Normal = new Vector3(0,1,0); verts[13].Color = Color.ToArgb(); verts[14].Position = new Vector3(-X2,Y2,-Z2); verts[14].Normal = new Vector3(0,1,0); verts[14].Color = Color.ToArgb(); verts[15].Position = new Vector3(-X2,Y2,Z2); verts[15].Normal = new Vector3(0,1,0); verts[15].Color = Color.ToArgb(); verts[16].Position = new Vector3(X2,Y2,Z2); verts[16].Normal = new Vector3(0,1,0); verts[16].Color = Color.ToArgb(); verts[17].Position = new Vector3(X2,Y2,-Z2); verts[17].Normal = new Vector3(0,1,0); verts[17].Color = Color.ToArgb(); //Bottom face. This is facing away from the camera, so vertices should be clockwise order. verts[18].Position = new Vector3(-X2,-Y2,Z2); verts[18].Normal = new Vector3(0,-1,0); verts[18].Color = Color.ToArgb(); verts[19].Position = new Vector3(-X2,-Y2,-Z2); verts[19].Normal = new Vector3(0,-1,0); verts[19].Color = Color.ToArgb(); verts[20].Position = new Vector3(X2,-Y2,-Z2); verts[20].Normal = new Vector3(0,-1,0); verts[20].Color = Color.ToArgb(); verts[21].Position = new Vector3(-X2,-Y2,Z2); verts[21].Normal = new Vector3(0,-1,0); verts[21].Color = Color.ToArgb(); verts[22].Position = new Vector3(X2,-Y2,-Z2); verts[22].Normal = new Vector3(0,-1,0); verts[22].Color = Color.ToArgb(); verts[23].Position = new Vector3(X2,-Y2,Z2); verts[23].Normal = new Vector3(0,-1,0); verts[23].Color = Color.ToArgb(); //Left face. verts[24].Position = new Vector3(-X2,Y2,Z2); verts[24].Normal = new Vector3(-1,0,0); verts[24].Color = Color.ToArgb(); verts[25].Position = new Vector3(-X2,-Y2,-Z2); verts[25].Normal = new Vector3(-1,0,0); verts[25].Color = Color.ToArgb(); verts[26].Position = new Vector3(-X2,-Y2,Z2); verts[26].Normal = new Vector3(-1,0,0); verts[26].Color = Color.ToArgb(); verts[27].Position = new Vector3(-X2,Y2,-Z2); verts[27].Normal = new Vector3(-1,0,0); verts[27].Color = Color.ToArgb(); verts[28].Position = new Vector3(-X2,-Y2,-Z2); verts[28].Normal = new Vector3(-1,0,0); verts[28].Color = Color.ToArgb(); verts[29].Position = new Vector3(-X2,Y2,Z2); verts[29].Normal = new Vector3(-1,0,0); verts[29].Color = Color.ToArgb(); //Right face. This is facing away from the camera, so vertices should be clockwise order. verts[30].Position = new Vector3(X2,Y2,Z2); verts[30].Normal = new Vector3(1,0,0); verts[30].Color = Color.ToArgb(); verts[31].Position = new Vector3(X2,-Y2,Z2); verts[31].Normal = new Vector3(1,0,0); verts[31].Color = Color.ToArgb(); verts[32].Position = new Vector3(X2,-Y2,-Z2); verts[32].Normal = new Vector3(1,0,0); verts[32].Color = Color.ToArgb(); verts[33].Position = new Vector3(X2,Y2,-Z2); verts[33].Normal = new Vector3(1,0,0); verts[33].Color = Color.ToArgb(); verts[34].Position = new Vector3(X2,Y2,Z2); verts[34].Normal = new Vector3(1,0,0); verts[34].Color = Color.ToArgb(); verts[35].Position = new Vector3(X2,-Y2,-Z2); verts[35].Normal = new Vector3(1,0,0); verts[35].Color = Color.ToArgb(); return verts; } }}[/source]

PARTNERS