OpenGL / SharpGL WPF

161 Views Asked by At

Situation: using the Instantiate Sprite() method I load the texture into memory, then in the DrawTexture() method I unload it to the scene. But why is it that when I load the second texture onto the scene, it replaces the first one?

First sprite render

After add second sprite

    private uint objTex1;
    private uint objTex2;

    private List<Bitmap> _textureImages = new List<Bitmap>();

    private List<float> _texturePostitonX = new List<float>();
    private List<float> _texturePostitonY = new List<float>();

    private List<float> _textureScaleX = new List<float>();
    private List<float> _textureScaleY = new List<float>();

    public void InstantiateSprite(Bitmap sprite, out int textureReferenceId)
    {
        gl.Enable(GL_TEXTURE_2D);
        int index = _textureImages.Count + 1;
        textureReferenceId = index - 1;
        _textureImages.Add(sprite);

        if (_textureImages.Count == 1)
        {
            uint[] _texturesNew1 = new uint[1];
            gl.GenTextures(0, _texturesNew1);
           
            objTex1 = _texturesNew1[0];
          
            gl.BindTexture(GL_TEXTURE_2D, objTex1);
        }
        else
        {
            uint[] _texturesNew2 = new uint[1];
            gl.GenTextures(0, _texturesNew2);
            objTex2 = _texturesNew2[0];
           
            gl.BindTexture(GL_TEXTURE_2D, objTex2);
        }

  
        gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        var bits = _textureImages[index - 1].LockBits(new Rectangle(0, 0, _textureImages[index - 1].Width,
            _textureImages[index - 1].Height), ImageLockMode.ReadWrite,
           PixelFormat.Format24bppRgb
           );

        gl.TexImage2D(GL_TEXTURE_2D, 0, 3, _textureImages[index - 1].Width, _textureImages[index - 1].Height, 0,
           GL_BGR_EXT, GL_UNSIGNED_BYTE,
            bits.Scan0);

        gl.BindTexture(GL_TEXTURE_2D, 0);
        _textureImages[index - 1].UnlockBits(bits);
    }

    public void OpenGL_DrawLoop()
    {
        gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      

        for (int i = 0; i < _textureImages.Count; i++)
            DrawTexture(i);

       

        gl.End();
        gl.Flush();
    }

    private void DrawTexture(int idTex)
    {
        gl.PushMatrix();

        gl.LoadIdentity();
        gl.Scale(-0.15, 0.28, 1);
        gl.Rotate(180, 0, 0, 0);

      
        if (idTex == 0)
        {

            gl.BindTexture(GL_TEXTURE_2D, objTex1);
        
        }
        else
        {
            gl.BindTexture(GL_TEXTURE_2D, objTex2);
         
        }
        gl.Begin(GL_QUADS);

        float dist = 10;
       

        gl.TexCoord(0, 0); gl.Vertex(_textureScaleX[idTex] * (-1 + _texturePostitonX[idTex] / dist), _textureScaleY[idTex] * (-1 + _texturePostitonY[idTex] / dist));
        gl.TexCoord(1, 0); gl.Vertex(_textureScaleX[idTex] * (1 + _texturePostitonX[idTex] / dist), _textureScaleY[idTex] * (-1 + _texturePostitonY[idTex] / dist));
        gl.TexCoord(1, 1); gl.Vertex(_textureScaleX[idTex] * (1 + _texturePostitonX[idTex] / dist), _textureScaleY[idTex] * (1 + _texturePostitonY[idTex] / dist));
        gl.TexCoord(0, 1); gl.Vertex(_textureScaleX[idTex] * (-1 + _texturePostitonX[idTex] / dist), _textureScaleY[idTex] * (1 + _texturePostitonY[idTex] / dist));



        gl.PopMatrix();
    }
    

While the code is written with the ability to render only two textures, then you can add it

0

There are 0 best solutions below