/SharpGL/Samples/WinForms/SharpGLTexturesSample/FormSharpGLTexturesSample.cs

# · C# · 116 lines · 75 code · 25 blank · 16 comment · 2 complexity · fce3a9e9b3ba30c16560cce3e1894c2c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using SharpGL;
  10. using SharpGL.SceneGraph.Assets;
  11. namespace SharpGLTexturesSample
  12. {
  13. public partial class FormSharpGLTexturesSample : Form
  14. {
  15. public FormSharpGLTexturesSample()
  16. {
  17. InitializeComponent();
  18. // Get the OpenGL object, for quick access.
  19. SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
  20. // A bit of extra initialisation here, we have to enable textures.
  21. gl.Enable(OpenGL.GL_TEXTURE_2D);
  22. // Create our texture object from a file. This creates the texture for OpenGL.
  23. texture.Create(gl, "Crate.bmp");
  24. }
  25. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  26. {
  27. System.Diagnostics.Process.Start("http://nehe.gamedev.net");
  28. }
  29. private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
  30. {
  31. // Get the OpenGL object, for quick access.
  32. SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
  33. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
  34. gl.LoadIdentity();
  35. gl.Translate(0.0f, 0.0f, -6.0f);
  36. gl.Rotate(rtri, 0.0f, 1.0f, 0.0f);
  37. // Bind the texture.
  38. texture.Bind(gl);
  39. gl.Begin(OpenGL.GL_QUADS);
  40. // Front Face
  41. gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  42. gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  43. gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
  44. gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
  45. // Back Face
  46. gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
  47. gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
  48. gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
  49. gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
  50. // Top Face
  51. gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
  52. gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  53. gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  54. gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
  55. // Bottom Face
  56. gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
  57. gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
  58. gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  59. gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  60. // Right face
  61. gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
  62. gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
  63. gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
  64. gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  65. // Left Face
  66. gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
  67. gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  68. gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
  69. gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
  70. gl.End();
  71. gl.Flush();
  72. rtri += 1.0f;// 0.2f; // Increase The Rotation Variable For The Triangle
  73. }
  74. float rtri = 0;
  75. // The texture identifier.
  76. Texture texture = new Texture();
  77. private void buttonLoad_Click(object sender, EventArgs e)
  78. {
  79. // Show a file open dialog.
  80. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  81. {
  82. // Destroy the existing texture.
  83. texture.Destroy(openGLControl1.OpenGL);
  84. // Create a new texture.
  85. texture.Create(openGLControl1.OpenGL, openFileDialog1.FileName);
  86. // Redraw.
  87. openGLControl1.Invalidate();
  88. }
  89. }
  90. }
  91. }