Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SharpGL;
- using System.Drawing;
- using System.IO;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using System.Drawing.Imaging;
- using Rectangle = System.Drawing.Rectangle;
- using Image = System.Windows.Controls.Image;
- namespace TEST_SHARPGL_TEXTURE
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private float rotationAngle = 0.0f;
- private Bitmap DefaultImage;
- private uint textureId;
- private OpenGL gl;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- gl = Scene_Viewport.OpenGL; ;
- DefaultImage = getImage();
- if (DefaultImage == null) { Environment.Exit(0); }
- // BitmapSaver.SaveBitmap(DefaultImage, @"C:\P__\image.jpg");
- // display d = new display(DefaultImage); d.ShowDialog();
- textureId = LoadTexture(Scene_Viewport.OpenGL, DefaultImage);
- // MessageBox.Show(textureId.ToString());
- }
- private void DrawScene(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
- {
- // Clear the color and depth buffer.
- gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
- // Load the identity matrix.
- gl.LoadIdentity();
- // gl.Enable(OpenGL.GL_BLEND);
- // gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
- // gl.Disable(OpenGL.GL_LIGHTING);
- // Move the cube slightly back so it’s visible.
- gl.Translate(0.0f, 0.0f, -6.0f);
- // Rotate the cube.
- gl.Rotate(rotationAngle, 1.0f, 1.0f, 0.0f);
- // Bind the texture.
- // Draw a textured cube.
- DrawCube(gl);
- gl.End();
- // Flush OpenGL commands.
- gl.Flush();
- // Update rotation angle.
- rotationAngle += 1.0f;
- if (rotationAngle >= 360.0f)
- rotationAngle -= 360.0f;
- }
- private void DrawCube(OpenGL gl)
- {
- gl.Begin(OpenGL.GL_QUADS);
- gl.Enable(OpenGL.GL_TEXTURE_2D);
- gl.BindTexture(OpenGL.GL_TEXTURE_2D, textureId);
- // Front face
- gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);
- gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);
- gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);
- gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);
- // Back face
- gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);
- gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);
- gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);
- gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);
- // Left face
- gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);
- gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);
- gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);
- gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);
- // Right face
- gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);
- gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);
- gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);
- gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);
- // Top face
- gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);
- gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);
- gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);
- gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);
- // Bottom face
- gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);
- gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);
- gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, -1.0f, 1.0f);
- gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);
- gl.End();
- }
- private uint LoadTexture(OpenGL gl, Bitmap bitmap)
- {
- if (bitmap == null)
- throw new ArgumentNullException(nameof(bitmap), "Bitmap cannot be null.");
- // Generate a texture ID.
- uint[] textureIds = new uint[1];
- gl.GenTextures(1, textureIds);
- var error = gl.GetError();
- if (error != OpenGL.GL_NO_ERROR)
- {
- MessageBox.Show($"OpenGL Error: {error}");
- }
- uint id = textureIds[0];
- // Set texture parameters.
- gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT);
- gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT);
- gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
- gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
- // Bind the texture to apply settings.
- gl.BindTexture(OpenGL.GL_TEXTURE_2D, id);
- // Lock the bitmap bits for OpenGL.
- BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
- ImageLockMode.ReadOnly,
- System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- // Create the texture from the bitmap data.
- gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, data.Width, data.Height, 0,
- OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, data.Scan0);
- // Unlock the bitmap bits.
- bitmap.UnlockBits(data);
- return id;
- }
- private Bitmap getImage()
- {
- string path = @"C:\Users\stan0033_laptop\source\repos\TEST_SHARPGL_TEXTURE\TEST_SHARPGL_TEXTURE\bin\Debug\net6.0-windows\test\box.jpg";
- // MessageBox.Show(File.Exists(path).ToString());
- return LoadImage(path);
- }
- public Bitmap LoadImage(string imagePath)
- {
- try
- {
- // Load the image from the file path and return it as a Bitmap
- Bitmap bitmap = new Bitmap(imagePath);
- return bitmap;
- }
- catch (Exception ex)
- {
- // Handle any errors that occur during the image loading process
- MessageBox.Show($"Error loading image: {ex.Message}");
- return null;
- }
- }
- }
- public class BitmapSaver
- {
- public static void SaveBitmap(Bitmap bitmap, string targetSavePath)
- {
- if (bitmap == null)
- {
- throw new ArgumentNullException(nameof(bitmap), "Bitmap cannot be null.");
- }
- if (string.IsNullOrWhiteSpace(targetSavePath))
- {
- throw new ArgumentException("Target save path cannot be null or empty.", nameof(targetSavePath));
- }
- try
- {
- // Ensure the directory exists
- string directory = Path.GetDirectoryName(targetSavePath);
- if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- // Determine the image format based on the file extension
- ImageFormat format;
- string extension = Path.GetExtension(targetSavePath)?.ToLowerInvariant();
- switch (extension)
- {
- case ".png":
- format = ImageFormat.Png;
- break;
- case ".jpg":
- case ".jpeg":
- format = ImageFormat.Jpeg;
- break;
- case ".bmp":
- format = ImageFormat.Bmp;
- break;
- case ".gif":
- format = ImageFormat.Gif;
- break;
- case ".tiff":
- format = ImageFormat.Tiff;
- break;
- default:
- throw new NotSupportedException($"File extension '{extension}' is not supported.");
- }
- // Save the bitmap to the specified path
- bitmap.Save(targetSavePath, format);
- }
- catch (Exception ex)
- {
- throw new InvalidOperationException($"Failed to save the bitmap to '{targetSavePath}'", ex);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment