@@ -145,7 +145,7 @@ typedef enum {
145
145
SW_LINES = GL_LINES ,
146
146
SW_TRIANGLES = GL_TRIANGLES ,
147
147
SW_QUADS = GL_QUADS ,
148
- } SWfill ;
148
+ } SWdraw ;
149
149
150
150
typedef enum {
151
151
SW_FRONT = GL_FRONT ,
@@ -232,7 +232,9 @@ void swViewport(int x, int y, int width, int height);
232
232
void swClearColor (float r , float g , float b , float a );
233
233
void swClear (void );
234
234
235
- void swBegin (SWfill mode );
235
+ void swCullFace (SWface face );
236
+
237
+ void swBegin (SWdraw mode );
236
238
void swEnd (void );
237
239
238
240
void swVertex2i (int x , int y );
@@ -270,7 +272,7 @@ void swNormal3f(float x, float y, float z);
270
272
void swNormal3fv (const float * v );
271
273
272
274
void swBindArray (SWarray type , void * buffer );
273
- void swDrawArrays (SWfill mode , int offset , int count );
275
+ void swDrawArrays (SWdraw mode , int offset , int count );
274
276
275
277
uint32_t swLoadTexture (const void * data , int width , int height , int format , int mipmapCount );
276
278
void swUnloadTexture (uint32_t id );
@@ -370,7 +372,7 @@ typedef struct {
370
372
sw_vertex_t vertexBuffer [4 ]; // Buffer used for storing primitive vertices, used for processing and rendering
371
373
int vertexCounter ; // Number of vertices in 'ctx.vertexBuffer'
372
374
373
- SWfill fillMode ; // Current polygon filling mode (e.g., lines, triangles)
375
+ SWdraw drawMode ; // Current polygon filling mode (e.g., lines, triangles)
374
376
float pointSize ; // Rasterized point size
375
377
float lineWidth ; // Rasterized line width
376
378
@@ -936,6 +938,18 @@ static inline void sw_triangle_project_and_clip(sw_vertex_t polygon[SW_MAX_CLIPP
936
938
sw_vec4_transform (v -> homogeneous , v -> position , RLSW .matMVP );
937
939
}
938
940
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
+
939
953
if (sw_triangle_clip_w (polygon , vertexCounter ) && sw_triangle_clip_xyz (polygon , vertexCounter )) {
940
954
for (int i = 0 ; i < * vertexCounter ; i ++ ) {
941
955
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
965
979
}
966
980
}
967
981
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 ) \
969
983
static inline void FUNC_NAME(const sw_texture_t* tex, const sw_vertex_t* start, \
970
984
const sw_vertex_t* end, float yDu, float yDv) \
971
985
{ \
@@ -1504,6 +1518,11 @@ static inline bool sw_is_texture_wrap_valid(int wrap)
1504
1518
return (wrap == SW_REPEAT || wrap == SW_CLAMP_TO_EDGE || SW_MIRRORED_REPEAT );
1505
1519
}
1506
1520
1521
+ static inline bool sw_is_face_valid (int face )
1522
+ {
1523
+ return (face == SW_FRONT || face == SW_BACK );
1524
+ }
1525
+
1507
1526
1508
1527
/* === Public Implementation === */
1509
1528
@@ -1546,6 +1565,8 @@ void swInit(int w, int h)
1546
1565
RLSW .vertexBuffer [0 ].normal [1 ] = 0.0f ;
1547
1566
RLSW .vertexBuffer [0 ].normal [2 ] = 1.0f ;
1548
1567
1568
+ RLSW .cullFace = SW_BACK ;
1569
+
1549
1570
static const float defTex [3 * 2 * 2 ] =
1550
1571
{
1551
1572
1.0f , 1.0f , 1.0f ,
@@ -1889,14 +1910,23 @@ void swClear(void)
1889
1910
}
1890
1911
}
1891
1912
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 )
1893
1923
{
1894
1924
if (mode < SW_POINTS || mode > SW_QUADS ) {
1895
1925
RLSW .errCode = SW_INVALID_ENUM ;
1896
1926
return ;
1897
1927
}
1898
1928
RLSW .vertexCounter = 0 ;
1899
- RLSW .fillMode = mode ;
1929
+ RLSW .drawMode = mode ;
1900
1930
}
1901
1931
1902
1932
void swEnd (void )
@@ -1960,7 +1990,7 @@ void swVertex4fv(const float* v)
1960
1990
RLSW .vertexCounter ++ ;
1961
1991
1962
1992
int neededVertices = 0 ;
1963
- switch (RLSW .fillMode ) {
1993
+ switch (RLSW .drawMode ) {
1964
1994
case SW_POINTS :
1965
1995
neededVertices = 1 ;
1966
1996
break ;
@@ -1981,7 +2011,7 @@ void swVertex4fv(const float* v)
1981
2011
sw_matrix_mul (RLSW .matMVP , RLSW .matModel , RLSW .matView );
1982
2012
sw_matrix_mul (RLSW .matMVP , RLSW .matMVP , RLSW .matProjection );
1983
2013
1984
- switch (RLSW .fillMode ) {
2014
+ switch (RLSW .drawMode ) {
1985
2015
case SW_POINTS :
1986
2016
break ;
1987
2017
case SW_LINES :
@@ -2259,7 +2289,7 @@ void swBindArray(SWarray type, void *buffer)
2259
2289
}
2260
2290
}
2261
2291
2262
- void swDrawArrays (SWfill mode , int offset , int count )
2292
+ void swDrawArrays (SWdraw mode , int offset , int count )
2263
2293
{
2264
2294
if (RLSW .array .positions == 0 ) {
2265
2295
RLSW .errCode = SW_INVALID_OPERATION ;
0 commit comments