Jump to content
  • Advertisement
Sign in to follow this  
mishal153

[SlimDX] Nothing gets rendered with this code. Any hints on what is wrong ?

This topic is 2990 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, This code does not render anything for some reason. All it does is clear the backbuffer. It should ideally render a triangle.
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;
        private Texture _texture = null;
        private Matrix _viewMatrix = Matrix.Identity;
        private Matrix _projectionMatrix = Matrix.Identity;

        struct MyVertex
        {
            public Vector3 Position;
            public Color4 Color;
            public Vector2 TextureCoordinates1;
            public Vector3 Normal;
        }

        MyVertex[] _vertices = new MyVertex[3];

        public Form1()
        {
            InitializeComponent();
        }

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

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

            _vertices[2].Position.X = 1.0f;
            _vertices[2].Position.Y = 0.0f;
            _vertices[2].Position.Z = 0.5f;
            _vertices[2].Color = new Color4(Color.Blue);
            _vertices[2].TextureCoordinates1.X = 1;
            _vertices[2].TextureCoordinates1.Y = 1;
            _vertices[2].Normal.X = 0;
            _vertices[2].Normal.Y = 0;
            _vertices[2].Normal.Z = -1;

            VertexElement [] vertexElements = new VertexElement[5];

            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].Usage = DeclarationUsage.TextureCoordinate;
            vertexElements[2].Type = DeclarationType.Float2;
            vertexElements[2].Stream = 0;
            vertexElements[2].Method = DeclarationMethod.Default;
            vertexElements[2].UsageIndex = 0;
            vertexElements[2].Offset = (short)(Vector3.SizeInBytes + Marshal.SizeOf(typeof(Color4)));

            vertexElements[3].Usage = DeclarationUsage.Normal;
            vertexElements[3].Type = DeclarationType.Float3;
            vertexElements[3].Stream = 0;
            vertexElements[3].Method = DeclarationMethod.Default;
            vertexElements[3].UsageIndex = 0;
            vertexElements[3].Offset = (short)(Vector3.SizeInBytes + Marshal.SizeOf(typeof(Color4)) + Vector2.SizeInBytes);

            vertexElements[4] = VertexElement.VertexDeclarationEnd;

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

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

        private void SetupMatrices()
        {
            Vector3 upVector = new Vector3(0,1,0);
            Vector3 lookatVector = new Vector3(0,0,0);
            Vector3 eyePositionVector = new Vector3(0,0,-1.0f);

            _viewMatrix = Matrix.LookAtLH(eyePositionVector, lookatVector, upVector);
            _projectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI/3.0f, 1.0f, 1.0f, 100.0f);
            _device.SetTransform(TransformState.World, Matrix.Identity);
            _device.SetTransform(TransformState.View, _viewMatrix);
            _device.SetTransform(TransformState.Projection, _projectionMatrix);
        }

        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();
            SetupMatrices();

            _device.SetRenderState(RenderState.Lighting, false);
            _device.SetStreamSource(0, _vertexBuffer, 0, Marshal.SizeOf(typeof(MyVertex)));

            _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();
        }
    }
}



If I do not set any Transforms, AND if i set the Z of the vertices to 0 then i see the triangle as expected. Any obvious problems with this code ? [Edited by - mishal153 on April 14, 2010 3:03:43 AM]

Share this post


Link to post
Share on other sites
Advertisement
The issue was that SlimDX automatically sets "EnableAutoDepthStencil" when creating object of type PresentParameters. This is not the case in managed DX.
In 'Clear' i was not clearing Zbuffer, and thus i was facing the issue.

thanks,
Mishal

Share this post


Link to post
Share on other sites
Quote:
Original post by ET3D
I'd suggest you use PIX to see what's being executed.


Thanks for info. I will dig into usage of pix.

Share this post


Link to post
Share on other sites
Quote:
Original post by mishal153
The issue was that SlimDX automatically sets "EnableAutoDepthStencil" when creating object of type PresentParameters.


You really should fill out structs completely so that you are explicit about what you want to happen, PresentParameters especially.

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!