Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | using System; |
| 2 | using System.Drawing; |
||
| 3 | using System.Drawing.Imaging; |
||
| 4 | |||
| 5 | using OpenTK; |
||
| 6 | using OpenTK.Graphics; |
||
| 7 | using OpenTK.Graphics.OpenGL; |
||
| 8 | using OpenTK.Input; |
||
| 9 | |||
| 10 | namespace Examples.Tutorial |
||
| 11 | { |
||
| 12 | |||
| 13 | [Example("Texture Matrix Wormhole", ExampleCategory.OpenGL, "1.x", Documentation = "TextureMatrix")] |
||
| 14 | |||
| 15 | class TextureMatrix : GameWindow |
||
| 16 | { |
||
| 17 | |||
| 18 | public TextureMatrix() |
||
| 19 | : base(800, 600, new GraphicsMode(32, 16, 0, 4)) |
||
| 20 | { |
||
| 21 | VSync = VSyncMode.On; |
||
| 22 | } |
||
| 23 | |||
| 24 | int Texture; |
||
| 25 | int list; |
||
| 26 | |||
| 27 | protected override void OnLoad(EventArgs e) |
||
| 28 | { |
||
| 29 | GL.ClearColor(0f, 0f, 0f, 0f); |
||
| 30 | GL.Enable(EnableCap.DepthTest); |
||
| 31 | GL.Enable(EnableCap.CullFace); |
||
| 32 | |||
| 33 | Texture = LoadTexture("Data/Textures/logo-dark.jpg"); |
||
| 34 | |||
| 35 | GL.Enable(EnableCap.Texture2D); |
||
| 36 | |||
| 37 | list = GL.GenLists(1); |
||
| 38 | |||
| 39 | GL.NewList(list, ListMode.Compile); |
||
| 40 | { |
||
| 41 | const int slices = 32; |
||
| 42 | const float distance = 0.25f; |
||
| 43 | |||
| 44 | GL.Begin(BeginMode.Quads); |
||
| 45 | |||
| 46 | for (float scale = 0.26f; scale < 5f; scale += distance) |
||
| 47 | for (int i = 0; i < slices; i++) |
||
| 48 | { |
||
| 49 | Vector3 MiddleCenter = new Vector3((float)(Math.Sin((double)i / slices * 2 * Math.PI) * scale), |
||
| 50 | (float)(Math.Cos((double)i / slices * 2 * Math.PI) * scale), |
||
| 51 | (float)(1f / scale)); |
||
| 52 | Vector3 MiddleRight = new Vector3((float)(Math.Sin((double)(i + 1) / slices * 2 * Math.PI) * scale), |
||
| 53 | (float)(Math.Cos((double)(i + 1) / slices * 2 * Math.PI) * scale), |
||
| 54 | (float)(1f / scale)); |
||
| 55 | Vector3 BottomRight = new Vector3((float)(Math.Sin((double)(i + 1) / slices * 2 * Math.PI) * (scale - distance)), |
||
| 56 | (float)(Math.Cos((double)(i + 1) / slices * 2 * Math.PI) * (scale - distance)), |
||
| 57 | (float)(1f / (scale - distance))); |
||
| 58 | Vector3 BottomCenter = new Vector3((float)(Math.Sin((double)i / slices * 2 * Math.PI) * (scale - distance)), |
||
| 59 | (float)(Math.Cos((double)i / slices * 2 * Math.PI) * (scale - distance)), |
||
| 60 | (float)(1f / (scale - distance))); |
||
| 61 | |||
| 62 | GL.TexCoord2(1f, 0f); |
||
| 63 | GL.Vertex3(MiddleCenter); |
||
| 64 | GL.TexCoord2(0f, 0f); |
||
| 65 | GL.Vertex3(MiddleRight); |
||
| 66 | GL.TexCoord2(0f, 1f); |
||
| 67 | GL.Vertex3(BottomRight); |
||
| 68 | GL.TexCoord2(1f, 1f); |
||
| 69 | GL.Vertex3(BottomCenter); |
||
| 70 | } |
||
| 71 | |||
| 72 | GL.End(); |
||
| 73 | } |
||
| 74 | GL.EndList(); |
||
| 75 | } |
||
| 76 | |||
| 77 | protected override void OnResize(EventArgs e) |
||
| 78 | { |
||
| 79 | GL.Viewport(this.ClientRectangle); |
||
| 80 | |||
| 81 | Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Width / (float)Height, 1.0f, 50.0f); |
||
| 82 | GL.MatrixMode(MatrixMode.Projection); |
||
| 83 | GL.LoadMatrix(ref projection); |
||
| 84 | } |
||
| 85 | |||
| 86 | protected override void OnUpdateFrame(FrameEventArgs e) |
||
| 87 | { |
||
| 88 | if (Keyboard[Key.Escape]) |
||
| 89 | Exit(); |
||
| 90 | } |
||
| 91 | |||
| 92 | protected override void OnRenderFrame(FrameEventArgs e) |
||
| 93 | { |
||
| 94 | GL.Clear(ClearBufferMask.ColorBufferBit | |
||
| 95 | ClearBufferMask.DepthBufferBit); |
||
| 96 | |||
| 97 | GL.MatrixMode(MatrixMode.Texture); |
||
| 98 | GL.Translate(e.Time/2, -e.Time, 0f); |
||
| 99 | |||
| 100 | Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY); |
||
| 101 | GL.MatrixMode(MatrixMode.Modelview); |
||
| 102 | GL.LoadMatrix(ref modelview); |
||
| 103 | |||
| 104 | GL.Translate(0f, 0f, 1.5f); |
||
| 105 | |||
| 106 | GL.Rotate(Mouse.X, Vector3.UnitY); |
||
| 107 | GL.Rotate(Mouse.Y, Vector3.UnitX); |
||
| 108 | |||
| 109 | GL.CallList(list); |
||
| 110 | |||
| 111 | SwapBuffers(); |
||
| 112 | } |
||
| 113 | |||
| 114 | public static int LoadTexture(string filename) |
||
| 115 | { |
||
| 116 | TextureTarget Target = TextureTarget.Texture2D; |
||
| 117 | |||
| 118 | int texture; |
||
| 119 | GL.GenTextures(1, out texture); |
||
| 120 | GL.BindTexture(Target, texture); |
||
| 121 | |||
| 122 | float version = Single.Parse(GL.GetString(StringName.Version).Substring(0, 3), System.Globalization.CultureInfo.InvariantCulture); |
||
| 123 | if (version >= 1.4) |
||
| 124 | { |
||
| 125 | GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, (int)All.True); |
||
| 126 | GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); |
||
| 127 | } |
||
| 128 | else |
||
| 129 | { |
||
| 130 | GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); |
||
| 131 | } |
||
| 132 | GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); |
||
| 133 | |||
| 134 | GL.TexParameter(Target, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); |
||
| 135 | GL.TexParameter(Target, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); |
||
| 136 | |||
| 137 | Bitmap bitmap = new Bitmap(filename); |
||
| 138 | BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); |
||
| 139 | GL.TexImage2D(Target, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); |
||
| 140 | GL.Finish(); |
||
| 141 | bitmap.UnlockBits(data); |
||
| 142 | |||
| 143 | if (GL.GetError() != ErrorCode.NoError) |
||
| 144 | throw new Exception("Error loading texture " + filename); |
||
| 145 | |||
| 146 | return texture; |
||
| 147 | } |
||
| 148 | |||
| 149 | /// <summary> |
||
| 150 | /// The main entry point for the application. |
||
| 151 | /// </summary> |
||
| 152 | [STAThread] |
||
| 153 | static void Main() |
||
| 154 | { |
||
| 155 | using (TextureMatrix example = new TextureMatrix()) |
||
| 156 | { |
||
| 157 | // Get the title and category of this example using reflection. |
||
| 158 | ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); |
||
| 159 | example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); |
||
| 160 | example.Run(30.0, 0.0); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |