code_junkie

Problem applying texture to square in OpenGL

Nov 13th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. [DllImport("coredll")]
  4. extern static IntPtr GetDC(IntPtr hwnd);
  5.  
  6. EGLDisplay myDisplay;
  7. EGLSurface mySurface;
  8. EGLContext myContext;
  9.  
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. myDisplay = egl.GetDisplay(new EGLNativeDisplayType(this));
  14.  
  15. int major, minor;
  16. egl.Initialize(myDisplay, out major, out minor);
  17.  
  18. EGLConfig[] configs = new EGLConfig[10];
  19. int[] attribList = new int[]
  20. {
  21. egl.EGL_RED_SIZE, 5,
  22. egl.EGL_GREEN_SIZE, 6,
  23. egl.EGL_BLUE_SIZE, 5,
  24. egl.EGL_DEPTH_SIZE, 16 ,
  25. egl.EGL_SURFACE_TYPE, egl.EGL_WINDOW_BIT,
  26. egl.EGL_STENCIL_SIZE, egl.EGL_DONT_CARE,
  27. egl.EGL_NONE, egl.EGL_NONE
  28. };
  29.  
  30. int numConfig;
  31. if (!egl.ChooseConfig(myDisplay, attribList, configs, configs.Length, out numConfig) || numConfig < 1)
  32. throw new InvalidOperationException("Unable to choose config.");
  33.  
  34. EGLConfig config = configs[0];
  35. mySurface = egl.CreateWindowSurface(myDisplay, config, Handle, null);
  36. myContext = egl.CreateContext(myDisplay, config, EGLContext.None, null);
  37.  
  38. egl.MakeCurrent(myDisplay, mySurface, mySurface, myContext);
  39. gl.ClearColor(0, 0, 0, 0);
  40. InitGL();
  41. }
  42.  
  43. void InitGL()
  44. {
  45. gl.ShadeModel(gl.GL_SMOOTH);
  46. gl.ClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  47. gl.BlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
  48. gl.Hint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST);
  49. }
  50.  
  51. public unsafe void DrawGLScene()
  52. {
  53. gl.MatrixMode(gl.GL_PROJECTION);
  54. gl.LoadIdentity();
  55. gl.Orthof(0, ClientSize.Width, ClientSize.Height, 0, 0, 1);
  56. gl.Disable(gl.GL_DEPTH_TEST);
  57.  
  58. gl.MatrixMode(gl.GL_MODELVIEW);
  59. gl.LoadIdentity();
  60.  
  61. Texture myImage;
  62. Bitmap Image = new Bitmap(@"Storage CardTexture.jpg");
  63. using (MemoryStream ms = new MemoryStream())
  64. {
  65. Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  66. myImage = Texture.LoadStream(ms, false);
  67. }
  68.  
  69. float[] rectangle = new float[] {
  70. 0, 0,
  71. myImage.Width, 0,
  72. 0, myImage.Height,
  73. myImage.Width, myImage.Height
  74. };
  75.  
  76. float[] texturePosition = new float[] {
  77. 0, 0,
  78. myImage.Width, 0,
  79. 0, myImage.Height,
  80. myImage.Width, myImage.Height
  81. };
  82.  
  83. //Bind texture
  84. gl.BindTexture(gl.GL_TEXTURE_2D, myImage.Name);
  85. gl.TexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
  86. gl.TexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
  87. gl.EnableClientState(gl.GL_TEXTURE_COORD_ARRAY);
  88. gl.EnableClientState(gl.GL_VERTEX_ARRAY);
  89.  
  90. //draw square and texture it.
  91. fixed (float* rectanglePointer = &rectangle[0], positionPointer = &texturePosition[0])
  92. {
  93. gl.TexCoordPointer(2, gl.GL_FLOAT, 0, (IntPtr)positionPointer);
  94. gl.VertexPointer(2, gl.GL_FLOAT, 0, (IntPtr)rectanglePointer);
  95. gl.DrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
  96. }
  97.  
  98. gl.DisableClientState(gl.GL_TEXTURE_COORD_ARRAY);
  99. gl.DisableClientState(gl.GL_VERTEX_ARRAY);
  100.  
  101. }
  102.  
  103. protected override void OnPaintBackground(PaintEventArgs e)
  104. {
  105.  
  106. }
  107.  
  108. protected override void OnPaint(PaintEventArgs e)
  109. {
  110. base.OnPaint(e);
  111. gl.Clear(gl.GL_COLOR_BUFFER_BIT);
  112.  
  113. DrawGLScene();
  114. egl.SwapBuffers(myDisplay, mySurface);
  115. gl.Clear(gl.GL_COLOR_BUFFER_BIT);
  116. }
  117.  
  118. protected override void OnClosing(CancelEventArgs e)
  119. {
  120. if (!egl.DestroySurface(myDisplay, mySurface))
  121. throw new Exception("Error while destroying surface.");
  122. if (!egl.DestroyContext(myDisplay, myContext))
  123. throw new Exception("Error while destroying context.");
  124. if (!egl.Terminate(myDisplay))
  125. throw new Exception("Error while terminating display.");
  126. base.OnClosing(e);
  127. }
  128. }
  129.  
  130. glEnable( GL_TEXTURE_2D );
Add Comment
Please, Sign In to add comment