Transformation of a single object

Hi!

I just started learning directx and here is the first of probably many newbie questions. Please, be patient :)

How can i transform a single object I used Sample Framwork to load a "tiny.x" mesh and created simple triangle. I can rotate them together (by rotating the whole world) but how can i transform only one object

I've got also small problem with my "tiny.x" mesh. It's not so tiny after all :) If i move my camera, so i can see the whole mesh and rotate the mesh (or move camera too far away) it disappears. Does it have something to do with my projection parameters Can i scale down this mesh after loading it (so do it only once) or do i have to scale it down every frame (if i now how to transform a single object :) )


Answer this question

Transformation of a single object

  • kokain

    Thanks! I'll try your first suggestion.


  • Dustin

     g0nzo wrote:
    I just started learning directx and here is the first of probably many newbie questions. Please, be patient :)

    Welcome to the WGGT forums Smile we're a patient bunch, so enjoy your stay and ask away...

     g0nzo wrote:
    How can i transform a single object I used Sample Framwork to load a "tiny.x" mesh and created simple triangle. I can rotate them together (by rotating the whole world) but how can i transform only one object

    Presuming you're using the fixed function pipeline (SetTransform() calls) then you need to realise that you can make multiple SetTransform() calls per frame. To transform different objects differently, just preceed the rendering call with an updated SetTransform():

    D3DXMATRIXA16 matObject1;
    // Compose matObject1
    Device->SetTransform( D3DTS_WORLD, &matObject1 );
    Device->DrawPrimitive( your_triangle );

    D3DXMATRIXA16 matObject2;
    // Compose matObject2
    Device->SetTransform( D3DTS_WORLD, &matObject2 );
    Mesh->DrawSubset( ... );


     g0nzo wrote:
    I've got also small problem with my "tiny.x" mesh. It's not so tiny after all :) If i move my camera, so i can see the whole mesh and rotate the mesh (or move camera too far away) it disappears. Does it have something to do with my projection parameters

    Possibly. If geometry goes "beyond" the far clipping plane (part of your projection matrix setup) then it will be clipped and effectively disappear. You can try setting a large difference for your projection matrix and it should work.

     g0nzo wrote:
    Can i scale down this mesh after loading it (so do it only once) or do i have to scale it down every frame (if i now how to transform a single object :) )

    The easiest way is to use matrices and do it per frame. Apart from when you construct the matrix, the complexity of the transforms is *not* important for performance. Constructing a matrix from a 100 D3DXMatrix**( ) calls won't slow down the eventual rendering.

    hth
    Jack


  • yllams

    The camera stops working becuase in the Update callback it also sets the world matrix - so you overwrite the camera value when you set it. The ModelView camera allows you to set the world matrix with the left mouse and the camera (or view matrix) with the right mouse button.

    You have a couple of choices.
    1. Accept that you want to control the world matrix in code and only use the right click/drag. Eventually you probably want to control everything in code anyway.
    2. Concatenate (multiply) the world matrix from the camera with the world matrix you want for each object. Make sure you mutliply them in the right order becuase it makes a difference. I've not actually tried this, I'm just guessing it would work.


  • J im Diamond

    Thanks for the warm welcome and quick answer!

    Well the scaling worked, but the camera stopped working.

    Here's the code (most of it is just sample framework code + Mykre's mesh tutorial):


    public void OnFrameRender(Device device, double appTime, float elapsedTime)
            {
                bool beginSceneCalled = false;
                device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x002D32AA, 1.0f, 0);
                try
                {
                    device.BeginScene();
                    beginSceneCalled = true;               

                    /* here shader parameters are set but the shader file does nothing so i assume that fixed pipeline is used */
                    effect.SetValue("worldViewProjection", camera.WorldMatrix * camera.ViewMatrix * camera.ProjectionMatrix);
                    effect.SetValue("worldMatrix", camera.WorldMatrix);
                    effect.SetValue("appTime", (float)appTime);

                    /* ok here's the part that's a bit confusing */
                    device.Transform.View = camera.ViewMatrix;
                    device.Transform.Projection = camera.ProjectionMatrix;
                    device.Transform.World = camera.WorldMatrix;               

                    device.Lights[0].Direction = -lightWidget.LightDirection;
                    device.Lights[0].Update();
                    lightWidget.OnRender(ColorValue.FromColor(System.Drawing.Color.Red), camera.ViewMatrix, camera.ProjectionMatrix, camera.EyeLocation);

                    /* i want to scale only the mesh */
                    device.SetTransform(TransformType.World, Matrix.Scaling(0.002f, 0.002f, 0.002f));
                   
                    for (int i = 0; i < material.Length; i++)
                    {
                        device.Material = material//emoticons/emotion-55.gif" alt="Idea" />;
                        device.SetTexture(0, textureIdea);
                        mesh.DrawSubset(i);
                    }                         
                   
                    RenderText();
                    hud.OnRender(elapsedTime);
                    sampleUi.OnRender(elapsedTime);
                }
                finally
                {
                    if (beginSceneCalled)
                        device.EndScene();
                }
            }


     


    Adding the line:
    device.SetTransform(TransformType.World, Matrix.Scaling(0.002f, 0.002f, 0.002f));
    disabled the camera (ModelViewerCamera).


  • Transformation of a single object