Skip to content

Commit

Permalink
fixed neo reflections; implemented leeds reflections
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed Apr 19, 2018
1 parent 4231fe1 commit 10c0232
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 52 deletions.
8 changes: 5 additions & 3 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ workspace "skygfx"

defines { "rsc_CompanyName=\"aap\"" }
defines { "rsc_LegalCopyright=\"\""}
defines { "rsc_FileVersion=\"3.6.0.0\"", "rsc_ProductVersion=\"3.6.0.0\"" }
defines { "rsc_FileVersion=\"3.7.0.0\"", "rsc_ProductVersion=\"3.7.0.0\"" }
defines { "rsc_InternalName=\"%{prj.name}\"", "rsc_ProductName=\"%{prj.name}\"", "rsc_OriginalFilename=\"%{prj.name}.dll\"" }
defines { "rsc_FileDescription=\"https://github.com/aap\"" }
defines { "rsc_UpdateUrl=\"https://github.com/aap/skygfx\"" }
Expand Down Expand Up @@ -33,18 +33,20 @@ project "skygfx"
targetextension ".dll"
characterset ("MBCS")

buildoptions { "/Zc:threadSafeInit-" }

filter "configurations:Debug"
defines { "DEBUG" }
symbols "On"
flags { "StaticRuntime" }
debugdir "C:/Users/aap/games/gtasa"
debugcommand "C:/Users/aap/games/gtasa/gta_sa.exe"
postbuildcommands "copy /y \"$(TargetPath)\" \"C:\\Users\\aap\\games\\gtasa\\dlls\\skygfx.dll\""
postbuildcommands "copy /y \"$(TargetPath)\" \"C:\\Users\\aap\\games\\gtasa\\plugins\\skygfx.dll\""

filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"
flags { "StaticRuntime" }
debugdir "C:/Users/aap/games/gtasa"
debugcommand "C:/Users/aap/games/gtasa/gta_sa.exe"
postbuildcommands "copy /y \"$(TargetPath)\" \"C:\\Users\\aap\\games\\gtasa\\dlls\\skygfx.dll\""
postbuildcommands "copy /y \"$(TargetPath)\" \"C:\\Users\\aap\\games\\gtasa\\plugins\\skygfx.dll\""
Binary file modified resources/Resource.rc
Binary file not shown.
Binary file modified resources/VersionInfo.rc
Binary file not shown.
Binary file modified resources/resource.h
Binary file not shown.
32 changes: 32 additions & 0 deletions shaders/vs/leedsCarFxVS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
float4x4 combined : register(c0);
float4 fxParams : register(c30);
float3x3 envmat : register(c32);
float4x4 texmat : register(c36);

#define shininess (fxParams.y)
#define lightmult (fxParams.w)

struct VS_INPUT {
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 Texcoord1: TEXCOORD1;
};

struct VS_OUTPUT {
float4 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4 Color : COLOR0;
};

VS_OUTPUT
main(VS_INPUT IN)
{
VS_OUTPUT OUT;
OUT.Position = mul(IN.Position, combined);

float3 envNormal = mul(envmat, IN.Normal);
OUT.Texcoord = mul(texmat, float4(envNormal.xy, 0.0, 1.0)).xy;
OUT.Color = float4(128.0, 128.0, 128.0, 255.0)/255.0*shininess*lightmult;
OUT.Color.a = 1.0;
return OUT;
}
3 changes: 1 addition & 2 deletions shaders/vs/neoVehiclePass1VS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ float3 surfProps : register(c32);

#define shininess (reflProps.x)
#define fresnel (reflProps.y)
#define lightmult (reflProps.z)
#define power (reflProps.w)
#define surfAmb (surfProps.x)
#define surfDiff (surfProps.z)
Expand All @@ -58,7 +57,7 @@ main(in VS_INPUT In)
uv2 = mul((float3x3)tex, uv2);
Out.texcoord1.xy = uv2.xy*0.5 + 0.5;
float b = 1.0 - saturate(dot(V, N));
Out.reflcolor = lerp(b*b*b*b*b, 1.0f, fresnel)*shininess;//*lightmult;
Out.reflcolor = lerp(b*b*b*b*b, 1.0f, fresnel)*shininess;

return Out;
}
3 changes: 1 addition & 2 deletions shaders/vs/neoVehiclePass2VS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ float3 surfProps : register(c32);

