Skip to content

Commit

Permalink
(WIP) tr2: use hybrid OpenGL/DirectX rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Nov 28, 2024
1 parent 072def7 commit a3a7ec1
Show file tree
Hide file tree
Showing 44 changed files with 992 additions and 1,611 deletions.
16 changes: 13 additions & 3 deletions data/tr1/ship/shaders/2d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@ void main(void) {
// Fragment shader

uniform sampler2D tex0;
uniform sampler1D pal0;
uniform bool paletteEnabled;

#ifdef OGL33C
#define OUTCOLOR outColor
#define TEXTURE texture
#define TEXTURE2D texture
#define TEXTURE1D texture

in vec2 vertTexCoords;
out vec4 outColor;
#else
#define OUTCOLOR gl_FragColor
#define TEXTURE texture2D
#define TEXTURE2D texture2D
#define TEXTURE1D texture1D

varying vec2 vertTexCoords;
#endif

void main(void) {
OUTCOLOR = TEXTURE(tex0, vertTexCoords);
if (paletteEnabled) {
float paletteIndex = TEXTURE2D(tex0, vertTexCoords).r;
vec4 tmp = TEXTURE1D(pal0, paletteIndex);
OUTCOLOR = vec4(paletteIndex, tmp.g, tmp.b, 0);
} else {
OUTCOLOR = TEXTURE2D(tex0, vertTexCoords);
}
}
#endif // VERTEX
47 changes: 47 additions & 0 deletions data/tr2/ship/shaders/2d.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifdef VERTEX
// Vertex shader

#ifdef OGL33C
out vec2 vertTexCoords;
#else
varying vec2 vertTexCoords;
#endif

layout(location = 0) in vec2 inPosition;

void main(void) {
vertTexCoords = inPosition;
gl_Position = vec4(vertTexCoords * vec2(2.0, -2.0) + vec2(-1.0, 1.0), 0.0, 1.0);
}

#else
// Fragment shader

uniform sampler2D tex0;
uniform sampler1D pal0;
uniform bool paletteEnabled;

#ifdef OGL33C
#define OUTCOLOR outColor
#define TEXTURE2D texture
#define TEXTURE1D texture

in vec2 vertTexCoords;
out vec4 outColor;
#else
#define OUTCOLOR gl_FragColor
#define TEXTURE2D texture2D
#define TEXTURE1D texture1D

varying vec2 vertTexCoords;
#endif

void main(void) {
if (paletteEnabled) {
float paletteIndex = TEXTURE2D(tex0, vertTexCoords).r;
OUTCOLOR = TEXTURE1D(pal0, paletteIndex);
} else {
OUTCOLOR = TEXTURE2D(tex0, vertTexCoords);
}
}
#endif // VERTEX
76 changes: 76 additions & 0 deletions data/tr2/ship/shaders/3d.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifdef VERTEX
// Vertex shader

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inTexCoords;
layout(location = 2) in vec4 inColor;

uniform mat4 matProjection;
uniform mat4 matModelView;

#ifdef OGL33C
out vec4 vertColor;
out vec3 vertTexCoords;
#else
varying vec4 vertColor;
varying vec3 vertTexCoords;
#endif

void main(void) {
gl_Position = matProjection * matModelView * vec4(inPosition, 1);
vertColor = inColor / 255.0;
vertTexCoords = inTexCoords;
}

#else
// Fragment shader

uniform sampler2D tex0;
uniform bool texturingEnabled;
uniform bool smoothingEnabled;

#ifdef OGL33C
#define OUTCOLOR outColor
#define TEXTURESIZE textureSize
#define TEXTURE texture
#define TEXELFETCH texelFetch

in vec4 vertColor;
in vec3 vertTexCoords;
out vec4 OUTCOLOR;
#else
#define OUTCOLOR gl_FragColor
#define TEXTURESIZE textureSize2D
#define TEXELFETCH texelFetch2D
#define TEXTURE texture2D

varying vec4 vertColor;
varying vec3 vertTexCoords;
#endif

void main(void) {
OUTCOLOR = vertColor;

if (texturingEnabled) {
#if defined(GL_EXT_gpu_shader4) || defined(OGL33C)
if (smoothingEnabled) {
// do not use smoothing for chroma key
ivec2 size = TEXTURESIZE(tex0, 0);
int tx = int((vertTexCoords.x / vertTexCoords.z) * size.x) % size.x;
int ty = int((vertTexCoords.y / vertTexCoords.z) * size.y) % size.y;
vec4 texel = TEXELFETCH(tex0, ivec2(tx, ty), 0);
if (texel.a == 0.0) {
discard;
}
}
#endif

vec4 texColor = TEXTURE(tex0, vertTexCoords.xy / vertTexCoords.z);
if (texColor.a == 0.0) {
discard;
}

OUTCOLOR = vec4(OUTCOLOR.rgb * texColor.rgb, texColor.a);
}
}
#endif // VERTEX
38 changes: 38 additions & 0 deletions data/tr2/ship/shaders/fbo.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifdef VERTEX
// Vertex shader

layout(location = 0) in vec2 inPosition;

#ifdef OGL33C
out vec2 vertTexCoords;
#else
varying vec2 vertTexCoords;
#endif

void main(void) {
vertTexCoords = inPosition;
gl_Position = vec4(vertTexCoords * vec2(2.0, 2.0) + vec2(-1.0, -1.0), 0.0, 1.0);
}

#else
// Fragment shader

uniform sampler2D tex0;

#ifdef OGL33C
#define OUTCOLOR outColor
#define TEXTURE texture

in vec2 vertTexCoords;
out vec4 OUTCOLOR;
#else
#define OUTCOLOR gl_FragColor
#define TEXTURE texture2D

varying vec2 vertTexCoords;
#endif

void main(void) {
OUTCOLOR = TEXTURE(tex0, vertTexCoords);
}
#endif // VERTEX
5 changes: 5 additions & 0 deletions docs/tr2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## [Unreleased](https://github.com/LostArtefacts/TRX/compare/tr2-0.6...develop) - ××××-××-××
- switched rendering to use hybrid OpenGL+DirectX approach
- (WIP) changed fullscreen behavior to use windowed desktop mode
- changed the F2 key behavior (F2 reduces screen size, Shift+F2 increases it again)
- removed triple buffering option
- removed dither option (which seems to have had no effect)
- added support for custom levels to enforce values for any config setting (#1846)
- added an option to fix inventory item usage duplication (#1586)
- added optional automatic key/puzzle inventory item pre-selection (#1884)
Expand Down
8 changes: 4 additions & 4 deletions docs/tr2/progress.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 31 additions & 10 deletions docs/tr2/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ typedef struct __unaligned {
bool dither;
bool zbuffer;
bool bilinear_filtering;
bool triple_buffering;
bool triple_buffering; // TODO: remove this option
bool fullscreen;
bool sound_enabled;
bool lara_mic;
Expand All @@ -207,16 +207,37 @@ typedef struct __unaligned {
} TEXPAGE_DESC;

typedef struct __unaligned {
uint8_t red;
uint8_t green;
uint8_t blue;
union {
uint8_t red;
uint8_t r;
};
union {
uint8_t green;
uint8_t g;
};
union {
uint8_t blue;
uint8_t b;
};
} RGB_888;

typedef struct __unaligned {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
union {
uint8_t red;
uint8_t r;
};
union {
uint8_t green;
uint8_t g;
};
union {
uint8_t blue;
uint8_t b;
};
union {
uint8_t alpha;
uint8_t a;
};
} RGBA_8888;

typedef struct {
Expand Down Expand Up @@ -3789,9 +3810,9 @@ typedef enum {
0x00443B50 0x00B9 +R int32_t __cdecl BGND_AddTexture(int32_t tile_idx, BYTE *bitmap, int32_t pal_index, RGB_888 *bmp_pal);
0x00443C10 0x0032 +R void __cdecl BGND_GetPageHandles(void);
0x00443C50 0x005F +R void __cdecl BGND_DrawInGameBlack(void);
0x00443CB0 0x00DC +R void __cdecl DrawQuad(float sx, float sy, float width, float height, D3DCOLOR color);
0x00443CB0 0x00DC +R void __cdecl BGND_DrawQuad(float sx, float sy, float width, float height, D3DCOLOR color);
0x00443D90 0x0220 +R void __cdecl BGND_DrawInGameBackground(void);
0x00443FB0 0x0251 +R void __cdecl DrawTextureTile(int32_t sx, int32_t sy, int32_t width, int32_t height, HWR_TEXTURE_HANDLE tex_source, int32_t tu, int32_t tv, int32_t t_width, int32_t t_height, D3DCOLOR color0, D3DCOLOR color1, D3DCOLOR color2, D3DCOLOR color3);
0x00443FB0 0x0251 +R void __cdecl BGND_DrawTextureTile(int32_t sx, int32_t sy, int32_t width, int32_t height, HWR_TEXTURE_HANDLE tex_source, int32_t tu, int32_t tv, int32_t t_width, int32_t t_height, D3DCOLOR color0, D3DCOLOR color1, D3DCOLOR color2, D3DCOLOR color3);
0x00444210 0x008B +R D3DCOLOR __cdecl BGND_CenterLighting(int32_t x, int32_t y, int32_t width, int32_t height);
0x004444C0 0x004D +R void __cdecl BGND_Free(void);
0x00444510 0x0030 +R bool __cdecl BGND_Init(void);
Expand Down
61 changes: 61 additions & 0 deletions src/libtrx/game/shell/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "game/shell.h"
#include "log.h"
#include "memory.h"

static void M_ShowFatalError(const char *const message)
{
LOG_ERROR("%s", message);
SDL_Window *const window = Shell_GetWindow();
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, "Tomb Raider Error", message, window);
Shell_Terminate(1);
}

void Shell_Terminate(int32_t exit_code)
{
Shell_Shutdown();

SDL_Window *const window = Shell_GetWindow();
if (window != NULL) {
SDL_DestroyWindow(window);
}
SDL_Quit();
exit(exit_code);
}

void Shell_ExitSystem(const char *message)
{
M_ShowFatalError(message);
Shell_Shutdown();
}

void Shell_ExitSystemFmt(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
int32_t size = vsnprintf(NULL, 0, fmt, va) + 1;
char *message = Memory_Alloc(size);
va_end(va);

va_start(va, fmt);
vsnprintf(message, size, fmt, va);
va_end(va);

Shell_ExitSystem(message);

Memory_FreePointer(&message);
}

int32_t Shell_GetCurrentDisplayWidth(void)
{
SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(0, &dm);
return dm.w;
}

int32_t Shell_GetCurrentDisplayHeight(void)
{
SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(0, &dm);
return dm.h;
}
Loading

0 comments on commit a3a7ec1

Please sign in to comment.