繁体   English   中英

将矩形纹理映射到球体上

[英]Mapping rectangular texture onto sphere

我想将矩形地球仪纹理映射到球体上。 我可以加载“ globe.jpg”纹理并将其显示在屏幕上。 我想我需要在特定的纹理坐标处检索“ globe.jpg”纹理的颜色,并使用该颜色为地球上的特定点着色。

我想将右侧中间的地球仪地图映射到左侧的一个球体上(参见图片)

在此处输入图片说明

加载纹理的代码:

int texture;

public Texture() {
   texture = LoadTexture("Content/globe.jpg");
}

public int LoadTexture(string file) {
        Bitmap bitmap = new Bitmap(file);

        int tex;
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

        GL.GenTextures(1, out tex);
        GL.BindTexture(TextureTarget.Texture2D, tex);

        BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
            ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

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


        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

        return tex;
    }

我还已经创建了一些代码,该代码将球体上的一个点映射到我认为的纹理上的点(使用了https://www.cs.unc.edu/~rademach/xroads上纹理映射球体部分中的代码-RT / RTarticle.html )。

point是光线与球体相交的Vector3

        vn = new Vector3(0f, 1f, 0f); //should be north pole of sphere, but it isn't based on sphere's position, so I think it's incorrect
        ve = new Vector3(1f, 0f, 0f); // should be a point on the equator

        float phi = (float) Math.Acos(-1 * Vector3.Dot(vn, point));
        float theta = (float) (Math.Acos(Vector3.Dot(point, ve) / Math.Sin(phi))) / (2 * (float) Math.PI);

        float v = phi / (float) Math.PI;

        float u = Vector3.Dot(Vector3.Cross(vn, ve), point) > 0 ? theta : 1 - theta;

我认为现在可以在加载的纹理上使用此u和v坐标来查找该纹理的颜色。 但是我不知道如何。 我也认为北极和赤道向量不正确。

我不知道4个月后您是否仍然需要答案,但是:

如果您拥有具有正确的uv信息的正确的球体模型(例如使用Blender创建的obj文件),则只需导入该模型(使用assimp或任何其他导入器)并在渲染过程中应用纹理。

您的问题有点含糊,因为我不知道您是否使用着色器。

我的方法是:

1:使用assimp库或任何其他导入库导入模型

2:实现顶点和片段着色器,并在片段着色器中包含用于纹理的sampler2D制服

3:在渲染过程中,选择您的着色器程序ID [GL.UseProgram(...)],然后将顶点,纹理uv和纹理像素(作为统一信息)上载到着色器。

4:使用标准的顶点着色器,如下所示:

#version 330

in      vec3 aPosition;
in      vec2 aTexture;
out     vec2 vTexture;
uniform mat4 uModelViewProjectionMatrix;

void main()
{
    vTexture = aTexture;
    gl_Position = uModelViewProjectionMatrix * vec4(aPosition, 1.0); 
}

5:使用标准片段着色器,如下所示:

#version 330

in      vec2 vTexture;
uniform sampler2D uTexture;
out vec4 fragcolor;

void main()
{
    fragcolor = texture(uTexture, vTexture);
}

如果您需要有效的obj文件用于具有矩形uv映射的球体,请放下一行(或两行)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM