Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[SlimDX] Program doesn't draw triangle?

Started by jdub Dec 9, 2008 at 8:44 PM 2 replies 900+ views
Original Post
jdub
jdub
I don't understand why this texture that I am trying to draw won't come up. All I get is a screen of black. I checked the values of the vertices in the debug window and the triangle should be in view: (These are the functions where I set up my vertices)

public void Init3D()
{

            d3d = new Direct3D ();

            PresentParameters pparams = new PresentParameters ();
            pparams.BackBufferHeight = this.Height;
            pparams.BackBufferWidth = this.Width;
            pparams.DeviceWindowHandle = this.Handle;
            pparams.SwapEffect = SwapEffect.Discard;
            pparams.Windowed = true;
            pparams.BackBufferFormat = Format.Unknown;

            device = new Device ( d3d, 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, pparams );

            texture = Texture.FromFile ( device, "C:\\Projects\\Jareds_CS_Projects\\SlimDxGame\\Resources\\image.png" );


            vertBuffer = new VertexBuffer ( device,
                                            D3DX.GetFVFVertexSize ( TexturedVertex.FVFFormat ) * 4,
                                            Usage.None, VertexFormat.None,
                                            Pool.Default );

            DataStream stream = vertBuffer.Lock ( 0, 0, LockFlags.None );

            Vector3 Position = new Vector3 (5, 5, 5);
            Vector2 Size = new Vector2 ( 1, 1 );

            TexturedVertex[] verts = new TexturedVertex[4]
            {
                new TexturedVertex( new Vector3(Position.X, Position.Y, Position.Z), Color.White.ToArgb(), 0.0f, 0.0f ), //upper left
                new TexturedVertex( new Vector3(Position.X + Size.X, Position.Y, Position.Z), Color.White.ToArgb(), 0.0f, 1.0f), //upper right
                new TexturedVertex( new Vector3(Position.X, Position.Y + Size.Y, Position.Z), Color.White.ToArgb(), 1.0f, 0.0f ), //bottom left
                new TexturedVertex( new Vector3(Position.X + Size.X, Position.Y + Size.Y, Position.Z), Color.White.ToArgb(), 1.0f, 1.0f)
            };

            stream.WriteRange ( verts );

            vertBuffer.Unlock ();

            SetUpCamera ();
}

public void Render()
{
            device.Viewport = new Viewport ( 0, 0, this.Width, this.Height, 1, 100 );

            device.VertexFormat = TexturedVertex.FVFFormat;

            device.Clear ( ClearFlags.ZBuffer | ClearFlags.Target, Color.Black.ToArgb (), 1.0f, 0 );
            device.BeginScene ();

            Matrix projection = Matrix.PerspectiveFovLH ( 0.785398163f, 640 / 480, 1, 10 );
            device.SetTransform ( TransformState.Projection, projection );

            Matrix view = Matrix.LookAtLH ( Vector3.Zero, new Vector3 ( 5, 5, 5 ), new Vector3 ( 0, 1, 0 ) );
            device.SetTransform ( TransformState.View, view );

            device.SetTransform ( TransformState.World, Matrix.AffineTransformation ( 10f, Vector3.Zero, Quaternion.RotationYawPitchRoll ( 0f, 0f, 0f ), new Vector3 ( 1, 1, 1 ) ) );
            
            device.SetTexture ( 0, texture );

            
            device.SetRenderState ( RenderState.Lighting, false );

            device.SetStreamSource ( 0, vertBuffer, 0, D3DX.GetFVFVertexSize ( TexturedVertex.FVFFormat ) );

            device.DrawPrimitives ( PrimitiveType.TriangleStrip, 0, 2 );

            device.EndScene ();
            device.Present ();
    
}

J.W.
Adam_42
Adam_42
It's probably facing away from the camera. Try turning off back face culling, or adjusting the winding order of your triangles by swapping the order of the middle two vertices.

Also make sure it's not coming out black by changing the clear colour.
jdub
jdub
Changed the order and turned off culling and it still doesn't draw anything. I also changed the color that I cleared the screen to red. Still... nothing appears. I am really stumped on this one.
J.W.
FeverGames
FeverGames
i am not fully familiar with such call:

vertBuffer = new VertexBuffer ( device,
D3DX.GetFVFVertexSize ( TexturedVertex.FVFFormat ) * 4,
Usage.None, VertexFormat.None,
Pool.Default );

but shouldn't VertexFormat.None be the VertexFormat you are using for your vertices?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.