using OpenTK.Graphics.OpenGL;
|
using OpenTK.Mathematics;
|
|
namespace Yw.WinFrmUI.Test.Core
|
{
|
public partial class Form2 : Form
|
{
|
public Form2()
|
{
|
InitializeComponent();
|
this.glControl1.Load += GlControl1_Load;
|
this.glControl1.Paint += GlControl1_Paint;
|
this.glControl1.Resize += GlControl1_Resize;
|
}
|
|
private Matrix4 _projectionMatrix;
|
private Matrix4 _viewMatrix;
|
private Matrix4 _modelMatrix;
|
private float _translationX, _translationY;
|
private float _rotationX, _rotationY;
|
|
private void GlControl1_Load(object sender, EventArgs e)
|
{
|
InitializeGLState();
|
}
|
|
private void GlControl1_Resize(object sender, EventArgs e)
|
{
|
UpdateViewPort();
|
UpdateModelMatrix();
|
UpdateViewMatrix();
|
UpdateProjectionMatrix();
|
}
|
|
private void GlControl1_Paint(object sender, PaintEventArgs e)
|
{
|
this.glControl1.MakeCurrent();
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
|
|
// 旋转立方体
|
_rotationX += 0.001f;
|
//_rotationY += 0.01f;
|
|
//投影矩阵
|
GL.MatrixMode(MatrixMode.Projection);
|
GL.LoadMatrix(ref _projectionMatrix);
|
|
//视图矩阵
|
GL.MatrixMode(MatrixMode.Modelview);
|
var viewMatrix = _viewMatrix;
|
GL.LoadMatrix(ref viewMatrix);
|
GL.MultMatrix(ref _modelMatrix);
|
|
|
//GL.Rotate(_rotationY, Vector3.UnitY); // 绕Y轴旋转45度
|
//GL.Rotate(_rotationX, Vector3.UnitX); // 绕X轴旋转30度
|
|
// 绘制立方体
|
DrawCube(100);
|
Draw3DAxes(100);
|
|
this.glControl1.SwapBuffers();
|
UpdateViewPort();
|
UpdateModelMatrix();
|
UpdateViewMatrix();
|
UpdateProjectionMatrix();
|
|
this.glControl1.Invalidate();
|
}
|
|
|
|
|
//初始化GL状态
|
private void InitializeGLState()
|
{
|
GL.ClearColor(Color.Transparent);
|
GL.Enable(EnableCap.DepthTest);
|
}
|
|
//设置ViewPort
|
private void UpdateViewPort()
|
{
|
GL.Viewport(0, 0, Width, Height);
|
}
|
|
//更新投影矩阵
|
private void UpdateProjectionMatrix()
|
{
|
var width = this.glControl1.Width;
|
var height = this.glControl1.Height;
|
_projectionMatrix = Matrix4.CreateOrthographic(width, height, 0.1f, 1000f);
|
}
|
|
//更新视图矩阵
|
private void UpdateViewMatrix()
|
{
|
_viewMatrix = Matrix4.LookAt(
|
new Vector3(0, 0, 150), // 摄像机位置
|
Vector3.Zero, // 观察目标
|
Vector3.UnitY // 上方向
|
);
|
}
|
|
//更新模型矩阵
|
private void UpdateModelMatrix()
|
{
|
_modelMatrix = Matrix4.CreateScale(2f) * Matrix4.CreateRotationX(_rotationX)
|
* Matrix4.CreateRotationY(_rotationY);
|
|
}
|
|
|
|
|
void Draw3DAxes(float length)
|
{
|
GL.LineWidth(3);
|
GL.Begin(PrimitiveType.Lines);
|
|
// X轴 (红)
|
GL.Color3(1, 0, 0);
|
GL.Vertex3(0, 0, 0); GL.Vertex3(length, 0, 0);
|
|
// Y轴 (绿)
|
GL.Color3(0, 1, 0);
|
GL.Vertex3(0, 0, 0); GL.Vertex3(0, length, 0);
|
|
// Z轴 (蓝)
|
GL.Color3(0, 0, 1);
|
GL.Vertex3(0, 0, 0); GL.Vertex3(0, 0, length);
|
|
GL.End();
|
}
|
|
void DrawCube(float size)
|
{
|
float half = size / 2;
|
|
GL.Begin(PrimitiveType.Quads);
|
|
// 前面 (Z+ 方向,红色)
|
GL.Color3(1.0, 0.0, 0.0);
|
GL.Vertex3(-half, -half, half);
|
GL.Vertex3(half, -half, half);
|
GL.Vertex3(half, half, half);
|
GL.Vertex3(-half, half, half);
|
|
// 后面 (Z- 方向,绿色)
|
GL.Color3(0.0, 1.0, 0.0);
|
GL.Vertex3(-half, -half, -half);
|
GL.Vertex3(-half, half, -half);
|
GL.Vertex3(half, half, -half);
|
GL.Vertex3(half, -half, -half);
|
|
// 左面 (X- 方向,蓝色)
|
GL.Color3(0.0, 0.0, 1.0);
|
GL.Vertex3(-half, -half, -half);
|
GL.Vertex3(-half, -half, half);
|
GL.Vertex3(-half, half, half);
|
GL.Vertex3(-half, half, -half);
|
|
// 右面 (X+ 方向,黄色)
|
GL.Color3(1.0, 1.0, 0.0);
|
GL.Vertex3(half, -half, half);
|
GL.Vertex3(half, -half, -half);
|
GL.Vertex3(half, half, -half);
|
GL.Vertex3(half, half, half);
|
|
// 顶面 (Y+ 方向,品红)
|
GL.Color3(1.0, 0.0, 1.0);
|
GL.Vertex3(-half, half, half);
|
GL.Vertex3(half, half, half);
|
GL.Vertex3(half, half, -half);
|
GL.Vertex3(-half, half, -half);
|
|
// 底面 (Y- 方向,青色)
|
GL.Color3(0.0, 1.0, 1.0);
|
GL.Vertex3(-half, -half, -half);
|
GL.Vertex3(half, -half, -half);
|
GL.Vertex3(half, -half, half);
|
GL.Vertex3(-half, -half, half);
|
|
GL.End();
|
}
|
|
|
}
|
}
|