[英]Which method inside GameWindow do we override to draw an object with OpenTk?
問題
我正在做一個視頻游戲環境設計研究,我碰巧從 OpenGL 和OpenTk
的基礎知識開始 - OpenGL 的OpenGL
變體。 我從這個站點獲得了使用OpenTk
在我的 GameWindow 中繪制立方體 object 的代碼。 我為我的GameWindow
object 覆蓋了OnLoad
方法,並調用了該方法來繪制立方體,但沒有任何反應,代碼在沒有任何圖形 Object 的情況下繪制了GameWindow
和NativeWIndow
。
期望
我希望代碼在GameWindow
Object 內繪制一個立方體。
應用代碼
//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();
請幫我完成這項工作。
您必須實現OnUpdateFrame
事件回調。 您還需要調用Context.SwapBuffers();
更新顯示:
class MyWindow : GameWindow
{
// [...]
protected override void OnUpdateFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
this.DrawBox(0.5f);
Context.SwapBuffers();
base.OnUpdateFrame(e);
}
}
如果您不使用投影矩陣,則坐標必須在 [-1.0, 1.0] 范圍內。 標准化設備空間是一個獨特的立方體,具有左、下、近角(-1、-1、-1)和右、上、遠角(1、1、1)。 如果沒有投影矩陣,這個立方體也是查看體積(投影在視口上的空間)。
由於您使用的是舊版 OpenGL ( glBegin
/ glEnd
),因此您需要創建一個兼容性配置文件OpenGL 上下文:
nativeWindowSettings.API = ContextAPI.OpenGL;
nativeWindowSettings.Profile = ContextProfile.Compatability;
有關使用“現代”OpenGL 的非常基本的示例,請參見c_sharp_opengl/ OpenTK_hello_triangle。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.