Jump to content
  • Advertisement
Sign in to follow this  
mishal153

[SlimDX] Corrupted triangle : What could be the problem with the code ?

This topic is 3051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to write a SlimDX app which uses vertex buffer, vertex declaration and draws a simple coloured triangle. I wrote the code but it producese a very weird and corrupted triangle. can someone take a quick look and give hints on what i can try to fix it ? I am not using any shaders. Here is the source:

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 System.Runtime.InteropServices;

namespace SimpleTriangleSlimDX
{
    public partial class Form1 : Form
    {
        private PresentParameters _presentParameters = null;
        private Device _device = null;
        private Direct3D _direct3D = null;
        private VertexBuffer _vertexBuffer = null;
        private Timer _timer = null;

        struct MyVertexColorPos
        {
            public Vector3 Position;
            public Color4 Color;
        }

        MyVertexColorPos[] _vertices = new MyVertexColorPos[3];

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeVertices()
        {
            _vertices[0].Position.X = -1.0f;
            _vertices[0].Position.Y = 0.0f;
            _vertices[0].Position.Z = 1.0f;
            _vertices[0].Color = new Color4(Color.Red);

            _vertices[1].Position.X = 0.0f;
            _vertices[1].Position.Y = 1.0f;
            _vertices[1].Position.Z = 1.0f;
            _vertices[1].Color = new Color4(Color.Green);

            _vertices[2].Position.X = 1.0f;
            _vertices[2].Position.Y = 0.0f;
            _vertices[2].Position.Z = 1.0f;
            _vertices[2].Color = new Color4(Color.Blue);

            VertexElement [] vertexElements = new VertexElement[3];

            vertexElements[0].Usage = DeclarationUsage.Position;
            vertexElements[0].Type = DeclarationType.Float3;
            vertexElements[0].Stream = 0;
            vertexElements[0].Method = DeclarationMethod.Default;
            vertexElements[0].UsageIndex = 0;
            vertexElements[0].Offset = 0;

            vertexElements[1].Usage = DeclarationUsage.Color;
            vertexElements[1].Type = DeclarationType.Float4;
            vertexElements[1].Stream = 0;
            vertexElements[1].Method = DeclarationMethod.Default;
            vertexElements[1].UsageIndex = 0;
            vertexElements[1].Offset = (short) Vector3.SizeInBytes;

            vertexElements[2] = VertexElement.VertexDeclarationEnd;

            VertexDeclaration vertexDeclaration = new VertexDeclaration(_device, vertexElements);
            _device.VertexDeclaration = vertexDeclaration;

            _vertexBuffer = new VertexBuffer(_device, Marshal.SizeOf(typeof(MyVertexColorPos)) * 3, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
            DataStream vbDataStream = _vertexBuffer.Lock(0, 0, LockFlags.Discard);
            vbDataStream.WriteRange(_vertices);
            _vertexBuffer.Unlock();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _presentParameters = new PresentParameters
                                     {
                                         Windowed = true,
                                         SwapEffect = SwapEffect.Discard,
                                         PresentationInterval = PresentInterval.Immediate
                                     };

            _direct3D = new Direct3D();

            _device = new Device(_direct3D, 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, _presentParameters);
            InitializeVertices();
            _device.SetRenderState(RenderState.Lighting, false);
            _device.SetStreamSource(0, _vertexBuffer, 0, Marshal.SizeOf(typeof(MyVertexColorPos)));

            _timer = new Timer();
            _timer.Interval += 50;
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Enabled = true;
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            _device.Clear(ClearFlags.Target, new Color4(Color.Red), 0.0f, 0);
            _device.BeginScene();
            _device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
            _device.EndScene();
            _device.Present();
        }
    }
}




I have already tried putting in manual color argb values for each vertex but still same output. The triangle drawn is this

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!