Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region License |
| 2 | // |
||
| 3 | // The Open Toolkit Library License |
||
| 4 | // |
||
| 5 | // Copyright (c) 2006 - 2009 the Open Toolkit library. |
||
| 6 | // |
||
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
||
| 8 | // of this software and associated documentation files (the "Software"), to deal |
||
| 9 | // in the Software without restriction, including without limitation the rights to |
||
| 10 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
||
| 11 | // the Software, and to permit persons to whom the Software is furnished to do |
||
| 12 | // so, subject to the following conditions: |
||
| 13 | // |
||
| 14 | // The above copyright notice and this permission notice shall be included in all |
||
| 15 | // copies or substantial portions of the Software. |
||
| 16 | // |
||
| 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
| 18 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
||
| 19 | // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
| 20 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
||
| 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
||
| 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||
| 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
||
| 24 | // OTHER DEALINGS IN THE SOFTWARE. |
||
| 25 | // |
||
| 26 | #endregion |
||
| 27 | |||
| 28 | using System; |
||
| 29 | using System.Diagnostics; |
||
| 30 | using System.IO; |
||
| 31 | |||
| 32 | using OpenTK; |
||
| 33 | using OpenTK.Graphics; |
||
| 34 | using OpenTK.Graphics.OpenGL; |
||
| 35 | |||
| 36 | namespace Examples.Tutorial |
||
| 37 | { |
||
| 38 | [Example("OpenGL 3.0", ExampleCategory.OpenGL, "3.x", Documentation="HelloGL3")] |
||
| 39 | public class HelloGL3 : GameWindow |
||
| 40 | { |
||
| 41 | string vertexShaderSource = @" |
||
| 42 | #version 130 |
||
| 43 | |||
| 44 | precision highp float; |
||
| 45 | |||
| 46 | uniform mat4 projection_matrix; |
||
| 47 | uniform mat4 modelview_matrix; |
||
| 48 | |||
| 49 | in vec3 in_position; |
||
| 50 | in vec3 in_normal; |
||
| 51 | |||
| 52 | out vec3 normal; |
||
| 53 | |||
| 54 | void main(void) |
||
| 55 | { |
||
| 56 | //works only for orthogonal modelview |
||
| 57 | normal = (modelview_matrix * vec4(in_normal, 0)).xyz; |
||
| 58 | |||
| 59 | gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1); |
||
| 60 | }"; |
||
| 61 | |||
| 62 | string fragmentShaderSource = @" |
||
| 63 | #version 130 |
||
| 64 | |||
| 65 | precision highp float; |
||
| 66 | |||
| 67 | const vec3 ambient = vec3(0.1, 0.1, 0.1); |
||
| 68 | const vec3 lightVecNormalized = normalize(vec3(0.5, 0.5, 2.0)); |
||
| 69 | const vec3 lightColor = vec3(0.9, 0.9, 0.7); |
||
| 70 | |||
| 71 | in vec3 normal; |
||
| 72 | |||
| 73 | out vec4 out_frag_color; |
||
| 74 | |||
| 75 | void main(void) |
||
| 76 | { |
||
| 77 | float diffuse = clamp(dot(lightVecNormalized, normalize(normal)), 0.0, 1.0); |
||
| 78 | out_frag_color = vec4(ambient + diffuse * lightColor, 1.0); |
||
| 79 | }"; |
||
| 80 | |||
| 81 | int vertexShaderHandle, |
||
| 82 | fragmentShaderHandle, |
||
| 83 | shaderProgramHandle, |
||
| 84 | modelviewMatrixLocation, |
||
| 85 | projectionMatrixLocation, |
||
| 86 | vaoHandle, |
||
| 87 | positionVboHandle, |
||
| 88 | normalVboHandle, |
||
| 89 | eboHandle; |
||
| 90 | |||
| 91 | Vector3[] positionVboData = new Vector3[]{ |
||
| 92 | new Vector3(-1.0f, -1.0f, 1.0f), |
||
| 93 | new Vector3( 1.0f, -1.0f, 1.0f), |
||
| 94 | new Vector3( 1.0f, 1.0f, 1.0f), |
||
| 95 | new Vector3(-1.0f, 1.0f, 1.0f), |
||
| 96 | new Vector3(-1.0f, -1.0f, -1.0f), |
||
| 97 | new Vector3( 1.0f, -1.0f, -1.0f), |
||
| 98 | new Vector3( 1.0f, 1.0f, -1.0f), |
||
| 99 | new Vector3(-1.0f, 1.0f, -1.0f) }; |
||
| 100 | |||
| 101 | int[] indicesVboData = new int[]{ |
||
| 102 | // front face |
||
| 103 | 0, 1, 2, 2, 3, 0, |
||
| 104 | // top face |
||
| 105 | 3, 2, 6, 6, 7, 3, |
||
| 106 | // back face |
||
| 107 | 7, 6, 5, 5, 4, 7, |
||
| 108 | // left face |
||
| 109 | 4, 0, 3, 3, 7, 4, |
||
| 110 | // bottom face |
||
| 111 | 0, 1, 5, 5, 4, 0, |
||
| 112 | // right face |
||
| 113 | 1, 5, 6, 6, 2, 1, }; |
||
| 114 | |||
| 115 | Matrix4 projectionMatrix, modelviewMatrix; |
||
| 116 | |||
| 117 | public HelloGL3() |
||
| 118 | : base(640, 480, |
||
| 119 | new GraphicsMode(), "OpenGL 3 Example", 0, |
||
| 120 | DisplayDevice.Default, 3, 0, |
||
| 121 | GraphicsContextFlags.ForwardCompatible | GraphicsContextFlags.Debug) |
||
| 122 | { } |
||
| 123 | |||
| 124 | protected override void OnLoad (System.EventArgs e) |
||
| 125 | { |
||
| 126 | VSync = VSyncMode.On; |
||
| 127 | |||
| 128 | CreateShaders(); |
||
| 129 | CreateVBOs(); |
||
| 130 | CreateVAOs(); |
||
| 131 | |||
| 132 | // Other state |
||
| 133 | GL.Enable(EnableCap.DepthTest); |
||
| 134 | GL.ClearColor(System.Drawing.Color.MidnightBlue); |
||
| 135 | } |
||
| 136 | |||
| 137 | void CreateShaders() |
||
| 138 | { |
||
| 139 | vertexShaderHandle = GL.CreateShader(ShaderType.VertexShader); |
||
| 140 | fragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader); |
||
| 141 | |||
| 142 | GL.ShaderSource(vertexShaderHandle, vertexShaderSource); |
||
| 143 | GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource); |
||
| 144 | |||
| 145 | GL.CompileShader(vertexShaderHandle); |
||
| 146 | GL.CompileShader(fragmentShaderHandle); |
||
| 147 | |||
| 148 | Debug.WriteLine(GL.GetShaderInfoLog(vertexShaderHandle)); |
||
| 149 | Debug.WriteLine(GL.GetShaderInfoLog(fragmentShaderHandle)); |
||
| 150 | |||
| 151 | // Create program |
||
| 152 | shaderProgramHandle = GL.CreateProgram(); |
||
| 153 | |||
| 154 | GL.AttachShader(shaderProgramHandle, vertexShaderHandle); |
||
| 155 | GL.AttachShader(shaderProgramHandle, fragmentShaderHandle); |
||
| 156 | |||
| 157 | GL.LinkProgram(shaderProgramHandle); |
||
| 158 | |||
| 159 | Debug.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle)); |
||
| 160 | |||
| 161 | GL.UseProgram(shaderProgramHandle); |
||
| 162 | |||
| 163 | // Set uniforms |
||
| 164 | projectionMatrixLocation = GL.GetUniformLocation(shaderProgramHandle, "projection_matrix"); |
||
| 165 | modelviewMatrixLocation = GL.GetUniformLocation(shaderProgramHandle, "modelview_matrix"); |
||
| 166 | |||
| 167 | float aspectRatio = ClientSize.Width / (float)(ClientSize.Height); |
||
| 168 | Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1, 100, out projectionMatrix); |
||
| 169 | modelviewMatrix = Matrix4.LookAt(new Vector3(0, 3, 5), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); |
||
| 170 | |||
| 171 | GL.UniformMatrix4(projectionMatrixLocation, false, ref projectionMatrix); |
||
| 172 | GL.UniformMatrix4(modelviewMatrixLocation, false, ref modelviewMatrix); |
||
| 173 | } |
||
| 174 | |||
| 175 | void CreateVBOs() |
||
| 176 | { |
||
| 177 | GL.GenBuffers(1, out positionVboHandle); |
||
| 178 | GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); |
||
| 179 | GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, |
||
| 180 | new IntPtr(positionVboData.Length * Vector3.SizeInBytes), |
||
| 181 | positionVboData, BufferUsageHint.StaticDraw); |
||
| 182 | |||
| 183 | GL.GenBuffers(1, out normalVboHandle); |
||
| 184 | GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle); |
||
| 185 | GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, |
||
| 186 | new IntPtr(positionVboData.Length * Vector3.SizeInBytes), |
||
| 187 | positionVboData, BufferUsageHint.StaticDraw); |
||
| 188 | |||
| 189 | GL.GenBuffers(1, out eboHandle); |
||
| 190 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); |
||
| 191 | GL.BufferData(BufferTarget.ElementArrayBuffer, |
||
| 192 | new IntPtr(sizeof(uint) * indicesVboData.Length), |
||
| 193 | indicesVboData, BufferUsageHint.StaticDraw); |
||
| 194 | |||
| 195 | GL.BindBuffer(BufferTarget.ArrayBuffer, 0); |
||
| 196 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); |
||
| 197 | } |
||
| 198 | |||
| 199 | void CreateVAOs() |
||
| 200 | { |
||
| 201 | // GL3 allows us to store the vertex layout in a "vertex array object" (VAO). |
||
| 202 | // This means we do not have to re-issue VertexAttribPointer calls |
||
| 203 | // every time we try to use a different vertex layout - these calls are |
||
| 204 | // stored in the VAO so we simply need to bind the correct VAO. |
||
| 205 | GL.GenVertexArrays(1, out vaoHandle); |
||
| 206 | GL.BindVertexArray(vaoHandle); |
||
| 207 | |||
| 208 | GL.EnableVertexAttribArray(0); |
||
| 209 | GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); |
||
| 210 | GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); |
||
| 211 | GL.BindAttribLocation(shaderProgramHandle, 0, "in_position"); |
||
| 212 | |||
| 213 | GL.EnableVertexAttribArray(1); |
||
| 214 | GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle); |
||
| 215 | GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); |
||
| 216 | GL.BindAttribLocation(shaderProgramHandle, 1, "in_normal"); |
||
| 217 | |||
| 218 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); |
||
| 219 | |||
| 220 | GL.BindVertexArray(0); |
||
| 221 | } |
||
| 222 | |||
| 223 | protected override void OnUpdateFrame(FrameEventArgs e) |
||
| 224 | { |
||
| 225 | Matrix4 rotation = Matrix4.CreateRotationY((float)e.Time); |
||
| 226 | Matrix4.Mult(ref rotation, ref modelviewMatrix, out modelviewMatrix); |
||
| 227 | GL.UniformMatrix4(modelviewMatrixLocation, false, ref modelviewMatrix); |
||
| 228 | |||
| 229 | if (Keyboard[OpenTK.Input.Key.Escape]) |
||
| 230 | Exit(); |
||
| 231 | } |
||
| 232 | |||
| 233 | protected override void OnRenderFrame(FrameEventArgs e) |
||
| 234 | { |
||
| 235 | GL.Viewport(0, 0, Width, Height); |
||
| 236 | |||
| 237 | GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
||
| 238 | |||
| 239 | GL.BindVertexArray(vaoHandle); |
||
| 240 | GL.DrawElements(BeginMode.Triangles, indicesVboData.Length, |
||
| 241 | DrawElementsType.UnsignedInt, IntPtr.Zero); |
||
| 242 | |||
| 243 | SwapBuffers(); |
||
| 244 | } |
||
| 245 | |||
| 246 | [STAThread] |
||
| 247 | public static void Main() |
||
| 248 | { |
||
| 249 | using (HelloGL3 example = new HelloGL3()) |
||
| 250 | { |
||
| 251 | Utilities.SetWindowTitle(example); |
||
| 252 | example.Run(30); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |