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 | |
| 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.
| -rw-r--r-- | examples/core_3d_camera_first_person.c | 9 | ||||
| -rw-r--r-- | examples/core_3d_camera_first_person.png | bin | 18402 -> 18883 bytes | |||
| -rw-r--r-- | examples/core_3d_camera_free.c | 17 | ||||
| -rw-r--r-- | examples/core_3d_camera_free.png | bin | 25167 -> 25293 bytes | |||
| -rw-r--r-- | examples/core_3d_picking.c | 2 | ||||
| -rw-r--r-- | examples/shapes_basic_shapes.png | bin | 26439 -> 30382 bytes | |||
| -rw-r--r-- | examples/textures_srcrec_dstrec.png | bin | 48478 -> 47563 bytes | |||
| -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 |
11 files changed, 93 insertions, 35 deletions
diff --git a/examples/core_3d_camera_first_person.c b/examples/core_3d_camera_first_person.c index 16d388df..56e38a23 100644 --- a/examples/core_3d_camera_first_person.c +++ b/examples/core_3d_camera_first_person.c @@ -74,10 +74,13 @@ int main() } End3dMode(); + + DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f)); + DrawRectangleLines( 10, 10, 220, 70, BLUE); - DrawText("First person camera default controls:", 20, 20, 10, GRAY); - DrawText("- Move with keys: W, A, S, D", 40, 50, 10, DARKGRAY); - DrawText("- Mouse move to look around", 40, 70, 10, DARKGRAY); + DrawText("First person camera default controls:", 20, 20, 10, BLACK); + DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY); + DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/core_3d_camera_first_person.png b/examples/core_3d_camera_first_person.png Binary files differindex 9373da2d..7130b0a2 100644 --- a/examples/core_3d_camera_first_person.png +++ b/examples/core_3d_camera_first_person.png diff --git a/examples/core_3d_camera_free.c b/examples/core_3d_camera_free.c index 234c46b3..fa7ad85f 100644 --- a/examples/core_3d_camera_free.c +++ b/examples/core_3d_camera_free.c @@ -59,13 +59,16 @@ int main() DrawGrid(10, 1.0f); End3dMode(); - - DrawText("Free camera default controls:", 20, 20, 10, GRAY); - DrawText("- Mouse Wheel to Zoom in-out", 40, 50, 10, DARKGRAY); - DrawText("- Mouse Wheel Pressed to Pan", 40, 70, 10, DARKGRAY); - DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 90, 10, DARKGRAY); - DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 110, 10, DARKGRAY); - DrawText("- Z to zoom to (0, 0, 0)", 40, 130, 10, DARKGRAY); + + DrawRectangle( 10, 10, 320, 133, Fade(SKYBLUE, 0.5f)); + DrawRectangleLines( 10, 10, 320, 133, BLUE); + + DrawText("Free camera default controls:", 20, 20, 10, BLACK); + DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, DARKGRAY); + DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY); + DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, DARKGRAY); + DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, DARKGRAY); + DrawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/examples/core_3d_camera_free.png b/examples/core_3d_camera_free.png Binary files differindex 17920620..8857a478 100644 --- a/examples/core_3d_camera_free.png +++ b/examples/core_3d_camera_free.png diff --git a/examples/core_3d_picking.c b/examples/core_3d_picking.c index 33eaed8a..da303107 100644 --- a/examples/core_3d_picking.c +++ b/examples/core_3d_picking.c @@ -77,7 +77,7 @@ int main() End3dMode(); - DrawText("Try selecting the box with mouse!", 240, 10, 20, GRAY); + DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY); if(collision) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN); diff --git a/examples/shapes_basic_shapes.png b/examples/shapes_basic_shapes.png Binary files differindex 03ecf2ec..a7d4a991 100644 --- a/examples/shapes_basic_shapes.png +++ b/examples/shapes_basic_shapes.png diff --git a/examples/textures_srcrec_dstrec.png b/examples/textures_srcrec_dstrec.png Binary files differindex 7459d6ec..9ea00fe4 100644 --- a/examples/textures_srcrec_dstrec.png +++ b/examples/textures_srcrec_dstrec.png @@ -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 |