#define shininess (reflProps.x)
#define fresnel (reflProps.y)
#define lightmult (reflProps.z)
#define power (reflProps.w)
#define surfSpec (surfProps.y)

Expand All @@ -51,7 +50,7 @@ main(in VS_INPUT In)
Out.color = float4(directSpec*specTerm(N, -directDir, V, power), 1.0);
for(int i = 0; i < 6; i++)
Out.color.rgb += lightCol[i]*specTerm(N, -lightDir[i], V, power*2);
Out.color = saturate(Out.color*surfSpec*lightmult);
Out.color = saturate(Out.color*surfSpec);

return Out;
}
42 changes: 33 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void *gpCurrentShaderForDefaultCallbacks;
float *gfLaRiotsLightMult = (float*)0x8CD060;
float *ambientColors = (float*)0xB7C4A0;

GlobalScene &Scene = *(GlobalScene*)0xC17038;

static int defaultColourLeftUOffset;
static int defaultColourRightUOffset;
static int defaultColourTopVOffset;
Expand Down Expand Up @@ -590,11 +592,29 @@ void CSkidmarks__Render(void)
RwRenderStateSet(rwRENDERSTATEALPHATESTFUNCTION, (void*)alphafunc);
}

static uint32_t RenderScene_A;
WRAPPER void RenderScene(void) { VARJMP(RenderScene_A); }

void RenderReflectionMap_leeds(void);
void RenderReflectionScene(void);

void
RenderScene_hook(void)
{
RenderScene();
// RenderReflectionScene();
if(config->vehiclePipe == 4)
CarPipe::RenderEnvTex();
else if(config->vehiclePipe == 5)
RenderReflectionMap_leeds();
}

void (*InitialiseGame)(void);
void
InitialiseGame_hook(void)
{
ONCE;
InterceptCall(&RenderScene_A, RenderScene_hook, 0x53EABF);
neoInit();
InitialiseGame();
}
Expand Down Expand Up @@ -647,7 +667,7 @@ int
StrAssoc::get(StrAssoc *desc, const char *key)
{
for(; desc->key[0] != '\0'; desc++)
if(strcmp(desc->key, key) == 0)
if(strcmpi(desc->key, key) == 0)
return desc->val;
return desc->val;
}
Expand Down Expand Up @@ -757,6 +777,7 @@ readIni(int n)
{"Xbox", 2},
{"Spec", 3},
{"Neo", 4},
{"Leeds", 5},
{"", -1},
};
c->vehiclePipe = StrAssoc::get(vehPipeMap, cfg.get("SkyGfx", "vehiclePipe", "").c_str());
Expand All @@ -766,8 +787,9 @@ readIni(int n)
}

