1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
| using System;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK;
namespace MitsubishiLogo
{
class Program
{
static void Main(string[] args)
{
using (var window = new Window())
{
window.Title = "OpenGL 3, C#";
window.Run();
}
}
}
class Window : GameWindow
{
private Matrix4 _projMatrix;
private Matrix4 _modelMatrix;
private Matrix4 _mpMatrix;
private int _uMPMatrixLocation;
public Window() : base(250, 250, new GraphicsMode(32, 0, 0, 4)) { }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var vShaderSource =
@"
#version 130
in vec3 aPosition;
uniform mat4 uMPMatrix;
void main()
{
gl_Position = uMPMatrix * vec4(aPosition, 1.0);
}
";
var fShaderSource =
@"
#version 130
precision mediump float;
out vec4 fragColor;
void main()
{
fragColor = vec4(0.0);
}
";
var vShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vShader, vShaderSource);
GL.CompileShader(vShader);
var fShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fShader, fShaderSource);
GL.CompileShader(fShader);
var program = GL.CreateProgram();
GL.AttachShader(program, vShader);
GL.AttachShader(program, fShader);
GL.LinkProgram(program);
GL.UseProgram(program);
int vbo;
GL.CreateBuffers(1, out vbo);
float[] positions = new float[]
{
0f, -0.866f, -1f,
0.5f, 0f, -1f,
0f, 0.866f, -1f,
-0.5f, 0f, -1f
};
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * positions.Length, positions, BufferUsageHint.StaticDraw);
var aPositionLocation = GL.GetAttribLocation(program, "aPosition");
GL.VertexAttribPointer(aPositionLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.EnableVertexAttribArray(aPositionLocation);
_uMPMatrixLocation = GL.GetUniformLocation(program, "uMPMatrix");
GL.ClearColor(1f, 1f, 1f, 1f);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
_modelMatrix =
Matrix4.CreateScale(5f, 5f, 1f) *
Matrix4.CreateTranslation(0f, 0.866f * 5f, 0f);
_mpMatrix = _modelMatrix * _projMatrix;
GL.UniformMatrix4(_uMPMatrixLocation, false, ref _mpMatrix);
GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);
_modelMatrix =
Matrix4.CreateScale(5f, 5f, 1f) *
Matrix4.CreateTranslation(0f, 0.866f * 5f, 0f) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(120f));
_mpMatrix = _modelMatrix * _projMatrix;
GL.UniformMatrix4(_uMPMatrixLocation, false, ref _mpMatrix);
GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);
_modelMatrix =
Matrix4.CreateScale(5f, 5f, 1f) *
Matrix4.CreateTranslation(0f, 0.866f * 5f, 0f) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(240f));
_mpMatrix = _modelMatrix * _projMatrix;
GL.UniformMatrix4(_uMPMatrixLocation, false, ref _mpMatrix);
GL.DrawArrays(PrimitiveType.TriangleFan, 0, 4);
SwapBuffers();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, Width, Height);
float aspect = (float) Width / Height;
float worldWidth = aspect * 20f;
_projMatrix = Matrix4.CreateOrthographic(worldWidth, 20f, 100f, -100f);
}
}
} |