Sign in to follow this  
MrM

Transformation Problem

Recommended Posts

Hi! I'm new to DX buit not to CG so i know how to use a Matrix but DX kills me.. :( here is what i try to do:
			m_Device.SetTexture(0,this.texture);
			m_Device.SetStreamSource(0, GraphicBuffer, 0);
			m_Device.VertexFormat=CustomVertex.TransformedTextured.Format;
			m_Device.Transform.World = Matrix.Translation(100.0f,0.0f,0.0f);
			m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
			m_Device.Transform.World = Matrix.Translation(-100.0f,0.0f,0.0f);
But nothing happens... The Object is drawn like before... ??? Anyone an idea why?

Share this post


Link to post
Share on other sites
hmm ... shouldn't there be a "SetIndices" somewhere ?
I don't know managed DirectX, but in C++, you make a SetStreamSource(), a SetIndices() and a DrawPrimitive(). If one of the 3 misses, it failes ^^

Edit : As said by Omid, it's only for indexed primitives, sorry I misread the intia
initial post ^^''

[Edited by - paic on October 7, 2005 8:44:07 AM]

Share this post


Link to post
Share on other sites
Hello,

paic, that is only when you are drawing indexed primitives =)

MrM,
Is that which you are showing all your code? In that case your problem here is that
you have to redraw your scene!
D3D works like this:
You set the device ( texture, matrices, e.t.c )
Then you draw

Any changes to the device after drawing will NOT affect what you have already drawn.

Since I don't know your goal, I can't help you more than to just point out the error!
But if you still have problems, let us know what you are trying to do, and we will gladly help you with a solution =)

Hope this helps!

Regards,
/Omid

Share this post


Link to post
Share on other sites
Sorry!

For shure there is more code but it is elsewhere...

these 4 lines are executed in another object.

the device this points to is already set to

device.BeginScene();

and after the code i do

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

hmm.. donؤT think there is something missing?
because the Polygons are drawn.. but the transformation takes no effect..
here is how i set my vertices: (just a Quad with a texture)


texture = new Texture(m_Device, bitmap, 0, Pool.Managed);

CustomVertex.TransformedTextured[] verts = new CustomVertex.TransformedTextured[4];
GraphicBuffer = new VertexBuffer(typeof(CustomVertex.TransformedTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.TransformedTextured.Format, Pool.Default);

verts = (CustomVertex.TransformedTextured[])GraphicBuffer.Lock(0, LockFlags.None);
verts[0] = new CustomVertex.TransformedTextured(m_x, m_y, 1, 1, 0, 0); // Top Left
verts[1] = new CustomVertex.TransformedTextured(m_x + m_witdh, m_y, 1, 1, 1, 0); // Top Right
verts[2] = new CustomVertex.TransformedTextured(m_x, m_y + m_height, 1, 1, 0, 1); // Bottom Left
verts[3] = new CustomVertex.TransformedTextured(m_x + m_witdh, m_y + m_height, 1, 1, 1, 1); // Bottom Right
GraphicBuffer.Unlock();



[Edited by - Coder on October 7, 2005 8:59:33 AM]

Share this post


Link to post
Share on other sites
I'm not 100% sure, but I don't think the TransformedTextured vertices use the world matrix at all. The TranformedTextured vertices are already in screen coordinates, so DX won't apply any matrix transformations to them. If you want to use the world matrix, use the PositionTextured type vertices instead.

However, this means you'll have to set up your camera properly, but as you're already locking the vertexbuffer to manipulate the vertices that shouldn't pose a problem I guess :)

Share this post


Link to post
Share on other sites
hmm... now the Polygon are gone...
Don't know why.. really strange...
Here is the new code:

Vertex init:


CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[4];
GraphicBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
verts = (CustomVertex.PositionTextured[])GraphicBuffer.Lock(0, LockFlags.None);
verts[0] = new CustomVertex.PositionTextured(m_x, m_y, 1, 0, 0); // Top Left
verts[1] = new CustomVertex.PositionTextured(m_x + m_witdh, m_y, 1, 1, 0); // Top Right
verts[2] = new CustomVertex.PositionTextured(m_x, m_y + m_height, 1, 0, 1); // Bottom Left
verts[3] = new CustomVertex.PositionTextured(m_x + m_witdh, m_y + m_height, 1, 1, 1); // Bottom Right
GraphicBuffer.Unlock();


Drawing:

m_Device.VertexFormat=CustomVertex.PositionTextured.Format;
m_Device.SetStreamSource(0, GraphicBuffer, 0);
m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);


And the Camera:

float width = (float)_targetControl.Size.Width;
float height = (float)_targetControl.Size.Height;
float centerX = width / 2.0f;
float centerY = height / 2.0f;
Vector3 cameraPosition = new Vector3(centerX, centerY, -5.0f);
Vector3 cameraTarget = new Vector3(centerX, centerY, 0.0f);


m_Device.Transform.View = Matrix.LookAtLH(cameraPosition, cameraTarget,
new Vector3(0.0f, 1.0f, 0.0f));
m_Device.Transform.Projection = Matrix.OrthoLH(width, height, 1.0f, 10.0f);

Share this post


Link to post
Share on other sites
I don't have any experience with orthogonal (sp?) projection matrices, but all MDX code I've seen so far typically uses Matrix.PerspectiveFOVLH as the projection matrix, like this:


// fov, aspect ratio, near plane, far plane
device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4.0f, ((float)width) / ((float)height), 0.1f, 100.0f);


I don't know if you picked the ortho projection with a reason (in fact, I haven't got a clue what it does :), but if you simply want to display that quad of yours PerspectiveFOVLH should do just fine. The only other things I can think of is that your xyz vertex coords are out of the camera view, or that you don't set your texture in your new code.

EDITED:

Aside from the orthogonal matrix, I just saw your still using screen space coordinates to set up your camera position and targets. This is proably causing your problem, since your camera is most likely positioned to the right of your quad (provided you haven't applied any world transforms on it and you used world space coordinates for the vertices), happily looking at the empty space next to your quad.

Try setting up both your camera position and targets in world space and make sure your vertex coordinates are in world space too. If your goal is to simply draw a number of quads (for sprites for example), you might be better off with multiple quads with the TransformedTextured, or even better yet by using the MDX Sprite class.

Hope this helps!

Share this post


Link to post
Share on other sites
You need ortho if you don't want a perspective change in legths..
I need this because it will get a 2Dimensional Game.. ;)
I don't think this will cause a problem in Rendering or?

Share this post


Link to post
Share on other sites
They aren't rendering because they're being clipped. Your far plane is set to 10.0f (the 4th argument of the Ortho matrix call) and the vertices get transformed to 100.0f. So they're clipped.

Either make your far plane further, or bring the geometry closer.

Share this post


Link to post
Share on other sites
Sorry my edit took so long :) Check my reply for the reason, but if your making a 2D game, I'd go with the MDX Sprites. That'll allow you to draw textures onto a predefined quad without any 3D things to worry about.

The only downside is that you obiously won't be able to perform any typical 3D things, like intersection tests etc. For occlusion (for lack of a better word, drawing something on top of something else :) you could sort the textures you want to draw before rendering, or you could use multiple sprites as layers.

Edited yet again:

Come to think of it, you also won't be able to perform world transformations all over again, what started this discussion... If you are really sure you want the world transforms, you'll have to work out your world coordinates first.... And yes, it might even be that simple clipping problem ;)

Hope this does help :D

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
They aren't rendering because they're being clipped. Your far plane is set to 10.0f (the 4th argument of the Ortho matrix call) and the vertices get transformed to 100.0f. So they're clipped.

Either make your far plane further, or bring the geometry closer.


Ok.. don't see where i move them to 100.0f in z.?? even changing the far plane doesn't change anything.. :(

Quote:
Original post by remigius
Aside from the orthogonal matrix, I just saw your still using screen space coordinates to set up your camera position and targets. This is proably causing your problem, since your camera is most likely positioned to the right of your quad (provided you haven't applied any world transforms on it and you used world space coordinates for the vertices), happily looking at the empty space next to your quad.

Try setting up both your camera position and targets in world space and make sure your vertex coordinates are in world space too.


Don't think that i look to the right of it? :/ tried to change the direction but that doesn't change anything.. :(

I Use The ScreenSpace just to get the camera to the correct point. so that i have the WorldCoordinates the same as the screen coordinates... I think it should work this way to simplify the whole thing.

Can you explain me the exact difference between TransformedTextured & PositionTextured? what does the TransformedTextured? Is there a "trick" to turn them around?

Share this post


Link to post
Share on other sites
Whoooooo goddamnit!

Like a lousy beginner...!

I had stored the Vertices in the wrong order--> normal verctors point to nowhere.. ;)

now it works!! thanks guys!!

Share this post


Link to post
Share on other sites
Good to see it worked out, funny how it's always something small like this :)

The exact difference between PositionTextured and NormalTextured is the way DX handles transformations on these (at least for the fixed function pipeline, ie. when you're not using shaders). For PositionTextured the coordinates are transformed to screen space like this:


object space coords -> world matrix -> view matrix -> projection matrix -> screen space coords


The TransformedTextured vertices can be used if you want to set something up in screen space coordinates. Using TransformedTextured vertices, DX won't transform these at all and just use them directly in screen space. This is especially useful for setting up sprites for 2D game components, like GUI's, 2D game objects and the like.

For translating TranformedTextured coordinates to PositionTextured coordinates, I think you can basically use the same approach as with the picking technique, since it's also a screen space to world space tranformation (could anyone confirm this?).

Well, I hope at least this explanation helped :) Good luck with your project!

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