您好我使用的是c#和OpenTK。
出于某种原因,我使用鼠标时我的y完全倒置了我需要设置y负值是正确的,但是当我渲染地图时地图被反转我一直试图找到问题但是我找不到什么使y倒置。
如果问题不在下面的代码中,我将发布c#项目的链接。
Game.cs,其中on渲染,加载或更新功能
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 :(得分:0)
我在spritebatch.cs中发现了问题所在的问题
它有这样的一行:
GL.Ortho(-ScreenWidth / 2f, ScreenWidth / 2f, -ScreenHeight / 2f, ScreenHeight / 2f, 0f, 1f);
假设是这样的
GL.Ortho(-ScreenWidth / 2f, ScreenWidth / 2f, ScreenHeight / 2f, -ScreenHeight / 2f, 0f, 1f);