Advertisement
Guest User

Untitled

a guest
May 5th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenTK;
  5. using OpenTK.Graphics.OpenGL;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8.  
  9.  
  10. namespace Engine
  11. {
  12.     class MainWindow : GameWindow
  13.     {        
  14.         string vertexShaderSource = @"
  15.            #version 410
  16.  
  17.            layout(location = 0) in vec3 attrib_Position;
  18.            layout(location = 3) in vec4 attrib_Color;
  19.  
  20.            uniform mat4 projection_matrix;
  21.            uniform mat4 modelview_matrix;
  22.  
  23.            out vec4 color;
  24.  
  25.            void main(void) {  
  26.                vec4 transformedPos = projection_matrix * modelview_matrix * vec4(attrib_Position, 1.0);
  27.                gl_Position = transformedPos;
  28.                color = attrib_Color;
  29.            }";
  30.  
  31.         string fragmentShaderSource = @"
  32.            #version 410
  33.  
  34.            in vec4 color;
  35.  
  36.            out vec4 out_Color0;
  37.  
  38.            void main(void) {              
  39.                out_Color0 = color;
  40.            }";
  41.                
  42.         private Matrix4 viewMatrix;
  43.         private Matrix4 modelViewMatrix;
  44.         private Matrix4 projectionMatrix;
  45.  
  46.         private int frameCounter;
  47.  
  48.         private int programName;
  49.         private int projectionLocation;
  50.         private int modelviewLocation;
  51.  
  52.         public MainWindow()
  53.             : base(1024, 768,
  54.             new OpenTK.Graphics.GraphicsMode(), "Blatt01", GameWindowFlags.Default,
  55.                 DisplayDevice.Default, 4, 1,
  56.             OpenTK.Graphics.GraphicsContextFlags.Default | OpenTK.Graphics.GraphicsContextFlags.Debug) {
  57.         }
  58.  
  59.         protected override void OnLoad(System.EventArgs e) {
  60.             InitShader();
  61.  
  62.             VSync = VSyncMode.On;
  63.  
  64.             GL.Enable(EnableCap.DepthTest);
  65.             GL.ClearColor(System.Drawing.Color.MidnightBlue);
  66.  
  67.             viewMatrix = Matrix4.CreateTranslation(0.0f, 0.0f, 4.0f);
  68.             viewMatrix.Invert();
  69.         }
  70.  
  71.         private void InitShader() {
  72.             if (vertexShaderSource == null || fragmentShaderSource == null)
  73.                 throw new NullReferenceException("Make sure vertex and fragment shader sources are set!");
  74.  
  75.             int vertexShaderName = GL.CreateShader(ShaderType.VertexShader);
  76.             int fragmentShaderName = GL.CreateShader(ShaderType.FragmentShader);
  77.  
  78.             GL.ShaderSource(vertexShaderName, vertexShaderSource);
  79.             GL.ShaderSource(fragmentShaderName, fragmentShaderSource);
  80.  
  81.             GL.CompileShader(vertexShaderName);
  82.             GL.CompileShader(fragmentShaderName);
  83.  
  84.             Debug.WriteLine(GL.GetShaderInfoLog(vertexShaderName));
  85.             Debug.WriteLine(GL.GetShaderInfoLog(fragmentShaderName));
  86.  
  87.             // Programmobjekt erzeugen
  88.             programName = GL.CreateProgram();
  89.  
  90.             GL.AttachShader(programName, vertexShaderName);
  91.             GL.AttachShader(programName, fragmentShaderName);
  92.  
  93.             GL.LinkProgram(programName);
  94.  
  95.             projectionLocation = GL.GetUniformLocation(programName, "projection_matrix");
  96.             modelviewLocation = GL.GetUniformLocation(programName, "modelview_matrix");
  97.         }
  98.  
  99.         protected override void OnResize(EventArgs e) {
  100.             projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(50.0f), 1.0f * Width / Height, 1.0f, 1000.0f);
  101.  
  102.             if (projectionLocation == -1)
  103.                 return;
  104.  
  105.             // Projektionsmatrix an den Shader 黚ergeben
  106.             GL.UseProgram(programName);
  107.             GL.UniformMatrix4(projectionLocation, false, ref projectionMatrix);
  108.         }
  109.  
  110.         protected override void OnUpdateFrame(FrameEventArgs e) {
  111.             if (Keyboard[OpenTK.Input.Key.Escape])
  112.                 Exit();
  113.             if (Keyboard[OpenTK.Input.Key.F1])
  114.                 switch (this.WindowState) {
  115.                     case OpenTK.WindowState.Fullscreen:
  116.                         this.WindowState = WindowState.Normal;
  117.                         break;
  118.                     case OpenTK.WindowState.Normal:
  119.                         this.WindowState = WindowState.Fullscreen;
  120.                         break;
  121.  
  122.                 }
  123.  
  124.             // Neue Modelmatrix berechnen. Dummy, ersetzen Sie die folgende Zeile durch die tats鋍hliche Berechnung
  125.             Matrix4 modelMatrix = Matrix4.Identity;
  126.             modelViewMatrix = modelMatrix * viewMatrix;
  127.            
  128.             // Modelmatrix an den Shader 黚ergeben
  129.             if (modelviewLocation == -1)
  130.                 return;
  131.  
  132.             GL.UseProgram(programName);
  133.             GL.UniformMatrix4(modelviewLocation, false, ref modelViewMatrix);
  134.         }
  135.  
  136.         protected override void OnRenderFrame(FrameEventArgs e) {
  137.             GL.Viewport(0, 0, Width, Height);
  138.  
  139.             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  140.  
  141.             GL.Begin(PrimitiveType.Triangles);
  142.  
  143.             GL.Vertex3 (new Vector3 (-1.0f, 0.0f, 0.0f));
  144.             GL.Vertex3 (new Vector3 (0.0f, 1.0f, 0.0f));
  145.             GL.Vertex3 (new Vector3 (1.0f, 0.0f, 0.0f));
  146.  
  147.             GL.End();
  148.  
  149.             SwapBuffers();
  150.  
  151.             this.Title = "Demonstration - " + this.RenderFrequency.ToString("00.00") + "fps.";
  152.  
  153.             frameCounter++;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement