//
// Modifications: By 85
// Credits: Xantrax, MS Detours 2.1
// elhacker.net
// etalking.com.ar
// david_bs@live.com
// 2013
//
///////////////////////////////////////////////////////////////////////////////////////
#pragma comment(lib,"detours.lib")// 1.5
#pragma comment(lib,"OpenGL32.lib")
#include <windows.h>
#include <gl/gl.h>
#include<stdio.h>
#include "detours.h"
///////////////////////////////////////////////////////////////////////////////////////
bool bOGLSubtractive = false;
bool g_bSky = false;
///////////////////////////////////////////////////////////////////////////////////////
class CDraw {
public:
CDraw();
int m_iVpCounter;
bool m_bDrawing;
void DrawPanel();
};
CDraw::CDraw( ){
m_iVpCounter = 0;
m_bDrawing = false;
}
void CDraw::DrawPanel( ) {
//Drawing Every 5th here
}
CDraw* pDraw = new CDraw();
///////////////////////////////////////////////////////////////////////////////////////
/* function prototype and original target */
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glBegin( GLenum mode ), glBegin );
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glViewport( GLint x, GLint y, GLsizei width, GLsizei height ), glViewport );
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glVertex3fv( const GLfloat *v ), glVertex3fv );
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glVertex3f( GLfloat x, GLfloat y, GLfloat z ), glVertex3f );
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glShadeModel( GLenum mode ), glShadeModel );
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glClear( GLbitfield mask ), glClear );
DETOUR_TRAMPOLINE( VOID WINAPI Trampoline_glColor3f( GLfloat red, GLfloat green, GLfloat blue ), glColor3f );
typedef void (WINAPI* wglSwapBuffers_t)(HDC);
///////////////////////////////////////////////////////////////////////////////////////
//#define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16))
DETOUR_TRAMPOLINE(FARPROC WINAPI Trampoline_GetProcAddress(HMODULE hModule,
LPCSTR lpProcName), GetProcAddress);
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glBeginDetour(GLenum mode)
{
if(mode == GL_TRIANGLE_STRIP || mode == GL_TRIANGLE_FAN)
glDepthRange( 0, 0.5 );
else
glDepthRange( 0.5, 1 );
if(mode == GL_TRIANGLE_STRIP || mode == GL_TRIANGLE_FAN)
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
if( mode == GL_QUADS )
g_bSky = true;
else
g_bSky = false;
Trampoline_glBegin(mode);
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glClearDetour(GLbitfield mask)
{
if(mask==GL_DEPTH_BUFFER_BIT)
{
mask+=GL_COLOR_BUFFER_BIT;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Trampoline_glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
return;
}
Trampoline_glClear(mask);
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glShadeModelDetour(GLenum mode)
{
//XQZ2 wallhack
if( mode == GL_SMOOTH ){
__asm {
push 0x00000100 //GL_DEPTH_BUFFER_BIT
call dword ptr[ Trampoline_glClear ]
}
}
Trampoline_glShadeModel(mode);
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glViewportDetour(GLint x, GLint y, GLsizei width, GLsizei height)
{
pDraw->m_iVpCounter++;
if( pDraw->m_iVpCounter >= 5 ){
pDraw->m_bDrawing = true;
}
Trampoline_glViewport(x, y, width, height);
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glColor3fDetour(GLfloat red, GLfloat green, GLfloat blue)
{
Trampoline_glColor3f(red, green, blue);
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glVertex3fvDetour(const GLfloat* v)
{
if(g_bSky && v[2]>300){
return;
/*float flZero = 0.0f;
__asm {
push flZero
push flZero
push flZero
call dword ptr[ Trampoline_glColor3f ]
}*/
}
Trampoline_glVertex3fv(v);
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI glVertex3fDetour(GLfloat x, GLfloat y, GLfloat z)
{
Trampoline_glVertex3f(x, y, z);
}
///////////////////////////////////////////////////////////////////////////////////////
wglSwapBuffers_t pwglSwapBuffers = NULL;
// DETOUR_TRAMPOLINE(void WINAPI _wglSwapBuffers(HDC hDC), wglSwapBuffers);
// since wglSwapBuffers is undefined, the macro wont work. this is, what it basically does:
static PVOID __fastcall _Detours_GetVA_wglSwapBuffers(VOID)
{
return pwglSwapBuffers;
}
///////////////////////////////////////////////////////////////////////////////////////
__declspec(naked) void WINAPI _Trampoline_wglSwapBuffers(HDC hDC)
{
__asm { nop };
__asm { nop };
__asm { call _Detours_GetVA_wglSwapBuffers };
__asm { jmp eax };
__asm { ret };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
__asm { nop };
}
///////////////////////////////////////////////////////////////////////////////////////
VOID WINAPI _wglSwapBuffers(HDC hDC)
{
pDraw->m_iVpCounter = 0;
_Trampoline_wglSwapBuffers( hDC );
}
///////////////////////////////////////////////////////////////////////////////////////
void DetourOpenGL()
{
#define Detour( pbTrampFunc, pbDetourFunc )\
DetourFunctionWithTrampoline( (PBYTE)##pbTrampFunc,( PBYTE )##pbDetourFunc )
Detour( Trampoline_glBegin, glBeginDetour );
Detour( Trampoline_glVertex3fv, glVertex3fvDetour );
Detour( Trampoline_glClear, glClearDetour );
/*
Detour( Trampoline_glShadeModel, glShadeModelDetour );
Detour( Trampoline_glViewport, glViewportDetour );
Detour( Trampoline_glVertex3f, glVertex3fDetour );
// Because wglSwapBuffers isnt declared in GL\GL.h...
pwglSwapBuffers = (wglSwapBuffers_t)GetProcAddress(LoadLibrary("opengl32.dll"),
"wglSwapBuffers");
Detour( _Trampoline_wglSwapBuffers, _wglSwapBuffers);
Detour( Trampoline_glColor3f, glColor3fDetour );*/
}
///////////////////////////////////////////////////////////////////////////////////////
void RemoveOpenGLDetour()
{
#define Destroy( pbTrampFunc, pbDetourFunc )\
DetourRemove( (PBYTE)##pbTrampFunc,( PBYTE )##pbDetourFunc )
Destroy( Trampoline_glBegin, glBeginDetour );
Destroy( Trampoline_glVertex3fv, glVertex3fvDetour );
Destroy( Trampoline_glClear, glClearDetour );
/*
Destroy( Trampoline_glShadeModel, glShadeModelDetour );
Destroy( Trampoline_glViewport, glViewportDetour );
Destroy( Trampoline_glVertex3f, glVertex3fDetour );
Destroy( Trampoline_glColor3f, glColor3fDetour );*/
}
///////////////////////////////////////////////////////////////////////////////////////
FARPROC WINAPI GetProcAddressDetour(HMODULE hModule, LPCSTR lpProcName)
{
FARPROC retval = Trampoline_GetProcAddress(hModule, lpProcName);
return retval;
}
///////////////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH://Let the magic happen...:->
DetourOpenGL();
//Extras
/* DetourFunctionWithTrampoline((PBYTE)Trampoline_GetProcAddress,
(PBYTE)GetProcAddressDetour);*/
break;
case DLL_PROCESS_DETACH:
RemoveOpenGLDetour();
break;
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////