2016-11-25 42 views
1

你好我正在使用c#和OpenTK。OpenTK Y是完全倒转的

由于某种原因,我使用鼠标时我的y完全倒置,我需要设置y负才能正确,但是当我渲染地图时,地图是倒置的,我一直在试图找到问题,但找不到是什么使y被倒置。

如果问题不在下面的代码中,我会让c#项目的链接。

Game.cs在上渲染,负载或更新funcions吃

using OpenTK; 
using OpenTK.Graphics.OpenGL; 
using System; 
using System.Drawing; 

namespace PlatformGame 
{ 
    class Game : GameWindow 
    { 

     public static int GridSize = 32, TileSize = 1024; 

     Texture2D Texture, TileSet; 
     Texture2D Water; 
     Texture2D Grass; 

     View view; 

     Level level; 

     public Game(int width,int height) 
      : base(width,height) 
     { 
      GL.Enable(EnableCap.Texture2D); 

      view = new View(Vector2.Zero, 1.0, 0.0); 

      Input.Initialize(this); 
     } 

     protected override void OnLoad(EventArgs e) 
     { 
      base.OnLoad(e); 

      Water = ContentPipe.LoadTexture("Water.png"); 
      Grass = ContentPipe.LoadTexture("Grass.png"); 
      TileSet = ContentPipe.LoadTexture("Tiles.png"); 
      level = new Level("Content/Level.tmx"); 
     } 

     protected override void OnUpdateFrame(FrameEventArgs e) 
     { 
      base.OnUpdateFrame(e); 

      if(Input.MousePress(OpenTK.Input.MouseButton.Left)) 
      { 
       Vector2 pos = new Vector2(Mouse.X, Mouse.Y) - new Vector2(Width, Height)/2f; 
       pos = view.ToWorld(pos); 
       view.SetPosition(pos, View.TweenType.QuarticOut, 15); 
      } 

      if (Input.Keydown(OpenTK.Input.Key.Right)) 
      { 
       view.SetPosition(view.PositionGoTo + new Vector2(5, 0), View.TweenType.QuarticOut, 15); 
      } 

      if (Input.Keydown(OpenTK.Input.Key.Left)) 
      { 
       view.SetPosition(view.PositionGoTo + new Vector2(-5, 0), View.TweenType.QuarticOut, 15); 
      } 

      if (Input.Keydown(OpenTK.Input.Key.Up)) 
      { 
       view.SetPosition(view.PositionGoTo + new Vector2(0, 5), View.TweenType.QuarticOut, 15); 
      } 

      if (Input.Keydown(OpenTK.Input.Key.Down)) 
      { 
       view.SetPosition(view.PositionGoTo + new Vector2(0, -5), View.TweenType.QuarticOut, 15); 
      } 

      view.Update(); 
      Input.Update(); 

     } 

     protected override void OnRenderFrame(FrameEventArgs e) 
     { 
      base.OnRenderFrame(e); 
      GL.Clear(ClearBufferMask.ColorBufferBit);   //Clear Window 
      GL.ClearColor(Color.CornflowerBlue);    //Set Windo background to Corn flower blue 

      SpriteBatch.Begin(Width, Height); 
      view.ApplyTransform(); 

      for (int x = 0; x < level.Width; x++) 
      { 
       for (int y = 0; y < level.Height; y++) 
       { 
        RectangleF Source = new RectangleF(0, 0, 0, 0); 

        switch (level[x,y].Type) 
        { 
         case BlockType.Ladder: 
          Source = new RectangleF(0 * TileSize, 2 * TileSize, TileSize, TileSize); 
          break; 
         case BlockType.Water: 
          Source = new RectangleF(0 * TileSize, 0 * TileSize, TileSize, TileSize); 
          break; 
         case BlockType.Stone: 
          Source = new RectangleF(0 * TileSize, 1 * TileSize, TileSize, TileSize); 
          break; 
         case BlockType.Platform: 
          Source = new RectangleF(1 * TileSize, 1 * TileSize, TileSize, TileSize); 
          break; 
         case BlockType.Dirt: 
          Source = new RectangleF(2 * TileSize, 0 * TileSize, TileSize, TileSize); 
          break; 
         case BlockType.Grass: 
          Source = new RectangleF(1 * TileSize, 0 * TileSize, TileSize, TileSize); 
          break; 
         case BlockType.LadderPlatform: 
          Source = new RectangleF(2 * TileSize, 1 * TileSize, TileSize, TileSize); 
          break; 
        } 

        SpriteBatch.Draw(TileSet, new Vector2(x * GridSize, y * GridSize), new Vector2((float)GridSize/TileSize), Color.Transparent, Vector2.Zero, Source); 
       } 
      } 

      SwapBuffers(); 
     } 
    } 
} 

View.cs什么控制相机

using System; 
using System.Drawing; 
using OpenTK; 
using OpenTK.Graphics.OpenGL; 

namespace PlatformGame 
{ 
    class View 
    { 

     public enum TweenType 
     { 
      Instante, 
      Linear, 
      QuadraticInOut, 
      CubicInOut, 
      QuarticOut, 
     } 

     private Vector2 position; 
     /// <summary> 
     /// In radians, + = clockwise 
     /// </summary> 
     public double rotation; 
     /// <summary> 
     /// 1 = no zoom 
     /// 2 = 2x zoom 
     /// </summary> 
     public double zoom; 

     private Vector2 PositionGoto, PositionFrom; 
     private TweenType tweenType; 
     private int CurrentStep, TweenStep; 

     public Vector2 Position 
     { 
      get 
      { 
       return position; 
      } 
     } 
     public Vector2 PositionGoTo 
     { 

      get { return PositionGoto; } 
     } 

     public Vector2 ToWorld(Vector2 input) 
     { 
      input /= (float)zoom; 
      Vector2 dX = new Vector2((float)Math.Cos(rotation),(float)Math.Sin(rotation)); 
      Vector2 dY = new Vector2((float)Math.Cos(rotation + MathHelper.PiOver2), (float)Math.Sin(rotation + -MathHelper.PiOver2)); 

      return (position + dX * input.X + dY * input.Y); 
     } 

     public View(Vector2 StartPosition,double StartZoom = 1.0, double StartRotation = 0.0) 
     { 
      position = StartPosition; 
      zoom = StartZoom; 
      rotation = StartRotation; 
     } 

     public void Update() 
     { 
      if (CurrentStep < TweenStep) 
      { 
       CurrentStep++; 
       switch (tweenType) 
       { 
        case TweenType.Linear: 
         position = PositionFrom + (PositionGoto - PositionFrom) * GetLinear((float)CurrentStep/TweenStep); 
         break; 
        case TweenType.QuadraticInOut: 
         position = PositionFrom + (PositionGoto - PositionFrom) * GetQuadraticInOut((float)CurrentStep/TweenStep); 
         break; 
        case TweenType.CubicInOut: 
         position = PositionFrom + (PositionGoto - PositionFrom) * GetCubicInOut((float)CurrentStep/TweenStep); 
         break; 
        case TweenType.QuarticOut: 
         position = PositionFrom + (PositionGoto - PositionFrom) * GetQuarticOut((float)CurrentStep/TweenStep); 
         break; 
       } 


      } 
      else 
      { 
       position = PositionGoto; 
      } 
     } 

     public void SetPosition(Vector2 NewPosition) 
     { 
      position = NewPosition; 
      PositionFrom = NewPosition; 
      PositionGoto = NewPosition; 
      tweenType = TweenType.Instante; 
      CurrentStep = 0; 
      TweenStep = 0; 
     } 

     public void SetPosition(Vector2 NewPosition, TweenType type, int NumSteps) 
     { 
      PositionFrom = position; 
      position = NewPosition; 
      PositionGoto = NewPosition; 
      tweenType = type; 
      CurrentStep = 0; 
      TweenStep = NumSteps; 
     } 

     public float GetLinear(float t) 
     { 
      return t; 
     } 

     public float GetQuadraticInOut(float t) 
     { 
      return (t * t)/((2 * t * t) - (2 * t) + 1); 
     } 

     public float GetCubicInOut(float t) 
     { 
      return (t * t * t)/((3 * t * t) - (3 * t) + 1); 
     } 

     public float GetQuarticOut(float t) 
     { 
      return -((t - 1) * (t - 1) * (t - 1) * (t - 1)) + 1; 
     } 

     public void ApplyTransform() 
     { 
      Matrix4 transfotm = Matrix4.Identity; 

      transfotm = Matrix4.Mult(transfotm, Matrix4.CreateTranslation(-position.X, -position.Y, 0)); 
      transfotm = Matrix4.Mult(transfotm, Matrix4.CreateRotationZ(-(float)rotation)); 
      transfotm = Matrix4.Mult(transfotm, Matrix4.CreateScale((float)zoom, (float)zoom, 1.0f)); 

      GL.MultMatrix(ref transfotm); 
     } 
    } 
} 

项目Link

+0

什么哟由y意味着反转?鼠标坐标从0/0开始在左上角,然后转到screen.width/screen.height(右下角)。 opentk的起源在左下角。 y在设计上是倒置的。 –

+0

@ThomasVoß,但地图由于某种原因,它倒过来 – Pedro

回答

0

我已经找到了问题出在spritebatch.cs中

它h AVE这样一行:

GL.Ortho(-ScreenWidth/2f, ScreenWidth/2f, -ScreenHeight/2f, ScreenHeight/2f, 0f, 1f); 

它是假设是这样的

GL.Ortho(-ScreenWidth/2f, ScreenWidth/2f, ScreenHeight/2f, -ScreenHeight/2f, 0f, 1f);