Pārlūkot izejas kodu

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.
Ray 9 gadi atpakaļ
vecāks
revīzija
e2ba22ec59

+ 6 - 3
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();
         //----------------------------------------------------------------------------------

BIN
examples/core_3d_camera_first_person.png


+ 10 - 7
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();
         //----------------------------------------------------------------------------------

BIN
examples/core_3d_camera_free.png


+ 1 - 1
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);
 

BIN
examples/shapes_basic_shapes.png


BIN
examples/textures_srcrec_dstrec.png


+ 4 - 0
src/core.c

@@ -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

+ 13 - 1
src/rlgl.c

@@ -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)

+ 2 - 0
src/rlgl.h

@@ -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

+ 57 - 23
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