Created
July 13, 2018 07:11
-
-
Save Daomephsta/2ffe8acf44ec7735c72a8c1c6eecacae to your computer and use it in GitHub Desktop.
Colours GL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenTK; | |
using OpenTK.Graphics; | |
using OpenTK.Graphics.OpenGL; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Tangerine; | |
namespace OpenGL_Testbed.Lighting | |
{ | |
class Colours : GameWindow | |
{ | |
private ShaderProgram shaderProgram; | |
private VertexBuffer vbo; | |
private VertexArray vao; | |
private PerspectiveCamera camera; | |
private readonly float[] cubeVertices = | |
{ | |
-0.5f, -0.5f, -0.5f, | |
0.5f, -0.5f, -0.5f, | |
0.5f, 0.5f, -0.5f, | |
0.5f, 0.5f, -0.5f, | |
-0.5f, 0.5f, -0.5f, | |
-0.5f, -0.5f, -0.5f, | |
-0.5f, -0.5f, 0.5f, | |
0.5f, -0.5f, 0.5f, | |
0.5f, 0.5f, 0.5f, | |
0.5f, 0.5f, 0.5f, | |
-0.5f, 0.5f, 0.5f, | |
-0.5f, -0.5f, 0.5f, | |
-0.5f, 0.5f, 0.5f, | |
-0.5f, 0.5f, -0.5f, | |
-0.5f, -0.5f, -0.5f, | |
-0.5f, -0.5f, -0.5f, | |
-0.5f, -0.5f, 0.5f, | |
-0.5f, 0.5f, 0.5f, | |
0.5f, 0.5f, 0.5f, | |
0.5f, 0.5f, -0.5f, | |
0.5f, -0.5f, -0.5f, | |
0.5f, -0.5f, -0.5f, | |
0.5f, -0.5f, 0.5f, | |
0.5f, 0.5f, 0.5f, | |
-0.5f, -0.5f, -0.5f, | |
0.5f, -0.5f, -0.5f, | |
0.5f, -0.5f, 0.5f, | |
0.5f, -0.5f, 0.5f, | |
-0.5f, -0.5f, 0.5f, | |
-0.5f, -0.5f, -0.5f, | |
-0.5f, 0.5f, -0.5f, | |
0.5f, 0.5f, -0.5f, | |
0.5f, 0.5f, 0.5f, | |
0.5f, 0.5f, 0.5f, | |
-0.5f, 0.5f, 0.5f, | |
-0.5f, 0.5f, -0.5f | |
}; | |
public Colours() : base(800, 600, GraphicsMode.Default, "Test Game", GameWindowFlags.FixedWindow, DisplayDevice.Default, 3, 3, GraphicsContextFlags.Default) { } | |
protected override void OnLoad(EventArgs e) | |
{ | |
//Setup the viewport | |
GL.Viewport(0, 0, this.Width, this.Height); | |
// Create a vertex shader | |
Shader vertShader = ResourceManager.CreateShaderFromFile(ShaderType.VertexShader, "resources/shaders/colours_vertex"); | |
//Create a fragment shader | |
Shader fragShader = ResourceManager.CreateShaderFromFile(ShaderType.FragmentShader, "resources/shaders/colours_fragment"); | |
//Create a shader program | |
shaderProgram = new ShaderProgram("default"); | |
shaderProgram.Attach(vertShader, delete: true); | |
shaderProgram.Attach(fragShader, delete: true); | |
shaderProgram.Link(); | |
//Initialise the camera | |
camera = new PerspectiveCamera(defaultFOV: 45.0F); | |
camera.Pos = new Vector3(0.0F, 0.0F, 3.0F); | |
camera.SetPitchBounds(-89.0D, 89.0D); | |
camera.SetFOVBounds(1.0F, 45.0F); | |
shaderProgram.SetUniform("model", Matrix4.Identity); | |
vao = new VertexArray(); | |
vao.Bind(); | |
//Create a VBO(Vertex Buffer Object) | |
vbo = new VertexBuffer(); | |
vbo.Bind(); | |
vbo.SetData(cubeVertices, BufferUsageHint.StaticDraw); | |
VertexFormat.POSITION.ApplyFormat(); | |
vbo.Unbind(); | |
vao.Unbind(); | |
shaderProgram.Use(); | |
} | |
protected override void OnRenderFrame(FrameEventArgs e) | |
{ | |
base.OnRenderFrame(e); | |
GL.Enable(EnableCap.DepthTest); | |
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); | |
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); | |
camera.UpdateView(shaderProgram, this.Width, this.Height); | |
vao.Bind(); | |
GL.DrawArrays(PrimitiveType.Triangles, 0, 36); | |
vao.Unbind(); | |
SwapBuffers(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
out vec4 fragmentColour; | |
void main() | |
{ | |
fragmentColour = vec4(1.0F, 0.0F, 0.0F, 1.0F); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
layout (location = 0) in vec3 position; | |
uniform mat4 model; | |
uniform mat4 view; | |
uniform mat4 projection; | |
void main() | |
{ | |
gl_Position = projection * view * model * vec4(position, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment