loco's picture

How to map sphere texture and not to rotate it?

Hi,
I have problems with sphere texturing on rotation and have no idea how to solve it

First, i created a texture

public static int CreateTextureFromBitmap(Bitmap bitmap)
        {
            int textureId = int.MinValue;
            if (bitmap == null)
            {
                return textureId;
            }
 
            GL.GenTextures(1, out textureId);
            GL.BindTexture(TextureTarget.Texture2D, textureId);
 
            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.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.Modulate);
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
 
            return textureId;
        }

Then I created a sphere using the code from OpenTK examples like

public void Draw()
        {
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.TextureGenS);
            GL.Enable(EnableCap.TextureGenT);
 
            VertexPositionNormalTexture[] sphereVertices = CalculateSphereVertices(1, 1, 16, 16);
            IEnumerable<ushort> sphereElements = CalculateSphereElements(1, 1, 16, 16);
 
            GL.Translate(Center.X, Center.Y, Center.Z + 1);
 
            GL.BindTexture(TextureTarget.Texture2D, _textureId);
 
            GL.Begin(BeginMode.Triangles);
            GL.Color4(Color.FloralWhite);
            foreach (var element in sphereElements)
            {
                var vertex = sphereVertices[element];
                GL.TexCoord2(vertex.Texture);
                GL.Normal3(vertex.Normal);
                GL.Vertex3(vertex.Position);
            }
 
            GL.ClearColor(Color.FloralWhite);
 
            GL.End();
            GL.Translate(-Center.X, -Center.Y, -Center.Z - 1);
        }

The problem is, if i start rotating the scene, texture is rotated also and looks weird, comparing to our previous viewer

Inline Images

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
darksign's picture

So how did you solved this problem? :)