Stan0033

Untitled

Dec 4th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.98 KB | None | 0 0
  1. using SharpGL;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using System.Drawing.Imaging;
  7.  
  8. using Rectangle = System.Drawing.Rectangle;
  9.  
  10. using Image = System.Windows.Controls.Image;
  11.  
  12. namespace TEST_SHARPGL_TEXTURE
  13. {
  14.     /// <summary>
  15.     /// Interaction logic for MainWindow.xaml
  16.     /// </summary>
  17.     public partial class MainWindow : Window
  18.     {
  19.         private float rotationAngle = 0.0f;
  20.         private Bitmap DefaultImage;
  21.         private uint textureId;
  22.         private OpenGL gl;
  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.          
  27.         }
  28.         private void Window_Loaded(object sender, RoutedEventArgs e)
  29.         {
  30.             gl = Scene_Viewport.OpenGL; ;
  31.             DefaultImage = getImage();
  32.             if (DefaultImage == null) { Environment.Exit(0); }
  33.             //  BitmapSaver.SaveBitmap(DefaultImage, @"C:\P__\image.jpg");
  34.  
  35.             // display d = new display(DefaultImage); d.ShowDialog();
  36.             textureId = LoadTexture(Scene_Viewport.OpenGL, DefaultImage);
  37.            // MessageBox.Show(textureId.ToString());
  38.          
  39.         }
  40.  
  41.  
  42.         private void DrawScene(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
  43.         {
  44.            
  45.  
  46.             // Clear the color and depth buffer.
  47.             gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
  48.              // Load the identity matrix.
  49.             gl.LoadIdentity();
  50.          
  51.  
  52.             //   gl.Enable(OpenGL.GL_BLEND);
  53.             // gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
  54.             //   gl.Disable(OpenGL.GL_LIGHTING);
  55.  
  56.             // Move the cube slightly back so it’s visible.
  57.             gl.Translate(0.0f, 0.0f, -6.0f);
  58.  
  59.  
  60.             // Rotate the cube.
  61.             gl.Rotate(rotationAngle, 1.0f, 1.0f, 0.0f);
  62.  
  63.             // Bind the texture.
  64.          
  65.  
  66.             // Draw a textured cube.
  67.             DrawCube(gl);
  68.  
  69.             gl.End();
  70.  
  71.             // Flush OpenGL commands.
  72.              gl.Flush();
  73.  
  74.             // Update rotation angle.
  75.             rotationAngle += 1.0f;
  76.             if (rotationAngle >= 360.0f)
  77.                 rotationAngle -= 360.0f;
  78.         }
  79.         private void DrawCube(OpenGL gl)
  80.         {
  81.             gl.Begin(OpenGL.GL_QUADS);
  82.             gl.Enable(OpenGL.GL_TEXTURE_2D);
  83.            
  84.             gl.BindTexture(OpenGL.GL_TEXTURE_2D, textureId);
  85.             // Front face
  86.             gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);
  87.             gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);
  88.             gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);
  89.             gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);
  90.  
  91.             // Back face
  92.             gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);
  93.             gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);
  94.             gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);
  95.             gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);
  96.  
  97.  
  98.             // Left face
  99.             gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);
  100.             gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);
  101.             gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);
  102.             gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);
  103.  
  104.             // Right face
  105.             gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);
  106.             gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);
  107.             gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);
  108.             gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);
  109.  
  110.             // Top face
  111.             gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);
  112.             gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);
  113.             gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);
  114.             gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);
  115.  
  116.             // Bottom face
  117.             gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);
  118.             gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);
  119.             gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, -1.0f, 1.0f);
  120.             gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);
  121.             gl.End();
  122.         }
  123.         private uint LoadTexture(OpenGL gl, Bitmap bitmap)
  124.         {
  125.             if (bitmap == null)
  126.                 throw new ArgumentNullException(nameof(bitmap), "Bitmap cannot be null.");
  127.  
  128.             // Generate a texture ID.
  129.             uint[] textureIds = new uint[1];
  130.             gl.GenTextures(1, textureIds);
  131.             var error = gl.GetError();
  132.             if (error != OpenGL.GL_NO_ERROR)
  133.             {
  134.                 MessageBox.Show($"OpenGL Error: {error}");
  135.             }
  136.             uint id = textureIds[0];
  137.            
  138.  
  139.             // Set texture parameters.
  140.             gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT);
  141.             gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT);
  142.             gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
  143.             gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
  144.  
  145.             // Bind the texture to apply settings.
  146.             gl.BindTexture(OpenGL.GL_TEXTURE_2D, id);
  147.             // Lock the bitmap bits for OpenGL.
  148.             BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
  149.                                               ImageLockMode.ReadOnly,
  150.                                               System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  151.  
  152.             // Create the texture from the bitmap data.
  153.             gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, data.Width, data.Height, 0,
  154.                           OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, data.Scan0);
  155.  
  156.             // Unlock the bitmap bits.
  157.             bitmap.UnlockBits(data);
  158.              
  159.             return id;
  160.         }
  161.         private Bitmap getImage()
  162.         {
  163.             string path = @"C:\Users\stan0033_laptop\source\repos\TEST_SHARPGL_TEXTURE\TEST_SHARPGL_TEXTURE\bin\Debug\net6.0-windows\test\box.jpg";
  164.          //   MessageBox.Show(File.Exists(path).ToString());
  165.             return LoadImage(path);
  166.         }
  167.         public Bitmap LoadImage(string imagePath)
  168.         {
  169.             try
  170.             {
  171.                 // Load the image from the file path and return it as a Bitmap
  172.                 Bitmap bitmap = new Bitmap(imagePath);
  173.                 return bitmap;
  174.             }
  175.             catch (Exception ex)
  176.             {
  177.                 // Handle any errors that occur during the image loading process
  178.                 MessageBox.Show($"Error loading image: {ex.Message}");
  179.                 return null;
  180.             }
  181.         }
  182.  
  183.      
  184.     }
  185.     public class BitmapSaver
  186.     {
  187.         public static void SaveBitmap(Bitmap bitmap, string targetSavePath)
  188.         {
  189.             if (bitmap == null)
  190.             {
  191.                 throw new ArgumentNullException(nameof(bitmap), "Bitmap cannot be null.");
  192.             }
  193.  
  194.             if (string.IsNullOrWhiteSpace(targetSavePath))
  195.             {
  196.                 throw new ArgumentException("Target save path cannot be null or empty.", nameof(targetSavePath));
  197.             }
  198.  
  199.             try
  200.             {
  201.                 // Ensure the directory exists
  202.                 string directory = Path.GetDirectoryName(targetSavePath);
  203.                 if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
  204.                 {
  205.                     Directory.CreateDirectory(directory);
  206.                 }
  207.  
  208.                 // Determine the image format based on the file extension
  209.                 ImageFormat format;
  210.                 string extension = Path.GetExtension(targetSavePath)?.ToLowerInvariant();
  211.                 switch (extension)
  212.                 {
  213.                     case ".png":
  214.                         format = ImageFormat.Png;
  215.                         break;
  216.                     case ".jpg":
  217.                     case ".jpeg":
  218.                         format = ImageFormat.Jpeg;
  219.                         break;
  220.                     case ".bmp":
  221.                         format = ImageFormat.Bmp;
  222.                         break;
  223.                     case ".gif":
  224.                         format = ImageFormat.Gif;
  225.                         break;
  226.                     case ".tiff":
  227.                         format = ImageFormat.Tiff;
  228.                         break;
  229.                     default:
  230.                         throw new NotSupportedException($"File extension '{extension}' is not supported.");
  231.                 }
  232.  
  233.                 // Save the bitmap to the specified path
  234.                 bitmap.Save(targetSavePath, format);
  235.             }
  236.             catch (Exception ex)
  237.             {
  238.                 throw new InvalidOperationException($"Failed to save the bitmap to '{targetSavePath}'", ex);
  239.             }
  240.         }
  241.     }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment