pokoko222
pokoko222

Reputation: 21

Question about glBindTexture

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.

Upvotes: 2

Views: 911

Answers (2)

Mihai Maruseac
Mihai Maruseac

Reputation: 21435

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

Upvotes: 6

Jet
Jet

Reputation: 59

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

Upvotes: 0

Related Questions