簡體   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