diff options
| author | Ray <raysan5@gmail.com> | 2016-03-17 13:51:48 +0100 |
|---|---|---|
| committer | Ray <raysan5@gmail.com> | 2016-03-17 13:51:48 +0100 |
| commit | e2ba22ec596757d62f8b22cf8b722d68040f23d3 (patch) | |
| tree | 28b9e0ddfe60534b167dd659e27c4fa10288d329 /src | |
| parent | 49df957058b2f602c7e6873fec0007fcd7a4dc4c (diff) | |
| download | raylib-e2ba22ec596757d62f8b22cf8b722d68040f23d3.tar.gz raylib-e2ba22ec596757d62f8b22cf8b722d68040f23d3.zip | |
Improved 2D-3D drawing
Depth test disabled for 2D and only used on 3D; consequently LINES vs
TRIANGLES vs QUADS buffers drawing order maters... but blending also
works ok.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.c | 4 | ||||
| -rw-r--r-- | src/rlgl.c | 14 | ||||
| -rw-r--r-- | src/rlgl.h | 2 | ||||
| -rw-r--r-- | src/shapes.c | 80 |
4 files changed, 76 insertions, 24 deletions
@@ -643,6 +643,8 @@ void Begin3dMode(Camera camera) // Setup Camera view Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up); rlMultMatrixf(MatrixToFloat(matView)); // Multiply MODELVIEW matrix by view matrix (camera) + + rlEnableDepthTest(); // Enable DEPTH_TEST for 3D } // Ends 3D mode and returns to default 2D orthographic mode @@ -657,6 +659,8 @@ void End3dMode(void) rlLoadIdentity(); // Reset current matrix (MODELVIEW) //rlTranslatef(0.375, 0.375, 0); // HACK to ensure pixel-perfect drawing on OpenGL (after exiting 3D mode) + + rlDisableDepthTest(); // Disable DEPTH_TEST for 2D } // Set target FPS for the game @@ -780,6 +780,18 @@ void rlDisableTexture(void) #endif } +// Enable depth test +void rlEnableDepthTest(void) +{ + glEnable(GL_DEPTH_TEST); +} + +// Disable depth test +void rlDisableDepthTest(void) +{ + glDisable(GL_DEPTH_TEST); +} + // Unload texture from GPU memory void rlDeleteTextures(unsigned int id) { @@ -1579,7 +1591,7 @@ void rlglInitGraphics(int offsetX, int offsetY, int width, int height) //glClearDepth(1.0f); // Clear depth buffer (default) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers, depth buffer is used for 3D - glEnable(GL_DEPTH_TEST); // Enables depth testing (required for 3D) + glDisable(GL_DEPTH_TEST); // Disable depth testing for 2D (only used for 3D) glDepthFunc(GL_LEQUAL); // Type of depth testing to apply glEnable(GL_BLEND); // Enable color blending (required to work with transparencies) @@ -244,6 +244,8 @@ void rlColor4f(float x, float y, float z, float w); // Define one vertex (color) //------------------------------------------------------------------------------------ void rlEnableTexture(unsigned int id); // Enable texture usage void rlDisableTexture(void); // Disable texture usage +void rlEnableDepthTest(void); // Enable depth test +void rlDisableDepthTest(void); // Disable depth test void rlDeleteTextures(unsigned int id); // Delete OpenGL texture from GPU void rlDeleteShader(unsigned int id); // Delete OpenGL shader program from GPU void rlDeleteVertexArrays(unsigned int id); // Unload vertex data (VAO) from GPU memory diff --git a/src/shapes.c b/src/shapes.c index 7e3b5634..14d11315 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -98,7 +98,7 @@ void DrawLineV(Vector2 startPos, Vector2 endPos, Color color) // Draw a color-filled circle void DrawCircle(int centerX, int centerY, float radius, Color color) { - DrawPoly((Vector2){ centerX, centerY }, 36, radius, 0, color); + DrawCircleV((Vector2){ centerX, centerY }, radius, color); } // Draw a gradient-filled circle @@ -119,17 +119,40 @@ void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Co } // Draw a color-filled circle (Vector version) +// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw) void DrawCircleV(Vector2 center, float radius, Color color) { - rlBegin(RL_TRIANGLES); - for (int i = 0; i < 360; i += 10) - { - rlColor4ub(color.r, color.g, color.b, color.a); - rlVertex2i(center.x, center.y); - rlVertex2f(center.x + sin(DEG2RAD*i)*radius, center.y + cos(DEG2RAD*i)*radius); - rlVertex2f(center.x + sin(DEG2RAD*(i + 10)) * radius, center.y + cos(DEG2RAD*(i + 10)) * radius); - } - rlEnd(); + if (rlGetVersion() == OPENGL_11) + { + rlBegin(RL_TRIANGLES); + for (int i = 0; i < 360; i += 10) + { + rlColor4ub(color.r, color.g, color.b, color.a); + + rlVertex2i(center.x, center.y); + rlVertex2f(center.x + sin(DEG2RAD*i)*radius, center.y + cos(DEG2RAD*i)*radius); + rlVertex2f(center.x + sin(DEG2RAD*(i + 10)) * radius, center.y + cos(DEG2RAD*(i + 10)) * radius); + } + rlEnd(); + } + else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) + { + rlEnableTexture(whiteTexture); // Default white texture + + rlBegin(RL_QUADS); + for (int i = 0; i < 360; i += 20) + { + rlColor4ub(color.r, color.g, color.b, color.a); + + rlVertex2i(center.x, center.y); + rlVertex2f(center.x + sin(DEG2RAD*i)*radius, center.y + cos(DEG2RAD*i)*radius); + rlVertex2f(center.x + sin(DEG2RAD*(i + 10)) * radius, center.y + cos(DEG2RAD*(i + 10)) * radius); + rlVertex2f(center.x + sin(DEG2RAD*(i + 20)) * radius, center.y + cos(DEG2RAD*(i + 20)) * radius); + } + rlEnd(); + + rlDisableTexture(); + } } // Draw circle outline @@ -178,6 +201,7 @@ void DrawRectangleGradient(int posX, int posY, int width, int height, Color colo } // Draw a color-filled rectangle (Vector version) +// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw) void DrawRectangleV(Vector2 position, Vector2 size, Color color) { if (rlGetVersion() == OPENGL_11) @@ -196,7 +220,6 @@ void DrawRectangleV(Vector2 position, Vector2 size, Color color) } else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) { - // NOTE: This shape uses QUADS to avoid drawing order issues (view rlglDraw) rlEnableTexture(whiteTexture); // Default white texture rlBegin(RL_QUADS); @@ -221,22 +244,33 @@ void DrawRectangleV(Vector2 position, Vector2 size, Color color) } // Draw rectangle outline +// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw) void DrawRectangleLines(int posX, int posY, int width, int height, Color color) -{ - rlBegin(RL_LINES); - rlColor4ub(color.r, color.g, color.b, color.a); - rlVertex2i(posX + 1, posY + 1); - rlVertex2i(posX + width, posY + 1); +{ + if (rlGetVersion() == OPENGL_11) + { + rlBegin(RL_LINES); + rlColor4ub(color.r, color.g, color.b, color.a); + rlVertex2i(posX + 1, posY + 1); + rlVertex2i(posX + width, posY + 1); - rlVertex2i(posX + width, posY + 1); - rlVertex2i(posX + width, posY + height); + rlVertex2i(posX + width, posY + 1); + rlVertex2i(posX + width, posY + height); - rlVertex2i(posX + width, posY + height); - rlVertex2i(posX + 1, posY + height); + rlVertex2i(posX + width, posY + height); + rlVertex2i(posX + 1, posY + height); - rlVertex2i(posX + 1, posY + height); - rlVertex2i(posX + 1, posY + 1); - rlEnd(); + rlVertex2i(posX + 1, posY + height); + rlVertex2i(posX + 1, posY + 1); + rlEnd(); + } + else if ((rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20)) + { + DrawRectangle(posX, posY, width, 1, color); + DrawRectangle(posX + width - 1, posY + 1, 1, height - 2, color); + DrawRectangle(posX, posY + height - 1, width, 1, color); + DrawRectangle(posX, posY + 1, 1, height - 2, color); + } } // Draw a triangle |
