Search code examples
opengltexture-mappingopentk

Texture mapping an entire image when only some inner pixels have corresponding coordinates


I have an image which I want to use as texture in my OpenTK (.NET wrapper for OpenGL) application. I don't know the world coordinates of the corners of the image but only some pixels inside the image.

Here's my code (_overlay is the image I'm mapping and mapping is a list of pixel-world correspondences):

    BitmapData data = _overlay.LockBits(new Rectangle(0, 0, _overlay.Width, _overlay.Height),
                                        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

    GL.BindTexture(TextureTarget.Texture2D, _textureID);
    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                  OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
    _overlay.UnlockBits(data);

    GL.Enable(EnableCap.Texture2D);
    GL.Enable(EnableCap.Blend);
    GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);

    GL.Begin(BeginMode.Polygon);
    foreach (var m in mapping)
    {
        var p = new Point(m.PixelInTopLeftCoordinates.X / Image.Width, m.PixelInTopLeftCoordinates.Y / Image.Height);
        GL.TexCoord2(p.X, p.Y);
        GL.Vertex2(m.WorldCoordinates.X, m.WorldCoordinates.Y);
    }

    GL.End();
    GL.Disable(EnableCap.Texture2D);
    GL.Disable(EnableCap.Blend);

The mapping works and the image is displayed in the correct location. The problem is that only the convex hull of the provided pixels is mapped. I want to provide the inner pixels but for the texture mapping to interpolate this mapping and display the entire image. Is this possible? I searched online but either this isn't possible or I wasn't searching for the right keywords, but I couldn't find anything...


Solution

  • This is not an answer to the question itself but rather a different solution to the same underlying problem. What I ended up doing is finding the affine transformation and then solving to find the corresponding coordinates for the corners of the image and then using the 4 corners for the texture mapping.