render: improve implementation of dpoly()

* Honor dwindow settings immediately (avoids useless dline() calls)
* Bound to ymin/ymax instead of doing many useless cut computations
* Remove the need for floating-point operations and division
This commit is contained in:
Lephenixnoir 2024-03-01 22:26:39 +00:00
parent b802e8edef
commit e0ac25fbb0
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
3 changed files with 79 additions and 63 deletions

View file

@ -107,6 +107,7 @@ set(SOURCES_COMMON
src/render/dhline.c
src/render/dimage.c
src/render/dline.c
src/render/dpoly.c
src/render/dprint.c
src/render/drect_border.c
src/render/dtext.c

View file

@ -244,6 +244,21 @@ void dcircle(int xm, int ym, int r, int fill_color, int border_color);
void dellipse(int x1, int y1, int x2, int y2, int fill_color,
int border_color);
/* dpoly(): Render an arbitrary polygon
Renders the polygon defined by vertices (x[i], y[i]) for 0 < i < N. A fill
color and border color can be specified separately. For filling, N must be
at least 3. For border, N must be at least 2.
Note: this is a fairly slow function, not designed for rendering triangles
in 3D games. There are faster methods for that.
@x @y Arrays of vertex coordinates
@N Number of vertices (length of x and y)
@fill_color Color of the polygon's interior, C_NONE to disable
@border_color Color of the polygon's border, C_NONE to disable */
void dpoly(int const *x, int const *y, int N, int fill_color, int border_color);
//---
// Text rendering (topti)
//---

View file

@ -1,67 +1,67 @@
#include <gint/defs/util.h>
#include <gint/display.h>
#include <stdlib.h>
void dpoly(int *polyX, int *polyY, int polyCorners, int fill_color, int border_color){
if (polyCorners<=2) return;
if (fill_color == C_NONE && border_color == C_NONE)
return;
if (fill_color != C_NONE) {
float *nodeX, pixelX, pixelY, swap ;
int i, j, nodes;
nodeX = malloc( polyCorners * sizeof(float));
// Loop through the rows of the image.
for (pixelY=0; pixelY<DHEIGHT; pixelY++) {
// Build a list of nodes.
nodes=0; j=polyCorners-1;
for (i=0; i<polyCorners; i++) {
if ((polyY[i]<(float) pixelY && polyY[j]>=(float) pixelY)
|| (polyY[j]<(float) pixelY && polyY[i]>=(float) pixelY)) {
nodeX[nodes++]=(int) (polyX[i]+(pixelY-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i]));
static int compare(void const *ptr1, void const *ptr2)
{
return *(int const *)ptr1 - *(int const *)ptr2;
}
static void dpoly_fill(int const *x, int const *y, int N, int color)
{
int *nodeX = malloc(N * sizeof *nodeX);
/* Find vertical bounds */
int ymin = y[0], ymax = y[0];
for(int i = 1; i < N; i++)
{
ymin = min(ymin, y[i]);
ymax = max(ymax, y[i]);
}
ymin = max(ymin, dwindow.top);
ymax = min(ymax, dwindow.bottom);
/* For each row, find vertical cuts from the segments and fill in-between
the cuts. */
for(int pixelY = ymin; pixelY <= ymax; pixelY++) {
/* Build a list of nodes */
int nodes = 0;
int j = N - 1;
for(int i = 0; i < N; i++)
{
if((y[i] < pixelY) != (y[j] < pixelY))
nodeX[nodes++] = x[i]+(pixelY-y[i])*(x[j]-x[i])/(y[j]-y[i]);
j = i;
}
// Sort the nodes, via a simple “Bubble” sort.
i=0;
while (i<nodes-1) {
if (nodeX[i]>nodeX[i+1]) {
swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap; if (i) i--;
}
else {
i++;
}
}
/* Sort the cuts' positions */
qsort(nodeX, nodes, sizeof nodeX[0], compare);
// Fill the pixels between node pairs.
for (i=0; i<nodes; i+=2) {
if (nodeX[i ]>=DWIDTH) break;
if (nodeX[i+1]<= 0 ) break;
if (nodeX[i ]< 0 ) nodeX[i ]=0 ;
if (nodeX[i+1]> DWIDTH) nodeX[i+1]=DWIDTH;
//for (pixelX=nodeX[i]; pixelX<nodeX[i+1]; pixelX++) fillPixel(pixelX,pixelY);
dline(nodeX[i], pixelY, nodeX[i+1], pixelY, fill_color);
/* Fill the pixels between cut pairs */
for(int i = 0; i < nodes; i += 2)
{
if(nodeX[i] >= dwindow.right || nodeX[i+1] < dwindow.left) break;
nodeX[i] = max(nodeX[i], dwindow.left);
nodeX[i+1] = min(nodeX[i+1], dwindow.right);
dline(nodeX[i], pixelY, nodeX[i+1], pixelY, color);
}
}
free(nodeX);
}
if (border_color != C_NONE) {
for (int i = 0; i < polyCorners; i++) {
int px = polyX[i];
int py = polyY[i];
int px2 = polyX[(i + 1) % polyCorners];
int py2 = polyY[(i + 1) % polyCorners];
dline(px, py, px2, py2, border_color);
}
static void dpoly_border(int const *x, int const *y, int N, int color)
{
for(int i = 0; i < N - 1; i++)
dline(x[i], y[i], x[i+1], y[i+1], color);
dline(x[N-1], y[N-1], x[0], y[0], color);
}
return;
void dpoly(int const *x, int const *y, int N, int fill_color, int border_color)
{
if(N > 2 && fill_color != C_NONE)
dpoly_fill(x, y, N, fill_color);
if(N >= 2 && border_color != C_NONE)
dpoly_border(x, y, N, border_color);
}