-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Scene.cs
136 lines (115 loc) · 5.79 KB
/
Scene.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
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using GlmNet;
using SharpGL;
using SharpGL.Shaders;
using SharpGL.VertexBuffers;
namespace ModernOpenGLSample
{
/// <summary>
/// Represents the Scene for this sample.
/// </summary>
/// <remarks>
/// This code is based on work from the OpenGL 4.x Swiftless tutorials, please see:
/// http://www.swiftless.com/opengl4tuts.html
/// </remarks>
public class Scene
{
// The projection, view and model matrices.
mat4 projectionMatrix;
mat4 viewMatrix;
mat4 modelMatrix;
// Constants that specify the attribute indexes.
const uint attributeIndexPosition = 0;
const uint attributeIndexColour = 1;
// The vertex buffer array which contains the vertex and colour buffers.
VertexBufferArray vertexBufferArray;
// The shader program for our vertex and fragment shader.
private ShaderProgram shaderProgram;
/// <summary>
/// Initialises the scene.
/// </summary>
/// <param name="gl">The OpenGL instance.</param>
/// <param name="width">The width of the screen.</param>
/// <param name="height">The height of the screen.</param>
public void Initialise(OpenGL gl, float width, float height)
{
// Set a blue clear colour.
gl.ClearColor(0.4f, 0.6f, 0.9f, 0.0f);
// Create the shader program.
var vertexShaderSource = ManifestResourceLoader.LoadTextFile("Shader.vert");
var fragmentShaderSource = ManifestResourceLoader.LoadTextFile("Shader.frag");
shaderProgram = new ShaderProgram();
shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null);
shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "in_Position");
shaderProgram.BindAttributeLocation(gl, attributeIndexColour, "in_Color");
shaderProgram.AssertValid(gl);
// Create a perspective projection matrix.
const float rads = (60.0f / 360.0f) * (float)Math.PI * 2.0f;
projectionMatrix = glm.perspective(rads, width / height, 0.1f, 100.0f);
// Create a view matrix to move us back a bit.
viewMatrix = glm.translate(new mat4(1.0f), new vec3(0.0f, 0.0f, -5.0f));
// Create a model matrix to make the model a little bigger.
modelMatrix = glm.scale(new mat4(1.0f), new vec3(2.5f));
// Now create the geometry for the square.
CreateVerticesForSquare(gl);
}
/// <summary>
/// Draws the scene.
/// </summary>
/// <param name="gl">The OpenGL instance.</param>
public void Draw(OpenGL gl)
{
// Clear the scene.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT);
// Bind the shader, set the matrices.
shaderProgram.Bind(gl);
shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array());
shaderProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array());
shaderProgram.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array());
// Bind the out vertex array.
vertexBufferArray.Bind(gl);
// Draw the square.
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6);
// Unbind our vertex array and shader.
vertexBufferArray.Unbind(gl);
shaderProgram.Unbind(gl);
}
/// <summary>
/// Creates the geometry for the square, also creating the vertex buffer array.
/// </summary>
/// <param name="gl">The OpenGL instance.</param>
private void CreateVerticesForSquare(OpenGL gl)
{
var vertices = new float[18];
var colors = new float[18]; // Colors for our vertices
vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = 0.0f; // Bottom left corner
colors[0] = 1.0f; colors[1] = 1.0f; colors[2] = 1.0f; // Bottom left corner
vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = 0.0f; // Top left corner
colors[3] = 1.0f; colors[4] = 0.0f; colors[5] = 0.0f; // Top left corner
vertices[6] = 0.5f; vertices[7] = 0.5f; vertices[8] = 0.0f; // Top Right corner
colors[6] = 0.0f; colors[7] = 1.0f; colors[8] = 0.0f; // Top Right corner
vertices[9] = 0.5f; vertices[10] = -0.5f; vertices[11] = 0.0f; // Bottom right corner
colors[9] = 0.0f; colors[10] = 0.0f; colors[11] = 1.0f; // Bottom right corner
vertices[12] = -0.5f; vertices[13] = -0.5f; vertices[14] = 0.0f; // Bottom left corner
colors[12] = 1.0f; colors[13] = 1.0f; colors[14] = 1.0f; // Bottom left corner
vertices[15] = 0.5f; vertices[16] = 0.5f; vertices[17] = 0.0f; // Top Right corner
colors[15] = 0.0f; colors[16] = 1.0f; colors[17] = 0.0f; // Top Right corner
// Create the vertex array object.
vertexBufferArray = new VertexBufferArray();
vertexBufferArray.Create(gl);
vertexBufferArray.Bind(gl);
// Create a vertex buffer for the vertex data.
var vertexDataBuffer = new VertexBuffer();
vertexDataBuffer.Create(gl);
vertexDataBuffer.Bind(gl);
vertexDataBuffer.SetData(gl, 0, vertices, false, 3);
// Now do the same for the colour data.
var colourDataBuffer = new VertexBuffer();
colourDataBuffer.Create(gl);
colourDataBuffer.Bind(gl);
colourDataBuffer.SetData(gl, 1, colors, false, 3);
// Unbind the vertex array, we've finished specifying data for it.
vertexBufferArray.Unbind(gl);
}
}
}