Skip to content

Commit 5f92a0f

Browse files
committed
impl face culling
1 parent dcd60be commit 5f92a0f

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/external/rlsw.h

+40-10
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ typedef enum {
145145
SW_LINES = GL_LINES,
146146
SW_TRIANGLES = GL_TRIANGLES,
147147
SW_QUADS = GL_QUADS,
148-
} SWfill;
148+
} SWdraw;
149149

150150
typedef enum {
151151
SW_FRONT = GL_FRONT,
@@ -232,7 +232,9 @@ void swViewport(int x, int y, int width, int height);
232232
void swClearColor(float r, float g, float b, float a);
233233
void swClear(void);
234234

235-
void swBegin(SWfill mode);
235+
void swCullFace(SWface face);
236+
237+
void swBegin(SWdraw mode);
236238
void swEnd(void);
237239

238240
void swVertex2i(int x, int y);
@@ -270,7 +272,7 @@ void swNormal3f(float x, float y, float z);
270272
void swNormal3fv(const float* v);
271273

272274
void swBindArray(SWarray type, void *buffer);
273-
void swDrawArrays(SWfill mode, int offset, int count);
275+
void swDrawArrays(SWdraw mode, int offset, int count);
274276

275277
uint32_t swLoadTexture(const void *data, int width, int height, int format, int mipmapCount);
276278
void swUnloadTexture(uint32_t id);
@@ -370,7 +372,7 @@ typedef struct {
370372
sw_vertex_t vertexBuffer[4]; // Buffer used for storing primitive vertices, used for processing and rendering
371373
int vertexCounter; // Number of vertices in 'ctx.vertexBuffer'
372374

373-
SWfill fillMode; // Current polygon filling mode (e.g., lines, triangles)
375+
SWdraw drawMode; // Current polygon filling mode (e.g., lines, triangles)
374376
float pointSize; // Rasterized point size
375377
float lineWidth; // Rasterized line width
376378

@@ -936,6 +938,18 @@ static inline void sw_triangle_project_and_clip(sw_vertex_t polygon[SW_MAX_CLIPP
936938
sw_vec4_transform(v->homogeneous, v->position, RLSW.matMVP);
937939
}
938940

941+
if (RLSW.stateFlags & SW_STATE_CULL_FACE) {
942+
float x0 = polygon[0].homogeneous[0], y0 = polygon[0].homogeneous[1];
943+
float x1 = polygon[1].homogeneous[0], y1 = polygon[1].homogeneous[1];
944+
float x2 = polygon[2].homogeneous[0], y2 = polygon[2].homogeneous[1];
945+
946+
float sgnArea = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
947+
if ((RLSW.cullFace == SW_BACK && sgnArea >= 0) || (RLSW.cullFace == SW_FRONT && sgnArea <= 0)) {
948+
*vertexCounter = 0;
949+
return;
950+
}
951+
}
952+
939953
if (sw_triangle_clip_w(polygon, vertexCounter) && sw_triangle_clip_xyz(polygon, vertexCounter)) {
940954
for (int i = 0; i < *vertexCounter; i++) {
941955
sw_vertex_t *v = polygon + i;
@@ -965,7 +979,7 @@ static inline void sw_triangle_project_and_clip(sw_vertex_t polygon[SW_MAX_CLIPP
965979
}
966980
}
967981

968-
#define DEFINE_TRIANGLE_RASTER_SCANLINE(FUNC_NAME, ENABLE_TEXTURE, ENABLE_DEPTH_TEST) \
982+
#define DEFINE_TRIANGLE_RASTER_SCANLINE(FUNC_NAME, ENABLE_TEXTURE, ENABLE_DEPTH_TEST) \
969983
static inline void FUNC_NAME(const sw_texture_t* tex, const sw_vertex_t* start, \
970984
const sw_vertex_t* end, float yDu, float yDv) \
971985
{ \
@@ -1504,6 +1518,11 @@ static inline bool sw_is_texture_wrap_valid(int wrap)
15041518
return (wrap == SW_REPEAT || wrap == SW_CLAMP_TO_EDGE || SW_MIRRORED_REPEAT);
15051519
}
15061520

1521+
static inline bool sw_is_face_valid(int face)
1522+
{
1523+
return (face == SW_FRONT || face == SW_BACK);
1524+
}
1525+
15071526

15081527
/* === Public Implementation === */
15091528

@@ -1546,6 +1565,8 @@ void swInit(int w, int h)
15461565
RLSW.vertexBuffer[0].normal[1] = 0.0f;
15471566
RLSW.vertexBuffer[0].normal[2] = 1.0f;
15481567

1568+
RLSW.cullFace = SW_BACK;
1569+
15491570
static const float defTex[3*2*2] =
15501571
{
15511572
1.0f, 1.0f, 1.0f,
@@ -1889,14 +1910,23 @@ void swClear(void)
18891910
}
18901911
}
18911912

1892-
void swBegin(SWfill mode)
1913+
void swCullFace(SWface face)
1914+
{
1915+
if (!sw_is_face_valid(face)) {
1916+
RLSW.errCode = SW_INVALID_ENUM;
1917+
return;
1918+
}
1919+
RLSW.cullFace = face;
1920+
}
1921+
1922+
void swBegin(SWdraw mode)
18931923
{
18941924
if (mode < SW_POINTS || mode > SW_QUADS) {
18951925
RLSW.errCode = SW_INVALID_ENUM;
18961926
return;
18971927
}
18981928
RLSW.vertexCounter = 0;
1899-
RLSW.fillMode = mode;
1929+
RLSW.drawMode = mode;
19001930
}
19011931

19021932
void swEnd(void)
@@ -1960,7 +1990,7 @@ void swVertex4fv(const float* v)
19601990
RLSW.vertexCounter++;
19611991

19621992
int neededVertices = 0;
1963-
switch (RLSW.fillMode) {
1993+
switch (RLSW.drawMode) {
19641994
case SW_POINTS:
19651995
neededVertices = 1;
19661996
break;
@@ -1981,7 +2011,7 @@ void swVertex4fv(const float* v)
19812011
sw_matrix_mul(RLSW.matMVP, RLSW.matModel, RLSW.matView);
19822012
sw_matrix_mul(RLSW.matMVP, RLSW.matMVP, RLSW.matProjection);
19832013

1984-
switch (RLSW.fillMode) {
2014+
switch (RLSW.drawMode) {
19852015
case SW_POINTS:
19862016
break;
19872017
case SW_LINES:
@@ -2259,7 +2289,7 @@ void swBindArray(SWarray type, void *buffer)
22592289
}
22602290
}
22612291

2262-
void swDrawArrays(SWfill mode, int offset, int count)
2292+
void swDrawArrays(SWdraw mode, int offset, int count)
22632293
{
22642294
if (RLSW.array.positions == 0) {
22652295
RLSW.errCode = SW_INVALID_OPERATION;

0 commit comments

Comments
 (0)