Search code examples
c#opengltexture2dsharpgl

OpenGL/SharpGL: Rendering 2D image is stretched horizontally


I'm playing around with OpenGL in C# (using SharpGL) and I come from a DirectX background... I figured OpenGL would be pretty similar, and it kinda is, except I'm having an issue with my projection matrices which causes my 2D image to be stretched horizontally a little bit (making Lena look VERY bloated).

I think my issue is stemming from my ortho projection, but I'm not positive - I stuffed a projection into the draw function, to make sure it was working as expected. The texture is rendering correctly to the vertices, the image is 512:512 as a BMP (verified when pulled into C#).

Perhaps someone could take a look at my code and help me out? I've copied most of this code from other examples, and have fiddled with it a bit, so this is my latest incarnation:

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

    private void InitialiseTexture(ref OpenGL gl)
    {
        gImage1 = new Bitmap(@"C:\Users\SJ\Pictures\lenaV.bmp");

        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_RGBA, gImage1.Width, gImage1.Height, 0, OpenGL.GL_BGR_EXT, OpenGL.GL_UNSIGNED_BYTE, gbitmapdata.Scan0);

        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);

        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);
        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);

        gl.MatrixMode(OpenGL.GL_PROJECTION);
        gl.LoadIdentity();
        gl.Ortho(0.0, (double)gImage1.Width, (double)gImage1.Height, 0.0, -1.0, 1.0);
        gl.MatrixMode(OpenGL.GL_MODELVIEW);
        gl.Disable(OpenGL.GL_DEPTH_TEST);
    }

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

        //  Set the clear color.
        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();
        if (!TexturesInitialised)
        {
            gl.Ortho(-1, 1, -1, 1, -1, 1);
        }
        else
        {
            gl.Ortho(0, gImage1.Width, gImage1.Height, 0, -1, 1);
        }
        gl.MatrixMode(OpenGL.GL_MODELVIEW);
        gl.Disable(OpenGL.GL_DEPTH_TEST);
    }

Solution

  • Full credit to GeirGrusom for this one. The change was as simple as setting:

    gl.Ortho(0.0, (double)gImage1.Width, (double)gImage1.Height, 0.0, -1.0, 1.0);
    

    to:

    gl.Ortho(0.0, (double)openGLControl.Width, (double)openGLControl.Height, 0.0, -1.0, 1.0);