Skip to content

Commit

Permalink
1. Fix signatures for R_SetupGL
Browse files Browse the repository at this point in the history
2. Fix R_GenerateWorldSurfaceModel
  • Loading branch information
hzqst committed Oct 10, 2024
1 parent 9adb7bd commit adadf07
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 132 deletions.
57 changes: 55 additions & 2 deletions Plugins/Renderer/gl_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
#define R_SETUPGL_SIG_HL25 "\x55\x8B\xEC\x83\xEC\x08\x2A\x2A\x68\x01\x17\x00\x00\xFF\x15\x2A\x2A\x2A\x2A\xFF\x15"
#define R_SETUPGL_SIG_HL25_9899 "\x55\x8B\xEC\x83\xEC\x08\x83\x3D\x2A\x2A\x2A\x2A\x00\x2A\x2A\x68\x02\x17\x00\x00\xFF\x15"
#define R_SETUPGL_SIG_SVENGINE "\x55\x8B\xEC\x83\xE4\xC0\x83\xEC\x2A\x53\x56\x57\x68\x01\x17\x00\x00"
#define R_SETUPGL_SIG_SVENGINE_10152 "\x51\x53\x56\x57\x68\x01\x17\x00\x00\xFF\x15"

#define GL_DISABLEMULTITEXTURE_SIG_BLOB "\xA1\x2A\x2A\x2A\x2A\x85\xC0\x2A\x2A\x68\xE1\x0D\x00\x00\xFF\x15\x2A\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x50\xE8"
#define GL_DISABLEMULTITEXTURE_SIG_NEW "\xA1\x2A\x2A\x2A\x2A\x85\xC0\x2A\x2A\x68\xE1\x0D\x00\x00\xFF\x15\x2A\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x50\xE8"
Expand Down Expand Up @@ -819,7 +820,32 @@ void R_FillAddress(void)

