Public Group

# Moving, Rotating etc....

This topic is 3041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys,

I am new in DirectX and now I am going through some tutorials.
At the moment I am at a Tutorial where I have a Triangle and I let it rotate.

When I was finished with the Tutorial, I tried to add instead of one, three triangles.

Now I have a question:

how can I move and rotate them seperate of each other.
In the tutorial I rotate and translate the world Matrix.
But if I do this, it effects every triangle same way.
How do I apply a translation or a rotation only to one of the triangles??
And for what do I exactly need the world Matrix??

kind regards.

Hope someone can help me

##### Share on other sites
Quote:
 Original post by KirjuxaNow I have a question:how can I move and rotate them seperate of each other.In the tutorial I rotate and translate the world Matrix.But if I do this, it effects every triangle same way.How do I apply a translation or a rotation only to one of the triangles??And for what do I exactly need the world Matrix??

In short, everything happens at the origin. When you rotate, you're rotating everything around the origin. Translating essentially moves your point of origin, setting a new point from which everything will be rotated around. To achieve the affect you're after, you need to translate your world matrix to each individual point where you want a triangle drawn, and then perform its rotation. But be careful how you approach this; after you have translated to your desired position, rotated, and then drawn your triangle, you will need to undo your matrix manipulations before moving on to the next triangle. You can achieve this easily if you are using DirectX's push and pop matrix stack methods. The complete order of operations should look like this: Push, Translate, Rotate, Draw, Pop. Repeat for each triangle.

##### Share on other sites
hmm...

I am to0 new in DirectX that I could understand what you ment.

Is there a possibility that I can just say like this:

RotateByX(triangle[1]);
RotateByY(triangle[2]);
RotateByZ(triangle[3]);

actually, every time I rotate the worldmatrix, EVERYTHING is rotating.

I want to rotate and translate the triangles by single.

I mean, how is it made in games?
Do they move the worldmatrix to every single Vertex?

Is there no possibility just to say that only one object of ten in the world should be moved or rotated??

Can you give an example how I can do this??

Tanks

##### Share on other sites
Let's go back a step. Where are you trying to draw each of your triangles? Do you have some coordinates in mind? Maybe something like (0, 0, 10), (0, 10, 0), and (10, 0, 0) -- each triangle drawn 10 units along each axis. Or something else? Are you trying to draw each triangle at the origin, just rotated?

##### Share on other sites
Well actually I make it like this:

I have a struct named CUSTOMVERTEX which stores 4 variables(float x,float y, float z, Color)
and an array of this struct called vertices[];

then I lock the vertexbuffer and copy the vertices in it (mmcpy). Then I unlock the vertexbuffer.

Seperatly in my Renderloop I have something like this:
D3DXMATRIXA16 matWorld;
UINT iTime = timeGetTime() % 1000;
FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f;
D3DXMatrixRotationY( &matWorld, fAngle );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

This code I took from the DirectX SDK sample folder.
Well here I rotate the whole world and not the single vertices.

my triangles are drawn like:

0,0,0/1,0,0/1,1,0
0,0,0/0,0,1/0,1,1
0,0,0/-1,0,0/-1,1,0

##### Share on other sites
I'm on my way to bed, otherwise I'd provide more detail. I suggest looking over this tutorial on transforming vertices; it explains how the camera is working, what the world matrix is doing, about view and projection, etc. Look it over, and if you're still stuck then we can continue this tomorrow.

##### Share on other sites
It looks like you've already got the answers you need, but just to clarify a little further, the (pseudo)code to render three triangles with different transforms (in its simplest form) would look something like this:
set the world transform for triangle 1render triangle 1set the world transform for triangle 2render triangle 2set the world transform for triangle 3render triangle 3
Using a transform stack is a convenience (and can be important when using transform hierarchies), but it's not strictly necessary and isn't really needed for something as simple as this.

Also, since it looks like you're using the fixed-function pipeline, 'set the world transform' in the above pseudocode would correspond to the 'device->SetTransform()' call in your code. If you were using the programmable pipeline though, the same concept would apply (you'd just be setting the transform as input to the vertex program instead).

##### Share on other sites
ok i understand a bit more now.

but how can I say:

render triangle 1
render triangle 2
render triangle 3

doesn't DX render them a once??

and second question:

how can i set the world transform for triangle 1 or triangle 2??

thanks

##### Share on other sites
Quote:
 but how can I say:render triangle 1render triangle 2render triangle 3doesn't DX render them a once??
Whether they are rendered all at once or one at a time is up to you.

In general, rendering proceeds as follows:

1. You set up some number of vertex and (possibly) index buffers containing the geometry to be rendered. This is usually a 'set-up' step (it's not something you would usually do per-frame, although in some cases you might want to fill an existing set of buffers on a per-frame basis).

2. You set the rendering state. For the fixed-function pipeline this might involve setting of transforms, as you're doing currently. For the programmable pipeline, this would most likely involve binding a vertex program and a fragment program. Setting of the state will also usually involve specifying from which vertex and index buffer(s) the geometry to render should be read.

3. You make a draw call, specifying what type of primitives to render, how many to render, and possibly where in the buffer to start reading the data. (Not sure about the last part - I can't remember off the top of my head how rendering from the middle of a buffer is done, but I'm pretty sure it's possible.)

This system is very flexible, and allows you to render geometry in any number of ways. In your example, you could place the world-space data for the triangles in a single set of buffers, and render them with one call. Or, you could place the local-space data for one triangle in a set of buffers, and render them with three calls, setting a different transform for each one. Or, you could place the world-space data for the triangles in a single set of buffers, and then render them with three calls, using a different render state (e.g. a different texture) for each call. And so on.

Again, how the triangles are rendered is really up to you.

##### Share on other sites
Man big thanks,

now I understand how it works.

For exampole if I have a function named Rotate(float angle) that sets a rotation matrix and sets the world matrix transformation by calling device->setTransformation(..)

the I do like this:

Rotate(angle1)
drawPrimitives(vertices of triangle 1)
Rotate(angle2)
drawPrimitives(vertices of triangle 2)
Rotate(angle3)
drawPrimitives(vertices of triangle 3)

and that I do every frame

Now I understand the system.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633345
• Total Posts
3011440
• ### Who's Online (See full list)

There are no registered users currently online

×