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.Drawing; |
||
| 31 | |||
| 32 | using OpenTK; |
||
| 33 | using OpenTK.Input; |
||
| 34 | using OpenTK.Graphics; |
||
| 35 | using OpenTK.Graphics.OpenGL; |
||
| 36 | |||
| 37 | namespace Examples.Tutorial |
||
| 38 | { |
||
| 39 | [Example("Basic Geometry Shader", ExampleCategory.OpenGL, "2.x", Documentation = "Simple usage of EXT_geometry_shader4")] |
||
| 40 | public class SimpleGeometryShader : GameWindow |
||
| 41 | { |
||
| 42 | public SimpleGeometryShader() |
||
| 43 | : base(800, 600) |
||
| 44 | { |
||
| 45 | } |
||
| 46 | |||
| 47 | int shaderProgram = 0; |
||
| 48 | |||
| 49 | protected override void OnLoad(EventArgs e) |
||
| 50 | { |
||
| 51 | if (!GL.GetString(StringName.Extensions).Contains("EXT_geometry_shader4")) |
||
| 52 | { |
||
| 53 | System.Windows.Forms.MessageBox.Show( |
||
| 54 | "Your video card does not support EXT_geometry_shader4. Please update your drivers.", |
||
| 55 | "EXT_geometry_shader4 not supported", |
||
| 56 | System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); |
||
| 57 | Exit(); |
||
| 58 | throw new NotSupportedException(); |
||
| 59 | } |
||
| 60 | |||
| 61 | // create a shader object. |
||
| 62 | shaderProgram = GL.CreateProgram(); |
||
| 63 | // create shader objects for all three types. |
||
| 64 | int vert = GL.CreateShader(ShaderType.VertexShader); |
||
| 65 | int frag = GL.CreateShader(ShaderType.FragmentShader); |
||
| 66 | int geom = GL.CreateShader(ShaderType.GeometryShaderExt); |
||
| 67 | |||
| 68 | // GLSL for fragment shader. |
||
| 69 | String fragSource = @" |
||
| 70 | void main( void ) |
||
| 71 | { |
||
| 72 | gl_FragColor = vec4(0, 1, 0, 0); |
||
| 73 | } |
||
| 74 | "; |
||
| 75 | |||
| 76 | // GLSL for vertex shader. |
||
| 77 | String vertSource = @" |
||
| 78 | void main( void ) |
||
| 79 | { |
||
| 80 | gl_Position = ftransform(); |
||
| 81 | } |
||
| 82 | "; |
||
| 83 | |||
| 84 | // GLSL for geometry shader. |
||
| 85 | // Note this is a version 1.20 shader |
||
| 86 | // Also note GL_EXT_geometry_shader4 must be enabled explicitly, correct |
||
| 87 | // OpenGL implementations should only have the new tokens, like |
||
| 88 | // EmitVertex and EndPrimitive, when this extension is enabled. |
||
| 89 | String geomSource = @" |
||
| 90 | #version 120 |
||
| 91 | #extension GL_EXT_geometry_shader4 : enable |
||
| 92 | |||
| 93 | void main(void) |
||
| 94 | { |
||
| 95 | // variable to use in for loops |
||
| 96 | int i; |
||
| 97 | |||
| 98 | // Emit the original vertices without changing, making |
||
| 99 | // this part exactly the same as if no geometry shader |
||
| 100 | // was used. |
||
| 101 | for(i=0; i< gl_VerticesIn; i++) |
||
| 102 | { |
||
| 103 | gl_Position = gl_PositionIn[i]; |
||
| 104 | EmitVertex(); |
||
| 105 | } |
||
| 106 | // End the one primitive with the original vertices |
||
| 107 | EndPrimitive(); |
||
| 108 | |||
| 109 | // Now we generate some more! This translates 0.2 over |
||
| 110 | // the positive x axis. |
||
| 111 | for(i=0; i< gl_VerticesIn; i++) |
||
| 112 | { |
||
| 113 | gl_Position = gl_PositionIn[i]; |
||
| 114 | gl_Position.x += 0.2; |
||
| 115 | EmitVertex(); |
||
| 116 | } |
||
| 117 | EndPrimitive(); |
||
| 118 | } |
||
| 119 | "; |
||
| 120 | |||
| 121 | // compile shaders. |
||
| 122 | compileShader(frag, fragSource); |
||
| 123 | compileShader(vert, vertSource); |
||
| 124 | compileShader(geom, geomSource); |
||
| 125 | |||
| 126 | // attach shaders and link the program. |
||
| 127 | GL.AttachShader(shaderProgram, frag); |
||
| 128 | GL.AttachShader(shaderProgram, vert); |
||
| 129 | GL.AttachShader(shaderProgram, geom); |
||
| 130 | |||
| 131 | // Set the input type of the primitives we are going to feed the geometry shader, this should be the same as |
||
| 132 | // the primitive type given to GL.Begin. If the types do not match a GL error will occur (todo: verify GL_INVALID_ENUM, on glBegin) |
||
| 133 | GL.Ext.ProgramParameter(shaderProgram, ExtGeometryShader4.GeometryInputTypeExt, (int)BeginMode.Lines); |
||
| 134 | // Set the output type of the geometry shader. Becasue we input Lines we will output LineStrip(s). |
||
| 135 | GL.Ext.ProgramParameter(shaderProgram, ExtGeometryShader4.GeometryOutputTypeExt, (int)BeginMode.LineStrip); |
||
| 136 | |||
| 137 | // We must tell the shader program how much vertices the geometry shader will output (at most). |
||
| 138 | // One simple way is to query the maximum and use that. |
||
| 139 | // NOTE: Make sure that the number of vertices * sum(components of active varyings) does not |
||
| 140 | // exceed MaxGeometryTotalOutputComponents. |
||
| 141 | GL.Ext.ProgramParameter(shaderProgram, ExtGeometryShader4.GeometryVerticesOutExt, 50); |
||
| 142 | |||
| 143 | // NOTE: calls to ProgramParameter do not take effect until you call LinkProgram. |
||
| 144 | GL.LinkProgram(shaderProgram); |
||
| 145 | |||
| 146 | // output link info log. |
||
| 147 | string info; |
||
| 148 | GL.GetProgramInfoLog(shaderProgram, out info); |
||
| 149 | Debug.WriteLine(info); |
||
| 150 | |||
| 151 | // Set clearcolor and bind the shader program. |
||
| 152 | GL.ClearColor(0.1f, 0.1f, 0.1f, 0.1f); |
||
| 153 | GL.UseProgram(shaderProgram); |
||
| 154 | // Set color to red. If the shader fails the fixed pipeline will be used and |
||
| 155 | // the lines will be red, if all is ok the fragment shader is used and they will be green. |
||
| 156 | GL.Color3(1.0f, 0, 0); |
||
| 157 | |||
| 158 | // Clean up resources. Note the program object is not deleted. |
||
| 159 | if (frag != 0) |
||
| 160 | GL.DeleteShader(frag); |
||
| 161 | if (vert != 0) |
||
| 162 | GL.DeleteShader(vert); |
||
| 163 | if (geom != 0) |
||
| 164 | GL.DeleteShader(geom); |
||
| 165 | } |
||
| 166 | |||
| 167 | /// <summary> |
||
| 168 | /// Helper method to avoid code duplication. |
||
| 169 | /// Compiles a shader and prints results using Debug.WriteLine. |
||
| 170 | /// </summary> |
||
| 171 | /// <param name="shader">A shader object, gotten from GL.CreateShader.</param> |
||
| 172 | /// <param name="source">The GLSL source to compile.</param> |
||
| 173 | void compileShader(int shader, string source) |
||
| 174 | { |
||
| 175 | GL.ShaderSource(shader, source); |
||
| 176 | GL.CompileShader(shader); |
||
| 177 | |||
| 178 | string info; |
||
| 179 | GL.GetShaderInfoLog(shader, out info); |
||
| 180 | Debug.WriteLine(info); |
||
| 181 | |||
| 182 | int compileResult; |
||
| 183 | GL.GetShader(shader, ShaderParameter.CompileStatus, out compileResult); |
||
| 184 | if (compileResult != 1) |
||
| 185 | { |
||
| 186 | Debug.WriteLine("Compile Error!"); |
||
| 187 | Debug.WriteLine(source); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | protected override void OnUnload(EventArgs e) |
||
| 192 | { |
||
| 193 | if (shaderProgram != 0) |
||
| 194 | GL.DeleteProgram(shaderProgram); |
||
| 195 | base.OnUnload(e); |
||
| 196 | } |
||
| 197 | |||
| 198 | /// <summary> |
||
| 199 | /// Sets the viewport and projection matrix for orthographic projection. |
||
| 200 | /// </summary> |
||
| 201 | /// <param name="e">resize event args</param> |
||
| 202 | protected override void OnResize(EventArgs e) |
||
| 203 | { |
||
| 204 | GL.Viewport(ClientRectangle); |
||
| 205 | |||
| 206 | // Set projection matrix |
||
| 207 | GL.MatrixMode(MatrixMode.Projection); |
||
| 208 | OpenTK.Matrix4 ortho = OpenTK.Matrix4.CreateOrthographicOffCenter(-1, 1, -1, 1, 1, -1); |
||
| 209 | GL.LoadMatrix(ref ortho); |
||
| 210 | |||
| 211 | // Set selector state back to matrix mode |
||
| 212 | GL.MatrixMode(MatrixMode.Modelview); |
||
| 213 | |||
| 214 | base.OnResize(e); |
||
| 215 | } |
||
| 216 | |||
| 217 | protected override void OnUpdateFrame(FrameEventArgs e) |
||
| 218 | { |
||
| 219 | base.OnUpdateFrame(e); |
||
| 220 | |||
| 221 | if (Keyboard[Key.Space]) |
||
| 222 | { |
||
| 223 | ErrorCode err = GL.GetError(); |
||
| 224 | //Console.WriteLine(err + " " + Glu.ErrorString((GluErrorCode)err)); |
||
| 225 | Console.WriteLine("GL error code: {0}", err); |
||
| 226 | } |
||
| 227 | |||
| 228 | if (Keyboard[Key.Escape]) |
||
| 229 | this.Exit(); |
||
| 230 | } |
||
| 231 | |||
| 232 | protected override void OnRenderFrame(FrameEventArgs e) |
||
| 233 | { |
||
| 234 | GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
||
| 235 | |||
| 236 | // draw two vertical lines |
||
| 237 | GL.Begin(BeginMode.Lines); |
||
| 238 | { |
||
| 239 | // line one |
||
| 240 | GL.Vertex2(-0.5f, -0.5f); |
||
| 241 | GL.Vertex2(-0.5f, 0.5f); |
||
| 242 | // line two |
||
| 243 | GL.Vertex2(0.5f, 0.5f); |
||
| 244 | GL.Vertex2(0.5f, -0.5f); |
||
| 245 | } |
||
| 246 | GL.End(); |
||
| 247 | |||
| 248 | this.SwapBuffers(); |
||
| 249 | } |
||
| 250 | |||
| 251 | #region public static void Main() |
||
| 252 | |||
| 253 | /// <summary> |
||
| 254 | /// Entry point of this example. |
||
| 255 | /// </summary> |
||
| 256 | [STAThread] |
||
| 257 | public static void Main() |
||
| 258 | { |
||
| 259 | using (SimpleGeometryShader example = new SimpleGeometryShader()) |
||
| 260 | { |
||
| 261 | Utilities.SetWindowTitle(example); |
||
| 262 | example.Run(30.0, 0.0); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | #endregion |
||
| 267 | } |
||
| 268 | } |