extract DetermineColor logic

This commit is contained in:
Chris Sarbora 2024-06-30 03:00:09 -05:00
parent 18ccdf5a81
commit f8043c1390
No known key found for this signature in database
3 changed files with 48 additions and 153 deletions

View File

@ -450,19 +450,9 @@ void rend_DrawPolygon2D(int handle, g3Point **p, int nv) {
int xAdd = gpu_state.clip_x1;
int yAdd = gpu_state.clip_y1;
float fr, fg, fb;
if (gpu_state.cur_light_state == LS_FLAT_GOURAUD || gpu_state.cur_texture_quality == 0) {
float scale = 1.0f / 255.0f;
fr = GR_COLOR_RED(gpu_state.cur_color) * scale;
fg = GR_COLOR_GREEN(gpu_state.cur_color) * scale;
fb = GR_COLOR_BLUE(gpu_state.cur_color) * scale;
}
// make sure our bitmap is ready to be drawn
gpu_BindTexture(handle, MAP_TYPE_BITMAP, 0);
float alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
PosColorUVVertex *vData = &vArray[0];
// Specify our coordinates
@ -470,38 +460,7 @@ void rend_DrawPolygon2D(int handle, g3Point **p, int nv) {
for (i = 0; i < nv; ++i, ++vData) {
g3Point *pnt = p[i];
if (gpu_state.cur_alpha_type & ATF_VERTEX) {
// the alpha should come from the vertex
alpha = pnt->p3_a * gpu_Alpha_multiplier * gpu_Alpha_factor;
}
// If we have a lighting model, apply the correct lighting!
if (gpu_state.cur_light_state == LS_FLAT_GOURAUD || gpu_state.cur_texture_quality == 0) {
// pull the color from the constant color data
vData->color.r = fr;
vData->color.g = fg;
vData->color.b = fb;
vData->color.a = alpha;
} else if (gpu_state.cur_light_state != LS_NONE) {
// Do lighting based on intensity (MONO) or colored (RGB)
if (gpu_state.cur_color_model == CM_MONO) {
vData->color.r = pnt->p3_l;
vData->color.g = pnt->p3_l;
vData->color.b = pnt->p3_l;
vData->color.a = alpha;
} else {
vData->color.r = pnt->p3_r;
vData->color.g = pnt->p3_g;
vData->color.b = pnt->p3_b;
vData->color.a = alpha;
}
} else {
// force white
vData->color.r = 1.0f;
vData->color.g = 1.0f;
vData->color.b = 1.0f;
vData->color.a = alpha;
}
vData->color = DeterminePointColor(pnt, false, true);
vData->uv.s = pnt->p3_u;
vData->uv.t = pnt->p3_v;
@ -517,6 +476,26 @@ void rend_DrawPolygon2D(int handle, g3Point **p, int nv) {
gpu_RenderPolygon(&vArray[0], nv);
}
color_array DeterminePointColor(g3Point const* pnt, bool disableGouraud, bool checkTextureQuality, bool flatColorForNoLight) {
auto alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
if (gpu_state.cur_alpha_type & ATF_VERTEX) {
alpha *= pnt->p3_a;
}
// If we have a lighting model, apply the correct lighting!
if ((gpu_state.cur_light_state == LS_FLAT_GOURAUD && !disableGouraud) ||
(gpu_state.cur_texture_quality == 0 && checkTextureQuality) ||
(gpu_state.cur_light_state == LS_NONE && flatColorForNoLight)) {
return {GR_COLOR_RED(gpu_state.cur_color) / 255.0f, GR_COLOR_GREEN(gpu_state.cur_color) / 255.0f,
GR_COLOR_BLUE(gpu_state.cur_color) / 255.0f, alpha};
} else if (gpu_state.cur_light_state == LS_NONE) {
return {1, 1, 1, alpha};
} else if (gpu_state.cur_color_model == CM_MONO) {
return {pnt->p3_l, pnt->p3_l, pnt->p3_l, alpha};
} else {
return {pnt->p3_r, pnt->p3_g, pnt->p3_b, alpha};
}
}
// Takes nv vertices and draws the 3D polygon defined by those vertices.
// Uses bitmap "handle" as a texture
@ -540,20 +519,12 @@ void rend_DrawPolygon3D(int handle, g3Point **p, int nv, int map_type) {
return;
}
if (gpu_state.cur_light_state == LS_FLAT_GOURAUD) {
fr = GR_COLOR_RED(gpu_state.cur_color) / 255.0;
fg = GR_COLOR_GREEN(gpu_state.cur_color) / 255.0;
fb = GR_COLOR_BLUE(gpu_state.cur_color) / 255.0;
}
if (UseMultitexture) {
gpu_SetMultitextureBlendMode(false);
}
gpu_BindTexture(handle, map_type, 0);
alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
PosColorUVVertex *vData = &vArray[0];
// Specify our coordinates
@ -563,38 +534,8 @@ void rend_DrawPolygon3D(int handle, g3Point **p, int nv, int map_type) {
// all points should be original
ASSERT(pnt->p3_flags & PF_ORIGPOINT);
if (gpu_state.cur_alpha_type & ATF_VERTEX) {
alpha = pnt->p3_a * gpu_Alpha_multiplier * gpu_Alpha_factor;
}
// If we have a lighting model, apply the correct lighting!
if (gpu_state.cur_light_state != LS_NONE) {
if (gpu_state.cur_light_state == LS_FLAT_GOURAUD) {
vData->color.r = fr;
vData->color.g = fg;
vData->color.b = fb;
vData->color.a = alpha;
} else {
// Do lighting based on intesity (MONO) or colored (RGB)
if (gpu_state.cur_color_model == CM_MONO) {
vData->color.r = pnt->p3_l;
vData->color.g = pnt->p3_l;
vData->color.b = pnt->p3_l;
vData->color.a = alpha;
} else {
vData->color.r = pnt->p3_r;
vData->color.g = pnt->p3_g;
vData->color.b = pnt->p3_b;
vData->color.a = alpha;
}
}
} else {
vData->color.r = 1;
vData->color.g = 1;
vData->color.b = 1;
vData->color.a = alpha;
}
vData->color = DeterminePointColor(pnt);
vData->uv.s = pnt->p3_u;
vData->uv.t = pnt->p3_v;
vData->uv.r = 0.0f;
@ -621,7 +562,6 @@ void rend_DrawPolygon3D(int handle, g3Point **p, int nv, int map_type) {
void rend_DrawMultitexturePolygon3D(int handle, g3Point **p, int nv, int map_type) {
g3Point *pnt;
int i;
float alpha;
float one_over_square_res = 1.0 / GameLightmaps[gpu_Overlay_map].square_res;
float xscalar = (float)GameLightmaps[gpu_Overlay_map].width * one_over_square_res;
@ -629,8 +569,6 @@ void rend_DrawMultitexturePolygon3D(int handle, g3Point **p, int nv, int map_typ
ASSERT(nv < 100);
alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
PosColorUV2Vertex *vData = &vArray2[0];
// Specify our coordinates
@ -638,29 +576,7 @@ void rend_DrawMultitexturePolygon3D(int handle, g3Point **p, int nv, int map_typ
pnt = p[i];
ASSERT(pnt->p3_flags & PF_ORIGPOINT);
if (gpu_state.cur_alpha_type & ATF_VERTEX)
alpha = pnt->p3_a * gpu_Alpha_multiplier * gpu_Alpha_factor;
// If we have a lighting model, apply the correct lighting!
if (gpu_state.cur_light_state != LS_NONE) {
// Do lighting based on intesity (MONO) or colored (RGB)
if (gpu_state.cur_color_model == CM_MONO) {
vData->color.r = pnt->p3_l;
vData->color.g = pnt->p3_l;
vData->color.b = pnt->p3_l;
vData->color.a = alpha;
} else {
vData->color.r = pnt->p3_r;
vData->color.g = pnt->p3_g;
vData->color.b = pnt->p3_b;
vData->color.a = alpha;
}
} else {
vData->color.r = 1;
vData->color.g = 1;
vData->color.b = 1;
vData->color.a = alpha;
}
vData->color = DeterminePointColor(pnt, true);
vData->uv0.s = pnt->p3_u;
vData->uv0.t = pnt->p3_v;

View File

@ -91,4 +91,14 @@ void gpu_RenderPolygonUV2(PosColorUV2Vertex *vData, uint32_t nv);
void gpu_DrawFlatPolygon3D(g3Point **p, int nv);
void rend_DrawMultitexturePolygon3D(int handle, g3Point **p, int nv, int map_type);
/*
* Returns the color to use for a given point, based on lighting and alpha modes
*
* pnt - Point to determine color for (includes an innate color value)
* disableGouraud - if set, ignore 'cur_light_state == LS_FLAT_GOURAUD' and use pnt-supplied color
* checkTextureQuality - if set and cur_texture_quality == 0, ignore pnt-supplied color
* flatColorForNoLight - if set and cur_light_state == LS_NONE, ignore pnt-supplied color
*/
color_array DeterminePointColor(g3Point const* pnt, bool disableGouraud = false, bool checkTextureQuality = false, bool flatColorForNoLight = false);
#endif

View File

@ -17,6 +17,7 @@
*/
#include <algorithm>
#include <array>
#include <cstdlib>
#include <cstdio>
#include <cstring>
@ -1138,56 +1139,24 @@ void gpu_SetMultitextureBlendMode(bool state) {
}
void gpu_DrawFlatPolygon3D(g3Point **p, int nv) {
float fr, fg, fb;
int i;
if (UseMultitexture) {
gpu_SetMultitextureBlendMode(false);
}
float alpha = gpu_Alpha_multiplier * gpu_Alpha_factor;
std::array<PosColorUVVertex, 100> vertices{};
std::transform(p, p + nv, std::begin(vertices), [](auto pnt) {
return PosColorUVVertex{
pnt->p3_vecPreRot,
DeterminePointColor(pnt, true, false, true),
tex_array{
// tex coord can be default-constructed, because it will ultimately be ignored
// because this function is only called when cur_texture_quality == 0, and anytime
// that is true, GL_TEXTURE_2D is also disabled
}
};
});
fr = GR_COLOR_RED(gpu_state.cur_color);
fg = GR_COLOR_GREEN(gpu_state.cur_color);
fb = GR_COLOR_BLUE(gpu_state.cur_color);
fr /= 255.0;
fg /= 255.0;
fb /= 255.0;
// And draw!
dglBegin(GL_POLYGON);
for (i = 0; i < nv; i++) {
g3Point *pnt = p[i];
ASSERT(pnt->p3_flags & PF_ORIGPOINT);
if (gpu_state.cur_alpha_type & ATF_VERTEX)
alpha = pnt->p3_a * gpu_Alpha_multiplier * gpu_Alpha_factor;
// If we have a lighting model, apply the correct lighting!
if (gpu_state.cur_light_state != LS_NONE) {
// Do lighting based on intesity (MONO) or colored (RGB)
if (gpu_state.cur_color_model == CM_MONO)
dglColor4f(pnt->p3_l, pnt->p3_l, pnt->p3_l, alpha);
else {
dglColor4f(pnt->p3_r, pnt->p3_g, pnt->p3_b, alpha);
}
} else {
dglColor4f(fr, fg, fb, alpha);
}
/*
// Finally, specify a vertex
float z = std::max(0,std::min(1.0,1.0-(1.0/(pnt->p3_z+Z_bias))));
dglVertex3f (pnt->p3_sx+x_add,pnt->p3_sy+y_add,-z);
*/
dglVertex3f(pnt->p3_vecPreRot.x, pnt->p3_vecPreRot.y, pnt->p3_vecPreRot.z);
}
dglEnd();
CHECK_ERROR(11)
OpenGL_polys_drawn++;
OpenGL_verts_processed += nv;
gpu_RenderPolygon(vertices.data(), nv);
}
// Sets the gamma correction value