NetGL - .Net Wrapper für OpenGL

    • Beta
    • Open Source

    Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Andy.

      NetGL - .Net Wrapper für OpenGL



      NetGL ist ein benutzerfreundlicher Wrapper, der es ermöglicht, OpenGL-Funktionen in .NET-Anwendungen zu verwenden. Dieses Projekt begann vor rund 9 Jahren und hat sich über die Jahre entwickelt. Aufgrund von beruflichen Verpflichtungen und anderen Umständen konnte ich es nicht so regelmäßig aktualisieren, wie ich es gerne hätte.

      Nun, da ich wieder mehr Zeit zur Verfügung habe und durch meine beruflichen Erfahrungen besser mit der Thematik vertraut bin, möchte ich das Projekt endlich zu einem erfolgreichen Abschluss bringen.

      NetGL bietet eine einfache Schnittstelle für .NET-Entwickler, um die leistungsstarken Grafikfunktionen von OpenGL zu nutzen. Es ermöglicht die nahtlose Integration von 3D-Grafiken in .NET-Anwendungen, und ich hoffe, dass es für viele Entwickler eine wertvolle Ressource sein wird.

      Ich freue mich über jedes Feedback und stehe gerne für Fragen und Anregungen zur Verfügung.

      Beispiele:
      01 - ModernGL Dreieck

      C#-Quellcode

      1. using NetGL;
      2. using System;
      3. using System.Collections.Generic;
      4. using System.ComponentModel;
      5. using System.Data;
      6. using System.Drawing;
      7. using System.Linq;
      8. using System.Text;
      9. using System.Threading;
      10. using System.Threading.Tasks;
      11. using System.Windows.Forms;
      12. namespace WindowsFormsApp1
      13. {
      14. /// <summary>
      15. /// Simple modern opengl application made with NetGL
      16. /// </summary>
      17. public partial class Form1 : Form
      18. {
      19. private NetGL.OpenGL gl;
      20. private float rotate;
      21. public Form1()
      22. {
      23. InitializeComponent();
      24. CheckForIllegalCrossThreadCalls = false;
      25. DoubleBuffered = false;
      26. }
      27. private void loop()
      28. {
      29. //Creating the Vertex shader
      30. string vertexShaderCode = @"
      31. #version 330 core
      32. layout(location = 0) in vec3 inPosition;
      33. layout(location = 1) in vec3 inColor;
      34. out vec3 color;
      35. void main()
      36. {
      37. gl_Position = vec4(inPosition, 1.0);
      38. color = inColor;
      39. }
      40. ";
      41. //Creating the fragment shader
      42. string fragmentShaderCode = @"
      43. #version 330 core
      44. in vec3 color;
      45. out vec4 fragColor;
      46. void main()
      47. {
      48. fragColor = vec4(color, 1.0);
      49. }
      50. ";
      51. //Setup netgl
      52. gl = new NetGL.OpenGL();
      53. gl.modernGL = true;
      54. gl.Initial(this.panel1.Handle);
      55. gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      56. //Create the Vertex Shader
      57. int vertexShader = gl.CreateShader(OpenGL.VertexShader);
      58. gl.SetShaderSource(vertexShader, 1, vertexShaderCode);
      59. gl.CompileShader(vertexShader);
      60. //Creat the fragment Shader
      61. int fragmentShader = gl.CreateShader(OpenGL.FragmentShader);
      62. gl.SetShaderSource(fragmentShader, 1, fragmentShaderCode);
      63. gl.CompileShader(fragmentShader);
      64. //Create a programm out of the vertex and fragment shader
      65. int program = gl.CreateProgram();
      66. gl.AttachShader(program, vertexShader);
      67. gl.AttachShader(program, fragmentShader);
      68. gl.LinkProgram(program);
      69. gl.UseProgram(program);
      70. //Generte the vbo
      71. float[] verticies =
      72. {
      73. 0f, 1f, -1f,
      74. -1f, -1f, -1f,
      75. 1f, -1f, -1f
      76. };
      77. int vertexBuffer = gl.GenBuffer(1);
      78. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      79. gl.BufferData(OpenGL.ArrayBuffer, verticies.Length * sizeof(float), verticies, OpenGL.StaticDraw);
      80. //Generate the cbo
      81. float[] colors =
      82. {
      83. 1.0f, 0.0f, 0.0f,
      84. 0.0f, 1.0f, 0.0f,
      85. 0.0f, 0.0f, 1.0f
      86. };
      87. int colorBuffer = gl.GenBuffer(1);
      88. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      89. gl.BufferData(OpenGL.ArrayBuffer, colors.Length * sizeof(float), colors, OpenGL.StaticDraw);
      90. //Load the 2D ortho matrix
      91. gl.Ortho(-2f, 2f, -2f, 2f, -20f, 20f);
      92. while (true) {
      93. Thread.Sleep(60);
      94. gl.Clear(NetGL.OpenGL.ColorBufferBit | NetGL.OpenGL.DepthBufferBit);
      95. //Send the vbo to the shader
      96. gl.EnableVertexAttribArray(0);
      97. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      98. gl.VertexAttribPointer(0, 3, OpenGL.Float, false, 0, 0);
      99. //Send the cbo to the shader
      100. gl.EnableVertexAttribArray(1);
      101. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      102. gl.VertexAttribPointer(1, 3, OpenGL.Float, false, 0, 0);
      103. //Render the Triangle
      104. gl.DrawArrays(OpenGL.Triangles, 0, 3);
      105. gl.Flush();
      106. rotate += 1;
      107. gl.SwapLayerBuffers(NetGL.OpenGL.SwapMainPlane);
      108. }
      109. }
      110. private void Form1_Shown(object sender, EventArgs e)
      111. {
      112. Thread renderThread = new Thread(new ThreadStart(loop));
      113. renderThread.Start();
      114. }
      115. }
      116. }


      02 - ModernGL Dreieck mit Textur

      C#-Quellcode

      1. using NetGL;
      2. using System;
      3. using System.Collections.Generic;
      4. using System.ComponentModel;
      5. using System.Data;
      6. using System.Drawing;
      7. using System.Linq;
      8. using System.Text;
      9. using System.Threading;
      10. using System.Threading.Tasks;
      11. using System.Windows.Forms;
      12. namespace WindowsFormsApp1
      13. {
      14. /// <summary>
      15. /// Simple modern opengl application made with NetGL
      16. /// </summary>
      17. public partial class Form1 : Form
      18. {
      19. private NetGL.OpenGL gl;
      20. private float rotate;
      21. public Form1()
      22. {
      23. InitializeComponent();
      24. CheckForIllegalCrossThreadCalls = false;
      25. DoubleBuffered = false;
      26. }
      27. private void loop()
      28. {
      29. //Creating the Vertex shader
      30. string vertexShaderCode = @"
      31. #version 330 core
      32. layout(location = 0) in vec3 inPosition;
      33. layout(location = 1) in vec3 inColor;
      34. layout(location = 2) in vec2 inTexCoord;
      35. out vec3 color;
      36. out vec2 texCoord;
      37. void main()
      38. {
      39. gl_Position = vec4(inPosition, 1.0);
      40. color = inColor;
      41. texCoord = inTexCoord;
      42. }
      43. ";
      44. //Creating the fragment shader
      45. string fragmentShaderCode = @"
      46. #version 330 core
      47. in vec3 color;
      48. in vec2 texCoord;
      49. out vec4 fragColor;
      50. uniform sampler2D textureSampler;
      51. void main()
      52. {
      53. fragColor = texture(textureSampler, texCoord) * vec4(color, 1.0);
      54. }
      55. ";
      56. //Setup netgl
      57. gl = new NetGL.OpenGL();
      58. gl.modernGL = true;
      59. gl.Initial(this.panel1.Handle);
      60. gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      61. //Create the Vertex Shader
      62. int vertexShader = gl.CreateShader(OpenGL.VertexShader);
      63. gl.SetShaderSource(vertexShader, 1, vertexShaderCode);
      64. gl.CompileShader(vertexShader);
      65. //Creat the fragment Shader
      66. int fragmentShader = gl.CreateShader(OpenGL.FragmentShader);
      67. gl.SetShaderSource(fragmentShader, 1, fragmentShaderCode);
      68. gl.CompileShader(fragmentShader);
      69. //Create a programm out of the vertex and fragment shader
      70. int program = gl.CreateProgram();
      71. gl.AttachShader(program, vertexShader);
      72. gl.AttachShader(program, fragmentShader);
      73. gl.LinkProgram(program);
      74. gl.UseProgram(program);
      75. //Load the texture
      76. int texid = gl.GenTextures(1);
      77. Bitmap bitmap = Properties.Resources.Smiley;
      78. gl.BindTexture(NetGL.OpenGL.Texture2D, texid);
      79. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMinFilter, NetGL.OpenGL.Nearest);
      80. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMagFilter, NetGL.OpenGL.Linear);
      81. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapS, NetGL.OpenGL.Repeate);
      82. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapT, NetGL.OpenGL.Repeate);
      83. gl.TexImage2D(NetGL.OpenGL.Texture2D, 0, NetGL.OpenGL.RGBA, bitmap.Width, bitmap.Height, 0, NetGL.OpenGL.BGRAExt, NetGL.OpenGL.UnsignedByte, bitmap);
      84. gl.Uniform1I(gl.GetUniformLocation(program, "textureSampler"), 0);
      85. //Generte the vbo
      86. float[] verticies =
      87. {
      88. 0f, 1f, -1f,
      89. -1f, -1f, -1f,
      90. 1f, -1f, -1f
      91. };
      92. int vertexBuffer = gl.GenBuffer(1);
      93. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      94. gl.BufferData(OpenGL.ArrayBuffer, verticies.Length * sizeof(float), verticies, OpenGL.StaticDraw);
      95. //Generate the cbo
      96. float[] colors =
      97. {
      98. 1.0f, 1.0f, 1.0f,
      99. 1.0f, 1.0f, 1.0f,
      100. 1.0f, 1.0f, 1.0f
      101. };
      102. int colorBuffer = gl.GenBuffer(1);
      103. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      104. gl.BufferData(OpenGL.ArrayBuffer, colors.Length * sizeof(float), colors, OpenGL.StaticDraw);
      105. //Generate the tbo
      106. float[] texCoords =
      107. {
      108. 0.5f, 0.0f,
      109. 0.0f, 1.0f,
      110. 1.0f, 1.0f
      111. };
      112. int texCordBuffer = gl.GenBuffer(1);
      113. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      114. gl.BufferData(OpenGL.ArrayBuffer, texCoords.Length * sizeof(float), texCoords, OpenGL.StaticDraw);
      115. //Load the 2D ortho matrix
      116. gl.Ortho(-2f, 2f, -2f, 2f, -20f, 20f);
      117. while (true) {
      118. Thread.Sleep(60);
      119. gl.Clear(NetGL.OpenGL.ColorBufferBit | NetGL.OpenGL.DepthBufferBit);
      120. //Send the vbo to the shader
      121. gl.EnableVertexAttribArray(0);
      122. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      123. gl.VertexAttribPointer(0, 3, OpenGL.Float, false, 0, 0);
      124. //Send the cbo to the shader
      125. gl.EnableVertexAttribArray(1);
      126. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      127. gl.VertexAttribPointer(1, 3, OpenGL.Float, false, 0, 0);
      128. //Send the tex cords to the shader
      129. gl.EnableVertexAttribArray(2);
      130. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      131. gl.VertexAttribPointer(2, 2, OpenGL.Float, false, 0, 0);
      132. //Render the Triangle
      133. gl.DrawArrays(OpenGL.Triangles, 0, 3);
      134. gl.Flush();
      135. gl.SwapLayerBuffers(NetGL.OpenGL.SwapMainPlane);
      136. Console.WriteLine(gl.GetError());
      137. }
      138. }
      139. private void Form1_Shown(object sender, EventArgs e)
      140. {
      141. Thread renderThread = new Thread(new ThreadStart(loop));
      142. renderThread.Start();
      143. }
      144. }
      145. }


      03 - Nutzung mehrere Texturen

      C#-Quellcode

      1. using NetGL;
      2. using System;
      3. using System.Collections.Generic;
      4. using System.ComponentModel;
      5. using System.Data;
      6. using System.Drawing;
      7. using System.Linq;
      8. using System.Text;
      9. using System.Threading;
      10. using System.Threading.Tasks;
      11. using System.Windows.Forms;
      12. namespace WindowsFormsApp1
      13. {
      14. /// <summary>
      15. /// Simple modern opengl application made with NetGL
      16. /// In this example we create an shader with multiple textures and use them
      17. /// It might be look a bit wired.
      18. /// </summary>
      19. public partial class Form1 : Form
      20. {
      21. private NetGL.OpenGL gl;
      22. private float rotate;
      23. public Form1()
      24. {
      25. InitializeComponent();
      26. CheckForIllegalCrossThreadCalls = false;
      27. DoubleBuffered = false;
      28. }
      29. private void loop()
      30. {
      31. //Creating the Vertex shader
      32. string vertexShaderCode = @"
      33. #version 330 core
      34. layout(location = 0) in vec3 inPosition;
      35. layout(location = 1) in vec3 inColor;
      36. layout(location = 2) in vec2 inTexCoord;
      37. out vec3 color;
      38. out vec2 texCoord;
      39. void main()
      40. {
      41. gl_Position = vec4(inPosition, 1.0);
      42. color = inColor;
      43. texCoord = inTexCoord;
      44. }
      45. ";
      46. //Creating the fragment shader
      47. string fragmentShaderCode = @"
      48. #version 330 core
      49. in vec3 color;
      50. in vec2 texCoord;
      51. out vec4 fragColor;
      52. uniform sampler2D textureSampler;
      53. uniform sampler2D textureSampler2;
      54. void main()
      55. {
      56. fragColor = mix(texture(textureSampler, texCoord), texture(textureSampler2, texCoord), 0.2) * vec4(color, 1.0);
      57. }
      58. ";
      59. //Setup netgl
      60. gl = new NetGL.OpenGL();
      61. gl.modernGL = true;
      62. gl.Initial(this.panel1.Handle);
      63. gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      64. //Create the Vertex Shader
      65. int vertexShader = gl.CreateShader(OpenGL.VertexShader);
      66. gl.SetShaderSource(vertexShader, 1, vertexShaderCode);
      67. gl.CompileShader(vertexShader);
      68. //Creat the fragment Shader
      69. int fragmentShader = gl.CreateShader(OpenGL.FragmentShader);
      70. gl.SetShaderSource(fragmentShader, 1, fragmentShaderCode);
      71. gl.CompileShader(fragmentShader);
      72. //Create a programm out of the vertex and fragment shader
      73. int program = gl.CreateProgram();
      74. gl.AttachShader(program, vertexShader);
      75. gl.AttachShader(program, fragmentShader);
      76. gl.LinkProgram(program);
      77. gl.UseProgram(program);
      78. //Load the textures. Here we select first the texture slot
      79. //and load the texture within this slot.
      80. gl.ActiveTexture(OpenGL.Texture0);
      81. int texture1 = LoadTexture(gl, Properties.Resources.Smiley);
      82. gl.ActiveTexture(OpenGL.Texture1);
      83. int texture2 = LoadTexture(gl, Properties.Resources.Affe);
      84. //Passing the texture to the shader.
      85. gl.Uniform1I(gl.GetUniformLocation(program, "textureSampler"), 0);
      86. gl.Uniform1I(gl.GetUniformLocation(program, "textureSampler2"), 1);
      87. //Reset the active texture to 0
      88. gl.ActiveTexture(OpenGL.Texture0);
      89. //Generte the vbo
      90. float[] verticies =
      91. {
      92. 0f, 1f, -1f,
      93. -1f, -1f, -1f,
      94. 1f, -1f, -1f
      95. };
      96. int vertexBuffer = gl.GenBuffer(1);
      97. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      98. gl.BufferData(OpenGL.ArrayBuffer, verticies.Length * sizeof(float), verticies, OpenGL.StaticDraw);
      99. //Generate the cbo
      100. float[] colors =
      101. {
      102. 1.0f, 1.0f, 1.0f,
      103. 1.0f, 1.0f, 1.0f,
      104. 1.0f, 1.0f, 1.0f
      105. };
      106. int colorBuffer = gl.GenBuffer(1);
      107. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      108. gl.BufferData(OpenGL.ArrayBuffer, colors.Length * sizeof(float), colors, OpenGL.StaticDraw);
      109. //Generate the tbo
      110. float[] texCoords =
      111. {
      112. 0.5f, 0.0f,
      113. 0.0f, 1.0f,
      114. 1.0f, 1.0f
      115. };
      116. int texCordBuffer = gl.GenBuffer(1);
      117. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      118. gl.BufferData(OpenGL.ArrayBuffer, texCoords.Length * sizeof(float), texCoords, OpenGL.StaticDraw);
      119. //Load the 2D ortho matrix
      120. gl.Ortho(-2f, 2f, -2f, 2f, -20f, 20f);
      121. while (true) {
      122. Thread.Sleep(60);
      123. gl.Clear(NetGL.OpenGL.ColorBufferBit | NetGL.OpenGL.DepthBufferBit);
      124. //Bind the textures
      125. gl.ActiveTexture(OpenGL.Texture0);
      126. gl.BindTexture(OpenGL.Texture2D, texture1);
      127. gl.ActiveTexture(OpenGL.Texture1);
      128. gl.BindTexture(OpenGL.Texture2D, texture2);
      129. //Send the vbo to the shader
      130. gl.EnableVertexAttribArray(0);
      131. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      132. gl.VertexAttribPointer(0, 3, OpenGL.Float, false, 0, 0);
      133. //Send the cbo to the shader
      134. gl.EnableVertexAttribArray(1);
      135. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      136. gl.VertexAttribPointer(1, 3, OpenGL.Float, false, 0, 0);
      137. //Send the tex cords to the shader
      138. gl.EnableVertexAttribArray(2);
      139. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      140. gl.VertexAttribPointer(2, 2, OpenGL.Float, false, 0, 0);
      141. //Render the Triangle
      142. gl.DrawArrays(OpenGL.Triangles, 0, 3);
      143. gl.Flush();
      144. gl.SwapLayerBuffers(NetGL.OpenGL.SwapMainPlane);
      145. Console.WriteLine(gl.GetError());
      146. }
      147. }
      148. /// <summary>
      149. /// We use this function to load our textures
      150. /// this will help to have a clean code
      151. /// </summary>
      152. /// <param name="gl"></param>
      153. /// <param name="bitmap"></param>
      154. /// <returns></returns>
      155. private int LoadTexture(OpenGL gl, Bitmap bitmap)
      156. {
      157. int textureID = gl.GenTextures(1);
      158. gl.BindTexture(NetGL.OpenGL.Texture2D, textureID);
      159. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMinFilter, NetGL.OpenGL.Nearest);
      160. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMagFilter, NetGL.OpenGL.Linear);
      161. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapS, NetGL.OpenGL.Repeate);
      162. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapT, NetGL.OpenGL.Repeate);
      163. gl.TexImage2D(NetGL.OpenGL.Texture2D, 0, NetGL.OpenGL.RGBA, bitmap.Width, bitmap.Height, 0, NetGL.OpenGL.BGRAExt, NetGL.OpenGL.UnsignedByte, bitmap);
      164. return textureID;
      165. }
      166. private void Form1_Shown(object sender, EventArgs e)
      167. {
      168. Thread renderThread = new Thread(new ThreadStart(loop));
      169. renderThread.Start();
      170. }
      171. }
      172. }


      04 ModernGL - Tranformation mit den OpenGL Matrixen

      Hier werden die eigenen Matrixen von OpenGL genutzt um ein Objekt zu Transformieren. Im nächsten Example werden externe Matrixen und eine MVP genutzt.

      C#-Quellcode

      1. using NetGL;
      2. using System;
      3. using System.Collections.Generic;
      4. using System.ComponentModel;
      5. using System.Data;
      6. using System.Drawing;
      7. using System.Linq;
      8. using System.Text;
      9. using System.Threading;
      10. using System.Threading.Tasks;
      11. using System.Windows.Forms;
      12. namespace WindowsFormsApp1
      13. {
      14. /// <summary>
      15. /// Simple modern opengl application made with NetGL
      16. /// </summary>
      17. public partial class Form1 : Form
      18. {
      19. private NetGL.OpenGL gl;
      20. private float rotate;
      21. public Form1()
      22. {
      23. InitializeComponent();
      24. CheckForIllegalCrossThreadCalls = false;
      25. DoubleBuffered = false;
      26. }
      27. private void loop()
      28. {
      29. //Creating the Vertex shader
      30. string vertexShaderCode = @"
      31. #version 330 core
      32. layout(location = 0) in vec3 inPosition;
      33. layout(location = 1) in vec3 inColor;
      34. layout(location = 2) in vec2 inTexCoord;
      35. out vec3 color;
      36. out vec2 texCoord;
      37. uniform mat4 modelMatrix; // Modellmatrix
      38. uniform mat4 projMatrix; // Projektionsmatrix
      39. void main()
      40. {
      41. gl_Position = projMatrix * modelMatrix * vec4(inPosition, 1.0);
      42. color = inColor;
      43. texCoord = inTexCoord;
      44. }
      45. ";
      46. //Creating the fragment shader
      47. string fragmentShaderCode = @"
      48. #version 330 core
      49. in vec3 color;
      50. in vec2 texCoord;
      51. out vec4 fragColor;
      52. uniform sampler2D textureSampler;
      53. void main()
      54. {
      55. fragColor = texture(textureSampler, texCoord) * vec4(color, 1.0);
      56. }
      57. ";
      58. //Setup netgl
      59. gl = new NetGL.OpenGL();
      60. gl.modernGL = true;
      61. gl.Initial(this.panel1.Handle);
      62. gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      63. //Create the Vertex Shader
      64. int vertexShader = gl.CreateShader(OpenGL.VertexShader);
      65. gl.SetShaderSource(vertexShader, 1, vertexShaderCode);
      66. gl.CompileShader(vertexShader);
      67. //Creat the fragment Shader
      68. int fragmentShader = gl.CreateShader(OpenGL.FragmentShader);
      69. gl.SetShaderSource(fragmentShader, 1, fragmentShaderCode);
      70. gl.CompileShader(fragmentShader);
      71. //Create a programm out of the vertex and fragment shader
      72. int program = gl.CreateProgram();
      73. gl.AttachShader(program, vertexShader);
      74. gl.AttachShader(program, fragmentShader);
      75. gl.LinkProgram(program);
      76. gl.UseProgram(program);
      77. //Load the texture
      78. int texid = gl.GenTextures(1);
      79. Bitmap bitmap = Properties.Resources.Smiley;
      80. gl.BindTexture(NetGL.OpenGL.Texture2D, texid);
      81. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMinFilter, NetGL.OpenGL.Nearest);
      82. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMagFilter, NetGL.OpenGL.Linear);
      83. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapS, NetGL.OpenGL.Repeate);
      84. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapT, NetGL.OpenGL.Repeate);
      85. gl.TexImage2D(NetGL.OpenGL.Texture2D, 0, NetGL.OpenGL.RGBA, bitmap.Width, bitmap.Height, 0, NetGL.OpenGL.BGRAExt, NetGL.OpenGL.UnsignedByte, bitmap);
      86. gl.Uniform1I(gl.GetUniformLocation(program, "textureSampler"), 0);
      87. //Generte the vbo
      88. float[] verticies =
      89. {
      90. 0f, 1f, -1f,
      91. -1f, -1f, -1f,
      92. 1f, -1f, -1f
      93. };
      94. int vertexBuffer = gl.GenBuffer(1);
      95. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      96. gl.BufferData(OpenGL.ArrayBuffer, verticies.Length * sizeof(float), verticies, OpenGL.StaticDraw);
      97. //Generate the cbo
      98. float[] colors =
      99. {
      100. 1.0f, 1.0f, 1.0f,
      101. 1.0f, 1.0f, 1.0f,
      102. 1.0f, 1.0f, 1.0f
      103. };
      104. int colorBuffer = gl.GenBuffer(1);
      105. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      106. gl.BufferData(OpenGL.ArrayBuffer, colors.Length * sizeof(float), colors, OpenGL.StaticDraw);
      107. //Generate the tbo
      108. float[] texCoords =
      109. {
      110. 0.5f, 0.0f,
      111. 0.0f, 1.0f,
      112. 1.0f, 1.0f
      113. };
      114. int texCordBuffer = gl.GenBuffer(1);
      115. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      116. gl.BufferData(OpenGL.ArrayBuffer, texCoords.Length * sizeof(float), texCoords, OpenGL.StaticDraw);
      117. while (true) {
      118. Thread.Sleep(60);
      119. //Transform the Projection and Modelview Matrix
      120. gl.MatrixMode(OpenGL.Projection);
      121. gl.LoadIdentity();
      122. gl.Ortho(-2f, 2f, -2f, 2f, -20f, 20f);
      123. gl.MatrixMode(OpenGL.ModelView);
      124. gl.LoadIdentity();
      125. gl.Translate(-1f, 0f, 0f);
      126. //Send the matrix data to the shader. This are custom functions within NetGL
      127. //to increase the performance.
      128. gl.SetProjectionMatrix(gl.GetUniformLocation(program, "projMatrix"), 1, false);
      129. gl.SetModelviewMatrix(gl.GetUniformLocation(program, "modelMatrix"), 1, false);
      130. gl.Clear(NetGL.OpenGL.ColorBufferBit | NetGL.OpenGL.DepthBufferBit);
      131. //Send the vbo to the shader
      132. gl.EnableVertexAttribArray(0);
      133. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      134. gl.VertexAttribPointer(0, 3, OpenGL.Float, false, 0, 0);
      135. //Send the cbo to the shader
      136. gl.EnableVertexAttribArray(1);
      137. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      138. gl.VertexAttribPointer(1, 3, OpenGL.Float, false, 0, 0);
      139. //Send the tex cords to the shader
      140. gl.EnableVertexAttribArray(2);
      141. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      142. gl.VertexAttribPointer(2, 2, OpenGL.Float, false, 0, 0);
      143. //Render the Triangle
      144. gl.DrawArrays(OpenGL.Triangles, 0, 3);
      145. gl.Flush();
      146. gl.SwapLayerBuffers(NetGL.OpenGL.SwapMainPlane);
      147. Console.WriteLine(gl.GetError());
      148. }
      149. }
      150. private void Form1_Shown(object sender, EventArgs e)
      151. {
      152. Thread renderThread = new Thread(new ThreadStart(loop));
      153. renderThread.Start();
      154. }
      155. }
      156. }


      05 ModernGL - Transformation mit einer MVP

      C#-Quellcode

      1. using NetGL;
      2. using System;
      3. using System.Collections.Generic;
      4. using System.ComponentModel;
      5. using System.Data;
      6. using System.Drawing;
      7. using System.Linq;
      8. using System.Net.Security;
      9. using System.Text;
      10. using System.Threading;
      11. using System.Threading.Tasks;
      12. using System.Windows.Forms;
      13. using GenesisMath.Math;
      14. namespace WindowsFormsApp1
      15. {
      16. /// <summary>
      17. /// This example renders an triangle with modern OpenGL and an MVP Matrix
      18. /// (Modelview, View (Camera Matrix) and Projection Matrix. If you want dive
      19. /// deeper into matrices you can have a look here on this website.
      20. ///
      21. /// http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
      22. ///
      23. /// You will need some math libary for this example. I exported the imported math
      24. /// files out of my game engine and created an libary out of it. You can find it here
      25. ///
      26. /// https://github.com/Andy16823/GenesisMath
      27. ///
      28. /// or you use some more complex math libary like glmsharp which have more features but
      29. /// we wont need them realy for this example.
      30. ///
      31. /// https://github.com/Philip-Trettner/GlmSharp
      32. /// </summary>
      33. public partial class Form1 : Form
      34. {
      35. private NetGL.OpenGL gl;
      36. private float rotate;
      37. /// <summary>
      38. /// Initial the Windows Form
      39. /// </summary>
      40. public Form1()
      41. {
      42. InitializeComponent();
      43. CheckForIllegalCrossThreadCalls = false;
      44. DoubleBuffered = false;
      45. }
      46. /// <summary>
      47. /// Our rendering thread
      48. /// </summary>
      49. private void loop()
      50. {
      51. //Creating the Vertex shader
      52. string vertexShaderCode = @"
      53. #version 330 core
      54. layout(location = 0) in vec3 inPosition;
      55. layout(location = 1) in vec3 inColor;
      56. layout(location = 2) in vec2 inTexCoord;
      57. out vec3 color;
      58. out vec2 texCoord;
      59. uniform mat4 mvp;
      60. uniform mat4 modelMatrix; // Modellmatrix
      61. uniform mat4 projMatrix; // Projektionsmatrix
      62. void main()
      63. {
      64. gl_Position = mvp * vec4(inPosition, 1.0);
      65. color = inColor;
      66. texCoord = inTexCoord;
      67. }
      68. ";
      69. //Creating the fragment shader
      70. string fragmentShaderCode = @"
      71. #version 330 core
      72. in vec3 color;
      73. in vec2 texCoord;
      74. out vec4 fragColor;
      75. uniform sampler2D textureSampler;
      76. void main()
      77. {
      78. fragColor = texture(textureSampler, texCoord) * vec4(color, 1.0);
      79. }
      80. ";
      81. //Setup netgl
      82. gl = new NetGL.OpenGL();
      83. gl.modernGL = true;
      84. gl.Initial(this.panel1.Handle);
      85. gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      86. //Create the Vertex Shader
      87. int vertexShader = gl.CreateShader(OpenGL.VertexShader);
      88. gl.SetShaderSource(vertexShader, 1, vertexShaderCode);
      89. gl.CompileShader(vertexShader);
      90. //Creat the fragment Shader
      91. int fragmentShader = gl.CreateShader(OpenGL.FragmentShader);
      92. gl.SetShaderSource(fragmentShader, 1, fragmentShaderCode);
      93. gl.CompileShader(fragmentShader);
      94. //Create a programm out of the vertex and fragment shader
      95. int program = gl.CreateProgram();
      96. gl.AttachShader(program, vertexShader);
      97. gl.AttachShader(program, fragmentShader);
      98. gl.LinkProgram(program);
      99. gl.UseProgram(program);
      100. //Load the texture
      101. int texid = gl.GenTextures(1);
      102. Bitmap bitmap = Properties.Resources.Smiley;
      103. gl.BindTexture(NetGL.OpenGL.Texture2D, texid);
      104. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMinFilter, NetGL.OpenGL.Nearest);
      105. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureMagFilter, NetGL.OpenGL.Linear);
      106. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapS, NetGL.OpenGL.Repeate);
      107. gl.TexParameteri(NetGL.OpenGL.Texture2D, NetGL.OpenGL.TextureWrapT, NetGL.OpenGL.Repeate);
      108. gl.TexImage2D(NetGL.OpenGL.Texture2D, 0, NetGL.OpenGL.RGBA, bitmap.Width, bitmap.Height, 0, NetGL.OpenGL.BGRAExt, NetGL.OpenGL.UnsignedByte, bitmap);
      109. gl.Uniform1I(gl.GetUniformLocation(program, "textureSampler"), 0);
      110. //Generte the vbo
      111. float[] verticies =
      112. {
      113. 0f, 1f, -1f,
      114. -1f, -1f, -1f,
      115. 1f, -1f, -1f
      116. };
      117. int vertexBuffer = gl.GenBuffer(1);
      118. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      119. gl.BufferData(OpenGL.ArrayBuffer, verticies.Length * sizeof(float), verticies, OpenGL.StaticDraw);
      120. //Generate the cbo
      121. float[] colors =
      122. {
      123. 1.0f, 1.0f, 1.0f,
      124. 1.0f, 1.0f, 1.0f,
      125. 1.0f, 1.0f, 1.0f
      126. };
      127. int colorBuffer = gl.GenBuffer(1);
      128. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      129. gl.BufferData(OpenGL.ArrayBuffer, colors.Length * sizeof(float), colors, OpenGL.StaticDraw);
      130. //Generate the tbo
      131. float[] texCoords =
      132. {
      133. 0.5f, 0.0f,
      134. 0.0f, 1.0f,
      135. 1.0f, 1.0f
      136. };
      137. int texCordBuffer = gl.GenBuffer(1);
      138. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      139. gl.BufferData(OpenGL.ArrayBuffer, texCoords.Length * sizeof(float), texCoords, OpenGL.StaticDraw);
      140. while (true) {
      141. Thread.Sleep(60);
      142. //Within the first stepp we create the projection matrix and then the view matrix
      143. Matrix4x4 p_mat = Matrix4x4.Ortho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 100.0f);
      144. Matrix4x4 v_mat = Matrix4x4.LookAt(new Vec3(0f, 0f, 1f), new Vec3(0f, 0f, 0f), new Vec3(0f, 1f, 0f));
      145. //Now we create the modelview matrix. Its a product from the Translate, Rotate and Scale matrix
      146. Matrix4x4 mt_mat = Matrix4x4.Translate(new Vec3(0f, 0f, 0f));
      147. Matrix4x4 mr_mat = Matrix4x4.RotateZ(rotate);
      148. Matrix4x4 ms_mat = Matrix4x4.Scale(new Vec3(5f, 5f, 0f));
      149. Matrix4x4 m_mat = mt_mat * mr_mat * ms_mat;
      150. //Now we have all matrices we need to create the "MVP" matrix
      151. Matrix4x4 mvp = p_mat * v_mat * m_mat;
      152. //Send the matrix data to the shader.
      153. gl.UniformMatrix4fv(gl.GetUniformLocation(program, "mvp"), 1, false, mvp.ToArray());
      154. gl.Clear(NetGL.OpenGL.ColorBufferBit | NetGL.OpenGL.DepthBufferBit);
      155. //Send the vbo to the shader
      156. gl.EnableVertexAttribArray(0);
      157. gl.BindBuffer(OpenGL.ArrayBuffer, vertexBuffer);
      158. gl.VertexAttribPointer(0, 3, OpenGL.Float, false, 0, 0);
      159. //Send the cbo to the shader
      160. gl.EnableVertexAttribArray(1);
      161. gl.BindBuffer(OpenGL.ArrayBuffer, colorBuffer);
      162. gl.VertexAttribPointer(1, 3, OpenGL.Float, false, 0, 0);
      163. //Send the tex cords to the shader
      164. gl.EnableVertexAttribArray(2);
      165. gl.BindBuffer(OpenGL.ArrayBuffer, texCordBuffer);
      166. gl.VertexAttribPointer(2, 2, OpenGL.Float, false, 0, 0);
      167. //Render the Triangle
      168. gl.DrawArrays(OpenGL.Triangles, 0, 3);
      169. gl.Flush();
      170. gl.SwapLayerBuffers(NetGL.OpenGL.SwapMainPlane);
      171. Console.WriteLine(gl.GetError());
      172. rotate += 0.5f;
      173. }
      174. }
      175. private void Form1_Shown(object sender, EventArgs e)
      176. {
      177. Thread renderThread = new Thread(new ThreadStart(loop));
      178. renderThread.Start();
      179. }
      180. private void Form1_Load(object sender, EventArgs e)
      181. {
      182. }
      183. }
      184. }




      Screenshot(s):


      Verwendete Programmiersprache(n) und IDE(s):
      C++/CLI - Visual Studio 2022 Enterprise

      Systemanforderungen:
      .Net4.5
      OpenGL fähige Grafikkarte

      Systemveränderungen:
      Keine Systemveränderungen

      Download(s):
      GitHub

      Lizenz/Weitergabe:
      OpenSource mit der MIT Lizenz

      Dieser Beitrag wurde bereits 7 mal editiert, zuletzt von „Andy“ ()

      Ich habe eben das letzte Beispiel hinzugefügt. Außerdem ein paar Änderungen etc.
      • Die Meisten OpenGL 4 Funktionen sollten jetzt verfügbar sein
      • Das erstellen von Shadern ist nun möglich
      • Das Laden von externen Matrizen ist nun möglich
      • Das Verwenden von mehreren Texturen ist nun möglich
      • Optimierung alter Funktionen, die die Performance beeinträchtigt haben.
        Wie z.b. das Kopieren von Managed Array Daten zu Unmanged Arrays entfällt.
        Stattdessen werden pin_ptr genutzt.
      • Außerdem habe ich eine kleine Bibliothek für Matrizen aus meiner GameEngine
        geladen. Das interessante hierbei ist, dass ich ChatGPT da mit eingebunden habe :)
        github.com/Andy16823/GenesisMath