1

我的图像在使用 SharpGL 渲染时显得太大。如何正确加载它?图像的尺寸仅为 313 x 79 像素,但在渲染时几乎占据了屏幕的其余部分。

我从 codeplex 得到了这段代码。给出的示例是如何渲染 3D 图像(项目名称为 NativeTexturesSample)。 https://sharpgl.codeplex.com/downloads/get/614989。我设法以 2D 进行渲染,但我认为我做得不对。

    private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e)
    {
        const int screenWidth = 1920;
        const int screenHeight = 1080;

        SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

        gl.MatrixMode(OpenGL.GL_PROJECTION);
        gl.Ortho2D(0, screenWidth , screenHeight , 0);
        gl.Disable(OpenGL.GL_DEPTH_TEST);

        gl.LoadIdentity();
        texture.Create(gl, @"C:\image\footerlogo.bmp");
        texture.Bind(gl);

        gl.Begin(OpenGL.GL_QUADS);

        gl.TexCoord(0.0f, 1.0f); gl.Vertex(-2.0f, 0.0f);
        gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 0.0f);
        gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f);
        gl.TexCoord(0.0f, 0.0f); gl.Vertex(-2.0f, 1.0f);

        gl.End();

        gl.Flush();
    }
4

1 回答 1

1

试试这个代码:

public partial class SharpGLForm : Form
{
    private bool TexturesInitialised = false;
    private float rotation = 0.0f;
    private float Scale = 1;

    private Bitmap gImage1;
    private System.Drawing.Imaging.BitmapData gbitmapdata;
    private uint[] gtexture = new uint[1];

    /// <summary>
    /// Initializes a new instance of the <see cref="SharpGLForm"/> class.
    /// </summary>
    public SharpGLForm()
    {
        InitializeComponent();
    }

    private void InitialiseTexture(ref OpenGL gl)
    {
        gImage1 = new Bitmap(@"C:\Jess1.bmp");// Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

        Rectangle rect = new Rectangle(0, 0, gImage1.Width, gImage1.Height);
        gbitmapdata = gImage1.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        gImage1.UnlockBits(gbitmapdata);
        gl.GenTextures(1, gtexture);
        gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]);
        gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGB8, gImage1.Width, gImage1.Height, 0, OpenGL.GL_BGR_EXT, OpenGL.GL_UNSIGNED_BYTE, gbitmapdata.Scan0);
        uint[] array = new uint[] { OpenGL.GL_NEAREST };
        gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, array);
        gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, array);
        TexturesInitialised = true;
    }

    private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
    {
        //  Get the OpenGL object.
        OpenGL gl = openGLControl.OpenGL;

        //  Clear the color and depth buffer.
        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

        //  Load the identity matrix.
        gl.LoadIdentity();

        if (!TexturesInitialised)
        {
            InitialiseTexture(ref gl);     
        }

        gl.Enable(OpenGL.GL_TEXTURE_2D);
        gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]);
        gl.Color(1.0f, 1.0f, 1.0f, 0.1f); //Must have, weirdness!
        gl.Begin(OpenGL.GL_QUADS);
        gl.TexCoord(1.0f, 1.0f);            
        gl.Vertex(gImage1.Width, gImage1.Height, 1.0f);     
        gl.TexCoord(0.0f, 1.0f);            
        gl.Vertex(0.0f, gImage1.Height, 1.0f);      
        gl.TexCoord(0.0f, 0.0f);        
        gl.Vertex(0.0f, 0.0f, 1.0f);
        gl.TexCoord(1.0f, 0.0f);        
        gl.Vertex(gImage1.Width, 0.0f, 1.0f);       
        gl.End();
        gl.Disable(OpenGL.GL_TEXTURE_2D);
    }

    private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
    {
        OpenGL gl = openGLControl.OpenGL;
        gl.ClearColor(0, 0, 0, 0);
    }

    private void openGLControl_Resized(object sender, EventArgs e)
    {
        OpenGL gl = openGLControl.OpenGL;
        gl.MatrixMode(OpenGL.GL_PROJECTION);
        gl.LoadIdentity();
        gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 10000.0);
        gl.LookAt(0, 0, -500, 0, 0, 0, 0, 1, 0);
        gl.MatrixMode(OpenGL.GL_MODELVIEW);
    }


}
于 2014-11-20T04:12:43.603 回答