Created
October 9, 2017 22:11
-
-
Save Daomephsta/096d044b94e0c7e6950a318a911d9d37 to your computer and use it in GitHub Desktop.
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 OpenGL_Testbed.Lib; | |
using OpenTK; | |
using OpenTK.Graphics; | |
using OpenTK.Graphics.OpenGL; | |
using System; | |
using System.IO; | |
namespace OpenGL_Testbed | |
{ | |
public class CoordinateSystems : GameWindow | |
{ | |
private ShaderProgram shaderProgram; | |
private VertexBuffer vbo; | |
private VertexArray vao; | |
private Texture texture1; | |
private Texture texture2; | |
private Matrix4 view; | |
private Matrix4 projection; | |
private readonly float[] vertices = | |
{ | |
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, | |
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, | |
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, | |
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, | |
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, | |
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, | |
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, | |
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, | |
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, | |
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, | |
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, | |
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, | |
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, | |
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, | |
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, | |
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, | |
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, | |
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, | |
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, | |
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, | |
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, | |
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, | |
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, | |
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, | |
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, | |
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, | |
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, | |
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, | |
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, | |
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, | |
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, | |
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, | |
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, | |
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, | |
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, | |
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f | |
}; | |
public CoordinateSystems() : base(800, 600, GraphicsMode.Default, "Test Game", GameWindowFlags.FixedWindow, DisplayDevice.Default, 3, 3, GraphicsContextFlags.Default) { } | |
protected override void OnResize(EventArgs e) | |
{ | |
base.OnResize(e); | |
GL.Viewport(0, 0, this.Width, this.Height); | |
} | |
protected override void OnLoad(EventArgs e) | |
{ | |
//Setup the viewport | |
GL.Viewport(0, 0, this.Width, this.Height); | |
//Load the texture | |
try | |
{ | |
texture1 = new Texture("resources/textures/container.jpg"); | |
texture2 = new Texture("resources/textures/awesomeface.png"); | |
} | |
catch (FileNotFoundException fnf) | |
{ | |
Console.Write(fnf.ToString()); | |
} | |
//Create a vertex shader | |
Shader vertShader = Shader.CreateShaderFromFile(ShaderType.VertexShader, "coordSystemsVertex"); | |
//Create a fragment shader | |
Shader fragShader = Shader.CreateShaderFromFile(ShaderType.FragmentShader, "coordSystemsFragment"); | |
//Create a shader program | |
shaderProgram = new ShaderProgram(); | |
shaderProgram.Attach(vertShader, true); | |
shaderProgram.Attach(fragShader, true); | |
shaderProgram.Link("shaderProgram"); | |
vao = new VertexArray(); | |
vao.Bind(); | |
//Create a VBO(Vertex Buffer Object) | |
vbo = new VertexBuffer(); | |
vbo.Bind(); | |
vbo.SetData(vertices, BufferUsageHint.StaticDraw); | |
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0); | |
GL.EnableVertexAttribArray(0); | |
GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); | |
GL.EnableVertexAttribArray(1); | |
vbo.Unbind(); | |
vao.Unbind(); | |
shaderProgram.Use(); | |
shaderProgram.SetUniform("texture1", 0); | |
shaderProgram.SetUniform("texture2", 1); | |
view = Matrix4.CreateTranslation(0.0F, 0.0F, -3.0F); | |
projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0F), (float)Width / Height, 0.1F, 100.0F); | |
shaderProgram.SetUniform("view", view); | |
shaderProgram.SetUniform("projection", projection); | |
} | |
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); | |
GL.ActiveTexture(TextureUnit.Texture0); | |
texture1.Bind(); | |
GL.ActiveTexture(TextureUnit.Texture1); | |
texture2.Bind(); | |
Vector3[] cubePositions = | |
{ | |
new Vector3( 0.0f, 0.0f, 0.0f), | |
new Vector3( 2.0f, 5.0f, -15.0f), | |
new Vector3(-1.5f, -2.2f, -2.5f), | |
new Vector3(-3.8f, -2.0f, -12.3f), | |
new Vector3( 2.4f, -0.4f, -3.5f), | |
new Vector3(-1.7f, 3.0f, -7.5f), | |
new Vector3( 1.3f, -2.0f, -2.5f), | |
new Vector3( 1.5f, 2.0f, -2.5f), | |
new Vector3( 1.5f, 0.2f, -1.5f), | |
new Vector3(-1.3f, 1.0f, -1.5f) | |
}; | |
vao.Bind(); | |
for(int i = 0; i < 10; i++) | |
{ | |
Matrix4 model = Matrix4.CreateFromAxisAngle(new Vector3(1.0f, 0.3f, 0.5f), MathHelper.DegreesToRadians(20.0F / 100 * Environment.TickCount)) * Matrix4.CreateTranslation(cubePositions[i]); | |
shaderProgram.SetUniform("model", model); | |
GL.DrawArrays(PrimitiveType.Triangles, 0, 36); | |
} | |
vao.Unbind(); | |
SwapBuffers(); | |
} | |
protected override void OnClosed(EventArgs e) | |
{ | |
base.OnClosed(e); | |
//Cleanup | |
vao.Delete(); | |
vbo.Delete(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment