Jump to content
  • Advertisement
Sign in to follow this  
jdub

[SlimDX]Program Doesn't Draw Cube. I'm Stumped

This topic is 3607 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

I am a noobie to SlimDX/3D Graphics. I wrote this little program so I could test out rendering using Index Buffers but the cube won't even appear on the screen. Render Functions:
public void Render()
        {
            device.Clear ( ClearFlags.ZBuffer | ClearFlags.Target , Color.DarkViolet.ToArgb (), 1.0f, 0 );

            device.BeginScene ();
            
            this.RenderCube ();

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

        void RenderCube()
        {
            IndexBuffer indexBuff;
            VertexBuffer vertexBuff;

            vertexBuff = new VertexBuffer ( device,
                                            D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * 8,
                                            Usage.WriteOnly,
                                            UntransformedVertex.FVFFormat,
                                            Pool.Default );

            indexBuff = new IndexBuffer ( device, sizeof(short) * 36, Usage.WriteOnly, Pool.Default, true );

            DataStream dStream = vertexBuff.Lock ( 0, 0, LockFlags.None );

            UntransformedVertex[] verts = new UntransformedVertex[8];

            //front lower right corner
            verts[0] = new UntransformedVertex ( new Vector3 ( 2f, 0f, 0f ), Color.Blue.ToArgb () );

            //front lower left corner
            verts[1] = new UntransformedVertex ( new Vector3 ( 0f, 0f, 0f ), Color.Green.ToArgb () );

            //front upper left corner
            verts[2] = new UntransformedVertex ( new Vector3 ( 0f, 2f, 0f ), Color.Green.ToArgb () );

            //front upper right corner
            verts[3] = new UntransformedVertex ( new Vector3 ( 2f, 2f, 0f ), Color.Red.ToArgb () );

            //back lower left corner
            verts[4] = new UntransformedVertex ( new Vector3 ( 0f, 0f, -4f ), Color.Green.ToArgb () );

            //back lower right corner
            verts[5] = new UntransformedVertex ( new Vector3 ( 2f, 0f, -4f ), Color.Red.ToArgb () );

            //back upper left corner
            verts[6] = new UntransformedVertex ( new Vector3 ( 0f, 2f, -4f ), Color.Green.ToArgb () );

            //back upper right corner
            verts[7] = new UntransformedVertex ( new Vector3 ( 2f, 2f, -4f ), Color.Red.ToArgb () );

            dStream.WriteRange ( verts );

            vertexBuff.Unlock ();

            dStream.Dispose ();

            dStream = indexBuff.Lock ( 0, 0, LockFlags.None );

            short[] indices = new short[36];

            //front face
            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 3; indices[4] = 0; indices[5] = 2;

            //left face 
            indices[6] = 4; indices[7] = 6; indices[8] = 1;
            indices[9] = 6; indices[10] = 2; indices[11] = 1;

            //bottom face
            indices[12] = 4; indices[13] = 5; indices[14] = 0;
            indices[15] = 1; indices[16] = 4; indices[17] = 0;

            //right face
            indices[18] = 0; indices[19] = 7; indices[20] = 3;
            indices[21] = 5; indices[22] = 7; indices[23] = 0;

            //top face
            indices[24] = 6; indices[25] = 7; indices[26] = 3;
            indices[27] = 2; indices[28] = 6; indices[29] = 3;

            //back face
            indices[30] = 6; indices[31] = 4; indices[32] = 7;
            indices[33] = 5; indices[34] = 7; indices[35] = 4;

            dStream.WriteRange ( indices );

            indexBuff.Unlock ();
            dStream.Dispose ();

            device.Indices = indexBuff;
            device.SetStreamSource ( 0, vertexBuff, 0, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) );

            device.VertexFormat = UntransformedVertex.FVFFormat;

            device.SetRenderState ( RenderState.Lighting, false );
            device.SetRenderState ( RenderState.CullMode, Cull.None );

            Matrix mat;
            mat = Matrix.Translation ( new Vector3 ( 10f, 10f, 10f ) );

            device.SetTransform ( TransformState.World, mat );

            device.DrawIndexedPrimitives ( PrimitiveType.TriangleList, 0, 0, 8, 0, 12 );
        }

Camera-Set-Up function:
        private void SetUpCamera()
        {

            device.Viewport = new Viewport ( 0, 0, this.Width, this.Height, 1f, 100f );

            Matrix projection = Matrix.PerspectiveFovLH ( MathHelper.ToRadians ( 45 ), 800 / 600, 1f, 100f );
            Matrix lookAt = Matrix.LookAtLH ( new Vector3 ( 0, 0, 0 ), new Vector3 ( 10f, 10f, 10f ), new Vector3 ( 0, 1, 0 ) );

            device.SetTransform ( TransformState.Projection, projection );
            device.SetTransform ( TransformState.View, lookAt );
        }

Share this post


Link to post
Share on other sites
Advertisement
I am still looking for help as to what is wrong. Everything looks fine to me but the program still does nothing.

Share this post


Link to post
Share on other sites
If you change your background clear color to something besides black, do you see a black outline of your cube?

If so, then your problem is not that your cube isn't being drawn, but that you are drawing a black cube on top of a black background.

Since your new to 3D graphics, you've got some conceptual gaps to fill in. You might want to start by reading my book The Direct3D Graphics Pipeline to get a better understanding of things.

Share this post


Link to post
Share on other sites
That's what I thought at first so I turned of culling and lighting and I also clear the screen to violet. Still though, got nothing.

I have read some of your book. It's great but do you have it in print (it's annoying to have to read it off of a computer screen)?

Share this post


Link to post
Share on other sites
Quote:
Original post by jdub
That's what I thought at first so I turned of culling and lighting and I also clear the screen to violet. Still though, got nothing.


That's telling you that your geometry isn't visible in the frustum you've set on the device. Your geometry is either behind the far plane, in front of the near plane, or off to the side.

Quote:
I have read some of your book. It's great but do you have it in print (it's annoying to have to read it off of a computer screen)?


You can print the PDFs.

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.

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!