Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5.  
  6. using OpenTK;
  7.  
  8. namespace OpenGLTutorial1
  9. {
  10. public class Program
  11. {
  12. static void Main ()
  13. {
  14. Game window = new Game (640, 480);
  15. window.Run ();
  16. }
  17. }
  18. }
  19.  
  20.  
  21.  
  22.  
  23. using System;
  24. using OpenTK;
  25. using OpenTK.Graphics;
  26. using OpenTK.Graphics.OpenGL;
  27. using System.Drawing;
  28. using OpenTK.Input;
  29.  
  30.  
  31. namespace OpenGLTutorial1
  32. {
  33. public class Game : GameWindow
  34. {
  35. int texture;
  36.  
  37. public Game(int width, int height): base (width, height)
  38. {
  39. GL.Enable (EnableCap.Texture2D);
  40. }
  41.  
  42. protected override void OnLoad(EventArgs e)
  43. {
  44. base.OnLoad (e);
  45. texture = ContentPipe.LoadTexture("tiles.png");
  46. }
  47.  
  48. protected override void OnUpdateFrame(FrameEventArgs e)
  49. {
  50. base.OnUpdateFrame (e);
  51. }
  52.  
  53. protected override void OnRenderFrame(FrameEventArgs e)
  54. {
  55. base.OnRenderFrame (e);
  56.  
  57. GL.Clear (ClearBufferMask.ColorBufferBit);
  58. GL.ClearColor (Color.CornflowerBlue);
  59. GL.BindTexture (TextureTarget.Texture2D, texture);
  60.  
  61. GL.Begin (PrimitiveType.Quads);
  62.  
  63. GL.Color3 (Color.Red);
  64. GL.TexCoord2 (0, 0);
  65. GL.Vertex2 (0, 0);
  66.  
  67. GL.Color3 (Color.Blue);
  68. GL.TexCoord2 (1, 0);
  69. GL.Vertex2 (1, 0);
  70.  
  71. GL.Color3 (Color.Green);
  72. GL.TexCoord2 (1, -1);
  73. GL.Vertex2 (1, -1);
  74.  
  75. GL.Color3 (Color.Orange);
  76. GL.TexCoord2 (0, -1);
  77. GL.Vertex2 (0, -1);
  78.  
  79. GL.End ();
  80.  
  81. this.SwapBuffers ();
  82. }
  83.  
  84. }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. using System;
  93. using System.Collections.Generic;
  94. using System.Text;
  95. using System.Threading.Tasks;
  96. using System.IO;
  97.  
  98. using OpenTK;
  99. using OpenTK.Graphics.OpenGL;
  100. using System.Drawing;
  101. using System.Drawing.Imaging;
  102.  
  103. namespace OpenGLTutorial1
  104. {
  105.  
  106. public class ContentPipe
  107. {
  108. public static int LoadTexture (string path)
  109. {
  110. if(!File.Exists ("Content/" + path))
  111. {
  112. throw new FileNotFoundException("File not found at Content/" + path + ".");
  113. }
  114.  
  115. int id = GL.GenTexture ();
  116. GL.BindTexture (TextureTarget.Texture2D, id);
  117.  
  118. Bitmap bmp = new Bitmap ("Content/" + path);
  119. BitmapData data = bmp.LockBits
  120. (
  121. new Rectangle (0, 0, bmp.Width, bmp.Height),
  122. System.Drawing.Imaging.ImageLockMode.ReadOnly,
  123. System.Drawing.Imaging.PixelFormat.Format32bppArgb
  124. );
  125. GL.TexImage2D
  126. (
  127. TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
  128. data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
  129. PixelType.UnsignedInt, data.Scan0
  130. );
  131.  
  132. bmp.UnlockBits (data);
  133. GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)
  134. TextureWrapMode.Clamp);
  135. GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)
  136. TextureWrapMode.Clamp);
  137. GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)
  138. TextureMinFilter.Linear);
  139. GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)
  140. TextureMagFilter.Linear);
  141. return id;
  142.  
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement