   Direct3D Perspective / Camera Question
  Hi, I have what I assume is a pretty basic problem that I hope someone will be able to give me a quick solution to!Basically I'm drawing a grid of cubes with each one rotated around the y-axis at different angles and all of them at the same z position. I'm using a perspective projection with the camera looking at the origin and the problem is that if two cubes are rotated by the same angle they won't look the same because they're at different locations in world space. That's fair enough because that's what you'd expect in the real world no doubt because of the perspective / camera effect.However I DO want them to look the same, i.e. I want each cube to look as though it were rotated and then translated to (0,0,0) no matter where it's actually translated to. Does that make sense?I'm using separate viewports at the moment for each cube and that gives me the desired effect. However changing the viewport 50 times each render pass doesn't *seem* like the most efficient way of doing things - does it make a difference? Is there a more efficient way of doing it staring me in the face?I was thinking of rendering to textures but again locking a texture doesn't seem very efficient. Is some kind of billboarding the solution? I also tried an orthogonal projection but that doesn't give me the perspective effect to show a cube's rotated.Hope someone can help.Cheers :)   
Quote:Does that make sense?Hmm.. maybe not.It sounds like you want each cube to appear to have the same rotation with respect to the eyepoint as it would have if it were actually at the origin.Assuming your eyepoint is at a -x position directed along the +x direction:for each cube:Move it to the origin so you get a "true" rotation.Rotate it about the origin the desired amount.Translate it along x to an x position (away from the eyepoint) so it is as far from the eye as its desired z position is from the eye.Rotate it about the _eye position_ by an angle that will position it along the z axis. By rotating it about the eye position, it will maintain the angle with respect to the eye that it had at the origin.Does that sound like what you want to do?   ;  If you render the cube to a texture there's no need to lock it, you can just bind it and use it directly. Try this:-Render the cube to a small texture-Bind the texture to the device-Render a quad in every spot where you want the cube.  You can use ID3DXSprite to accomplish this easily (just specify the texture and the screen-space coordinate).   ;  Thanks for your replies Buckeye and MJP.Firstly Buckeye,Quote:It sounds like you want each cube to appear to have the same rotation with respect to the eyepoint as it would have if it were actually at the origin.Yep, that's EXACTLY what I want to do.Let's say, as an example, I've got two cubes (dimensions 1x1x1) and I rotate them both by 45 degrees along the y-axis and then translate the second cube along the x-axis by -1.5. I've got my camera at (0,0,-4) and it's looking at (0,0,0).I tried following your steps but the last two instructions I didn't quite understand:Quote:Translate it along x to an x position (away from the eyepoint) so it is as far from the eye as its desired z position is from the eye.Rotate it about the _eye position_ by an angle that will position it along the z axis. By rotating it about the eye position, it will maintain the angle with respect to the eye that it had at the origin.Using the two example cubes above, could you give me an example of how I'd work it out and what the values would end up being? When you're talking about rotating around the eye position, do you mean using a function such as D3DXMatrixRotationAxis() and then passing the vector for the camera position (0,0,-4) to denote the axis?Secondly MJP, when you say:Quote:If you render the cube to a texture there's no need to lock it, you can just bind it and use it directlyWhen you say binding do you mean the same as calling the SetTexture() method of the device? The thing is I need to render multiple cubes to the texture/textures if they each have different angles of rotation. And also if I want to animate the rotation then I'll need to re-render them for each frame.So is it okay to have a large number of cubes being re-rendered to a texture. Isn't rendering to multiple viewports basically the same thing as they're both 2D surfaces that are being rendered to. Do you know if one method is more efficient than the other?Cheers :)   ;  This is ending up an explanation of the trigonometry, rather than DirectX stuff.However,Quote:I tried following your steps but the last two instructions I didn't quite understand. Yeah, it's difficult to describe stuff like that. There's a lot of geometry involved and I didn't know what axes setup you're using and where your camera is. I thought your camera was at x = -something, not z = -something.Quote:do you mean using a function such as D3DXMatrixRotationAxis() and then passing the vector for the camera position (0,0,-4) to denote the axis?As it turns out (with my incorrect assumptions) that's not the function(!). You can use D3DXMatrixRotationY instead.My previous explanation was not based on the camera being on the locations you mention so let's try again.The principle is to get the cube rotated and positioned correctly as if the camera were at the origin. Then translate the cube to it's final position.I'm going to assume you're using left-hand axes. That is, with the camera at [ 0, 0, -4] and looking at the origin, +z is straight ahead and +x is to the right (-x to the left) and y is up.Assume you want the cube to end up at [5, 0, 7], 5 units to the right of the origin and 7 units "away" from the origin, away from the camera.The distance from the final position to the camera, applying Pythagorus' Theorem (distance = square-root of the sum of the squares of the x and z distances):x distance = 5.z distance = 7 (from the origin) + 4 (from the camera to the origin) = 11.So the total distance from the camera to the final location is sqrt( 5*5 + 11*11 ) = 12.08.With me, so far?Rotate the cube at the origin.Translate the cube along the z-axis to the same distance away from the origin that it will be from the eye when it's at its final location. In this case, that position is [ 0, 0, 12.08 ].Now, rotate the cube about the origin (it will swing in a circle around the origin).It needs to be at the same angle with respect to the origin that it will have with respect to the camera when it's at its final position.The easiest trig to use is the atan2 function. Given the opposite side and the adjacent side of a triangle, it gives you the angle.The angle you want is the angle from the camera (looking along the z-axis) to the cube's final position assuming a clockwise rotation (which D3DXMatrix rotations assume). ( Confusing, ain't it? )For this example, the opposite side is the x dimension = 5.The adjacent side is the z dimension = 11.atan2(5,11) = 0.43 (radians, not degrees)Rotate the cube about the y-axis at the origin [ 0, 1, 0 ]. You can just use D3DXMatrixRotationY because it's at the origin.In this example, that puts the cube at [ 5, 0, 11 ].Now translate the cube to its final position ( translate by [ 0, 0, -4] ).You (hopefully) will see the desired result, positioned at [5,0,7] with the desired rotation.Make sure you apply the translation and rotation matrices in the correct order!   ;Quote:Original post by BuckeyeThis is ending up an explanation of the trigonometry, rather than DirectX stuff ...Thanks for the very detailed response Buckeye; I can understand how it's hard to explain certain concepts without the use of diagrams and having to soley use text. But I admire your attempt at it! :DI'll try your method later today when I've finished work and tell you how it goes.Cheers again :)   