Sign in to follow this  
transformation

[.net] MDX - drawing stuff inside a panel control.

Recommended Posts

transformation    140
I've decided to give MDX a serious go, and for my first project, I'm making a terrain visualizer kind of thing. The main panel will have the terrain rendered in and the other controls will control how the terrain is generated. Now I got d3d initialized, and I'm clearing the panel and *trying* to render a triangle in the panel. Problem is that it doesn't show. And I can't gifure out why, so any help would be appreciated. Here're some code snippets:
// TerrainPanel is a panel control object
Device = new D3d.Device( 0, D3d.DeviceType.Hardware, TerrainPanel, 
                D3d.CreateFlags.SoftwareVertexProcessing, PresentParams );

// and this is my render function that I call in the panels OnPaint method
void Render()
{
    if( Device == null )
        return;

    Device.Transform.Projection = Dx.Matrix.PerspectiveFovLH( 
        (float)Math.PI / 4.0f, 
        TerrainPanel.Width / TerrainPanel.Height, 
        1.0f, 
        100.0f 
        );

    Device.Transform.View = Dx.Matrix.LookAtLH(
        new Dx.Vector3(0, 0, 5.0f), 
        new Dx.Vector3(),
        new Dx.Vector3(0,1,0));

    Device.Transform.World = Dx.Matrix.Identity;

    Device.RenderState.Lighting = false;
    Device.RenderState.CullMode = D3d.Cull.None;

    Device.Clear( 
        D3d.ClearFlags.Target | D3d.ClearFlags.ZBuffer, 
        System.Drawing.Color.AliceBlue, 
        1.0f, 
        0 
        );

    D3d.CustomVertex.PositionColored[] verts = new D3d.CustomVertex.PositionColored[3];
    verts[0].Position = new Dx.Vector3(0.0f, 1.0f, 1.0f);
    verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
    verts[1].Position = new Dx.Vector3(-1.0f, -1.0f, 1.0f);
    verts[1].Color = System.Drawing.Color.Black.ToArgb();
    verts[2].Position = new Dx.Vector3(1.0f, -1.0f, 1.0f);
    verts[2].Color = System.Drawing.Color.Purple.ToArgb();

    Device.BeginScene();
    
    Device.VertexFormat = D3d.CustomVertex.PositionColored.Format;
    Device.DrawUserPrimitives( D3d.PrimitiveType.TriangleList, 1, verts );

    Device.EndScene();

    Device.Present();
}

Share this post


Link to post
Share on other sites
Enselic    829
Could it be that you have 1.0f as the znear clipping plane but the vertices of the triangle is even nearer, i.e. -1.0f?

To solve this, just set the znear to -10.0f or something, or move the triangle further "back".

Share this post


Link to post
Share on other sites
transformation    140
But the triangle's Z is not -1, it's +1. Either way, I've tried it with a lot of values. THe following should work, but it doesnt:

camera: pos( 0, 0, -5 )
camera: look( 0, 0, 0 )

znear: 1.0f
zfar: 100.0f

triangle: zvalue: 1

Is the setup needed to render into a control correct? It seems just too simple, maybe I'm forgetting to do something?

Share this post


Link to post
Share on other sites
Enselic    829
Does it work if you set the triangle at 2.0f instead?

Now it is precisly in the plane of the near clipping plane, and maybe there's some rounding off error?

Share this post


Link to post
Share on other sites
Etherstar    200
I noticed that you are using the onPaint method as your rendering loop.

Have you added the necessary code to do this? At the end of your onPaint method you need to invalidate the form by calling this.Invalidate();

Secondly, did you remember to set your control style? In the Form's constructor make sure you have the following method call:

this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);

Hope this helps.

Share this post


Link to post
Share on other sites
transformation    140
I can't add the .invalidate method because my form crashes. But It should still show the triangle. It's not animated or anything so it doesnt matter if it's drawn once. I just want to render into a control successfully before I worry about the other things.

I created a new application and instead of rendering into a panel control I rendered to the entire window and everything worked fine. Triangle showed up with no problems. So I'm assuming that there's something wrong with the method I'm using to render into a control.

I'm figuring either I need to create a custom control, or I'm missing a step or two. Can anyone help me?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this