From 682cdc319c37322481285a0a4e1992dfd3116cd4 Mon Sep 17 00:00:00 2001 From: Horrowind Date: Mon, 11 Oct 2021 22:29:24 +0200 Subject: [PATCH 1/6] Add DrawCylinderEx and DrawCylinderWiresEx --- src/raylib.h | 2 + src/rmodels.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index 7bcb39e37..3ef4133e8 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1384,7 +1384,9 @@ RLAPI void DrawSphere(Vector3 centerPos, float radius, Color color); RLAPI void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere with extended parameters RLAPI void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Color color); // Draw sphere wires RLAPI void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone +RLAPI void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float endRadius, int sides, Color color); // Draw a cylinder with base at startPos and top at endPos RLAPI void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int slices, Color color); // Draw a cylinder/cone wires +RLAPI void DrawCylinderWiresEx(Vector3 startPos, Vector3 endPos, float startRadius, float endRadius, int sides, Color color); // Draw a cylinder wires with base at startPos and top at endPos RLAPI void DrawPlane(Vector3 centerPos, Vector2 size, Color color); // Draw a plane XZ RLAPI void DrawRay(Ray ray, Color color); // Draw a ray line RLAPI void DrawGrid(int slices, float spacing); // Draw a grid (centered at (0, 0, 0)) diff --git a/src/rmodels.c b/src/rmodels.c index 2069aaabb..708a25cf9 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -767,6 +767,64 @@ void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float h rlPopMatrix(); } +// Draw a cylinder with base at startPos and top at endPos +// NOTE: It could be also used for pyramid and cone +void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float endRadius, int sides, Color color) +{ + if (sides < 3) sides = 3; + + int numVertex = sides*6; + rlCheckRenderBatchLimit(numVertex); + if(sides < 3) sides = 3; + + Vector3 difference = Vector3Subtract(endPos, startPos); + // Construct a basis of the base and the top face: + Vector3 b1 = Vector3Normalize(Vector3Perpendicular(difference)); + Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, difference)); + + float base_angle = (2.0 * PI) / sides; + + rlBegin(RL_TRIANGLES); + rlColor4ub(color.r, color.g, color.b, color.a); + + for (int i = 0; i < sides; i++) { + // compute the four vertices + float s1 = sinf(base_angle * (i+0)) * startRadius; + float c1 = cosf(base_angle * (i+0)) * startRadius; + Vector3 w1 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s1), Vector3Scale(b2, c1))); + float s2 = sinf(base_angle * (i+1)) * startRadius; + float c2 = cosf(base_angle * (i+1)) * startRadius; + Vector3 w2 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s2), Vector3Scale(b2, c2))); + float s3 = sinf(base_angle * (i+0)) * endRadius; + float c3 = cosf(base_angle * (i+0)) * endRadius; + Vector3 w3 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s3), Vector3Scale(b2, c3))); + float s4 = sinf(base_angle * (i+1)) * endRadius; + float c4 = cosf(base_angle * (i+1)) * endRadius; + Vector3 w4 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s4), Vector3Scale(b2, c4))); + + if (startRadius > 0) { // + rlVertex3f(startPos.x, startPos.y, startPos.z); // | + rlVertex3f(w2.x, w2.y, w2.z); // T0 + rlVertex3f(w1.x, w1.y, w1.z); // | + } // + // w2 x.-----------x startPos + rlVertex3f(w1.x, w1.y, w1.z); // | |\'. T0 / + rlVertex3f(w2.x, w2.y, w2.z); // T1 | \ '. / + rlVertex3f(w3.x, w3.y, w3.z); // | |T \ '. / + // | 2 \ T 'x w1 + rlVertex3f(w2.x, w2.y, w2.z); // | w4 x.---\-1-|---x endPos + rlVertex3f(w4.x, w4.y, w4.z); // T2 '. \ |T3/ + rlVertex3f(w3.x, w3.y, w3.z); // | '. \ | / + // '.\|/ + if (endRadius > 0) { // 'x w3 + rlVertex3f(endPos.x, endPos.y, endPos.z); // | + rlVertex3f(w3.x, w3.y, w3.z); // T3 + rlVertex3f(w4.x, w4.y, w4.z); // | + } // + } + rlEnd(); +} + // Draw a wired cylinder // NOTE: It could be also used for pyramid and cone void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color) @@ -800,6 +858,54 @@ void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, fl rlPopMatrix(); } + +// Draw a wired cylinder with base at startPos and top at endPos +// NOTE: It could be also used for pyramid and cone +void DrawCylinderWiresEx(Vector3 startPos, Vector3 endPos, float startRadius, float endRadius, int sides, Color color) +{ + if (sides < 3) sides = 3; + + int numVertex = sides*6; + rlCheckRenderBatchLimit(numVertex); + + Vector3 difference = Vector3Subtract(endPos, startPos); + // Construct a basis of the base and the top face: + Vector3 b1 = Vector3Normalize(Vector3Perpendicular(difference)); + Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, difference)); + + float base_angle = (2.0 * PI) / sides; + + rlBegin(RL_LINES); + rlColor4ub(color.r, color.g, color.b, color.a); + + for (int i = 0; i < sides; i++) { + // compute the four vertices + float s1 = sinf(base_angle * (i+0)) * startRadius; + float c1 = cosf(base_angle * (i+0)) * startRadius; + Vector3 w1 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s1), Vector3Scale(b2, c1))); + float s2 = sinf(base_angle * (i+1)) * startRadius; + float c2 = cosf(base_angle * (i+1)) * startRadius; + Vector3 w2 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s2), Vector3Scale(b2, c2))); + float s3 = sinf(base_angle * (i+0)) * endRadius; + float c3 = cosf(base_angle * (i+0)) * endRadius; + Vector3 w3 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s3), Vector3Scale(b2, c3))); + float s4 = sinf(base_angle * (i+1)) * endRadius; + float c4 = cosf(base_angle * (i+1)) * endRadius; + Vector3 w4 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s4), Vector3Scale(b2, c4))); + + rlVertex3f(w1.x, w1.y, w1.z); + rlVertex3f(w2.x, w2.y, w2.z); + + rlVertex3f(w1.x, w1.y, w1.z); + rlVertex3f(w3.x, w3.y, w3.z); + + rlVertex3f(w3.x, w3.y, w3.z); + rlVertex3f(w4.x, w4.y, w4.z); + } + rlEnd(); +} + + // Draw a plane void DrawPlane(Vector3 centerPos, Vector2 size, Color color) { From f49b2598dd3bfc3219d414f24558c68f7ebe9eb5 Mon Sep 17 00:00:00 2001 From: Horrowind Date: Mon, 11 Oct 2021 22:30:28 +0200 Subject: [PATCH 2/6] Modify examples/models/models_geometric_shapes.c to show the usage of DrawCylinder(Wires)Ex --- examples/models/models_geometric_shapes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/models/models_geometric_shapes.c b/examples/models/models_geometric_shapes.c index c1feb8027..19524f293 100644 --- a/examples/models/models_geometric_shapes.c +++ b/examples/models/models_geometric_shapes.c @@ -58,8 +58,8 @@ int main(void) DrawCylinderWires((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, DARKBLUE); DrawCylinderWires((Vector3){4.5f, -1.0f, 2.0f}, 1.0f, 1.0f, 2.0f, 6, BROWN); - DrawCylinder((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, GOLD); - DrawCylinderWires((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, PINK); + DrawCylinderEx((Vector3){1.0f, 0.0f, -4.0f}, (Vector3){-2.0f, 0.0f, -4.0f}, 1.5f, 0.0f, 8, GOLD); + DrawCylinderWiresEx((Vector3){1.0f, 0.0f, -4.0f}, (Vector3){-2.0f, 0.0f, -4.0f}, 1.5f, 0.0f, 8, PINK); DrawGrid(10, 1.0f); // Draw a grid From 4542b32e4ece9ddae775e7395d4219fa148039a8 Mon Sep 17 00:00:00 2001 From: Horrowind Date: Mon, 11 Oct 2021 22:40:33 +0200 Subject: [PATCH 3/6] Simplified DrawCylinder and DrawCylinderWires to use the -Ex versions. --- src/rmodels.c | 86 ++++----------------------------------------------- 1 file changed, 6 insertions(+), 80 deletions(-) diff --git a/src/rmodels.c b/src/rmodels.c index 708a25cf9..be329f244 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -712,59 +712,9 @@ void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Col // NOTE: It could be also used for pyramid and cone void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color) { - if (sides < 3) sides = 3; - - int numVertex = sides*6; - rlCheckRenderBatchLimit(numVertex); - - rlPushMatrix(); - rlTranslatef(position.x, position.y, position.z); - - rlBegin(RL_TRIANGLES); - rlColor4ub(color.r, color.g, color.b, color.a); - - if (radiusTop > 0) - { - // Draw Body ------------------------------------------------------------------------------------- - for (int i = 0; i < 360; i += 360/sides) - { - rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); //Bottom Left - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); //Bottom Right - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); //Top Right - - rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); //Top Left - rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); //Bottom Left - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); //Top Right - } - - // Draw Cap -------------------------------------------------------------------------------------- - for (int i = 0; i < 360; i += 360/sides) - { - rlVertex3f(0, height, 0); - rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); - } - } - else - { - // Draw Cone ------------------------------------------------------------------------------------- - for (int i = 0; i < 360; i += 360/sides) - { - rlVertex3f(0, height, 0); - rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); - } - } - - // Draw Base ----------------------------------------------------------------------------------------- - for (int i = 0; i < 360; i += 360/sides) - { - rlVertex3f(0, 0, 0); - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); - rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); - } - rlEnd(); - rlPopMatrix(); + Vector3 endPos = position; + endPos.y += height; + DrawCylinderEx(position, endPos, radiusBottom, radiusTop, sides, color); } // Draw a cylinder with base at startPos and top at endPos @@ -829,33 +779,9 @@ void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float e // NOTE: It could be also used for pyramid and cone void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color) { - if (sides < 3) sides = 3; - - int numVertex = sides*8; - rlCheckRenderBatchLimit(numVertex); - - rlPushMatrix(); - rlTranslatef(position.x, position.y, position.z); - - rlBegin(RL_LINES); - rlColor4ub(color.r, color.g, color.b, color.a); - - for (int i = 0; i < 360; i += 360/sides) - { - rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); - - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); - - rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); - rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); - - rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); - rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); - } - rlEnd(); - rlPopMatrix(); + Vector3 endPos = position; + endPos.y += height; + DrawCylinderWiresEx(position, endPos, radiusBottom, radiusTop, sides, color); } From bd6ab4630e91e307b449ce5ffd9e2634fa8ec526 Mon Sep 17 00:00:00 2001 From: Horrowind Date: Tue, 12 Oct 2021 14:50:13 +0200 Subject: [PATCH 4/6] This reverts commits f49b2598dd3bfc3219d414f24558c68f7ebe9eb5 and 4542b32e4ece9ddae775e7395d4219fa148039a8. --- examples/models/models_geometric_shapes.c | 4 +- src/rmodels.c | 86 +++++++++++++++++++++-- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/examples/models/models_geometric_shapes.c b/examples/models/models_geometric_shapes.c index 19524f293..c1feb8027 100644 --- a/examples/models/models_geometric_shapes.c +++ b/examples/models/models_geometric_shapes.c @@ -58,8 +58,8 @@ int main(void) DrawCylinderWires((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, DARKBLUE); DrawCylinderWires((Vector3){4.5f, -1.0f, 2.0f}, 1.0f, 1.0f, 2.0f, 6, BROWN); - DrawCylinderEx((Vector3){1.0f, 0.0f, -4.0f}, (Vector3){-2.0f, 0.0f, -4.0f}, 1.5f, 0.0f, 8, GOLD); - DrawCylinderWiresEx((Vector3){1.0f, 0.0f, -4.0f}, (Vector3){-2.0f, 0.0f, -4.0f}, 1.5f, 0.0f, 8, PINK); + DrawCylinder((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, GOLD); + DrawCylinderWires((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, PINK); DrawGrid(10, 1.0f); // Draw a grid diff --git a/src/rmodels.c b/src/rmodels.c index be329f244..708a25cf9 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -712,9 +712,59 @@ void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Col // NOTE: It could be also used for pyramid and cone void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color) { - Vector3 endPos = position; - endPos.y += height; - DrawCylinderEx(position, endPos, radiusBottom, radiusTop, sides, color); + if (sides < 3) sides = 3; + + int numVertex = sides*6; + rlCheckRenderBatchLimit(numVertex); + + rlPushMatrix(); + rlTranslatef(position.x, position.y, position.z); + + rlBegin(RL_TRIANGLES); + rlColor4ub(color.r, color.g, color.b, color.a); + + if (radiusTop > 0) + { + // Draw Body ------------------------------------------------------------------------------------- + for (int i = 0; i < 360; i += 360/sides) + { + rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); //Bottom Left + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); //Bottom Right + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); //Top Right + + rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); //Top Left + rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); //Bottom Left + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); //Top Right + } + + // Draw Cap -------------------------------------------------------------------------------------- + for (int i = 0; i < 360; i += 360/sides) + { + rlVertex3f(0, height, 0); + rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); + } + } + else + { + // Draw Cone ------------------------------------------------------------------------------------- + for (int i = 0; i < 360; i += 360/sides) + { + rlVertex3f(0, height, 0); + rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); + } + } + + // Draw Base ----------------------------------------------------------------------------------------- + for (int i = 0; i < 360; i += 360/sides) + { + rlVertex3f(0, 0, 0); + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); + rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); + } + rlEnd(); + rlPopMatrix(); } // Draw a cylinder with base at startPos and top at endPos @@ -779,9 +829,33 @@ void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float e // NOTE: It could be also used for pyramid and cone void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color) { - Vector3 endPos = position; - endPos.y += height; - DrawCylinderWiresEx(position, endPos, radiusBottom, radiusTop, sides, color); + if (sides < 3) sides = 3; + + int numVertex = sides*8; + rlCheckRenderBatchLimit(numVertex); + + rlPushMatrix(); + rlTranslatef(position.x, position.y, position.z); + + rlBegin(RL_LINES); + rlColor4ub(color.r, color.g, color.b, color.a); + + for (int i = 0; i < 360; i += 360/sides) + { + rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); + + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusBottom, 0, cosf(DEG2RAD*(i + 360.0f/sides))*radiusBottom); + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); + + rlVertex3f(sinf(DEG2RAD*(i + 360.0f/sides))*radiusTop, height, cosf(DEG2RAD*(i + 360.0f/sides))*radiusTop); + rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); + + rlVertex3f(sinf(DEG2RAD*i)*radiusTop, height, cosf(DEG2RAD*i)*radiusTop); + rlVertex3f(sinf(DEG2RAD*i)*radiusBottom, 0, cosf(DEG2RAD*i)*radiusBottom); + } + rlEnd(); + rlPopMatrix(); } From ede66df9c3d0fd6abb9cebeb0c2a1e9e7206a2cb Mon Sep 17 00:00:00 2001 From: Horrowind Date: Tue, 12 Oct 2021 19:33:00 +0200 Subject: [PATCH 5/6] Fixed formatting. Renamed base_angle to baseAngle. Remove most of the raymath.h calls. --- src/rmodels.c | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/rmodels.c b/src/rmodels.c index 708a25cf9..bfdebe3b8 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -777,30 +777,30 @@ void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float e rlCheckRenderBatchLimit(numVertex); if(sides < 3) sides = 3; - Vector3 difference = Vector3Subtract(endPos, startPos); + Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z }; // Construct a basis of the base and the top face: - Vector3 b1 = Vector3Normalize(Vector3Perpendicular(difference)); - Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, difference)); + Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction)); + Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, direction)); - float base_angle = (2.0 * PI) / sides; + float baseAngle = (2.0*PI)/sides; rlBegin(RL_TRIANGLES); rlColor4ub(color.r, color.g, color.b, color.a); for (int i = 0; i < sides; i++) { // compute the four vertices - float s1 = sinf(base_angle * (i+0)) * startRadius; - float c1 = cosf(base_angle * (i+0)) * startRadius; - Vector3 w1 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s1), Vector3Scale(b2, c1))); - float s2 = sinf(base_angle * (i+1)) * startRadius; - float c2 = cosf(base_angle * (i+1)) * startRadius; - Vector3 w2 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s2), Vector3Scale(b2, c2))); - float s3 = sinf(base_angle * (i+0)) * endRadius; - float c3 = cosf(base_angle * (i+0)) * endRadius; - Vector3 w3 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s3), Vector3Scale(b2, c3))); - float s4 = sinf(base_angle * (i+1)) * endRadius; - float c4 = cosf(base_angle * (i+1)) * endRadius; - Vector3 w4 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s4), Vector3Scale(b2, c4))); + float s1 = sinf(baseAngle*(i + 0))*startRadius; + float c1 = cosf(baseAngle*(i + 0))*startRadius; + Vector3 w1 = { startPos.x + s1*b1.x + c1*b2.x, startPos.y + s1*b1.y + c1*b2.y, startPos.z + s1*b1.z + c1*b2.z }; + float s2 = sinf(baseAngle*(i + 1))*startRadius; + float c2 = cosf(baseAngle*(i + 1))*startRadius; + Vector3 w2 = { startPos.x + s2*b1.x + c2*b2.x, startPos.y + s2*b1.y + c2*b2.y, startPos.z + s2*b1.z + c2*b2.z }; + float s3 = sinf(baseAngle*(i + 0))*endRadius; + float c3 = cosf(baseAngle*(i + 0))*endRadius; + Vector3 w3 = { endPos.x + s3*b1.x + c3*b2.x, endPos.y + s3*b1.y + c3*b2.y, endPos.z + s3*b1.z + c3*b2.z }; + float s4 = sinf(baseAngle*(i + 1))*endRadius; + float c4 = cosf(baseAngle*(i + 1))*endRadius; + Vector3 w4 = { endPos.x + s4*b1.x + c4*b2.x, endPos.y + s4*b1.y + c4*b2.y, endPos.z + s4*b1.z + c4*b2.z }; if (startRadius > 0) { // rlVertex3f(startPos.x, startPos.y, startPos.z); // | @@ -873,25 +873,25 @@ void DrawCylinderWiresEx(Vector3 startPos, Vector3 endPos, float startRadius, fl Vector3 b1 = Vector3Normalize(Vector3Perpendicular(difference)); Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, difference)); - float base_angle = (2.0 * PI) / sides; + float baseAngle = (2.0*PI)/sides; rlBegin(RL_LINES); rlColor4ub(color.r, color.g, color.b, color.a); for (int i = 0; i < sides; i++) { // compute the four vertices - float s1 = sinf(base_angle * (i+0)) * startRadius; - float c1 = cosf(base_angle * (i+0)) * startRadius; - Vector3 w1 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s1), Vector3Scale(b2, c1))); - float s2 = sinf(base_angle * (i+1)) * startRadius; - float c2 = cosf(base_angle * (i+1)) * startRadius; - Vector3 w2 = Vector3Add(startPos, Vector3Add(Vector3Scale(b1, s2), Vector3Scale(b2, c2))); - float s3 = sinf(base_angle * (i+0)) * endRadius; - float c3 = cosf(base_angle * (i+0)) * endRadius; - Vector3 w3 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s3), Vector3Scale(b2, c3))); - float s4 = sinf(base_angle * (i+1)) * endRadius; - float c4 = cosf(base_angle * (i+1)) * endRadius; - Vector3 w4 = Vector3Add(endPos, Vector3Add(Vector3Scale(b1, s4), Vector3Scale(b2, c4))); + float s1 = sinf(baseAngle*(i + 0))*startRadius; + float c1 = cosf(baseAngle*(i + 0))*startRadius; + Vector3 w1 = { startPos.x + s1*b1.x + c1*b2.x, startPos.y + s1*b1.y + c1*b2.y, startPos.z + s1*b1.z + c1*b2.z }; + float s2 = sinf(baseAngle*(i + 1))*startRadius; + float c2 = cosf(baseAngle*(i + 1))*startRadius; + Vector3 w2 = { startPos.x + s2*b1.x + c2*b2.x, startPos.y + s2*b1.y + c2*b2.y, startPos.z + s2*b1.z + c2*b2.z }; + float s3 = sinf(baseAngle*(i + 0))*endRadius; + float c3 = cosf(baseAngle*(i + 0))*endRadius; + Vector3 w3 = { endPos.x + s3*b1.x + c3*b2.x, endPos.y + s3*b1.y + c3*b2.y, endPos.z + s3*b1.z + c3*b2.z }; + float s4 = sinf(baseAngle*(i + 1))*endRadius; + float c4 = cosf(baseAngle*(i + 1))*endRadius; + Vector3 w4 = { endPos.x + s4*b1.x + c4*b2.x, endPos.y + s4*b1.y + c4*b2.y, endPos.z + s4*b1.z + c4*b2.z }; rlVertex3f(w1.x, w1.y, w1.z); rlVertex3f(w2.x, w2.y, w2.z); From 063671ee647c3bb16d412c2d1eb0a276c536cd64 Mon Sep 17 00:00:00 2001 From: Horrowind Date: Tue, 12 Oct 2021 19:38:29 +0200 Subject: [PATCH 6/6] Added check for empty cylinder. --- src/rmodels.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/rmodels.c b/src/rmodels.c index bfdebe3b8..fea1d4589 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -778,6 +778,10 @@ void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float e if(sides < 3) sides = 3; Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z }; + if(direction.x == direction.x && direction.y == direction.y && direction.z == direction.z) { + return; + } + // Construct a basis of the base and the top face: Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction)); Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, direction)); @@ -868,10 +872,14 @@ void DrawCylinderWiresEx(Vector3 startPos, Vector3 endPos, float startRadius, fl int numVertex = sides*6; rlCheckRenderBatchLimit(numVertex); - Vector3 difference = Vector3Subtract(endPos, startPos); + Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z }; + if(direction.x == direction.x && direction.y == direction.y && direction.z == direction.z) { + return; + } + // Construct a basis of the base and the top face: - Vector3 b1 = Vector3Normalize(Vector3Perpendicular(difference)); - Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, difference)); + Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction)); + Vector3 b2 = Vector3Normalize(Vector3CrossProduct(b1, direction)); float baseAngle = (2.0*PI)/sides;