简体   繁体   中英

Mapping rectangular texture onto sphere

I want to map a rectangular globe texture onto a sphere. I can load the "globe.jpg" texture and display it onto the screen. I think I need to retrieve the color of the "globe.jpg" texture at specific texture coordinates and use that to colorize a specific point on the globe.

I want to map the globe map on the rightmiddle side onto one of the spheres on the left side (see picture)

在此处输入图片说明

Code for loading texture:

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

I also already created some code that maps a point on a sphere to a point on a point on the texture I think (used code from the texture mapping spheres section on https://www.cs.unc.edu/~rademach/xroads-RT/RTarticle.html ).

point is a Vector3 of where a ray intersects the sphere:

        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;

I think that I can now use this u and v coordinate on the texture I loaded to find the color of the texture there. But I don't know how. I also think the north pole and equator vectors are not correct.

I don't know if you still need an answer after 4 months, but:

If you have a proper sphere model (like an obj file created with blender) with the correct uv information, you just need to import that model (using assimp or any other importer) and apply the texture during render pass.

Your question is a bit vague because I do not know if you use shaders.

My approach would be:

1: Import model with assimp library or any other import library

2: Implement vertex and fragment shaders and include a sampler2D uniform for the texture in the fragment shader

3: During render pass select your shader program id [ GL.UseProgram(...) ] and then upload vertices and texture uv and texture pixel (as a uniform) information to the shaders.

4: Use a standard vertex shader like this:

#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: Use a standard fragment shader like this:

#version 330

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

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

If you need a valid obj file for a sphere with rectangular uv mapping, feel free to drop a line (or two).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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