Skip to content

Commit f1bd77b

Browse files
mardyWinterMute
authored andcommitted
Minimal implementation of glPolygonOffset()
Implement support for the `offset` parameter of glPolygonOffset() (the `factor` parameter requires analyzing the vertices' coordinates and some more complex computation), to add an offset to the Z coordinates. We do this by altering the projection matrix. The 0.00001f has been found empirically, it may be that in the future we need to tweak it or compute it at runtime.
1 parent 869472a commit f1bd77b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/gc_gl.c

+20-3
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,16 @@ static inline void update_projection_matrix()
128128
get_projection_info(&type, &near, &far);
129129
memcpy(proj, glparamstate.projection_matrix, sizeof(Mtx44));
130130
float tmp = 1.0f / (far - near);
131+
/* TODO: also use the polygon_offset_factor variable */
132+
float zoffset = glparamstate.polygon_offset_fill ?
133+
(glparamstate.polygon_offset_units * 0.00001f) : 0.0;
131134
if (glparamstate.projection_matrix[3][3] != 0) {
132135
proj[2][2] = -tmp;
133-
proj[2][3] = -far * tmp;
136+
proj[2][3] = -far * tmp + zoffset;
134137
GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
135138
} else {
136139
proj[2][2] = -near * tmp;
137-
proj[2][3] = -near * far * tmp;
140+
proj[2][3] = -near * far * tmp + zoffset;
138141
GX_LoadProjectionMtx(proj, GX_PERSPECTIVE);
139142
}
140143
}
@@ -475,6 +478,10 @@ void glEnable(GLenum cap)
475478
glparamstate.lighting.lights[cap - GL_LIGHT0].enabled = 1;
476479
glparamstate.dirty.bits.dirty_lighting = 1;
477480
break;
481+
case GL_POLYGON_OFFSET_FILL:
482+
glparamstate.polygon_offset_fill = 1;
483+
glparamstate.dirty.bits.dirty_matrices = 1;
484+
break;
478485
default:
479486
break;
480487
}
@@ -545,6 +552,10 @@ void glDisable(GLenum cap)
545552
glparamstate.lighting.lights[cap - GL_LIGHT0].enabled = 0;
546553
glparamstate.dirty.bits.dirty_lighting = 1;
547554
break;
555+
case GL_POLYGON_OFFSET_FILL:
556+
glparamstate.polygon_offset_fill = 0;
557+
glparamstate.dirty.bits.dirty_matrices = 1;
558+
break;
548559
default:
549560
break;
550561
}
@@ -1310,6 +1321,13 @@ void glLineWidth(GLfloat width)
13101321
GX_SetLineWidth((unsigned int)(width * 16), GX_TO_ZERO);
13111322
}
13121323

1324+
void glPolygonOffset(GLfloat factor, GLfloat units)
1325+
{
1326+
glparamstate.polygon_offset_factor = factor;
1327+
glparamstate.polygon_offset_units = units;
1328+
glparamstate.dirty.bits.dirty_matrices = 1;
1329+
}
1330+
13131331
void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
13141332
{
13151333
glparamstate.color_update = (red | green | blue | alpha) != 0;
@@ -2462,7 +2480,6 @@ void glPopAttrib(void) {}
24622480
void glPushClientAttrib(GLbitfield mask) {}
24632481
void glPopClientAttrib(void) {}
24642482
void glPolygonMode(GLenum face, GLenum mode) {}
2465-
void glPolygonOffset(GLfloat offset, GLfloat factor) {}
24662483
void glReadBuffer(GLenum mode) {}
24672484
void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *data) {}
24682485

src/state.h

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ typedef struct glparams_
9494
unsigned char matrixmode;
9595
unsigned char frontcw, cullenabled;
9696
bool color_update;
97+
bool polygon_offset_fill;
9798
uint8_t alpha_func, alpha_ref, alphatest_enabled;
9899
uint8_t clip_plane_mask;
99100
uint16_t texture_env_mode;
@@ -107,6 +108,8 @@ typedef struct glparams_
107108
int draw_count;
108109
GXColor clear_color;
109110
float clearz;
111+
float polygon_offset_factor;
112+
float polygon_offset_units;
110113

111114
GLuint *name_stack;
112115
GLuint *select_buffer;

0 commit comments

Comments
 (0)