使用 OpenGL 绘制图像 [英] Draw image with OpenGL

查看:104
本文介绍了使用 OpenGL 绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用开放的 OpenGL/OpenTK(来自无人机的视频)在地图 (GMap.Net) 上绘制视频/图像

How to draw video/image over map (GMap.Net) with open OpenGL/OpenTK (video from drone)

我的问题是如何在地图上绘制视频图像,因为可以在地图上以不同的角度观看视频,并且图像在地图上应该是梯形而不是四边形?

My question is how to draw video image on map since video can be looked with different angle on map and image should be trapezoid on the map and not quad?

我从其他问题复制了图像...目前我的代码在下面绘制四边形,但在四边形中我看到 2 个不相等的三角形.

I copied image from other question... Currently my code bellow draw quad but in quad I see 2 triangles which are not equal.

当前代码:

public override void OnRender()
{
    if (!backgroundColor.HasValue)
        backgroundColor = new Pen(Fill).Color;
    GL.Color4(backgroundColor.Value);

    lock (bitmapSync)
    {
        if (bitmap != null)
            createTexture();
    }

    GL.Enable(EnableCap.Texture2D);
    GL.BindTexture(TextureTarget.Texture2D, texture);

    GL.Begin(PrimitiveType.Quads);
    {
        //TODO this works fine only for correct square image and not for trapezoid 
        GL.TexCoord2(0, 0); GL.Vertex2(LocalPoints[0].X, LocalPoints[0].Y);
        GL.TexCoord2(1, 0); GL.Vertex2(LocalPoints[1].X, LocalPoints[1].Y);
        GL.TexCoord2(1, 1); GL.Vertex2(LocalPoints[2].X, LocalPoints[2].Y);
        GL.TexCoord2(0, 1); GL.Vertex2(LocalPoints[3].X, LocalPoints[3].Y);
    }
    GL.End();
    GL.Disable(EnableCap.Texture2D);

    float wid = Stroke.Width;
    Color col = Stroke.Color;

    if (wid > 0)
    {
        for (int i = 0; i < LocalPoints.Count; i++)
        {
            int j = (i + 1) % LocalPoints.Count;
            GMapControl.line(LocalPoints[i].X, LocalPoints[i].Y, LocalPoints[j].X, LocalPoints[j].Y, wid, col);
        }
    }
}

答案在OpenGL ES 2.0 中梯形的透视正确纹理http://www.reedbeta.com/blog/quadrilateral-interpolation-part-1/https://gamedev.stackexchange.com/questions/68021/我如何绘制透视正确四边形如何在openGL中对随机凸四边形进行纹理化没有帮助...

推荐答案

我找到了 Quad 透视校正的解决方案

I found solution with perspective correction for Quad

public override void OnRender()
{
    if (LocalPoints.Count == 4 && !targetPosition.IsEmpty)
    {
        if (!backgroundColor.HasValue)
            backgroundColor = new Pen(Fill).Color;
        GL.Color4(backgroundColor.Value);

        lock (bitmapSync)
        {
            if (bitmap != null)
                createTexture();
        }

        GL.Enable(EnableCap.Texture2D);
        GL.BindTexture(TextureTarget.Texture2D, texture);

        // center point
        GPoint localTargetPosition = MainForm.instance.gMapControl.FromLatLngToLocalWithOffset(targetPosition);
        // determines distances to center for all vertexes
        double dUL = Common.distance(new double[] { LocalPoints[0].X, LocalPoints[0].Y }, new double[] { localTargetPosition.X, localTargetPosition.Y });
        double dUR = Common.distance(new double[] { LocalPoints[1].X, LocalPoints[1].Y }, new double[] { localTargetPosition.X, localTargetPosition.Y });
        double dLR = Common.distance(new double[] { LocalPoints[2].X, LocalPoints[2].Y }, new double[] { localTargetPosition.X, localTargetPosition.Y });
        double dLL = Common.distance(new double[] { LocalPoints[3].X, LocalPoints[3].Y }, new double[] { localTargetPosition.X, localTargetPosition.Y });

        var texCoords = new[]
        {
            new Vector4(0, 0, 1, 1),
            new Vector4(1, 0, 1, 1),
            new Vector4(1, 1, 1, 1),
            new Vector4(0, 1, 1, 1)
        };

        texCoords[0] *= (float)((dUL + dLR) / dLR);
        texCoords[1] *= (float)((dUR + dLL) / dLL);
        texCoords[2] *= (float)((dLR + dUL) / dUL);
        texCoords[3] *= (float)((dLL + dUR) / dUR);

        GL.Begin(PrimitiveType.Quads);
        {
            //GL.TexCoord2(0, 0); //UL  LocalPoints[0] gimbalUL
            //GL.TexCoord2(1, 0); //UR  LocalPoints[1] gimbalUR
            //GL.TexCoord2(1, 1); //LR  LocalPoints[2] gimbalLR
            //GL.TexCoord2(0, 1); //LL  LocalPoints[3] gimbalLL
            GL.TexCoord4(texCoords[0]); GL.Vertex4(LocalPoints[0].X, LocalPoints[0].Y, 1, 1);
            GL.TexCoord4(texCoords[1]); GL.Vertex4(LocalPoints[1].X, LocalPoints[1].Y, 1, 1);
            GL.TexCoord4(texCoords[2]); GL.Vertex4(LocalPoints[2].X, LocalPoints[2].Y, 1, 1);
            GL.TexCoord4(texCoords[3]); GL.Vertex4(LocalPoints[3].X, LocalPoints[3].Y, 1, 1);
        }

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

        float wid = Stroke.Width;
        Color col = Stroke.Color;

        if (wid > 0)
        {
            for (int i = 0; i < LocalPoints.Count; i++)
            {
                int j = (i + 1) % LocalPoints.Count;
                GMapControl.line(LocalPoints[i].X, LocalPoints[i].Y, LocalPoints[j].X, LocalPoints[j].Y, wid, col);
            }
        }
    }
    else
    {
        base.OnRender();
    }

}

这篇关于使用 OpenGL 绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