if (SetupGL_Call)
{
gPrivateFuncs.R_SetupGL = (decltype(gPrivateFuncs.R_SetupGL))g_pMetaHookAPI->ReverseSearchFunctionBegin(SetupGL_Call, 0x600);
gPrivateFuncs.R_SetupGL = (decltype(gPrivateFuncs.R_SetupGL))g_pMetaHookAPI->ReverseSearchFunctionBeginEx(SetupGL_Call, 0x600, [](PUCHAR Candidate) {

if (Candidate[0] == 0x55 &&
Candidate[1] == 0x8B &&
Candidate[2] == 0xEC)
{
if (Search_Pattern_From_Size(Candidate, 15, "\x68\x01\x17\x00\x00") ||
Search_Pattern_From_Size(Candidate, 15, "\x68\x02\x17\x00\x00"))
{
return TRUE;
}
}

if (Candidate[-1] == 0xCC &&
Candidate[0] >= 0x50 &&
Candidate[0] <= 0x57)
{
if (Search_Pattern_From_Size(Candidate, 15, "\x68\x01\x17\x00\x00") ||
Search_Pattern_From_Size(Candidate, 15, "\x68\x02\x17\x00\x00"))
{
return TRUE;
}
}

return FALSE;
});
}
else
{
Expand All @@ -837,7 +863,32 @@ void R_FillAddress(void)

if (SetupGL_Call)
{
gPrivateFuncs.R_SetupGL = (decltype(gPrivateFuncs.R_SetupGL))g_pMetaHookAPI->ReverseSearchFunctionBegin(SetupGL_Call, 0x600);
gPrivateFuncs.R_SetupGL = (decltype(gPrivateFuncs.R_SetupGL))g_pMetaHookAPI->ReverseSearchFunctionBeginEx(SetupGL_Call, 0x600, [](PUCHAR Candidate) {

if (Candidate[0] == 0x55 &&
Candidate[1] == 0x8B &&
Candidate[2] == 0xEC)
{
if (Search_Pattern_From_Size(Candidate, 15, "\x68\x01\x17\x00\x00") ||
Search_Pattern_From_Size(Candidate, 15, "\x68\x02\x17\x00\x00"))
{
return TRUE;
}
}

if (Candidate[-1] == 0xCC &&
Candidate[0] >= 0x50 &&
Candidate[0] <= 0x57)
{
if (Search_Pattern_From_Size(Candidate, 15, "\x68\x01\x17\x00\x00") ||
Search_Pattern_From_Size(Candidate, 15, "\x68\x02\x17\x00\x00"))
{
return TRUE;
}
}

return FALSE;
});
}
}
}
Expand All @@ -848,6 +899,8 @@ void R_FillAddress(void)
if (g_iEngineType == ENGINE_SVENGINE)
{
gPrivateFuncs.R_SetupGL = (decltype(gPrivateFuncs.R_SetupGL))Search_Pattern(R_SETUPGL_SIG_SVENGINE);
if(!gPrivateFuncs.R_SetupGL)
gPrivateFuncs.R_SetupGL = (decltype(gPrivateFuncs.R_SetupGL))Search_Pattern(R_SETUPGL_SIG_SVENGINE_10152);
}
else if (g_iEngineType == ENGINE_GOLDSRC_HL25)
{
Expand Down
18 changes: 9 additions & 9 deletions Plugins/Renderer/gl_portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ CWorldPortalModel* R_GetPortalSurfaceModel(void *ClientPortalManager, void * Cli
return nullptr;
}

auto brushface = &pWorldModel->vFaceBuffer[surfIndex];
auto pBrushFace = &pWorldModel->vFaceBuffer[surfIndex];

auto pPortalModel = R_FindPortalSurfaceModel(ClientPortalManager, ClientPortal, surf, textureId);

Expand All @@ -203,11 +203,11 @@ CWorldPortalModel* R_GetPortalSurfaceModel(void *ClientPortalManager, void * Cli

pPortalModel->SurfaceSet.emplace(surfIndex);

for (int j = 0; j < brushface->num_polys; ++j)
for (int j = 0; j < pBrushFace->num_polys; ++j)
{
for (int k = 0; k < brushface->num_vertexes[j]; ++k)
for (int k = 0; k < pBrushFace->num_vertexes[j]; ++k)
{
pPortalModel->vIndicesBuffer.emplace_back(brushface->start_vertex[j] + k);
pPortalModel->vIndicesBuffer.emplace_back(pBrushFace->start_vertex[j] + k);
}
pPortalModel->vIndicesBuffer.emplace_back((GLuint)0xFFFFFFFF);
}
Expand All @@ -233,7 +233,7 @@ CWorldPortalModel* R_GetPortalSurfaceModel(void *ClientPortalManager, void * Cli
glDisableVertexAttribArray(VERTEX_ATTRIBUTE_INDEX_TEXCOORD);
});

pPortalModel->iPolyCount += brushface->num_polys;
pPortalModel->iPolyCount += pBrushFace->num_polys;

portal_vbo_hash_t hash(ClientPortal, surf->texinfo->texture->name[0] == '{' ? surf->texinfo->texture->gl_texturenum : 0, textureId);

Expand All @@ -247,18 +247,18 @@ CWorldPortalModel* R_GetPortalSurfaceModel(void *ClientPortalManager, void * Cli
{
pPortalModel->SurfaceSet.emplace(surfIndex);

for (int j = 0; j < brushface->num_polys; ++j)
for (int j = 0; j < pBrushFace->num_polys; ++j)
{
for (int k = 0; k < brushface->num_vertexes[j]; ++k)
for (int k = 0; k < pBrushFace->num_vertexes[j]; ++k)
{
pPortalModel->vIndicesBuffer.emplace_back(brushface->start_vertex[j] + k);
pPortalModel->vIndicesBuffer.emplace_back(pBrushFace->start_vertex[j] + k);
}
pPortalModel->vIndicesBuffer.emplace_back((GLuint)0xFFFFFFFF);
}

GL_UploadDataToEBODynamicDraw(pPortalModel->hEBO, sizeof(unsigned int) * pPortalModel->vIndicesBuffer.size(), pPortalModel->vIndicesBuffer.data());

pPortalModel->iPolyCount += brushface->num_polys;
pPortalModel->iPolyCount += pBrushFace->num_polys;
}
}

Expand Down
2 changes: 0 additions & 2 deletions Plugins/Renderer/gl_rsurf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ void GL_BuildLightmaps(void)
{
//Generate vertex buffer and iNumLightmapTextures first

R_GetWorldSurfaceWorldModel(mod);

g_WorldSurfaceRenderer.vWorldModels.emplace_back(mod);
}
}
Expand Down
210 changes: 106 additions & 104 deletions Plugins/Renderer/gl_water.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,101 @@ void R_RenderReflectView(water_reflect_cache_t *ReflectCache)
GL_SetCurrentSceneFBO(NULL);
}