c->dualPassVehicle = readint(cfg.get("SkyGfx", "dualPassVehicle", ""), config->dualPassGlobal);
c->neoShininess = readfloat(cfg.get("SkyGfx", "neoShininess", ""), 0.75);
c->neoSpecularity = readfloat(cfg.get("SkyGfx", "neoSpecularity", ""), 0.75);
c->leedsShininessMult = readfloat(cfg.get("SkyGfx", "leedsShininessMult", ""), 1.0);
c->neoShininessMult = readfloat(cfg.get("SkyGfx", "neoShininessMult", ""), 1.0);
c->neoSpecularityMult = readfloat(cfg.get("SkyGfx", "neoSpecularityMult", ""), 1.0);
envMapSize = readint(cfg.get("SkyGfx", "neoEnvMapSize", ""), 128);
int i = 1;
while(i < envMapSize) i *= 2;
Expand Down Expand Up @@ -921,8 +943,9 @@ afterStreamIni(void)
X(dualPassGrass) \
X(buildingPipe) \
X(vehiclePipe) \
X(neoShininess) \
X(neoSpecularity) \
X(leedsShininessMult) \
X(neoShininessMult) \
X(neoSpecularityMult) \
X(doglare) \
X(fixGrassPlacement) \
X(grassAddAmbient) \
Expand Down Expand Up @@ -998,7 +1021,7 @@ installMenu(void)
if(DebugMenuLoad()){
static const char *ps2pcStr[] = { "PS2", "PC" };
static const char *buildPipeStr[] = { "PS2", "PC" };
static const char *vehPipeStr[] = { "PS2", "PC", "Xbox", "Spec", "Neo" };
static const char *vehPipeStr[] = { "PS2", "PC", "Xbox", "Spec", "Neo", "Leeds" };
static const char *colFilterStr[] = { "None", "PS2", "PC", "Mobile", "III", "VC", "VCS" };
static const char *lightningStr[] = { "Sky only", "Sky and objects" };
static const char *shadStr[] = { "Default", "PS2", "PC" };
Expand All @@ -1013,7 +1036,7 @@ installMenu(void)
DebugMenuEntrySetWrap(menu.buildingPipe, true);
}
if(iCanHasvehiclePipe){
menu.vehiclePipe = DebugMenuAddVar("SkyGFX", "Vehicle Pipeline", &config->vehiclePipe, nil, 1, 0, 4, vehPipeStr);
menu.vehiclePipe = DebugMenuAddVar("SkyGFX", "Vehicle Pipeline", &config->vehiclePipe, nil, 1, 0, 5, vehPipeStr);
DebugMenuEntrySetWrap(menu.vehiclePipe, true);
}
menu.grassAddAmbient = DebugMenuAddVarBool32("SkyGFX", "Add Ambient to Grass", &config->grassAddAmbient, nil);
Expand All @@ -1038,8 +1061,9 @@ installMenu(void)
DebugMenuAddVarBool8("SkyGFX|Misc", "Blur PS2 Colour Filter", (int8_t*)&CPostEffects::m_bBlurColourFilter, nil);
if(iCanHasSunGlare)
menu.doglare = DebugMenuAddVarBool32("SkyGFX|Misc", "Sun Glare", &config->doglare, nil);
menu.neoShininess = DebugMenuAddVar("SkyGFX|Misc", "Neo Car Shininess", &config->neoShininess, nil, 0.1f, 0.0f, 10.0f);
menu.neoSpecularity = DebugMenuAddVar("SkyGFX|Misc", "Neo Car Specularity", &config->neoSpecularity, nil, 0.1f, 0.0f, 10.0f);
menu.leedsShininessMult = DebugMenuAddVar("SkyGFX|Misc", "Leeds Car Shininess", &config->leedsShininessMult, nil, 0.1f, 0.0f, 10.0f);
menu.neoShininessMult = DebugMenuAddVar("SkyGFX|Misc", "Neo Car Shininess", &config->neoShininessMult, nil, 0.1f, 0.0f, 10.0f);
menu.neoSpecularityMult = DebugMenuAddVar("SkyGFX|Misc", "Neo Car Specularity", &config->neoSpecularityMult, nil, 0.1f, 0.0f, 10.0f);
menu.fixGrassPlacement = DebugMenuAddVarBool32("SkyGFX|Misc", "Fix Grass Placement", &config->fixGrassPlacement, nil);
menu.lightningIlluminatesWorld = DebugMenuAddVar("SkyGFX|Misc", "Lightning illuminates", &config->lightningIlluminatesWorld, nil, 1, 0, 1, lightningStr);
DebugMenuEntrySetWrap(menu.lightningIlluminatesWorld, true);
Expand Down
3 changes: 2 additions & 1 deletion src/neo.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ void neoInit(void);

