/// <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.5f); { // Create the shader program. var vertexShaderSource = ManifestResourceLoader.LoadTextFile("Shader.vert"); var fragmentShaderSource = ManifestResourceLoader.LoadTextFile("Shader.frag"); var shaderProgram = new ShaderProgram(); shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null); shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "in_Position"); shaderProgram.BindAttributeLocation(gl, attributeIndexColour, "in_Color"); shaderProgram.AssertValid(gl); this.shaderProgram = shaderProgram; } { // Create the shader program. var vertexShaderSource = ColorCodedPickingShaderHelper.GetShaderSource(ColorCodedPickingShaderHelper.ShaderTypes.VertexShader); var fragmentShaderSource = ColorCodedPickingShaderHelper.GetShaderSource(ColorCodedPickingShaderHelper.ShaderTypes.FragmentShader); var shaderProgram = new ShaderProgram(); shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null); shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "in_Position"); shaderProgram.BindAttributeLocation(gl, attributeIndexColour, "in_Color"); shaderProgram.AssertValid(gl); this.pickingShaderProgram = shaderProgram; } // 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)); IPerspectiveCamera camera = this.cameraRotation.Camera; projectionMatrix = camera.GetProjectionMat4(); viewMatrix = this.cameraRotation.Camera.GetViewMat4(); modelMatrix = mat4.identity(); // Now create the geometry for the square. CreateVertices(gl); }
/// <summary> /// Draws the scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Draw(OpenGL gl, RenderMode renderMode = RenderMode.Render) { var shader = (renderMode == RenderMode.HitTest) ? pickingShaderProgram : shaderProgram; if (renderMode == RenderMode.HitTest) { gl.ClearColor(1, 1, 1, 1); } else { // Set a blue clear colour. gl.ClearColor(0.4f, 0.6f, 0.9f, 0.5f); } // Clear the scene. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); gl.PointSize(3); // Update matrices. IPerspectiveCamera camera = this.cameraRotation.Camera; projectionMatrix = camera.GetProjectionMat4(); viewMatrix = this.cameraRotation.Camera.GetViewMat4(); modelMatrix = mat4.identity(); // Bind the shader, set the matrices. shader.Bind(gl); shader.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shader.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shader.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. //gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, vertices.Length); gl.DrawArrays(OpenGL.GL_POINTS, 0, vertices.Length); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shader.Unbind(gl); gl.Flush(); }