[英]Which method inside GameWindow do we override to draw an object with OpenTk?
Problem问题
I am doing a video game environment design research and I happened to start with the basics of OpenGL with OpenTk
-The C# variant of OpenGL
.我正在做一个视频游戏环境设计研究,我碰巧从 OpenGL 和
OpenTk
的基础知识开始 - OpenGL 的OpenGL
变体。 I got code to draw a cube object in my GameWindow with OpenTk
from this site.我从这个站点获得了使用
OpenTk
在我的 GameWindow 中绘制立方体 object 的代码。 I override the method OnLoad
for my GameWindow
object and I call the method to draw the cube but nothing happens, the code draws the GameWindow
and NativeWIndow
without any Graphical Object.我为我的
GameWindow
object 覆盖了OnLoad
方法,并调用了该方法来绘制立方体,但没有任何反应,代码在没有任何图形 Object 的情况下绘制了GameWindow
和NativeWIndow
。
Expectations期望
I expected the code to draw a cube inside the GameWindow
Object.我希望代码在
GameWindow
Object 内绘制一个立方体。
Code Applied应用代码
//extend the GameWindow object to access methods from the super
class MyWindow : GameWindow
{
public MyWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings)
{
WindowState = OpenTK.Windowing.Common.WindowState.Maximized;
}
//when the window is loaded, draw the cube
protected override void OnLoad()
{
this.DrawBox(10);
}
//method to draw the cube
private void DrawBox(float size)
{
float[,] n = new float[,]{
{-1.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f},
{1.0f, 0.0f, 0.0f},
{0.0f, -1.0f, 0.0f},
{0.0f, 0.0f, 1.0f},
{0.0f, 0.0f, -1.0f}
};
int[,] faces = new int[,]{
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
float[,] v = new float[8, 3];
int i;
v[0, 0] = v[1, 0] = v[2, 0] = v[3, 0] = -size / 2;
v[4, 0] = v[5, 0] = v[6, 0] = v[7, 0] = size / 2;
v[0, 1] = v[1, 1] = v[4, 1] = v[5, 1] = -size / 2;
v[2, 1] = v[3, 1] = v[6, 1] = v[7, 1] = size / 2;
v[0, 2] = v[3, 2] = v[4, 2] = v[7, 2] = -size / 2;
v[1, 2] = v[2, 2] = v[5, 2] = v[6, 2] = size / 2;
GL.Begin(BeginMode.Quads);
for (i = 5; i >= 0; i--)
{
GL.Normal3(ref n[i, 0]);
GL.Vertex3(ref v[faces[i, 0], 0]);
GL.Vertex3(ref v[faces[i, 1], 0]);
GL.Vertex3(ref v[faces[i, 2], 0]);
GL.Vertex3(ref v[faces[i, 3], 0]);
}
GL.End();
}
}
//instantiation in the main method
GameWindowSettings gameWindowSettings= new GameWindowSettings();
NativeWindowSettings nativeWindowSettings = new NativeWindowSettings();
gameWindowSettings.IsMultiThreaded = false;
new MyWindow(gameWindowSettings, nativeWindowSettings).Run();
Please help me make this work.请帮我完成这项工作。
You must implement the OnUpdateFrame
event callback.您必须实现
OnUpdateFrame
事件回调。 You also need to call Context.SwapBuffers();
您还需要调用
Context.SwapBuffers();
to update the display:更新显示:
class MyWindow : GameWindow
{
// [...]
protected override void OnUpdateFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
this.DrawBox(0.5f);
Context.SwapBuffers();
base.OnUpdateFrame(e);
}
}
If you are not using a projection matrix, the coordinates must be in the range [-1.0, 1.0].如果您不使用投影矩阵,则坐标必须在 [-1.0, 1.0] 范围内。 The Normalized Device Space is a unique cube with the left, bottom, near corner (-1, -1, -1) and right, top, far corner (1, 1, 1).
标准化设备空间是一个独特的立方体,具有左、下、近角(-1、-1、-1)和右、上、远角(1、1、1)。 Without a projection matrix this cube is also the viewing volume (the space which is projected on the viewport).
如果没有投影矩阵,这个立方体也是查看体积(投影在视口上的空间)。
Since you are using Legacy OpenGL ( glBegin
/ glEnd
) you need to create a Compatibility profile OpenGL Context :由于您使用的是旧版 OpenGL (
glBegin
/ glEnd
),因此您需要创建一个兼容性配置文件OpenGL 上下文:
nativeWindowSettings.API = ContextAPI.OpenGL;
nativeWindowSettings.Profile = ContextProfile.Compatability;
See c_sharp_opengl/OpenTK_hello_triangle for a very basic example using "modern" OpenGL.有关使用“现代”OpenGL 的非常基本的示例,请参见c_sharp_opengl/ OpenTK_hello_triangle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.