mirror of
https://github.com/love2d/love.git
synced 2026-08-17 02:58:31 +02:00
Moved line and shape drawing code out of the opengl folder, since it no longer directly calls any OpenGL functions.
--HG-- branch : minor
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "Graphics.h"
|
||||
#include "font/Font.h"
|
||||
#include "Polyline.h"
|
||||
#include "graphics/Polyline.h"
|
||||
#include "math/MathModule.h"
|
||||
#include "window/Window.h"
|
||||
|
||||
@@ -1793,261 +1793,6 @@ void Graphics::points(const float *coords, const Colorf *colors, size_t numpoint
|
||||
}
|
||||
}
|
||||
|
||||
void Graphics::polyline(const float *coords, size_t count)
|
||||
{
|
||||
const DisplayState &state = states.back();
|
||||
float pixelsize = 1.0f / std::max((float) pixelScaleStack.back(), 0.000001f);
|
||||
|
||||
if (state.lineJoin == LINE_JOIN_NONE)
|
||||
{
|
||||
NoneJoinPolyline line;
|
||||
line.render(coords, count, state.lineWidth * .5f, pixelsize, state.lineStyle == LINE_SMOOTH);
|
||||
line.draw(this);
|
||||
}
|
||||
else if (state.lineJoin == LINE_JOIN_BEVEL)
|
||||
{
|
||||
BevelJoinPolyline line;
|
||||
line.render(coords, count, state.lineWidth * .5f, pixelsize, state.lineStyle == LINE_SMOOTH);
|
||||
line.draw(this);
|
||||
}
|
||||
else // LINE_JOIN_MITER
|
||||
{
|
||||
MiterJoinPolyline line;
|
||||
line.render(coords, count, state.lineWidth * .5f, pixelsize, state.lineStyle == LINE_SMOOTH);
|
||||
line.draw(this);
|
||||
}
|
||||
}
|
||||
|
||||
void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
|
||||
{
|
||||
float coords[] = {x,y, x,y+h, x+w,y+h, x+w,y, x,y};
|
||||
polygon(mode, coords, 5 * 2);
|
||||
}
|
||||
|
||||
void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points)
|
||||
{
|
||||
if (rx == 0 || ry == 0)
|
||||
{
|
||||
rectangle(mode, x, y, w, h);
|
||||
return;
|
||||
}
|
||||
|
||||
// Radius values that are more than half the rectangle's size aren't handled
|
||||
// correctly (for now)...
|
||||
if (w >= 0.02f)
|
||||
rx = std::min(rx, w / 2.0f - 0.01f);
|
||||
if (h >= 0.02f)
|
||||
ry = std::min(ry, h / 2.0f - 0.01f);
|
||||
|
||||
points = std::max(points / 4, 1);
|
||||
|
||||
const float half_pi = static_cast<float>(LOVE_M_PI / 2);
|
||||
float angle_shift = half_pi / ((float) points + 1.0f);
|
||||
|
||||
int num_coords = (points + 2) * 8;
|
||||
float *coords = getScratchBuffer<float>(num_coords + 2);
|
||||
float phi = .0f;
|
||||
|
||||
for (int i = 0; i <= points + 2; ++i, phi += angle_shift)
|
||||
{
|
||||
coords[2 * i + 0] = x + rx * (1 - cosf(phi));
|
||||
coords[2 * i + 1] = y + ry * (1 - sinf(phi));
|
||||
}
|
||||
|
||||
phi = half_pi;
|
||||
|
||||
for (int i = points + 2; i <= 2 * (points + 2); ++i, phi += angle_shift)
|
||||
{
|
||||
coords[2 * i + 0] = x + w - rx * (1 + cosf(phi));
|
||||
coords[2 * i + 1] = y + ry * (1 - sinf(phi));
|
||||
}
|
||||
|
||||
phi = 2 * half_pi;
|
||||
|
||||
for (int i = 2 * (points + 2); i <= 3 * (points + 2); ++i, phi += angle_shift)
|
||||
{
|
||||
coords[2 * i + 0] = x + w - rx * (1 + cosf(phi));
|
||||
coords[2 * i + 1] = y + h - ry * (1 + sinf(phi));
|
||||
}
|
||||
|
||||
phi = 3 * half_pi;
|
||||
|
||||
for (int i = 3 * (points + 2); i <= 4 * (points + 2); ++i, phi += angle_shift)
|
||||
{
|
||||
coords[2 * i + 0] = x + rx * (1 - cosf(phi));
|
||||
coords[2 * i + 1] = y + h - ry * (1 + sinf(phi));
|
||||
}
|
||||
|
||||
coords[num_coords + 0] = coords[0];
|
||||
coords[num_coords + 1] = coords[1];
|
||||
|
||||
polygon(mode, coords, num_coords + 2);
|
||||
}
|
||||
|
||||
int Graphics::calculateEllipsePoints(float rx, float ry) const
|
||||
{
|
||||
int points = (int) sqrtf(((rx + ry) / 2.0f) * 20.0f * (float) pixelScaleStack.back());
|
||||
return std::max(points, 8);
|
||||
}
|
||||
|
||||
void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry)
|
||||
{
|
||||
rectangle(mode, x, y, w, h, rx, ry, calculateEllipsePoints(rx, ry));
|
||||
}
|
||||
|
||||
void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
|
||||
{
|
||||
ellipse(mode, x, y, radius, radius, points);
|
||||
}
|
||||
|
||||
void Graphics::circle(DrawMode mode, float x, float y, float radius)
|
||||
{
|
||||
ellipse(mode, x, y, radius, radius);
|
||||
}
|
||||
|
||||
void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
|
||||
{
|
||||
float two_pi = (float) (LOVE_M_PI * 2);
|
||||
if (points <= 0) points = 1;
|
||||
float angle_shift = (two_pi / points);
|
||||
float phi = .0f;
|
||||
|
||||
float *coords = getScratchBuffer<float>(2 * (points + 1));
|
||||
for (int i = 0; i < points; ++i, phi += angle_shift)
|
||||
{
|
||||
coords[2*i+0] = x + a * cosf(phi);
|
||||
coords[2*i+1] = y + b * sinf(phi);
|
||||
}
|
||||
|
||||
coords[2*points+0] = coords[0];
|
||||
coords[2*points+1] = coords[1];
|
||||
|
||||
polygon(mode, coords, (points + 1) * 2);
|
||||
}
|
||||
|
||||
void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
|
||||
{
|
||||
ellipse(mode, x, y, a, b, calculateEllipsePoints(a, b));
|
||||
}
|
||||
|
||||
void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
|
||||
{
|
||||
// Nothing to display with no points or equal angles. (Or is there with line mode?)
|
||||
if (points <= 0 || angle1 == angle2)
|
||||
return;
|
||||
|
||||
// Oh, you want to draw a circle?
|
||||
if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
|
||||
{
|
||||
circle(drawmode, x, y, radius, points);
|
||||
return;
|
||||
}
|
||||
|
||||
float angle_shift = (angle2 - angle1) / points;
|
||||
// Bail on precision issues.
|
||||
if (angle_shift == 0.0)
|
||||
return;
|
||||
|
||||
// Prevent the connecting line from being drawn if a closed line arc has a
|
||||
// small angle. Avoids some visual issues when connected lines are at sharp
|
||||
// angles, due to the miter line join drawing code.
|
||||
if (drawmode == DRAW_LINE && arcmode == ARC_CLOSED && fabsf(angle1 - angle2) < LOVE_TORAD(4))
|
||||
arcmode = ARC_OPEN;
|
||||
|
||||
// Quick fix for the last part of a filled open arc not being drawn (because
|
||||
// polygon(DRAW_FILL, ...) doesn't work without a closed loop of vertices.)
|
||||
if (drawmode == DRAW_FILL && arcmode == ARC_OPEN)
|
||||
arcmode = ARC_CLOSED;
|
||||
|
||||
float phi = angle1;
|
||||
|
||||
float *coords = nullptr;
|
||||
int num_coords = 0;
|
||||
|
||||
const auto createPoints = [&](float *coordinates)
|
||||
{
|
||||
for (int i = 0; i <= points; ++i, phi += angle_shift)
|
||||
{
|
||||
coordinates[2 * i + 0] = x + radius * cosf(phi);
|
||||
coordinates[2 * i + 1] = y + radius * sinf(phi);
|
||||
}
|
||||
};
|
||||
|
||||
if (arcmode == ARC_PIE)
|
||||
{
|
||||
num_coords = (points + 3) * 2;
|
||||
coords = getScratchBuffer<float>(num_coords);
|
||||
|
||||
coords[0] = coords[num_coords - 2] = x;
|
||||
coords[1] = coords[num_coords - 1] = y;
|
||||
|
||||
createPoints(coords + 2);
|
||||
}
|
||||
else if (arcmode == ARC_OPEN)
|
||||
{
|
||||
num_coords = (points + 1) * 2;
|
||||
coords = getScratchBuffer<float>(num_coords);
|
||||
|
||||
createPoints(coords);
|
||||
}
|
||||
else // ARC_CLOSED
|
||||
{
|
||||
num_coords = (points + 2) * 2;
|
||||
coords = getScratchBuffer<float>(num_coords);
|
||||
|
||||
createPoints(coords);
|
||||
|
||||
// Connect the ends of the arc.
|
||||
coords[num_coords - 2] = coords[0];
|
||||
coords[num_coords - 1] = coords[1];
|
||||
}
|
||||
|
||||
polygon(drawmode, coords, num_coords);
|
||||
}
|
||||
|
||||
void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2)
|
||||
{
|
||||
float points = (float) calculateEllipsePoints(radius, radius);
|
||||
|
||||
// The amount of points is based on the fraction of the circle created by the arc.
|
||||
float angle = fabsf(angle1 - angle2);
|
||||
if (angle < 2.0f * (float) LOVE_M_PI)
|
||||
points *= angle / (2.0f * (float) LOVE_M_PI);
|
||||
|
||||
arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
|
||||
}
|
||||
|
||||
/// @param mode the draw mode
|
||||
/// @param coords the coordinate array
|
||||
/// @param count the number of coordinates/size of the array
|
||||
void Graphics::polygon(DrawMode mode, const float *coords, size_t count)
|
||||
{
|
||||
// coords is an array of a closed loop of vertices, i.e.
|
||||
// coords[count-2] = coords[0], coords[count-1] = coords[1]
|
||||
if (mode == DRAW_LINE)
|
||||
{
|
||||
polyline(coords, count);
|
||||
}
|
||||
else
|
||||
{
|
||||
StreamDrawRequest req;
|
||||
req.formats[0] = vertex::CommonFormat::XYf;
|
||||
req.formats[1] = vertex::CommonFormat::RGBAub;
|
||||
req.indexMode = vertex::TriangleIndexMode::FAN;
|
||||
req.vertexCount = (int)count/2 - 1;
|
||||
|
||||
StreamVertexData data = requestStreamDraw(req);
|
||||
|
||||
const Matrix4 &t = getTransform();
|
||||
t.transform((Vector *) data.stream[0], (const Vector *) coords, req.vertexCount);
|
||||
|
||||
Color c = toColor(getColor());
|
||||
Color *colordata = (Color *) data.stream[1];
|
||||
for (int i = 0; i < req.vertexCount; i++)
|
||||
colordata[i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
Graphics::RendererInfo Graphics::getRendererInfo() const
|
||||
{
|
||||
RendererInfo info;
|
||||
|
||||
@@ -279,25 +279,13 @@ public:
|
||||
void setLineStyle(LineStyle style);
|
||||
|
||||
/**
|
||||
* Sets the line style.
|
||||
* @param style LINE_ROUGH or LINE_SMOOTH.
|
||||
* Sets the line join mode.
|
||||
**/
|
||||
void setLineJoin(LineJoin style);
|
||||
|
||||
/**
|
||||
* Gets the line width.
|
||||
**/
|
||||
float getLineWidth() const;
|
||||
|
||||
/**
|
||||
* Gets the line style.
|
||||
**/
|
||||
LineStyle getLineStyle() const;
|
||||
|
||||
/**
|
||||
* Gets the line style.
|
||||
**/
|
||||
LineJoin getLineJoin() const;
|
||||
float getLineWidth() const override;
|
||||
LineStyle getLineStyle() const override;
|
||||
LineJoin getLineJoin() const override;
|
||||
|
||||
/**
|
||||
* Sets the size of points.
|
||||
@@ -342,81 +330,6 @@ public:
|
||||
**/
|
||||
void points(const float *coords, const Colorf *colors, size_t numpoints);
|
||||
|
||||
/**
|
||||
* Draws a series of lines connecting the given vertices.
|
||||
* @param coords Vertex components (x1, y1, ..., xn, yn). If x1,y1 == xn,yn the line will be drawn closed.
|
||||
* @param count Number of items in the array, i.e. count = 2 * n
|
||||
**/
|
||||
void polyline(const float *coords, size_t count);
|
||||
|
||||
/**
|
||||
* Draws a rectangle.
|
||||
* @param x Position along x-axis for top-left corner.
|
||||
* @param y Position along y-axis for top-left corner.
|
||||
* @param w The width of the rectangle.
|
||||
* @param h The height of the rectangle.
|
||||
**/
|
||||
void rectangle(DrawMode mode, float x, float y, float w, float h);
|
||||
|
||||
/**
|
||||
* Variant of rectangle that draws a rounded rectangle.
|
||||
* @param mode The mode of drawing (line/filled).
|
||||
* @param x X-coordinate of top-left corner
|
||||
* @param y Y-coordinate of top-left corner
|
||||
* @param w The width of the rectangle.
|
||||
* @param h The height of the rectangle.
|
||||
* @param rx The radius of the corners on the x axis
|
||||
* @param ry The radius of the corners on the y axis
|
||||
* @param points The number of points to use per corner
|
||||
**/
|
||||
void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points);
|
||||
void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry);
|
||||
|
||||
/**
|
||||
* Draws a circle using the specified arguments.
|
||||
* @param mode The mode of drawing (line/filled).
|
||||
* @param x X-coordinate.
|
||||
* @param y Y-coordinate.
|
||||
* @param radius Radius of the circle.
|
||||
* @param points Number of points to use to draw the circle.
|
||||
**/
|
||||
void circle(DrawMode mode, float x, float y, float radius, int points);
|
||||
void circle(DrawMode mode, float x, float y, float radius);
|
||||
|
||||
/**
|
||||
* Draws an ellipse using the specified arguments.
|
||||
* @param mode The mode of drawing (line/filled).
|
||||
* @param x X-coordinate of center
|
||||
* @param y Y-coordinate of center
|
||||
* @param a Radius in x-direction
|
||||
* @param b Radius in y-direction
|
||||
* @param points Number of points to use to draw the circle.
|
||||
**/
|
||||
void ellipse(DrawMode mode, float x, float y, float a, float b, int points);
|
||||
void ellipse(DrawMode mode, float x, float y, float a, float b);
|
||||
|
||||
/**
|
||||
* Draws an arc using the specified arguments.
|
||||
* @param drawmode The mode of drawing (line/filled).
|
||||
* @param arcmode The type of arc.
|
||||
* @param x X-coordinate.
|
||||
* @param y Y-coordinate.
|
||||
* @param radius Radius of the arc.
|
||||
* @param angle1 The angle at which the arc begins.
|
||||
* @param angle2 The angle at which the arc terminates.
|
||||
* @param points Number of points to use to draw the arc.
|
||||
**/
|
||||
void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points);
|
||||
void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2);
|
||||
|
||||
/**
|
||||
* Draws a polygon with an arbitrary number of vertices.
|
||||
* @param mode The type of drawing (line/filled).
|
||||
* @param coords Vertex components (x1, y1, x2, y2, etc.)
|
||||
* @param count Coord array size
|
||||
**/
|
||||
void polygon(DrawMode mode, const float *coords, size_t count);
|
||||
|
||||
void captureScreenshot(const ScreenshotInfo &info);
|
||||
|
||||
/**
|
||||
@@ -516,12 +429,8 @@ private:
|
||||
|
||||
void checkSetDefaultFont();
|
||||
|
||||
int calculateEllipsePoints(float rx, float ry) const;
|
||||
|
||||
StrongRef<Font> defaultFont;
|
||||
|
||||
std::vector<double> pixelScaleStack;
|
||||
|
||||
std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
|
||||
|
||||
std::unordered_map<uint32, GLuint> framebufferObjects;
|
||||
|
||||
@@ -1,419 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2016 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "Polyline.h"
|
||||
#include "graphics/Graphics.h"
|
||||
|
||||
// C++
|
||||
#include <algorithm>
|
||||
|
||||
// treat adjacent segments with angles between their directions <5 degree as straight
|
||||
static const float LINES_PARALLEL_EPS = 0.05f;
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace graphics
|
||||
{
|
||||
namespace opengl
|
||||
{
|
||||
|
||||
void Polyline::render(const float *coords, size_t count, size_t size_hint, float halfwidth, float pixel_size, bool draw_overdraw)
|
||||
{
|
||||
static std::vector<Vector> anchors;
|
||||
anchors.clear();
|
||||
anchors.reserve(size_hint);
|
||||
|
||||
static std::vector<Vector> normals;
|
||||
normals.clear();
|
||||
normals.reserve(size_hint);
|
||||
|
||||
// prepare vertex arrays
|
||||
if (draw_overdraw)
|
||||
halfwidth -= pixel_size * 0.3f;
|
||||
|
||||
// compute sleeve
|
||||
bool is_looping = (coords[0] == coords[count - 2]) && (coords[1] == coords[count - 1]);
|
||||
Vector s;
|
||||
if (!is_looping) // virtual starting point at second point mirrored on first point
|
||||
s = Vector(coords[2] - coords[0], coords[3] - coords[1]);
|
||||
else // virtual starting point at last vertex
|
||||
s = Vector(coords[0] - coords[count - 4], coords[1] - coords[count - 3]);
|
||||
|
||||
float len_s = s.getLength();
|
||||
Vector ns = s.getNormal(halfwidth / len_s);
|
||||
|
||||
Vector q, r(coords[0], coords[1]);
|
||||
for (size_t i = 0; i + 3 < count; i += 2)
|
||||
{
|
||||
q = r;
|
||||
r = Vector(coords[i + 2], coords[i + 3]);
|
||||
renderEdge(anchors, normals, s, len_s, ns, q, r, halfwidth);
|
||||
}
|
||||
|
||||
q = r;
|
||||
r = is_looping ? Vector(coords[2], coords[3]) : r + s;
|
||||
renderEdge(anchors, normals, s, len_s, ns, q, r, halfwidth);
|
||||
|
||||
vertex_count = normals.size();
|
||||
|
||||
size_t extra_vertices = 0;
|
||||
|
||||
if (draw_overdraw)
|
||||
{
|
||||
calc_overdraw_vertex_count(is_looping);
|
||||
|
||||
// When drawing overdraw lines using triangle strips, we want to add an
|
||||
// extra degenerate triangle in between the core line and the overdraw
|
||||
// line in order to break up the strip into two. This will let us draw
|
||||
// everything in one draw call.
|
||||
if (triangle_mode == vertex::TriangleIndexMode::STRIP)
|
||||
extra_vertices = 2;
|
||||
}
|
||||
|
||||
// Use a single linear array for both the regular and overdraw vertices.
|
||||
vertices = new Vector[vertex_count + extra_vertices + overdraw_vertex_count];
|
||||
|
||||
for (size_t i = 0; i < vertex_count; ++i)
|
||||
vertices[i] = anchors[i] + normals[i];
|
||||
|
||||
if (draw_overdraw)
|
||||
{
|
||||
overdraw = vertices + vertex_count + extra_vertices;
|
||||
overdraw_vertex_start = vertex_count + extra_vertices;
|
||||
render_overdraw(normals, pixel_size, is_looping);
|
||||
}
|
||||
|
||||
// Add the degenerate triangle strip.
|
||||
if (extra_vertices)
|
||||
{
|
||||
vertices[vertex_count + 0] = vertices[vertex_count - 1];
|
||||
vertices[vertex_count + 1] = vertices[overdraw_vertex_start];
|
||||
}
|
||||
}
|
||||
|
||||
void NoneJoinPolyline::renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw)
|
||||
{
|
||||
// ns1------ns2
|
||||
// | |
|
||||
// q ------ r
|
||||
// | |
|
||||
// (-ns1)----(-ns2)
|
||||
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
normals.push_back(ns);
|
||||
normals.push_back(-ns);
|
||||
|
||||
s = (r - q);
|
||||
len_s = s.getLength();
|
||||
ns = s.getNormal(hw / len_s);
|
||||
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
normals.push_back(ns);
|
||||
normals.push_back(-ns);
|
||||
}
|
||||
|
||||
|
||||
/** Calculate line boundary points.
|
||||
*
|
||||
* Sketch:
|
||||
*
|
||||
* u1
|
||||
* -------------+---...___
|
||||
* | ```'''-- ---
|
||||
* p- - - - - - q- - . _ _ | w/2
|
||||
* | ` ' ' r +
|
||||
* -------------+---...___ | w/2
|
||||
* u2 ```'''-- ---
|
||||
*
|
||||
* u1 and u2 depend on four things:
|
||||
* - the half line width w/2
|
||||
* - the previous line vertex p
|
||||
* - the current line vertex q
|
||||
* - the next line vertex r
|
||||
*
|
||||
* u1/u2 are the intersection points of the parallel lines to p-q and q-r,
|
||||
* i.e. the point where
|
||||
*
|
||||
* (q + w/2 * ns) + lambda * (q - p) = (q + w/2 * nt) + mu * (r - q) (u1)
|
||||
* (q - w/2 * ns) + lambda * (q - p) = (q - w/2 * nt) + mu * (r - q) (u2)
|
||||
*
|
||||
* with nt,nt being the normals on the segments s = p-q and t = q-r,
|
||||
*
|
||||
* ns = perp(s) / |s|
|
||||
* nt = perp(t) / |t|.
|
||||
*
|
||||
* Using the linear equation system (similar for u2)
|
||||
*
|
||||
* q + w/2 * ns + lambda * s - (q + w/2 * nt + mu * t) = 0 (u1)
|
||||
* <=> q-q + lambda * s - mu * t = (nt - ns) * w/2
|
||||
* <=> lambda * s - mu * t = (nt - ns) * w/2
|
||||
*
|
||||
* the intersection points can be efficiently calculated using Cramer's rule.
|
||||
*/
|
||||
void MiterJoinPolyline::renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw)
|
||||
{
|
||||
Vector t = (r - q);
|
||||
float len_t = t.getLength();
|
||||
Vector nt = t.getNormal(hw / len_t);
|
||||
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
|
||||
float det = s ^ t;
|
||||
if (fabs(det) / (len_s * len_t) < LINES_PARALLEL_EPS && s * t > 0)
|
||||
{
|
||||
// lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2
|
||||
normals.push_back(ns);
|
||||
normals.push_back(-ns);
|
||||
}
|
||||
else
|
||||
{
|
||||
// cramers rule
|
||||
float lambda = ((nt - ns) ^ t) / det;
|
||||
Vector d = ns + s * lambda;
|
||||
normals.push_back(d);
|
||||
normals.push_back(-d);
|
||||
}
|
||||
|
||||
s = t;
|
||||
ns = nt;
|
||||
len_s = len_t;
|
||||
}
|
||||
|
||||
/** Calculate line boundary points.
|
||||
*
|
||||
* Sketch:
|
||||
*
|
||||
* uh1___uh2
|
||||
* .' '.
|
||||
* .' q '.
|
||||
* .' ' ' '.
|
||||
*.' ' .'. ' '.
|
||||
* ' .' ul'. '
|
||||
* p .' '. r
|
||||
*
|
||||
*
|
||||
* ul can be found as above, uh1 and uh2 are much simpler:
|
||||
*
|
||||
* uh1 = q + ns * w/2, uh2 = q + nt * w/2
|
||||
*/
|
||||
void BevelJoinPolyline::renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw)
|
||||
{
|
||||
Vector t = (r - q);
|
||||
float len_t = t.getLength();
|
||||
|
||||
float det = s ^ t;
|
||||
if (fabs(det) / (len_s * len_t) < LINES_PARALLEL_EPS && s * t > 0)
|
||||
{
|
||||
// lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2
|
||||
Vector n = t.getNormal(hw / len_t);
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
normals.push_back(n);
|
||||
normals.push_back(-n);
|
||||
s = t;
|
||||
len_s = len_t;
|
||||
return; // early out
|
||||
}
|
||||
|
||||
// cramers rule
|
||||
Vector nt= t.getNormal(hw / len_t);
|
||||
float lambda = ((nt - ns) ^ t) / det;
|
||||
Vector d = ns + s * lambda;
|
||||
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
anchors.push_back(q);
|
||||
if (det > 0) // 'left' turn -> intersection on the top
|
||||
{
|
||||
normals.push_back(d);
|
||||
normals.push_back(-ns);
|
||||
normals.push_back(d);
|
||||
normals.push_back(-nt);
|
||||
}
|
||||
else
|
||||
{
|
||||
normals.push_back(ns);
|
||||
normals.push_back(-d);
|
||||
normals.push_back(nt);
|
||||
normals.push_back(-d);
|
||||
}
|
||||
s = t;
|
||||
len_s = len_t;
|
||||
ns = nt;
|
||||
}
|
||||
|
||||
void Polyline::calc_overdraw_vertex_count(bool is_looping)
|
||||
{
|
||||
overdraw_vertex_count = 2 * vertex_count + (is_looping ? 0 : 2);
|
||||
}
|
||||
|
||||
void Polyline::render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping)
|
||||
{
|
||||
// upper segment
|
||||
for (size_t i = 0; i + 1 < vertex_count; i += 2)
|
||||
{
|
||||
overdraw[i] = vertices[i];
|
||||
overdraw[i+1] = vertices[i] + normals[i] * (pixel_size / normals[i].getLength());
|
||||
}
|
||||
// lower segment
|
||||
for (size_t i = 0; i + 1 < vertex_count; i += 2)
|
||||
{
|
||||
size_t k = vertex_count - i - 1;
|
||||
overdraw[vertex_count + i] = vertices[k];
|
||||
overdraw[vertex_count + i+1] = vertices[k] + normals[k] * (pixel_size / normals[i].getLength());
|
||||
}
|
||||
|
||||
// if not looping, the outer overdraw vertices need to be displaced
|
||||
// to cover the line endings, i.e.:
|
||||
// +- - - - //- - + +- - - - - //- - - +
|
||||
// +-------//-----+ : +-------//-----+ :
|
||||
// | core // line | --> : | core // line | :
|
||||
// +-----//-------+ : +-----//-------+ :
|
||||
// +- - //- - - - + +- - - //- - - - - +
|
||||
if (!is_looping)
|
||||
{
|
||||
// left edge
|
||||
Vector spacer = (overdraw[1] - overdraw[3]);
|
||||
spacer.normalize(pixel_size);
|
||||
overdraw[1] += spacer;
|
||||
overdraw[overdraw_vertex_count - 3] += spacer;
|
||||
|
||||
// right edge
|
||||
spacer = (overdraw[vertex_count-1] - overdraw[vertex_count-3]);
|
||||
spacer.normalize(pixel_size);
|
||||
overdraw[vertex_count-1] += spacer;
|
||||
overdraw[vertex_count+1] += spacer;
|
||||
|
||||
// we need to draw two more triangles to close the
|
||||
// overdraw at the line start.
|
||||
overdraw[overdraw_vertex_count-2] = overdraw[0];
|
||||
overdraw[overdraw_vertex_count-1] = overdraw[1];
|
||||
}
|
||||
}
|
||||
|
||||
void NoneJoinPolyline::calc_overdraw_vertex_count(bool /*is_looping*/)
|
||||
{
|
||||
overdraw_vertex_count = 4 * (vertex_count-2); // less than ideal
|
||||
}
|
||||
|
||||
void NoneJoinPolyline::render_overdraw(const std::vector<Vector> &/*normals*/, float pixel_size, bool /*is_looping*/)
|
||||
{
|
||||
for (size_t i = 2; i + 3 < vertex_count; i += 4)
|
||||
{
|
||||
// v0-v2
|
||||
// | / | <- main quad line
|
||||
// v1-v3
|
||||
|
||||
Vector s = vertices[i+0] - vertices[i+2];
|
||||
Vector t = vertices[i+0] - vertices[i+1];
|
||||
s.normalize(pixel_size);
|
||||
t.normalize(pixel_size);
|
||||
|
||||
const size_t k = 4 * (i - 2);
|
||||
|
||||
overdraw[k+0] = vertices[i+0];
|
||||
overdraw[k+1] = vertices[i+1];
|
||||
overdraw[k+2] = vertices[i+0] + s + t;
|
||||
overdraw[k+3] = vertices[i+1] + s - t;
|
||||
|
||||
overdraw[k+4] = vertices[i+1];
|
||||
overdraw[k+5] = vertices[i+3];
|
||||
overdraw[k+6] = vertices[i+1] + s - t;
|
||||
overdraw[k+7] = vertices[i+3] - s - t;
|
||||
|
||||
overdraw[k+ 8] = vertices[i+3];
|
||||
overdraw[k+ 9] = vertices[i+2];
|
||||
overdraw[k+10] = vertices[i+3] - s - t;
|
||||
overdraw[k+11] = vertices[i+2] - s + t;
|
||||
|
||||
overdraw[k+12] = vertices[i+2];
|
||||
overdraw[k+13] = vertices[i+0];
|
||||
overdraw[k+14] = vertices[i+2] - s + t;
|
||||
overdraw[k+15] = vertices[i+0] + s + t;
|
||||
}
|
||||
}
|
||||
|
||||
Polyline::~Polyline()
|
||||
{
|
||||
if (vertices)
|
||||
delete[] vertices;
|
||||
}
|
||||
|
||||
void Polyline::draw(love::graphics::Graphics *gfx)
|
||||
{
|
||||
int total_vertex_count = (int) vertex_count;
|
||||
if (overdraw)
|
||||
total_vertex_count = (int) (overdraw_vertex_start + overdraw_vertex_count);
|
||||
|
||||
Graphics::StreamDrawRequest req;
|
||||
req.formats[0] = vertex::CommonFormat::XYf;
|
||||
req.formats[1] = vertex::CommonFormat::RGBAub;
|
||||
req.indexMode = triangle_mode;
|
||||
req.vertexCount = total_vertex_count;
|
||||
|
||||
Graphics::StreamVertexData data = gfx->requestStreamDraw(req);
|
||||
|
||||
const Matrix4 &t = gfx->getTransform();
|
||||
t.transform((Vector *) data.stream[0], vertices, total_vertex_count);
|
||||
|
||||
Color curcolor = toColor(gfx->getColor());
|
||||
Color *colordata = (Color *) data.stream[1];
|
||||
|
||||
for (int i = 0; i < (int) vertex_count; i++)
|
||||
colordata[i] = curcolor;
|
||||
|
||||
if (overdraw)
|
||||
fill_color_array(curcolor, colordata + overdraw_vertex_start);
|
||||
}
|
||||
|
||||
void Polyline::fill_color_array(Color constant_color, Color *colors)
|
||||
{
|
||||
for (size_t i = 0; i < overdraw_vertex_count; ++i)
|
||||
{
|
||||
Color c = constant_color;
|
||||
c.a *= (i+1) % 2; // avoids branching. equiv to if (i%2 == 1) c.a = 0;
|
||||
colors[i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
void NoneJoinPolyline::fill_color_array(Color constant_color, Color *colors)
|
||||
{
|
||||
for (size_t i = 0; i < overdraw_vertex_count; ++i)
|
||||
{
|
||||
Color c = constant_color;
|
||||
c.a *= (i & 3) < 2; // if (i % 4 == 2 || i % 4 == 3) c.a = 0
|
||||
colors[i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
} // opengl
|
||||
} // graphics
|
||||
} // love
|
||||
@@ -1,195 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2016 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_GRAPHICS_OPENGL_POLYLINE_H
|
||||
#define LOVE_GRAPHICS_OPENGL_POLYLINE_H
|
||||
|
||||
// LOVE
|
||||
#include "common/config.h"
|
||||
#include "common/Vector.h"
|
||||
#include "graphics/vertex.h"
|
||||
|
||||
// C++
|
||||
#include <vector>
|
||||
#include <string.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace graphics
|
||||
{
|
||||
|
||||
class Graphics;
|
||||
|
||||
namespace opengl
|
||||
{
|
||||
|
||||
/**
|
||||
* Abstract base class for a chain of segments.
|
||||
* @author Matthias Richter
|
||||
**/
|
||||
class Polyline
|
||||
{
|
||||
public:
|
||||
|
||||
Polyline(vertex::TriangleIndexMode mode = vertex::TriangleIndexMode::STRIP)
|
||||
: vertices(nullptr)
|
||||
, overdraw(nullptr)
|
||||
, vertex_count(0)
|
||||
, overdraw_vertex_count(0)
|
||||
, triangle_mode(mode)
|
||||
, overdraw_vertex_start(0)
|
||||
{}
|
||||
|
||||
virtual ~Polyline();
|
||||
|
||||
/**
|
||||
* @param vertices Vertices defining the core line segments
|
||||
* @param count Number of coordinates (= size of the array vertices)
|
||||
* @param size_hint Expected number of vertices of the rendering sleeve around the core line.
|
||||
* @param halfwidth linewidth / 2.
|
||||
* @param pixel_size Dimension of one pixel on the screen in world coordinates.
|
||||
* @param draw_overdraw Fake antialias the line.
|
||||
*/
|
||||
void render(const float *vertices, size_t count, size_t size_hint, float halfwidth, float pixel_size, bool draw_overdraw);
|
||||
|
||||
/** Draws the line on the screen
|
||||
*/
|
||||
void draw(love::graphics::Graphics *gfx);
|
||||
|
||||
protected:
|
||||
|
||||
virtual void calc_overdraw_vertex_count(bool is_looping);
|
||||
virtual void render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping);
|
||||
virtual void fill_color_array(Color constant_color, Color *colors);
|
||||
|
||||
/** Calculate line boundary points.
|
||||
*
|
||||
* @param[out] anchors Anchor points defining the core line.
|
||||
* @param[out] normals Normals defining the edge of the sleeve.
|
||||
* @param[in,out] s Direction of segment pq (updated to the segment qr).
|
||||
* @param[in,out] len_s Length of segment pq (updated to the segment qr).
|
||||
* @param[in,out] ns Normal on the segment pq (updated to the segment qr).
|
||||
* @param[in] q Current point on the line.
|
||||
* @param[in] r Next point on the line.
|
||||
* @param[in] hw Half line width (see Polyline.render()).
|
||||
*/
|
||||
virtual void renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw) = 0;
|
||||
|
||||
Vector *vertices;
|
||||
Vector *overdraw;
|
||||
size_t vertex_count;
|
||||
size_t overdraw_vertex_count;
|
||||
vertex::TriangleIndexMode triangle_mode;
|
||||
size_t overdraw_vertex_start;
|
||||
|
||||
}; // Polyline
|
||||
|
||||
|
||||
/**
|
||||
* A Polyline whose segments are not connected.
|
||||
* @author Matthias Richter
|
||||
*/
|
||||
class NoneJoinPolyline : public Polyline
|
||||
{
|
||||
public:
|
||||
|
||||
NoneJoinPolyline()
|
||||
: Polyline(vertex::TriangleIndexMode::QUADS)
|
||||
{}
|
||||
|
||||
void render(const float *vertices, size_t count, float halfwidth, float pixel_size, bool draw_overdraw)
|
||||
{
|
||||
Polyline::render(vertices, count, 2 * count - 4, halfwidth, pixel_size, draw_overdraw);
|
||||
|
||||
// discard the first and last two vertices. (these are redundant)
|
||||
for (size_t i = 0; i < vertex_count - 4; ++i)
|
||||
this->vertices[i] = this->vertices[i+2];
|
||||
|
||||
// The last quad is now garbage, so zero it out to make sure it doesn't
|
||||
// get rasterized. These vertices are in between the core line vertices
|
||||
// and the overdraw vertices in the combined vertex array, so they still
|
||||
// get "rendered" since we draw everything with one draw call.
|
||||
memset(&this->vertices[vertex_count - 4], 0, sizeof(love::Vector) * 4);
|
||||
|
||||
vertex_count -= 4;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void calc_overdraw_vertex_count(bool is_looping);
|
||||
virtual void render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping);
|
||||
virtual void fill_color_array(Color constant_color, Color *colors);
|
||||
virtual void renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw);
|
||||
|
||||
}; // NoneJoinPolyline
|
||||
|
||||
|
||||
/**
|
||||
* A Polyline whose segments are connected by a sharp edge.
|
||||
* @author Matthias Richter
|
||||
*/
|
||||
class MiterJoinPolyline : public Polyline
|
||||
{
|
||||
public:
|
||||
|
||||
void render(const float *vertices, size_t count, float halfwidth, float pixel_size, bool draw_overdraw)
|
||||
{
|
||||
Polyline::render(vertices, count, count, halfwidth, pixel_size, draw_overdraw);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw);
|
||||
|
||||
}; // MiterJoinPolyline
|
||||
|
||||
|
||||
/**
|
||||
* A Polyline whose segments are connected by a flat edge.
|
||||
* @author Matthias Richter
|
||||
*/
|
||||
class BevelJoinPolyline : public Polyline
|
||||
{
|
||||
public:
|
||||
|
||||
void render(const float *vertices, size_t count, float halfwidth, float pixel_size, bool draw_overdraw)
|
||||
{
|
||||
Polyline::render(vertices, count, 2 * count - 4, halfwidth, pixel_size, draw_overdraw);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
|
||||
Vector &s, float &len_s, Vector &ns,
|
||||
const Vector &q, const Vector &r, float hw);
|
||||
|
||||
}; // BevelJoinPolyline
|
||||
|
||||
} // opengl
|
||||
} // graphics
|
||||
} // love
|
||||
|
||||
#endif // LOVE_GRAPHICS_OPENGL_POLYLINE_H
|
||||
Reference in New Issue
Block a user