Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 04 May 2011 - 03:37 PM
Posted 04 May 2011 - 04:47 PM
Posted 05 May 2011 - 05:52 AM
// X vertices[0] = new VertexPositionColor(Vector3.Zero, Color.Red); vertices[1] = new VertexPositionColor(Vector3.UnitX, Color.Red); quadrangles[0] = new Quadrangle(vertices[1].Position, 0.25f, 0.25f, GlobalSettings.FONT_INDEXSTART + 33); // Y vertices[2] = new VertexPositionColor(Vector3.Zero, Color.Green); vertices[3] = new VertexPositionColor(Vector3.UnitY, Color.Green); quadrangles[1] = new Quadrangle(vertices[3].Position, 0.25f, 0.25f, GlobalSettings.FONT_INDEXSTART + 34); // Z vertices[4] = new VertexPositionColor(Vector3.Zero, Color.Blue); vertices[5] = new VertexPositionColor(-Vector3.UnitZ, Color.Blue); quadrangles[2] = new Quadrangle(vertices[5].Position, 0.25f, 0.25f, GlobalSettings.FONT_INDEXSTART + 35);
VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; output.Position = input.Position; output.Colour = input.Colour; return output; }
VertexPositionColor[] v = new VertexPositionColor[6]; // Set the translation based on the current camera's position Vector3 translation = game.ActiveCamera.Position + game.ActiveCamera.Forward; Matrix transform = Matrix.CreateScale(0.25f); //transform.Right = game.ActiveCamera.Right; //transform.Up = game.ActiveCamera.Up; //transform.Forward = game.ActiveCamera.Forward; transform.M41 = translation.X; transform.M42 = translation.Y; transform.M43 = translation.Z; //transform *= game.ActiveCamera.ViewMatrix; //transform *= game.ActiveCamera.ProjectionMatrix; //transform *= game.ActiveCamera.ViewProjectionMatrix; // Transform all the vertices for (int i = 0; i < 6; ++i) { v[i].Color = vertices[i].Color; v[i].Position = Vector3.Transform(vertices[i].Position, transform); } Effect effect = game.Effects["PreTransformed"]; // Disable the z buffer so that the axes are drawn on top of all other objects game.GraphicsDevice.DepthStencilState = DepthStencilState.None; effect.CurrentTechnique.Passes[0].Apply(); game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>( PrimitiveType.LineList, v, 0, 6, indices, 0, 3 );
Scale them to the appropriate size
Rotate them based on the camera rotation
Position them in front of the camera, but do not transform them from world to view space
transform *= game.ActiveCamera.ProjectionMatrix;
Also, turn off depth writes so it overlaps anything else that renders
game.GraphicsDevice.DepthStencilState = DepthStencilState.None;
You're are essentially rendering UI, but instead of using an orthographic projection you are using a perspective projection.
Posted 06 May 2011 - 05:47 AM
// Set the translation based on the current camera's position and an x, y, z offset float xOffset = 0.4f; float yOffset = 0.2f; float zOffset = 1f; Vector3 translation = camera.Position; translation += camera.Right * xOffset; translation += camera.Up * yOffset; translation += camera.Forward * zOffset; Matrix transform = new Matrix(); transform.M11 = 0.125f; transform.M22 = 0.125f; transform.M33 = 0.125f; transform.M41 = translation.X; transform.M42 = translation.Y; transform.M43 = translation.Z; transform.M44 = 1f;
// Transforming by camera rotation will make the fonts always face the camera transform.Right = game.ActiveCamera.Right * 0.125f; transform.Up = game.ActiveCamera.Up * 0.125f; transform.Forward = game.ActiveCamera.Forward * 0.125f;
Posted 06 May 2011 - 06:39 AM
Posted 06 May 2011 - 09:48 AM
BTW I implemented the same thing in my paper modeller, so I know what I'm talking about
Posted 06 May 2011 - 10:15 AM
void RenderCoordinateSystem() { int old_viewport[4]; glDisable(GL_DEPTH_TEST); glGetIntegerv(GL_VIEWPORT,old_viewport); glViewport(0,0,90,90); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-45,45,-45,45,-45,45); glMatrixMode(GL_MODELVIEW); TransformWithCameraRotation(); glBegin(GL_LINES); glColor4f(1,0,0,1); glVertex3f(0,0,0); glVertex3f(38,0,0); glColor4f(0,1,0,1); glVertex3f(0,0,0); glVertex3f(0,38,0); glColor4f(0,0,1,1); glVertex3f(0,0,0); glVertex3f(0,0,38); glEnd(); glColor4f(1,1,1,1); glPrint(40,0,0,"x"); glPrint(0,40,0,"y"); glPrint(0,0,40,"z"); glViewport(old_viewport[0],old_viewport[1],old_viewport[2],old_viewport[3]); glEnable(GL_DEPTH_TEST); } void TransformWithCameraRotation() { double mvm[16]; mvm[ 0] = MainCamera.mv_matrix[ 0]; mvm[ 1] = MainCamera.mv_matrix[ 1]; mvm[ 2] = MainCamera.mv_matrix[ 2]; mvm[ 3] = MainCamera.mv_matrix[ 3]; mvm[ 4] = MainCamera.mv_matrix[ 4]; mvm[ 5] = MainCamera.mv_matrix[ 5]; mvm[ 6] = MainCamera.mv_matrix[ 6]; mvm[ 7] = MainCamera.mv_matrix[ 7]; mvm[ 8] = MainCamera.mv_matrix[ 8]; mvm[ 9] = MainCamera.mv_matrix[ 9]; mvm[10] = MainCamera.mv_matrix[10]; mvm[11] = MainCamera.mv_matrix[11]; mvm[12] = 0; mvm[13] = 0; mvm[14] = 0; mvm[15] = 1; glLoadMatrixd(mvm); }
Posted 09 May 2011 - 06:24 AM
// Set viewport to the bottom left corner Viewport viewport = game.GraphicsDevice.Viewport; // Disable the z buffer so that the axes are drawn on top of all other objects game.GraphicsDevice.DepthStencilState = DepthStencilState.None; Matrix orthogonal = Matrix.CreateOrthographic( viewport.Width, viewport.Height, -viewport.Width, viewport.Width ); Camera camera = game.ActiveCamera; Effect effect = game.Effects["PreTransformed"]; // Get the camera/modelview matrix Matrix viewMatrix = camera.ViewMatrix; // Set it's last column (the translate part) to 0,0,0,1 viewMatrix.M41 = 0f; viewMatrix.M42 = 0f; viewMatrix.M43 = 0f; viewMatrix.M44 = 1f; effect.Parameters["ViewProjection"].SetValue(viewMatrix * orthogonal); effect.CurrentTechnique.Passes[0].Apply(); game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>( PrimitiveType.LineList, vertices, 0, 6, indices, 0, 3 );
Posted 09 May 2011 - 06:30 AM
Posted 09 May 2011 - 06:48 AM
Project the coordinates of the labels (I assume you know what I mean by that)
Posted 09 May 2011 - 07:12 AM
Posted 11 May 2011 - 02:56 PM
look into "world coordinates to screen coordinates"
// Always use the active camera to get the label to face the current camera Camera camera = game.ActiveCamera; Viewport viewport = game.GraphicsDevice.Viewport; // Scale //Matrix worldMatrix = Matrix.CreateScale(0.5f); Matrix worldMatrix = Matrix.CreateScale(50f); // Rotate worldMatrix *= camera.ViewMatrix; // Set its last column (the translation part) to (0, 0, 0, 1) worldMatrix.M41 = 0f; worldMatrix.M42 = 0f; worldMatrix.M43 = 0f; worldMatrix.M44 = 1f; // The transpose of view matrix to make the vertices face the current camera Matrix.Transpose(ref worldMatrix, out worldMatrix); // Translate worldMatrix.M41 = ObjectPosition.X; worldMatrix.M42 = ObjectPosition.Y; worldMatrix.M43 = ObjectPosition.Z; vertices[0] = Vector3.Transform(corners[0], worldMatrix); vertices[1] = Vector3.Transform(corners[1], worldMatrix); vertices[2] = Vector3.Transform(corners[2], worldMatrix); vertices[3] = Vector3.Transform(corners[3], worldMatrix); Matrix orthogonal = Matrix.CreateOrthographic( viewport.Width, viewport.Height, -viewport.Width, viewport.Width ); Matrix viewMatrix = camera.ViewMatrix; // Set its last column (the translation part) to (0, 0, 0, 1) viewMatrix.M41 = 0f; viewMatrix.M42 = 0f; viewMatrix.M43 = 0f; viewMatrix.M44 = 1f; game.WireShapeDrawer.Begin(viewMatrix, orthogonal); game.WireShapeDrawer.DrawLine(vertices[0], vertices[1], Color.White); game.WireShapeDrawer.DrawLine(vertices[1], vertices[3], Color.White); game.WireShapeDrawer.DrawLine(vertices[3], vertices[2], Color.White); game.WireShapeDrawer.DrawLine(vertices[2], vertices[0], Color.White); game.WireShapeDrawer.End();
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.