2

This is a method I use to draw sprites:

public void DrawSprite(Sprite sprite)
        {
            Gl.glBegin(Gl.GL_TRIANGLES);
            {
                for (int i = 0; i < Sprite.VertexAmount; i++)
                {
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, sprite.Texture.Id);
                    DrawImmediateModeVertex(
                    sprite.VertexPositions[i],
                    sprite.VertexColors[i],
                    sprite.VertexUVs[i]);
                }
            }
            Gl.glEnd();
        }

DrawImmediateModeVertex - draws one vertex.

Should I get Gl.glBindTexture out of the for loop? Also strange things happen when I render a texture, even though i provide sprites with different texture ids, just one same texture is drawn every time.

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
pokoko222
  • 21
  • 1

2 Answers2

6

From OpenGL documentation:

GL_INVALID_OPERATION is generated if glBindTexture is executed between the execution of glBegin and the corresponding execution of glEnd.

You should extract glBindTexture outside glBegin...glEnd pair

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
  • 1
    This, and you should preferrably not bind a new texture for every sprite _at all_ anyway. Binding textures requires non-trivial work in the driver, and while vertex-per-vertex immediate mode already is not terribly high-performance, thousands of texture binds additionally kill performance. Sorting sprites by texture would be the correct thing to do, but even caching the texture identifier in a variable and only binding if the last known value is not the current one would already be better. – Damon Aug 11 '11 at 09:48
0

Yes, move the bind outside of the loop, no need to repeatedly bind the same texture.

Jet
  • 59
  • 5