void R_RenderWaterPass_CollectWater(cl_entity_t *e)
{
vec3_t entity_mins, entity_maxs;

bool rotated = false;

if (e->angles[0] || e->angles[1] || e->angles[2])
{
rotated = true;

for (int i = 0; i < 3; i++)
{
entity_mins[i] = e->origin[i] - e->model->radius;
entity_maxs[i] = e->origin[i] + e->model->radius;
}
}
else
{
rotated = false;

VectorAdd(e->origin, e->model->mins, entity_mins);
VectorAdd(e->origin, e->model->maxs, entity_maxs);
}

if (R_CullBox(entity_mins, entity_maxs))
return;

VectorSubtract((*r_refdef.vieworg), e->origin, modelorg);

if (rotated)
{
vec3_t temp;
vec3_t forward, right, up;

VectorCopy(modelorg, temp);
AngleVectors(e->angles, forward, right, up);
modelorg[0] = DotProduct(temp, forward);
modelorg[1] = -DotProduct(temp, right);
modelorg[2] = DotProduct(temp, up);
}

auto pModel = R_GetWorldSurfaceModel(e->model);

if (pModel->vLeaves.size() >= 1)
{
auto pLeaf = pModel->vLeaves[0];

for (size_t i = 0; i < pLeaf->vWaterSurfaceModels.size(); ++i)
{
auto pWaterModel = pLeaf->vWaterSurfaceModels[i];

auto pplane = pWaterModel->plane;

if (g_iEngineType == ENGINE_SVENGINE)
{
if (entity_mins[2] >= pplane->dist)
continue;
}
else
{
if (entity_mins[2] + 1.0f >= pplane->dist)
continue;
}

auto dot = DotProduct(modelorg, pWaterModel->normal) - pWaterModel->planedist;

if (dot > 0)
{
g_VisibleWaterEntity.emplace_back(e);
g_VisibleWaterSurfaceModels.emplace_back(pWaterModel);

if ((*cl_waterlevel) >= 2)
{
auto pSourcePalette = pWaterModel->texture->pPal;

gWaterColor->r = pSourcePalette[9];
gWaterColor->g = pSourcePalette[10];
gWaterColor->b = pSourcePalette[11];
cshift_water->destcolor[0] = pSourcePalette[9];
cshift_water->destcolor[1] = pSourcePalette[10];
cshift_water->destcolor[2] = pSourcePalette[11];
cshift_water->percent = pSourcePalette[12];

if (gWaterColor->r == 0 && gWaterColor->g == 0 && gWaterColor->b == 0)
{
gWaterColor->r = pSourcePalette[0];
gWaterColor->g = pSourcePalette[1];
gWaterColor->b = pSourcePalette[2];
}
}
}
}
}
}

void R_RenderWaterPass(void)
{
if (R_IsRenderingWaterView())
Expand All @@ -787,124 +882,31 @@ void R_RenderWaterPass(void)
viewleaf = Mod_PointInLeaf(r_origin, r_worldmodel);
}

auto pModel = R_GetWorldSurfaceModel(r_worldmodel);

int leafIndex = R_GetWorldLeafIndex(r_worldmodel, viewleaf);

