Skip to content

[rlgl] introduced rlStartBatch #4905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions examples/models/models_draw_cube_texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
float y = position.y;
float z = position.z;

// Set desired texture to be enabled while drawing following vertex data
rlSetTexture(texture.id);

// Vertex data transformation can be defined with the commented lines,
// but in this example we calculate the transformed vertex data directly when calling rlVertex3f()
Expand All @@ -114,7 +112,8 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
//rlRotatef(45, 0, 1, 0);
//rlScalef(2.0f, 2.0f, 2.0f);

rlBegin(RL_QUADS);
// Set desired texture to be enabled while drawing following vertex data
rlStartBatch(RL_QUADS, texture.id);
rlColor4ub(color.r, color.g, color.b, color.a);
// Front Face
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
Expand Down Expand Up @@ -155,7 +154,7 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
rlEnd();
//rlPopMatrix();

rlSetTexture(0);
rlEndBatch();
}

// Draw cube with texture piece applied to all faces
Expand All @@ -168,11 +167,9 @@ void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, f
float texHeight = (float)texture.height;

// Set desired texture to be enabled while drawing following vertex data
rlSetTexture(texture.id);

// We calculate the normalized texture coordinates for the desired texture-source-rectangle
// It means converting from (tex.width, tex.height) coordinates to [0.0f, 1.0f] equivalent
rlBegin(RL_QUADS);
rlStartBatch(RL_QUADS, texture.id);
// We calculate the normalized texture coordinates for the desired texture-source-rectangle
// It means converting from (tex.width, tex.height) coordinates to [0.0f, 1.0f] equivalent
rlColor4ub(color.r, color.g, color.b, color.a);

// Front face
Expand Down Expand Up @@ -243,5 +240,5 @@ void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, f

rlEnd();

rlSetTexture(0);
rlEndBatch();
}
2 changes: 1 addition & 1 deletion examples/models/models_rlgl_solar_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void DrawSphereBasic(Color color)
// buffer to store all required vertex, batch is reseted if required
rlCheckRenderBatchLimit((rings + 2)*slices*6);

rlBegin(RL_TRIANGLES);
rlStartBatch(RL_TRIANGLES, rlGetTextureIdDefault());
rlColor4ub(color.r, color.g, color.b, color.a);

for (int i = 0; i < (rings + 2); i++)
Expand Down
8 changes: 4 additions & 4 deletions examples/others/rlgl_standalone.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i
// Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
{
rlBegin(RL_TRIANGLES);
rlStartBatch(RL_TRIANGLES, rlGetTextureIdDefault());
rlColor4ub(color.r, color.g, color.b, color.a);

rlVertex2f(position.x, position.y);
Expand All @@ -299,7 +299,7 @@ static void DrawGrid(int slices, float spacing)
{
int halfSlices = slices / 2;

rlBegin(RL_LINES);
rlStartBatch(RL_LINES, rlGetTextureIdDefault());
for (int i = -halfSlices; i <= halfSlices; i++)
{
if (i == 0)
Expand Down Expand Up @@ -341,7 +341,7 @@ static void DrawCube(Vector3 position, float width, float height, float length,
//rlScalef(2.0f, 2.0f, 2.0f);
//rlRotatef(45, 0, 1, 0);

rlBegin(RL_TRIANGLES);
rlStartBatch(RL_TRIANGLES, rlGetTextureIdDefault());
rlColor4ub(color.r, color.g, color.b, color.a);

// Front Face -----------------------------------------------------
Expand Down Expand Up @@ -413,7 +413,7 @@ static void DrawCubeWires(Vector3 position, float width, float height, float len
rlTranslatef(position.x, position.y, position.z);
//rlRotatef(45, 0, 1, 0);

rlBegin(RL_LINES);
rlStartBatch(RL_LINES, rlGetTextureIdDefault());
rlColor4ub(color.r, color.g, color.b, color.a);

// Front Face -----------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions examples/shapes/shapes_rectangle_advanced.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ static void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, fl
const float angles[4] = { 180.0f, 270.0f, 0.0f, 90.0f };

#if defined(SUPPORT_QUADS_DRAW_MODE)
rlSetTexture(GetShapesTexture().id);
Texture texShapes = GetShapesTexture();
Rectangle shapeRect = GetShapesTextureRectangle();

rlBegin(RL_QUADS);
rlStartBatch(RL_QUADS, texShapes.id);
// Draw all the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
for (int k = 0; k < 4; ++k)
{
Expand Down Expand Up @@ -262,15 +262,15 @@ static void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, fl
rlVertex2f(point[9].x, point[9].y);

rlEnd();
rlSetTexture(0);
rlEndBatch();
#else

// Here we use the 'Diagram' to guide ourselves to which point receives what color.
// By choosing the color correctly associated with a pointe the gradient effect
// will naturally come from OpenGL interpolation.
// But this time instead of Quad, we think in triangles.

rlBegin(RL_TRIANGLES);
rlStartBatch(RL_TRIANGLES, rlGetTextureIdDefault());
// Draw all of the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
for (int k = 0; k < 4; ++k)
{
Expand Down
5 changes: 2 additions & 3 deletions examples/text/text_draw_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,11 @@ static void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, floa
if (SHOW_LETTER_BOUNDRY) DrawCubeWiresV((Vector3){ position.x + width/2, position.y, position.z + height/2}, (Vector3){ width, LETTER_BOUNDRY_SIZE, height }, LETTER_BOUNDRY_COLOR);

rlCheckRenderBatchLimit(4 + 4*backface);
rlSetTexture(font.texture.id);

rlPushMatrix();
rlTranslatef(position.x, position.y, position.z);

rlBegin(RL_QUADS);
rlStartBatch(RL_QUADS, font.texture.id);
rlColor4ub(tint.r, tint.g, tint.b, tint.a);

// Front Face
Expand All @@ -515,7 +514,7 @@ static void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, floa
rlEnd();
rlPopMatrix();

rlSetTexture(0);
rlEndBatch();
}
}

Expand Down
6 changes: 2 additions & 4 deletions examples/textures/textures_polygon.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ int main(void)
// without crossing perimeter, points must be in anticlockwise order
void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointCount, Color tint)
{
rlBegin(RL_TRIANGLES);
rlStartBatch(RL_TRIANGLES, texture.id);

rlSetTexture(texture.id);

rlColor4ub(tint.r, tint.g, tint.b, tint.a);

for (int i = 0; i < pointCount - 1; i++)
Expand All @@ -134,5 +132,5 @@ void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2
}
rlEnd();

rlSetTexture(0);
rlEndBatch();
}
3 changes: 1 addition & 2 deletions examples/textures/textures_textured_curve.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ static void DrawTexturedCurve(void)
Vector2 currentNegNormal = Vector2Add(current, Vector2Scale(normal, -curveWidth));

// Draw the segment as a quad
rlSetTexture(texRoad.id);
rlBegin(RL_QUADS);
rlStartBatch(RL_QUADS, texRoad.id);
rlColor4ub(255,255,255,255);
rlNormal3f(0.0f, 0.0f, 1.0f);

Expand Down
75 changes: 43 additions & 32 deletions src/rlgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ RLAPI double rlGetCullDistanceFar(void); // Get cull plane distan
//------------------------------------------------------------------------------------
RLAPI void rlBegin(int mode); // Initialize drawing mode (how to organize vertex)
RLAPI void rlEnd(void); // Finish vertex providing
RLAPI void rlStartBatch(int mode, unsigned int textureId);
RLAPI void rlEndBatch(void);
RLAPI void rlVertex2i(int x, int y); // Define one vertex (position) - 2 int
RLAPI void rlVertex2f(float x, float y); // Define one vertex (position) - 2 float
RLAPI void rlVertex3f(float x, float y, float z); // Define one vertex (position) - 3 float
Expand Down Expand Up @@ -1430,6 +1432,23 @@ double rlGetCullDistanceFar(void)
#if defined(GRAPHICS_API_OPENGL_11)
// Fallback to OpenGL 1.1 function calls
//---------------------------------------
void rlStartBatch(int mode, unsigned int textureId)
{
switch (mode)
{
case RL_LINES: glBegin(GL_LINES); break;
case RL_TRIANGLES: glBegin(GL_TRIANGLES); break;
case RL_QUADS: glBegin(GL_QUADS); break;
default: break;
}
rlEnableTexture(id);
}

void rlEndBatch(void)
{
rlDisableTexture();
}

void rlBegin(int mode)
{
switch (mode)
Expand All @@ -1452,12 +1471,11 @@ void rlColor3f(float x, float y, float z) { glColor3f(x, y, z); }
void rlColor4f(float x, float y, float z, float w) { glColor4f(x, y, z, w); }
#endif
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
// Initialize drawing mode (how to organize vertex)
void rlBegin(int mode)

void rlStartBatch(int mode, unsigned int textureId)
{
// Draw mode can be RL_LINES, RL_TRIANGLES and RL_QUADS
// NOTE: In all three cases, vertex are accumulated over default internal vertex buffer
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode != mode)
if (mode != RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode
|| textureId != RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId)
{
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0)
{
Expand All @@ -1481,10 +1499,28 @@ void rlBegin(int mode)

RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = mode;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = RLGL.State.defaultTextureId;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = textureId;
}
}

void rlEndBatch(void)
{
// NOTE: If quads batch limit is reached, we force a draw call and next batch starts
if (RLGL.State.vertexCounter >=
RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4)
{
rlDrawRenderBatch(RLGL.currentBatch);
}
}

// Initialize drawing mode (how to organize vertex)
void rlBegin(int mode)
{
// Draw mode can be RL_LINES, RL_TRIANGLES and RL_QUADS
// NOTE: In all three cases, vertex are accumulated over default internal vertex buffer
rlStartBatch(mode, RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId);
}

// Finish vertex providing
void rlEnd(void)
{
Expand Down Expand Up @@ -1653,32 +1689,7 @@ void rlSetTexture(unsigned int id)
#if defined(GRAPHICS_API_OPENGL_11)
rlEnableTexture(id);
#else
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId != id)
{
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0)
{
// Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4,
// that way, following QUADS drawing will keep aligned with index processing
// It implies adding some extra alignment vertex at the end of the draw,
// those vertex are not processed but they are considered as an additional offset
// for the next set of vertex to be drawn
if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount : RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4);
else if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? 1 : (4 - (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4)));
else RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = 0;

if (!rlCheckRenderBatchLimit(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment))
{
RLGL.State.vertexCounter += RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment;

RLGL.currentBatch->drawCounter++;
}
}

if (RLGL.currentBatch->drawCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch);

RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = id;
RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0;
}
rlStartBatch(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode, id);
#endif
}
}
Expand Down
Loading
Loading