/
Program2.cs
123 lines (96 loc) · 4.25 KB
/
Program2.cs
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
using System;
using Tao.FreeGlut;
using OpenGL;
namespace OpenGLTutorial2
{
class Program
{
private static int width = 1280, height = 720;
private static ShaderProgram program;
private static VBO<Vector3> triangle, square;
private static VBO<int> triangleElements, squareElements;
static void Main(string[] args)
{
// create an OpenGL window
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
Glut.glutInitWindowSize(width, height);
Glut.glutCreateWindow("OpenGL Tutorial");
// provide the Glut callbacks that are necessary for running this tutorial
Glut.glutIdleFunc(OnRenderFrame);
Glut.glutDisplayFunc(OnDisplay);
Glut.glutCloseFunc(OnClose);
// compile the shader program
program = new ShaderProgram(VertexShader, FragmentShader);
// set the view and projection matrix, which are static throughout this tutorial
program.Use();
program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up));
// create a triangle
triangle = new VBO<Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(-1, -1, 0), new Vector3(1, -1, 0) });
triangleElements = new VBO<int>(new int[] { 0, 1, 2 }, BufferTarget.ElementArrayBuffer);
// create a square
square = new VBO<Vector3>(new Vector3[] { new Vector3(-1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, -1, 0), new Vector3(-1, -1, 0) });
squareElements = new VBO<int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);
Glut.glutMainLoop();
}
private static void OnClose()
{
// dispose of all of the resources that were created
triangle.Dispose();
triangleElements.Dispose();
square.Dispose();
squareElements.Dispose();
program.DisposeChildren = true;
program.Dispose();
}
private static void OnDisplay()
{
}
private static void OnRenderFrame()
{
// set up the OpenGL viewport and clear both the color and depth bits
Gl.Viewport(0, 0, width, height);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// use our shader program
Gl.UseProgram(program);
// transform the triangle
program["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(-1.5f, 0, 0)));
// bind the vertex attribute arrays for the triangle (the hard way)
uint vertexPositionIndex = (uint)Gl.GetAttribLocation(program.ProgramID, "vertexPosition");
Gl.EnableVertexAttribArray(vertexPositionIndex);
Gl.BindBuffer(triangle);
Gl.VertexAttribPointer(vertexPositionIndex, triangle.Size, triangle.PointerType, true, 12, IntPtr.Zero);
Gl.BindBuffer(triangleElements);
// draw the triangle
Gl.DrawElements(BeginMode.Triangles, triangleElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
// transform the square
program["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(1.5f, 0, 0)));
// bind the vertex attribute arrays for the square (the easy way)
Gl.BindBufferToShaderAttribute(square, program, "vertexPosition");
Gl.BindBuffer(squareElements);
// draw the square
Gl.DrawElements(BeginMode.Quads, squareElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
Glut.glutSwapBuffers();
}
public static string VertexShader = @"
#version 130
in vec3 vertexPosition;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void)
{
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";
public static string FragmentShader = @"
#version 130
out vec4 fragment;
void main(void)
{
fragment = vec4(1, 1, 1, 1);
}
";
}
}