if (leafIndex != -1)
if (leafIndex >= 0 && leafIndex < (int)pModel->vLeaves.size())
{
auto pModel = R_GetWorldSurfaceModel(r_worldmodel);
auto pLeaf = pModel->vLeaves[leafIndex];

if (pModel)
//TODO Frustum Culling for world water?
for (size_t i = 0; i < pLeaf->vWaterSurfaceModels.size(); ++i)
{
if (leafIndex >= 0 && leafIndex < (int)pModel->vLeaves.size())
{
auto pLeaf = pModel->vLeaves[leafIndex];

//TODO Frustum Culling for world water?
for (size_t i = 0; i < pLeaf->vWaterSurfaceModels.size(); ++i)
{
auto pWaterModel = pLeaf->vWaterSurfaceModels[i];
auto pWaterModel = pLeaf->vWaterSurfaceModels[i];

g_VisibleWaterEntity.emplace_back(r_worldentity);
g_VisibleWaterSurfaceModels.emplace_back(pWaterModel);
}
}
g_VisibleWaterEntity.emplace_back(r_worldentity);
g_VisibleWaterSurfaceModels.emplace_back(pWaterModel);
}
}

for (int i = 0; i < (*cl_numvisedicts); ++i)
{
vec3_t entity_mins, entity_maxs;

auto e = cl_visedicts[i];

bool rotated = false;

if (e->angles[0] || e->angles[1] || e->angles[2])
{
rotated = true;

for (int i = 0; i < 3; i++)
{
entity_mins[i] = e->origin[i] - e->model->radius;
entity_maxs[i] = e->origin[i] + e->model->radius;
}
}
else
{
rotated = false;

VectorAdd(e->origin, e->model->mins, entity_mins);
VectorAdd(e->origin, e->model->maxs, entity_maxs);
}

if (R_CullBox(entity_mins, entity_maxs))
continue;

VectorSubtract((*r_refdef.vieworg) , e->origin, modelorg);

if (rotated)
if (e->model && e->model->type == mod_brush)
{
vec3_t temp;
vec3_t forward, right, up;

VectorCopy(modelorg, temp);
AngleVectors(e->angles, forward, right, up);
modelorg[0] = DotProduct(temp, forward);
modelorg[1] = -DotProduct(temp, right);
modelorg[2] = DotProduct(temp, up);
}

auto pModel = R_GetWorldSurfaceModel(e->model);

if (pModel->vLeaves.size() >= 1)
{
auto pLeaf = pModel->vLeaves[0];

for (size_t i = 0; i < pLeaf->vWaterSurfaceModels.size(); ++i)
{
auto pWaterModel = pLeaf->vWaterSurfaceModels[i];

auto pplane = pWaterModel->plane;

if (g_iEngineType == ENGINE_SVENGINE)
{
if (entity_mins[2] >= pplane->dist)
continue;
}
else
{
if (entity_mins[2] + 1.0f >= pplane->dist)
continue;
}

auto dot = DotProduct(modelorg, pWaterModel->normal) - pWaterModel->planedist;

if (dot > 0)
{
g_VisibleWaterEntity.emplace_back(e);
g_VisibleWaterSurfaceModels.emplace_back(pWaterModel);

if ((*cl_waterlevel) >= 2)
{
auto pSourcePalette = pWaterModel->texture->pPal;

gWaterColor->r = pSourcePalette[9];
gWaterColor->g = pSourcePalette[10];
gWaterColor->b = pSourcePalette[11];
cshift_water->destcolor[0] = pSourcePalette[9];
cshift_water->destcolor[1] = pSourcePalette[10];
cshift_water->destcolor[2] = pSourcePalette[11];
cshift_water->percent = pSourcePalette[12];

if (gWaterColor->r == 0 && gWaterColor->g == 0 && gWaterColor->b == 0)
{
gWaterColor->r = pSourcePalette[0];
gWaterColor->g = pSourcePalette[1];
gWaterColor->b = pSourcePalette[2];
}
}
}
}
R_RenderWaterPass_CollectWater(e);
}
}

Expand Down
Loading

0 comments on commit adadf07

Please sign in to comment.