Skip to content

Commit

Permalink
Initial shader support, add "High" effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedSnark committed Apr 4, 2024
1 parent db3d1ce commit 72a0ca5
Show file tree
Hide file tree
Showing 19 changed files with 33,214 additions and 7 deletions.
1 change: 1 addition & 0 deletions GSChaos/CChaos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ void CChaos::FeatureInit()
RegisterChaosFeature<CFeatureNice>();
RegisterChaosFeature<CFeatureHalfGravity>();
RegisterChaosFeature<CFeatureRollin>();
RegisterChaosFeature<CFeatureHigh>();

RegisterChaosFeature<CFeatureCombineEffects>(); // must be last!!!

Expand Down
23 changes: 23 additions & 0 deletions GSChaos/CFeatureHigh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "includes.h"

void CFeatureHigh::Init()
{
CChaosFeature::Init();
}

void CFeatureHigh::ActivateFeature()
{
CChaosFeature::ActivateFeature();
g_bActivatedShader = true;
}

void CFeatureHigh::DeactivateFeature()
{
CChaosFeature::DeactivateFeature();
g_bActivatedShader = false;
}

const char* CFeatureHigh::GetFeatureName()
{
return "High";
}
37 changes: 37 additions & 0 deletions GSChaos/CFeatureHigh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright - ScriptedSnark, 2024.
* CFeatureHigh.h
*
* Project (GSChaos) header file
* Authors: ScriptedSnark.
* Do not delete this comment block. Respect others' work!
*/

#ifdef CFEATUREHIGH_H_RECURSE_GUARD
#error Recursive header files inclusion detected in CFeatureHigh.h
#else //CFEATUREHIGH_H_RECURSE_GUARD

#define CFEATUREHIGH_H_RECURSE_GUARD

#ifndef CFEATUREHIGH_H_GUARD
#define CFEATUREHIGH_H_GUARD
#pragma once

#ifdef __cplusplus

class CFeatureHigh : public CChaosFeature
{
void Init() override;
void ActivateFeature() override;
void DeactivateFeature() override;
const char* GetFeatureName() override;
};

#else //!__cplusplus
#error C++ compiler required to compile CFeatureHigh.h
#endif //__cplusplus

#endif //CFEATUREHIGH_H_GUARD

#undef CFEATUREHIGH_H_RECURSE_GUARD
#endif //CFEATUREHIGH_H_RECURSE_GUARD
38 changes: 37 additions & 1 deletion GSChaos/GSChaos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ _V_CalcRefdef ORIG_V_CalcRefdef = NULL;
typedef void (*_LoadThisDll)(char* szDllFilename);
typedef void (*_LoadEntityDLLs)(char* szBaseDir);
typedef void (*_ServerActivate)(edict_t* pEdictList, int edictCount, int clientMax);
typedef void (*_R_DrawWorld)();

_LoadThisDll ORIG_LoadThisDll = NULL;
_LoadEntityDLLs ORIG_LoadEntityDLLs = NULL;
_ServerActivate ORIG_ServerActivate = NULL;
_R_DrawWorld ORIG_R_DrawWorld = NULL;

Utils utils = Utils::Utils(NULL, NULL, NULL);

Expand Down Expand Up @@ -57,6 +59,8 @@ bool g_bPreSteamPipe = false;

bool g_bEncrypted = false;

bool g_bActivatedShader = false;

extern texture_t** r_notexture_mip;
extern volatile dma_t* shm;

Expand All @@ -73,23 +77,37 @@ _wglSwapBuffers GetSwapBuffersAddr()
return reinterpret_cast<_wglSwapBuffers>(GetProcAddress(LoadLibrary(TEXT("OpenGL32.dll")), "wglSwapBuffers"));
}

GLuint program;

int __stdcall HOOKED_wglSwapBuffers(HDC a1)
{
static bool initialized = false;

if (!initialized)
{
GLenum result = glewInit();
if (result != GLEW_OK)
{
MessageBoxA(NULL, "Failed to initialize GLEW. Exiting...\n", "GSChaos", MB_OK | MB_ICONERROR);
exit(1);
}

HookEngine();
HookClient();
MH_EnableHook(MH_ALL_HOOKS);

g_hWndOfGame = WindowFromDC(a1);

ORIG_WndProc = reinterpret_cast<WNDPROC>(SetWindowLongPtrA(g_hWndOfGame, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(HOOKED_WndProc)));

gImGui.Init();
gImGui.InitBackends(g_hWndOfGame);
gChaos.LoadFonts();

GLuint fragment = OpenGLShader::CreateFragmentShader("chaos/frag.glsl");
//GLuint vertex = OpenGLShader::CreateVertexShader("chaos/vertex.glsl");
program = OpenGLShader::CreateProgram(NULL, fragment);

initialized = true;
}

Expand Down Expand Up @@ -241,6 +259,22 @@ void HOOKED_LoadEntityDLLs(char* szBaseDir)
MH_EnableHook(MH_ALL_HOOKS);
}

void HOOKED_R_DrawWorld()
{
if (!g_bActivatedShader)
{
ORIG_R_DrawWorld();
return;
}

glUseProgram(program);
glUniform2f(glGetUniformLocation(program, "iResolution"), ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y);
glUniform1f(glGetUniformLocation(program, "iTime"), (float)gChaos.GetGlobalTime());
ORIG_R_DrawWorld();

glUseProgram(0);
}

void HookClient()
{
g_lpClient = GetModuleHandle("client.dll");
Expand Down Expand Up @@ -551,8 +585,10 @@ void HookEngine()

SPTFind(LoadThisDll);
SPTFind(LoadEntityDLLs);
SPTFind(R_DrawWorld);
EngineCreateHook(LoadThisDll);
EngineCreateHook(LoadEntityDLLs);
EngineCreateHook(R_DrawWorld);

MH_EnableHook(MH_ALL_HOOKS);
}
Expand Down
16 changes: 10 additions & 6 deletions GSChaos/GSChaos.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>WIN32;BUILD_HL25;GS_DEBUG;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;BUILD_HL25;GLEW_STATIC;GS_DEBUG;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>../external/MinHook;../external/sptlib;../external/imgui;../external/sdl2;../external/sdl2/include;../external/miniaudio;../external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>../external/MinHook;../external/sptlib;../external/imgui;../external/sdl2;../external/sdl2/include;../external/miniaudio;../external/glew/include;../external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wsock32.lib;Dbghelp.lib;../external/funchook/funchook.lib;../external/libircclient.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>wsock32.lib;Dbghelp.lib;../external/funchook/funchook.lib;../external/libircclient.lib;../external/glew/lib/glew32s.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>
</ModuleDefinitionFile>
<AdditionalOptions>/MAP %(AdditionalOptions)</AdditionalOptions>
Expand All @@ -80,18 +80,18 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>WIN32;BUILD_HL25;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;BUILD_HL25;GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalIncludeDirectories>../external/MinHook;../external/sptlib;../external/imgui;../external/sdl2;../external/sdl2/include;../external/miniaudio;../external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>../external/MinHook;../external/sptlib;../external/imgui;../external/sdl2;../external/sdl2/include;../external/miniaudio;../external/glew/include;../external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wsock32.lib;Dbghelp.lib;../external/funchook/funchook.lib;../external/libircclient.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>wsock32.lib;Dbghelp.lib;../external/funchook/funchook.lib;../external/libircclient.lib;../external/glew/lib/glew32s.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>
</ModuleDefinitionFile>
<AdditionalOptions>/MAP %(AdditionalOptions)</AdditionalOptions>
Expand Down Expand Up @@ -142,6 +142,7 @@
<ClCompile Include="CFeatureGiveOneHP.cpp" />
<ClCompile Include="CFeatureGiveRandomWeapon.cpp" />
<ClCompile Include="CFeatureHalfGravity.cpp" />
<ClCompile Include="CFeatureHigh.cpp" />
<ClCompile Include="CFeatureHL2Movement.cpp" />
<ClCompile Include="CFeatureInsaneStepsize.cpp" />
<ClCompile Include="CFeatureMakeItBunDem.cpp" />
Expand Down Expand Up @@ -206,6 +207,7 @@
<ClCompile Include="GSChaos.cpp" />
<ClCompile Include="imgui_manager.cpp" />
<ClCompile Include="parsemsg.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -271,6 +273,7 @@
<ClInclude Include="CFeatureGiveOneHP.h" />
<ClInclude Include="CFeatureGiveRandomWeapon.h" />
<ClInclude Include="CFeatureHalfGravity.h" />
<ClInclude Include="CFeatureHigh.h" />
<ClInclude Include="CFeatureHL2Movement.h" />
<ClInclude Include="CFeatureInsaneStepsize.h" />
<ClInclude Include="CFeatureMakeItBunDem.h" />
Expand Down Expand Up @@ -345,6 +348,7 @@
<ClInclude Include="pricedown.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="sf_arborcrest.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions GSChaos/GSChaos.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@
<ClCompile Include="CFeatureRollin.cpp">
<Filter>Effects</Filter>
</ClCompile>
<ClCompile Include="Shader.cpp" />
<ClCompile Include="CFeatureHigh.cpp">
<Filter>Effects</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="includes.h" />
Expand Down Expand Up @@ -685,6 +689,10 @@
<ClInclude Include="CFeatureRollin.h">
<Filter>Effects</Filter>
</ClInclude>
<ClInclude Include="Shader.h" />
<ClInclude Include="CFeatureHigh.h">
<Filter>Effects</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Hooking">
Expand Down
115 changes: 115 additions & 0 deletions GSChaos/Shader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "includes.h"

namespace OpenGLShader
{
std::string loadShaderFromFile(const std::string& fileName)
{
std::ifstream shaderFile(fileName);
std::stringstream shaderStream;
shaderStream << shaderFile.rdbuf();
return shaderStream.str();
}

GLuint CreateVertexShader(const std::string& fileName)
{
std::string shaderSource = loadShaderFromFile(fileName);
const char* shaderSourceCStr = shaderSource.c_str();

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &shaderSourceCStr, NULL);
glCompileShader(vertexShader);

// checking for errors
GLint success;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
printf("Error compiling vertex shader: %s\n", infoLog);
return 0;
}

return vertexShader;
}

GLuint CreateFragmentShader(const std::string& fileName)
{
std::string shaderSource = loadShaderFromFile(fileName);
const char* shaderSourceCStr = shaderSource.c_str();

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &shaderSourceCStr, NULL);
glCompileShader(fragmentShader);

// checking for errors
GLint success;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
printf("Error compiling fragment shader: %s\n", infoLog);
return 0;
}

return fragmentShader;
}

GLuint CreateProgram(GLuint vertexShader, GLuint fragmentShader)
{
// shader program creation
GLuint shaderProgram = glCreateProgram();

if (vertexShader)
glAttachShader(shaderProgram, vertexShader);

if (fragmentShader)
glAttachShader(shaderProgram, fragmentShader);

glLinkProgram(shaderProgram);

// checking for compiler & linker errors
GLint success;
GLchar infoLog[512];

if (vertexShader)
{
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
printf("Error compiling vertex shader: %s\n", infoLog);
}
}

if (fragmentShader)
{
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);

if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
printf("Error compiling fragment shader: %s\n", infoLog);
}
}

glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);

if (!success)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
printf("Error linking shader program: %s\n", infoLog);
}

return shaderProgram;
}

void Cleanup(GLuint vertexShader, GLuint fragmentShader)
{
// cleaning up shaders
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
};
Loading

0 comments on commit 72a0ca5

Please sign in to comment.