Instead of the specified Texture, black circles on a green background are getting rendered. Why?

Posted by vinzBad on Game Development See other posts from Game Development or by vinzBad
Published on 2012-11-20T17:51:02Z Indexed on 2012/11/20 23:24 UTC
Read the original article Hit count: 192

Filed under:
|
|

I'm trying to render a Texture via OpenGL. But instead of the texture black circles on a green background are rendered. (They scale, depending what the rotation of the texture is)

Example:

enter image description here

The texture I'm trying to render is the following:

enter image description here

This is the code I use to render the texture, it's located in my Sprite-class.

    public void Render()
    {
        Matrix4 matrix = Matrix4.CreateTranslation(-OriginX, -OriginY, 0) *
                        Matrix4.CreateRotationZ(Rotation) *
                        Matrix4.CreateTranslation(X, Y, 0);


        Vector2[] corners =
        {
            new Vector2(0,0), //top left
            new Vector2(Width ,0),//top right
            new Vector2(Width,Height),//bottom rigth
            new Vector2(0,Height)//bottom left
        };

        //copy the corners to the uv coordinates
        Vector2[] uv = corners.ToArray<Vector2>();

//transform the coordinates
        for (int i = 0; i < 4; i++) corners[i] = new Vector2(Vector3.Transform(new Vector3(corners[i]), matrix));
        //GL.Color3(TintColor);
        GL.BindTexture(TextureTarget.Texture2D, _ID);
        GL.Begin(BeginMode.Quads);
        {
            for (int i = 0; i < 4; i++)
            {
                GL.TexCoord2(uv[i]);
                GL.Vertex3(corners[i].X, corners[i].Y, _layerDepth);
            }
        }
        GL.End();

        if (EnableDebugDraw)
        {
            GL.Color3(Color.Violet);
            GL.PointSize(3);
            GL.Begin(BeginMode.Points);
            {
                for (int i = 0; i < 4; i++) GL.Vertex2(corners[i]);
            }
            GL.End();

            GL.Color3(Color.Green);
            GL.Begin(BeginMode.Points);
            GL.Vertex2(X, Y);
            GL.End();
        }

    }

This is how I setup OpenGL.

    public static void SetupGL()
    {
        GL.Enable(EnableCap.AlphaTest);
        GL.AlphaFunc(AlphaFunction.Greater, 0.1f);

        GL.Enable(EnableCap.Texture2D);
        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

    }

With this function I load the texture:

public static uint LoadTexture(string path)
    {
        uint id;
        GL.GenTextures(1, out id);
        GL.BindTexture(TextureTarget.Texture2D, id);

        Bitmap bitmap = new Bitmap(path);

        BitmapData data = bitmap.LockBits(new System.Drawing.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);
        return id;
    }

And here I call Sprite.Render()

protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.ClearColor(Color.MidnightBlue);
        GL.Clear(ClearBufferMask.ColorBufferBit);
        _sprite.Render();
        SwapBuffers();
        base.OnRenderFrame(e);
    }

As I stole this code from the Textures-Example from OpenTK, I don't understand why this doesn't work.

© Game Development or respective owner

Related posts about c#

Related posts about opengl