class CarPipe : public CustomPipe
{
public:
void CreateShaders(void);
static void LoadTweakingTable(void);

static void MakeScreenQuad(void);
static void MakeQuadTexCoords(bool textureSpace);
static void RenderReflectionScene(void);
public:

static InterpolatedFloat fresnel;
static InterpolatedFloat power;
static InterpolatedLight diffColor;
Expand Down
60 changes: 28 additions & 32 deletions src/neoCarpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ enum {

//#define DEBUGTEX

WRAPPER void CRenderer__RenderRoads(void) { EAXJMP(0x553A10); }
WRAPPER void CRenderer__RenderEverythingBarRoads(void) { EAXJMP(0x553AA0); }
WRAPPER void CRenderer__RenderFadingInEntities(void) { EAXJMP(0x553220); }
WRAPPER void CRenderer__RenderFadingInEntities(void) { EAXJMP(0x5531E0); }
WRAPPER void CRenderer__RenderFadingInUnderwaterEntities(void) { EAXJMP(0x553220); }

short &skyBotRed = *(short*)0xB7C4CA;
short &skyBotGreen = *(short*)0xB7C4CC;
Expand All @@ -45,32 +47,19 @@ RwImVertexIndex CarPipe::screenindices[6] = { 0, 1, 2, 0, 2, 3 };

CarPipe carpipe;

static uint32_t RenderScene_A;
WRAPPER void RenderScene(void) { VARJMP(RenderScene_A); }

void
RenderScene_hook(void)
{
RenderScene();
if(config->vehiclePipe == 4)
CarPipe::RenderEnvTex();
}

void
neoCarPipeInit(void)
{
ONCE;
carpipe.Init();
CarPipe::SetupEnvMap();
InterceptCall(&RenderScene_A, RenderScene_hook, 0x53EABF);
}

//
// Reflection map
//

int envMapSize;
RpWorld *&scene = *(RpWorld**)0xC17038;

void
CarPipe::SetupEnvMap(void)
Expand All @@ -88,7 +77,7 @@ CarPipe::SetupEnvMap(void)
RwV2d vw;
vw.x = vw.y = 0.4f;
RwCameraSetViewWindow(reflectionCam, &vw);
RpWorldAddCamera(scene, reflectionCam);
RpWorldAddCamera(Scene.world, reflectionCam);

reflectionTex = RwTextureCreate(envFB);
RwTextureSetFilterMode(reflectionTex, rwFILTERLINEAR);
Expand Down Expand Up @@ -148,16 +137,15 @@ void
CarPipe::RenderReflectionScene(void)
{
RwRenderStateSet(rwRENDERSTATEFOGENABLE, 0);
CRenderer__RenderRoads();
CRenderer__RenderEverythingBarRoads();
CRenderer__RenderFadingInEntities();
}

RwCamera *&gtacam = *(RwCamera**)0xC1703C;

void
CarPipe::RenderEnvTex(void)
{
RwCameraEndUpdate(gtacam);
RwCameraEndUpdate(Scene.camera);

RwV2d oldvw, vw = { 2.0f, 2.0f };
oldvw = reflectionCam->viewWindow;
Expand All @@ -176,18 +164,22 @@ CarPipe::RenderEnvTex(void)
reflectionMatrix->at.y = 0.0f;
reflectionMatrix->at.z = 1.0f;
}
RwMatrix *cammatrix = RwFrameGetMatrix(RwCameraGetFrame(gtacam));
RwMatrix *cammatrix = RwFrameGetMatrix(RwCameraGetFrame(Scene.camera));
reflectionMatrix->pos = cammatrix->pos;
RwMatrixUpdate(reflectionMatrix);
RwFrameTransform(RwCameraGetFrame(reflectionCam), reflectionMatrix, rwCOMBINEREPLACE);
RwRGBA color = { skyBotRed, skyBotGreen, skyBotBlue, 255 };
// blend a bit of white into the sky color, otherwise it tends to be very blue
color.red = color.red*0.6f + 255*0.4f;
color.green = color.green*0.6f + 255*0.4f;
color.blue = color.blue*0.6f + 255*0.4f;
RwCameraClear(reflectionCam, &color, rwCAMERACLEARIMAGE | rwCAMERACLEARZ);

RwCameraBeginUpdate(reflectionCam);
RwCamera *savedcam = gtacam;
gtacam = reflectionCam; // they do some begin/end updates with this in the called functions :/
RwCamera *savedcam = Scene.camera;
Scene.camera = reflectionCam; // they do some begin/end updates with this in the called functions :/
RenderReflectionScene();
gtacam = savedcam;
Scene.camera = savedcam;

RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, (void*)1);
RwRenderStateSet(rwRENDERSTATESRCBLEND, (void*)rwBLENDZERO);
Expand All @@ -201,7 +193,7 @@ CarPipe::RenderEnvTex(void)
RwCameraEndUpdate(reflectionCam);
RwCameraSetViewWindow(reflectionCam, &oldvw);

RwCameraBeginUpdate(gtacam);
RwCameraBeginUpdate(Scene.camera);
#ifdef DEBUGTEX
RwRenderStateSet(rwRENDERSTATETEXTURERASTER, reflectionTex->raster);
RwIm2DRenderIndexedPrimitive(rwPRIMTYPETRILIST, screenQuad, 4, screenindices, 6);
Expand Down Expand Up @@ -363,13 +355,10 @@ CarPipe::DiffusePass(RxD3D9ResEntryHeader *header, RpAtomic *atomic)
envData = *RWPLUGINOFFSET(CustomEnvMapPipeMaterialData*, material, CCustomCarEnvMapPipeline__ms_envMapPluginOffset);

RwUInt32 materialFlags = *(RwUInt32*)&material->surfaceProps.specular;
bool hasRefl = !((materialFlags & 1) == 0);
bool hasEnv = !((materialFlags & 2) == 0);
bool hasEnv = !!(materialFlags & 3);
int matfx = RpMatFXMaterialGetEffects(material);
if(matfx != rpMATFXEFFECTENVMAP){
if(matfx != rpMATFXEFFECTENVMAP)
hasEnv = false;
hasRefl = false;
}

Color c = diffColor.Get();
Color diff(c.r*c.a, c.g*c.a, c.b*c.a, 1.0f-c.a);
Expand All @@ -381,13 +370,14 @@ CarPipe::DiffusePass(RxD3D9ResEntryHeader *header, RpAtomic *atomic)
RwD3D9SetVertexShaderConstant(LOC_matCol, (void*)&mat, 1);

RwSurfaceProperties surfprops = material->surfaceProps;
surfprops.specular = config->neoSpecularity;
// if ambient light is too dark reflections don't look too good, so bump it
if(surfprops.ambient > 0.1f && surfprops.ambient < 0.8f)
surfprops.ambient = max(surfprops.ambient, 0.8f);
RwD3D9SetVertexShaderConstant(LOC_surfProps, &surfprops, 1);

float reflProps[4];
reflProps[0] = (hasEnv||hasRefl) && !noRefl ? config->neoShininess : 0.0f;
reflProps[0] = hasEnv && !noRefl ? envData->shininess/255.0f * 8.0f * config->neoShininessMult : 0.0f;
reflProps[1] = fresnel.Get();
reflProps[2] = CCustomCarEnvMapPipeline__m_EnvMapLightingMult;
reflProps[3] = power.Get();
RwD3D9SetVertexShaderConstant(LOC_reflProps, (void*)reflProps, 1);

Expand Down Expand Up @@ -426,6 +416,7 @@ CarPipe::SpecularPass(RxD3D9ResEntryHeader *header, RpAtomic *atomic)
RwD3D9GetRenderState(D3DRS_LIGHTING, &lighting);
noRefl = CVisibilityPlugins__GetAtomicId(atomic) & 0x6000;

float lightmult = 1.85f * CCustomCarEnvMapPipeline__m_EnvMapLightingMult;
for(uint i = 0; i < header->numMeshes; i++){
RwUInt32 materialFlags = *(RwUInt32*)&inst->material->surfaceProps.specular;
bool hasSpec = !((materialFlags & 4) == 0 || !lighting);
Expand All @@ -434,8 +425,13 @@ CarPipe::SpecularPass(RxD3D9ResEntryHeader *header, RpAtomic *atomic)
hasSpec = false;

specData = *RWPLUGINOFFSET(CustomSpecMapPipeMaterialData*, inst->material, CCustomCarEnvMapPipeline__ms_specularMapPluginOffset);
if(hasSpec && !noRefl)
if(hasSpec && !noRefl){
RwSurfaceProperties surfprops;
surfprops.specular = specData->specularity*5.0f*config->neoSpecularityMult*lightmult;
if(surfprops.specular > 1.0f) surfprops.specular = 1.0f;
RwD3D9SetVertexShaderConstant(LOC_surfProps, &surfprops, 1);
D3D9Render(header, inst);
}
inst++;
}
RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, (void*)zwrite);
Expand Down
2 changes: 1 addition & 1 deletion src/neoWaterdrops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int WaterDrops::ms_numDropsMoving;
bool WaterDrops::ms_enabled;
bool WaterDrops::ms_movingEnabled;

int WaterDrops::ms_splashDuration;
int WaterDrops::ms_splashDuration = -1;

float WaterDrops::ms_distMoved, WaterDrops::ms_vecLen, WaterDrops::ms_rainStrength;
RwV3d WaterDrops::ms_vec;
Expand Down
Loading

0 comments on commit 10c0232

Please sign in to comment.