Guest User

Untitled

a guest
Jan 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. //Draw Cube in openGL
  2. glPushMatrix();
  3. glTranslatef(1000,0,1000);
  4. glRotatef(90,0,1,0);
  5. DrawCube();
  6. glPopMatrix();
  7.  
  8. protected override void Update(GameTime gameTime)
  9. {
  10. // Allows the game to exit
  11. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  12. this.Exit();
  13.  
  14. float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
  15. ProcessInput(timeDifference);
  16.  
  17. base.Update(gameTime);
  18. }
  19. protected override void Draw(GameTime gameTime)
  20. {
  21. GraphicsDevice.Clear(Color.CornflowerBlue);
  22.  
  23. //Text
  24. DrawText();
  25.  
  26. //CUBE
  27. // Set the World matrix which defines the position of the cube
  28. cubeEffect.World = Matrix.CreateTranslation(modelPosition);
  29. // Set the View matrix which defines the camera and what it's looking at
  30. cubeEffect.View = Matrix.CreateLookAt(cameraPositionCube, modelPosition, Vector3.Up);
  31. // Set the Projection matrix which defines how we see the scene (Field of view)
  32. cubeEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1.0f, 1000.0f);
  33. // Enable textures on the Cube Effect. this is necessary to texture the model
  34. cubeEffect.TextureEnabled = true;
  35. cubeEffect.Texture = cubeTexture;
  36. // Enable some pretty lights
  37. cubeEffect.EnableDefaultLighting();
  38. // apply the effect and render the cube
  39. foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)
  40. {
  41. pass.Apply();
  42. cubeToDraw.RenderToDevice(GraphicsDevice);
  43. }
  44.  
  45. base.Draw(gameTime);
  46. }
  47.  
  48. private void UpdateViewMatrix()
  49. {
  50. Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
  51.  
  52. Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
  53. Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
  54. Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;
  55.  
  56. Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
  57. Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
  58.  
  59. viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
  60. }
  61.  
  62. private void ProcessInput(float amount)
  63. {
  64. MouseState currentMouseState = Mouse.GetState();
  65. if (currentMouseState != originalMouseState)
  66. {
  67. float xDifference = currentMouseState.X - originalMouseState.X;
  68. float yDifference = currentMouseState.Y - originalMouseState.Y;
  69. leftrightRot -= rotationSpeed * xDifference * amount;
  70. updownRot -= rotationSpeed * yDifference * amount;
  71. Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
  72. UpdateViewMatrix();
  73. }
  74.  
  75. Vector3 moveVector = new Vector3(0, 0, 0);
  76. KeyboardState keyState = Keyboard.GetState();
  77. if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
  78. moveVector += new Vector3(0, 0, -1);
  79. if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
  80. moveVector += new Vector3(0, 0, 1);
  81. if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
  82. moveVector += new Vector3(1, 0, 0);
  83. if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
  84. moveVector += new Vector3(-1, 0, 0);
  85. if (keyState.IsKeyDown(Keys.Q))
  86. moveVector += new Vector3(0, 1, 0);
  87. if (keyState.IsKeyDown(Keys.Z))
  88. moveVector += new Vector3(0, -1, 0);
  89. AddToCameraPosition(moveVector * amount);
  90. }
  91.  
  92. private void AddToCameraPosition(Vector3 vectorToAdd)
  93. {
  94. Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
  95. Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
  96. cameraPosition += moveSpeed * rotatedVector;
  97. UpdateViewMatrix();
  98. }
Add Comment
Please, Sign In to add comment