Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using OpenTK;
- using OpenTK.Graphics.OpenGL;
- using System.Diagnostics;
- using System.Drawing;
- namespace Engine
- {
- class MainWindow : GameWindow
- {
- string vertexShaderSource = @"
- #version 410
- layout(location = 0) in vec3 attrib_Position;
- layout(location = 3) in vec4 attrib_Color;
- uniform mat4 projection_matrix;
- uniform mat4 modelview_matrix;
- out vec4 color;
- void main(void) {
- vec4 transformedPos = projection_matrix * modelview_matrix * vec4(attrib_Position, 1.0);
- gl_Position = transformedPos;
- color = attrib_Color;
- }";
- string fragmentShaderSource = @"
- #version 410
- in vec4 color;
- out vec4 out_Color0;
- void main(void) {
- out_Color0 = color;
- }";
- private Matrix4 viewMatrix;
- private Matrix4 modelViewMatrix;
- private Matrix4 projectionMatrix;
- private int frameCounter;
- private int programName;
- private int projectionLocation;
- private int modelviewLocation;
- public MainWindow()
- : base(1024, 768,
- new OpenTK.Graphics.GraphicsMode(), "Blatt01", GameWindowFlags.Default,
- DisplayDevice.Default, 4, 1,
- OpenTK.Graphics.GraphicsContextFlags.Default | OpenTK.Graphics.GraphicsContextFlags.Debug) {
- }
- protected override void OnLoad(System.EventArgs e) {
- InitShader();
- VSync = VSyncMode.On;
- GL.Enable(EnableCap.DepthTest);
- GL.ClearColor(System.Drawing.Color.MidnightBlue);
- viewMatrix = Matrix4.CreateTranslation(0.0f, 0.0f, 4.0f);
- viewMatrix.Invert();
- }
- private void InitShader() {
- if (vertexShaderSource == null || fragmentShaderSource == null)
- throw new NullReferenceException("Make sure vertex and fragment shader sources are set!");
- int vertexShaderName = GL.CreateShader(ShaderType.VertexShader);
- int fragmentShaderName = GL.CreateShader(ShaderType.FragmentShader);
- GL.ShaderSource(vertexShaderName, vertexShaderSource);
- GL.ShaderSource(fragmentShaderName, fragmentShaderSource);
- GL.CompileShader(vertexShaderName);
- GL.CompileShader(fragmentShaderName);
- Debug.WriteLine(GL.GetShaderInfoLog(vertexShaderName));
- Debug.WriteLine(GL.GetShaderInfoLog(fragmentShaderName));
- // Programmobjekt erzeugen
- programName = GL.CreateProgram();
- GL.AttachShader(programName, vertexShaderName);
- GL.AttachShader(programName, fragmentShaderName);
- GL.LinkProgram(programName);
- projectionLocation = GL.GetUniformLocation(programName, "projection_matrix");
- modelviewLocation = GL.GetUniformLocation(programName, "modelview_matrix");
- }
- protected override void OnResize(EventArgs e) {
- projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(50.0f), 1.0f * Width / Height, 1.0f, 1000.0f);
- if (projectionLocation == -1)
- return;
- // Projektionsmatrix an den Shader 黚ergeben
- GL.UseProgram(programName);
- GL.UniformMatrix4(projectionLocation, false, ref projectionMatrix);
- }
- protected override void OnUpdateFrame(FrameEventArgs e) {
- if (Keyboard[OpenTK.Input.Key.Escape])
- Exit();
- if (Keyboard[OpenTK.Input.Key.F1])
- switch (this.WindowState) {
- case OpenTK.WindowState.Fullscreen:
- this.WindowState = WindowState.Normal;
- break;
- case OpenTK.WindowState.Normal:
- this.WindowState = WindowState.Fullscreen;
- break;
- }
- // Neue Modelmatrix berechnen. Dummy, ersetzen Sie die folgende Zeile durch die tats鋍hliche Berechnung
- Matrix4 modelMatrix = Matrix4.Identity;
- modelViewMatrix = modelMatrix * viewMatrix;
- // Modelmatrix an den Shader 黚ergeben
- if (modelviewLocation == -1)
- return;
- GL.UseProgram(programName);
- GL.UniformMatrix4(modelviewLocation, false, ref modelViewMatrix);
- }
- protected override void OnRenderFrame(FrameEventArgs e) {
- GL.Viewport(0, 0, Width, Height);
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- GL.Begin(PrimitiveType.Triangles);
- GL.Vertex3 (new Vector3 (-1.0f, 0.0f, 0.0f));
- GL.Vertex3 (new Vector3 (0.0f, 1.0f, 0.0f));
- GL.Vertex3 (new Vector3 (1.0f, 0.0f, 0.0f));
- GL.End();
- SwapBuffers();
- this.Title = "Demonstration - " + this.RenderFrequency.ToString("00.00") + "fps.";
- frameCounter++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement