我从C# and MonoGame
for开始MonoDevelop
。当我尝试构建一个三角形时,我得到了这个错误:
System.NullReferenceException: Object reference not set to an instance of an object
at OpenTK.Graphics.ES11.GL.GetError () [0x00000] in <filename unknown>:0
at OpenTK.Graphics.ES11.ErrorHelper.ResetErrors () [0x00000] in <filename unknown>:0
at OpenTK.Graphics.ES11.ErrorHelper..ctor (IGraphicsContext context) [0x00000] in <filename unknown>:0
at OpenTK.Graphics.ES11.GL.EnableClientState (All array) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.Graphics.GLStateManager.VertexArray (Boolean enable) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.Graphics.VertexDeclaration.PrepareForUse (Microsoft.Xna.Framework.Graphics.VertexDeclaration vd) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.Graphics.GraphicsDevice.DrawUserPrimitives[VertexPositionColor] (PrimitiveType primitiveType, Microsoft.Xna.Framework.Graphics.VertexPositionColor[] vertexData, Int32 vertexOffset, Int32 primitiveCount) [0x00000] in <filename unknown>:0
at teste.Game1.Draw (Microsoft.Xna.Framework.GameTime gameTime) [0x000a6] in /home/luizeduardo/Área de Trabalho/learning/teste/teste/Game1.cs:110
at Microsoft.Xna.Framework.Game.DoDraw (Microsoft.Xna.Framework.GameTime gameTime) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.GameWindow.OnRenderFrame (System.Object sender, OpenTK.FrameEventArgs e) [0x00000] in <filename unknown>:0
at OpenTK.GameWindow.OnRenderFrame (OpenTK.FrameEventArgs e) [0x00000] in <filename unknown>:0
at OpenTK.GameWindow.OnRenderFrameInternal (OpenTK.FrameEventArgs e) [0x00000] in <filename unknown>:0
at OpenTK.GameWindow.RaiseRenderFrame (System.Diagnostics.Stopwatch render_watch, System.Double& next_render, OpenTK.FrameEventArgs render_args) [0x00000] in <filename unknown>:0
at OpenTK.GameWindow.DispatchUpdateAndRenderFrame (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0
at OpenTK.GameWindow.Run (Double updates_per_second, Double frames_per_second) [0x00000] in <filename unknown>:0
at OpenTK.GameWindow.Run (Double updateRate) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.GameWindow.Run (Double updateRate) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.LinuxGamePlatform.RunLoop () [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.Game.Run (GameRunBehavior runBehavior) [0x00000] in <filename unknown>:0
at Microsoft.Xna.Framework.Game.Run () [0x00000] in <filename unknown>:0
at teste.Program.Main () [0x0000a] in /home/luizeduardo/Área de Trabalho/learning/teste/teste/Program.cs:21
有谁知道我该如何解决这个问题?
以下是我使用的变量:
VertexPositionColor[] verts;
BasicEffect effect;
VertexBuffer buffer;
这是 Initialize() 方法:
{
verts = new VertexPositionColor[3]
{
new VertexPositionColor(new Vector3(0.0f, 1.0f, 0.0f), Color.Red),
new VertexPositionColor(new Vector3(-1.0f, -1.0f, 0.0f), Color.Blue),
new VertexPositionColor(new Vector3(1.0f, -1.0f, 0.0f), Color.Green),
};
effect = new BasicEffect(GraphicsDevice);
buffer = new VertexBuffer(GraphicsDevice, VertexPositionColor.VertexDeclaration, 3, BufferUsage.WriteOnly);
buffer.SetData(verts);
// TODO: Add your initialization logic here
base.Initialize();
}
而且,最重要的 Draw() 方法:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.001f, 1000.0f);
effect.View = Matrix.CreateLookAt(new Vector3(0, 0, -5), Vector3.Forward, Vector3.Up);
effect.World = Matrix.Identity;
effect.VertexColorEnabled = true;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, verts, 0, 1);
}
base.Draw(gameTime);
}
}
我正在使用在 Linux Xubuntu 上运行的 MonoDevelop 3.0.3.2。