Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* Licensed under the MIT/X11 license. |
||
| 3 | * Copyright (c) 2006-2008 the OpenTK Team. |
||
| 4 | * This notice may not be removed from any source distribution. |
||
| 5 | * See license.txt for licensing details. |
||
| 6 | */ |
||
| 7 | #endregion |
||
| 8 | |||
| 9 | using System; |
||
| 10 | using System.Drawing; |
||
| 11 | using System.Collections.Generic; |
||
| 12 | using System.Diagnostics; |
||
| 13 | using System.Windows.Forms; |
||
| 14 | using System.IO; |
||
| 15 | using System.Text; |
||
| 16 | |||
| 17 | using OpenTK; |
||
| 18 | using OpenTK.Graphics; |
||
| 19 | using OpenTK.Graphics.OpenGL; |
||
| 20 | |||
| 21 | using Examples.Shapes; |
||
| 22 | using Examples.TextureLoaders; |
||
| 23 | |||
| 24 | namespace Examples.Tutorial |
||
| 25 | { |
||
| 26 | [Example("DDS Cube Map", ExampleCategory.OpenGL, "2.x", Documentation = "DDSCubeMap")] |
||
| 27 | public class T13_GLSL_Earth: GameWindow |
||
| 28 | { |
||
| 29 | public T13_GLSL_Earth( ) |
||
| 30 | : base( 800, 800 ) |
||
| 31 | { |
||
| 32 | } |
||
| 33 | |||
| 34 | #region internal Fields |
||
| 35 | |||
| 36 | // Shader |
||
| 37 | int VertexShaderObject, FragmentShaderObject, ProgramObject; |
||
| 38 | const string VertexShaderFilename = "Data/Shaders/CubeMap_VS.glsl"; |
||
| 39 | const string FragmentShaderFilename = "Data/Shaders/CubeMap_FS.glsl"; |
||
| 40 | |||
| 41 | // Textures |
||
| 42 | const TextureUnit TMU0_Unit = TextureUnit.Texture0; |
||
| 43 | const int TMU0_UnitInteger = 0; |
||
| 44 | const string TMU0_Filename = "Data/Textures/earth-cubemap.dds"; |
||
| 45 | uint TMU0_Handle; |
||
| 46 | TextureTarget TMU0_Target; |
||
| 47 | |||
| 48 | // DL |
||
| 49 | DrawableShape sphere; |
||
| 50 | |||
| 51 | // Camera |
||
| 52 | Vector3 EyePos = new Vector3( 0.0f, 0.0f, 6.0f ); |
||
| 53 | Vector3 Trackball = Vector3.Zero; |
||
| 54 | |||
| 55 | #endregion internal Fields |
||
| 56 | |||
| 57 | /// <summary>Setup OpenGL and load resources here.</summary> |
||
| 58 | /// <param name="e">Not used.</param> |
||
| 59 | protected override void OnLoad(EventArgs e) |
||
| 60 | { |
||
| 61 | this.VSync = VSyncMode.Off; |
||
| 62 | |||
| 63 | // Check for necessary capabilities: |
||
| 64 | string extensions = GL.GetString(StringName.Extensions); |
||
| 65 | if (!GL.GetString(StringName.Extensions).Contains("GL_ARB_shading_language")) |
||
| 66 | { |
||
| 67 | throw new NotSupportedException(String.Format("This example requires OpenGL 2.0. Found {0}. Aborting.", |
||
| 68 | GL.GetString(StringName.Version).Substring(0, 3))); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (!extensions.Contains("GL_ARB_texture_compression") || |
||
| 72 | !extensions.Contains("GL_EXT_texture_compression_s3tc")) |
||
| 73 | { |
||
| 74 | throw new NotSupportedException("This example requires support for texture compression. Aborting."); |
||
| 75 | } |
||
| 76 | |||
| 77 | #region GL State |
||
| 78 | |||
| 79 | GL.ClearColor( 0f, 0f, 0f, 0f ); |
||
| 80 | |||
| 81 | GL.Disable( EnableCap.Dither ); |
||
| 82 | |||
| 83 | GL.Enable( EnableCap.CullFace ); |
||
| 84 | GL.FrontFace( FrontFaceDirection.Ccw ); |
||
| 85 | GL.PolygonMode( MaterialFace.Front, PolygonMode.Fill ); |
||
| 86 | // GL.PolygonMode( MaterialFace.Back, PolygonMode.Line ); |
||
| 87 | |||
| 88 | #endregion GL State |
||
| 89 | |||
| 90 | #region Shaders |
||
| 91 | |||
| 92 | string LogInfo; |
||
| 93 | |||
| 94 | // Load&Compile Vertex Shader |
||
| 95 | |||
| 96 | using ( StreamReader sr = new StreamReader( VertexShaderFilename ) ) |
||
| 97 | { |
||
| 98 | VertexShaderObject = GL.CreateShader( ShaderType.VertexShader ); |
||
| 99 | GL.ShaderSource( VertexShaderObject, sr.ReadToEnd( ) ); |
||
| 100 | GL.CompileShader( VertexShaderObject ); |
||
| 101 | } |
||
| 102 | |||
| 103 | GL.GetShaderInfoLog( VertexShaderObject, out LogInfo ); |
||
| 104 | if ( LogInfo.Length > 0 && !LogInfo.Contains( "hardware" ) ) |
||
| 105 | Trace.WriteLine( "Vertex Shader failed!\nLog:\n" + LogInfo ); |
||
| 106 | else |
||
| 107 | Trace.WriteLine( "Vertex Shader compiled without complaint." ); |
||
| 108 | |||
| 109 | // Load&Compile Fragment Shader |
||
| 110 | |||
| 111 | using ( StreamReader sr = new StreamReader( FragmentShaderFilename ) ) |
||
| 112 | { |
||
| 113 | FragmentShaderObject = GL.CreateShader( ShaderType.FragmentShader ); |
||
| 114 | GL.ShaderSource( FragmentShaderObject, sr.ReadToEnd( ) ); |
||
| 115 | GL.CompileShader( FragmentShaderObject ); |
||
| 116 | } |
||
| 117 | GL.GetShaderInfoLog( FragmentShaderObject, out LogInfo ); |
||
| 118 | |||
| 119 | if ( LogInfo.Length > 0 && !LogInfo.Contains( "hardware" ) ) |
||
| 120 | Trace.WriteLine( "Fragment Shader failed!\nLog:\n" + LogInfo ); |
||
| 121 | else |
||
| 122 | Trace.WriteLine( "Fragment Shader compiled without complaint." ); |
||
| 123 | |||
| 124 | // Link the Shaders to a usable Program |
||
| 125 | ProgramObject = GL.CreateProgram( ); |
||
| 126 | GL.AttachShader( ProgramObject, VertexShaderObject ); |
||
| 127 | GL.AttachShader( ProgramObject, FragmentShaderObject ); |
||
| 128 | |||
| 129 | // link it all together |
||
| 130 | GL.LinkProgram( ProgramObject ); |
||
| 131 | |||
| 132 | // flag ShaderObjects for delete when not used anymore |
||
| 133 | GL.DeleteShader( VertexShaderObject ); |
||
| 134 | GL.DeleteShader( FragmentShaderObject ); |
||
| 135 | |||
| 136 | int[] temp = new int[1]; |
||
| 137 | GL.GetProgram( ProgramObject, ProgramParameter.LinkStatus, out temp[0] ); |
||
| 138 | Trace.WriteLine( "Linking Program (" + ProgramObject + ") " + ( ( temp[0] == 1 ) ? "succeeded." : "FAILED!" ) ); |
||
| 139 | if ( temp[0] != 1 ) |
||
| 140 | { |
||
| 141 | GL.GetProgramInfoLog( ProgramObject, out LogInfo ); |
||
| 142 | Trace.WriteLine( "Program Log:\n" + LogInfo ); |
||
| 143 | } |
||
| 144 | |||
| 145 | GL.GetProgram( ProgramObject, ProgramParameter.ActiveAttributes, out temp[0] ); |
||
| 146 | Trace.WriteLine( "Program registered " + temp[0] + " Attributes. (Should be 4: Pos, UV, Normal, Tangent)" ); |
||
| 147 | |||
| 148 | Trace.WriteLine( "Tangent attribute bind location: " + GL.GetAttribLocation( ProgramObject, "AttributeTangent" ) ); |
||
| 149 | |||
| 150 | Trace.WriteLine( "End of Shader build. GL Error: " + GL.GetError( ) ); |
||
| 151 | |||
| 152 | #endregion Shaders |
||
| 153 | |||
| 154 | #region Textures |
||
| 155 | |||
| 156 | TextureLoaderParameters.FlipImages = false; |
||
| 157 | TextureLoaderParameters.MagnificationFilter = TextureMagFilter.Linear; |
||
| 158 | TextureLoaderParameters.MinificationFilter = TextureMinFilter.Linear; |
||
| 159 | TextureLoaderParameters.WrapModeS = TextureWrapMode.ClampToEdge; |
||
| 160 | TextureLoaderParameters.WrapModeT = TextureWrapMode.ClampToEdge; |
||
| 161 | TextureLoaderParameters.EnvMode = TextureEnvMode.Modulate; |
||
| 162 | |||
| 163 | ImageDDS.LoadFromDisk( TMU0_Filename, out TMU0_Handle, out TMU0_Target ); |
||
| 164 | Trace.WriteLine( "Loaded " + TMU0_Filename + " with handle " + TMU0_Handle + " as " + TMU0_Target ); |
||
| 165 | |||
| 166 | #endregion Textures |
||
| 167 | |||
| 168 | Trace.WriteLine( "End of Texture Loading. GL Error: " + GL.GetError( ) ); |
||
| 169 | Trace.WriteLine( ""); |
||
| 170 | |||
| 171 | sphere = new SlicedSphere(1.5f, Vector3d.Zero, SlicedSphere.eSubdivisions.Four, new SlicedSphere.eDir[] { SlicedSphere.eDir.All }, true); |
||
| 172 | |||
| 173 | } |
||
| 174 | |||
| 175 | protected override void OnUnload(EventArgs e) |
||
| 176 | { |
||
| 177 | sphere.Dispose(); |
||
| 178 | |||
| 179 | GL.DeleteProgram( ProgramObject ); |
||
| 180 | GL.DeleteTextures( 1, ref TMU0_Handle ); |
||
| 181 | |||
| 182 | base.OnUnload( e ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /// <summary>Respond to resize events here.</summary> |
||
| 186 | /// <param name="e">Contains information on the new GameWindow size.</param> |
||
| 187 | /// <remarks>There is no need to call the base implementation.</remarks> |
||
| 188 | protected override void OnResize( EventArgs e ) |
||
| 189 | { |
||
| 190 | GL.Viewport( 0, 0, Width, Height ); |
||
| 191 | |||
| 192 | GL.MatrixMode( MatrixMode.Projection ); |
||
| 193 | Matrix4 p = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Width / (float)Height, 0.1f, 10.0f); |
||
| 194 | GL.LoadMatrix(ref p); |
||
| 195 | |||
| 196 | GL.MatrixMode( MatrixMode.Modelview ); |
||
| 197 | GL.LoadIdentity( ); |
||
| 198 | |||
| 199 | base.OnResize( e ); |
||
| 200 | } |
||
| 201 | |||
| 202 | /// <summary>Add your game logic here.</summary> |
||
| 203 | /// <param name="e">Contains timing information.</param> |
||
| 204 | /// <remarks>There is no need to call the base implementation.</remarks> |
||
| 205 | protected override void OnUpdateFrame( FrameEventArgs e ) |
||
| 206 | { |
||
| 207 | base.OnUpdateFrame( e ); |
||
| 208 | |||
| 209 | if ( Keyboard[OpenTK.Input.Key.Escape] ) |
||
| 210 | this.Exit( ); |
||
| 211 | if ( Keyboard[OpenTK.Input.Key.Space] ) |
||
| 212 | Trace.WriteLine( "GL: " + GL.GetError( ) ); |
||
| 213 | |||
| 214 | Trackball.X = Mouse.X; |
||
| 215 | Trackball.Y = Mouse.Y; |
||
| 216 | Trackball.Z = Mouse.Wheel * 0.5f; |
||
| 217 | |||
| 218 | } |
||
| 219 | |||
| 220 | /// <summary>Add your game rendering code here.</summary> |
||
| 221 | /// <param name="e">Contains timing information.</param> |
||
| 222 | /// <remarks>There is no need to call the base implementation.</remarks> |
||
| 223 | protected override void OnRenderFrame(FrameEventArgs e) |
||
| 224 | { |
||
| 225 | this.Title = "FPS: " + (1 / e.Time).ToString("0."); |
||
| 226 | |||
| 227 | GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
||
| 228 | |||
| 229 | GL.UseProgram(ProgramObject); |
||
| 230 | |||
| 231 | #region Textures |
||
| 232 | |||
| 233 | GL.ActiveTexture(TMU0_Unit); |
||
| 234 | GL.BindTexture(TMU0_Target, TMU0_Handle); |
||
| 235 | |||
| 236 | #endregion Textures |
||
| 237 | |||
| 238 | #region Uniforms |
||
| 239 | |||
| 240 | GL.Uniform1(GL.GetUniformLocation(ProgramObject, "Earth"), TMU0_UnitInteger); |
||
| 241 | |||
| 242 | #endregion Uniforms |
||
| 243 | |||
| 244 | GL.PushMatrix(); |
||
| 245 | Matrix4 temp = Matrix4.LookAt(EyePos, Vector3.Zero, Vector3.UnitY); |
||
| 246 | GL.MultMatrix(ref temp); |
||
| 247 | |||
| 248 | GL.Rotate(Trackball.X, Vector3.UnitY); |
||
| 249 | GL.Rotate(Trackball.Y, Vector3.UnitX); |
||
| 250 | |||
| 251 | #region Draw |
||
| 252 | |||
| 253 | GL.Color3(1f, 1f, 1f); |
||
| 254 | |||
| 255 | sphere.Draw(); |
||
| 256 | |||
| 257 | #endregion Draw |
||
| 258 | |||
| 259 | GL.PopMatrix(); |
||
| 260 | |||
| 261 | this.SwapBuffers(); |
||
| 262 | } |
||
| 263 | |||
| 264 | /// <summary>Entry point</summary> |
||
| 265 | [STAThread] |
||
| 266 | public static void Main( ) |
||
| 267 | { |
||
| 268 | using ( T13_GLSL_Earth example = new T13_GLSL_Earth( ) ) |
||
| 269 | { |
||
| 270 | Utilities.SetWindowTitle(example); |
||
| 271 | example.Run( 30.0, 0.0 ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |