Skip to content

Commit

Permalink
OpenGL: Use framebuffer in GL(ES)2, fixes, code refactor
Browse files Browse the repository at this point in the history
OpenGL(ES) 2 changes:
- render to texture,
- rename "UseWindowSizeForFullScreen" to "FixedRenderingSize",
- change framebuffer size instead of screen resolution for
"FixedRenderingSize" option,
- fix gamma in some locations (#68, apply gamma in post-processing),
- MSAA is no longer available for OpenGL(ES) 2,
- fix textures brightness due to incorrect texture format conversion,
- fix triple-buffering flickering in some menu animations or loading
screen,
- some code refactor,
  • Loading branch information
zaps166 committed Mar 7, 2024
1 parent e937664 commit c3b29fa
Show file tree
Hide file tree
Showing 5 changed files with 718 additions and 353 deletions.
15 changes: 9 additions & 6 deletions Need For Speed II SE/nfs2se.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
# 0 - VSync disabled
# 1 - VSync enabled (default)
# >1 - VSync enabled, limit the framerate
#MSAA (Multisample antialiasing):
#MSAA (Multisample antialiasing, OpenGL 1 only):
# 0 - Don't change (default)
# 1 - Disable MSAA
# 2, 4, 8, 16 - Enable MSAA. If not supported, the game probably won't run.
#UseWindowSizeForFullScreen:
# 0 - Don't change screen size (default)
# 1 - Use screen size from "WindowSize"
#FixedRenderingSize:
# 0 - Window size determines game rendering size (default)
# 1 - Fixed game rendering size (value is specified in "WindowSize"):
# - OpenGL 1 - window has fixed size, full screen will change screen resolution
# - OpenGL(ES) 2 - framebuffer has fixed size
# 2 - Same as "1", but OpenGL(ES) 2 renderer doesn't use linear image upscaling
#WindowSize:
# Type your own window size (default: 640x480)
#KeepAspectRatio:
Expand Down Expand Up @@ -61,7 +64,7 @@
# 0 - Original samplerate - 22050Hz (default)
# 1 - Linear interpolated sound to 44100Hz (artificial highs)
#UseGlBleginGlEnd:
# Use glBegin(), glEnd() instead of glDrawArrays() - can be slower, disabled by default
# Use glBegin(), glEnd() instead of glDrawArrays() in OpenGL 1 - can be slower, disabled by default
#Port1:
# TCP and UDP port (default: 1030)
#Port2:
Expand All @@ -75,7 +78,7 @@
#StartInFullScreen=1
#VSync=-1
#MSAA=4
#UseWindowSizeForFullScreen=0
#FixedRenderingSize=1
#WindowSize=640x480
#KeepAspectRatio=1
#LinearTextureFiltering=1
Expand Down
51 changes: 26 additions & 25 deletions src/Glide2x.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// SPDX-License-Identifier: MIT

static void handleDpr();
static void swapBufferCommon();
#include "Glide2x.h"

static inline void handleDpr();
static inline BOOL clearUnusedArea(int32_t xOffset, int32_t yOffset, int32_t visibleWidth, int32_t visibleHeight);
static inline void convertColor(GrColor_t color, uint8_t *alpha, float *r, float *g, float *b, float *a);

#ifdef OPENGL1X
#include "Glide2x/OpenGL1.c"
#else
#include "Glide2x/OpenGL2.c"
#endif

static void handleDpr()
static inline void handleDpr()
{
extern double dpr;
SDL_GetWindowSize(sdlWin, &winWidth, &winHeight);
Expand All @@ -19,25 +22,11 @@ static void handleDpr()
winWidth *= dpr;
winHeight *= dpr;
}
static void swapBufferCommon()
static inline BOOL clearUnusedArea(int32_t xOffset, int32_t yOffset, int32_t visibleWidth, int32_t visibleHeight)
{
if (windowResized)
{
grClipWindow(0, 0, 640, 480);
windowResized = false;
}
else
{
drawTriangles();
}

if (keepAspectRatio && (xOffset > 0 || yOffset > 0))
{
GLint scissorBox[4] = {0, 0, winWidth, winHeight};
glGetIntegerv(GL_SCISSOR_BOX, scissorBox);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if (xOffset > 0)
{
glScissor(0, 0, xOffset, winHeight);
Expand All @@ -53,14 +42,26 @@ static void swapBufferCommon()
glScissor(0, 0, winWidth, winHeight - visibleHeight - yOffset);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glScissor(0, yOffset + visibleHeight, winWidth, yOffset);
glScissor(0, yOffset + visibleHeight, winWidth, yOffset + 1);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}

glScissor(scissorBox[0], scissorBox[1], scissorBox[2], scissorBox[3]);
return true;
}
return false;
}
static inline void convertColor(GrColor_t color, uint8_t *alpha, float *r, float *g, float *b, float *a)
{
/* Game uses ARGB color format */
*r = ((color & 0x00FF0000) >> 16) / 255.0f;
*g = ((color & 0x0000FF00) >> 8) / 255.0f;
*b = ((color & 0x000000FF) ) / 255.0f;
if (a)
{
if (alpha)
*a = *alpha / 255.0f;
else
*a = 1.0f;
}

SDL_GL_SwapWindow(sdlWin);
}

REALIGN STDCALL void grFogTable(const GrFog_t ft[GR_FOG_TABLE_SIZE])
Expand Down Expand Up @@ -90,7 +91,7 @@ REALIGN STDCALL void grFogTable(const GrFog_t ft[GR_FOG_TABLE_SIZE])

for (i = 0; i < GR_FOG_TABLE_SIZE; ++i)
for (j = intStartEnd[i]; j < intStartEnd[i + 1]; ++j)
fogTable[j] = (uint8_t)(glideFogTable[i] + (glideFogTable[i + 1] - glideFogTable[i]) * (j - intStartEnd[i]) / intEndMinusStart[i]);
g_fogTable[j] = (uint8_t)(glideFogTable[i] + (glideFogTable[i + 1] - glideFogTable[i]) * (j - intStartEnd[i]) / intEndMinusStart[i]);

// printf("grFogTable\n");
}
Expand Down
47 changes: 32 additions & 15 deletions src/Glide2x/OpenGL1.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

void SetBrightness(float);

#include <signal.h>

BOOL contextError = false;

#define MaxTriangles 0x000400
#define TextureMem 0x200000
#define VertexSnap 0x0C0000
Expand Down Expand Up @@ -52,7 +56,7 @@ typedef struct
} TextureInfo;
static TextureInfo textures[TextureMem >> 2];

static uint8_t *lfb, textureMem[TextureMem], fogTable[0x10000];
static uint8_t *lfb, textureMem[TextureMem], g_fogTable[0x10000];
static uint32_t *palette, tmpTexture[0x400];
static uint32_t trianglesCount, maxTexIdx;

Expand All @@ -74,15 +78,6 @@ static void setTextureFiltering()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

static inline void convertColor(GrColor_t color, float *r, float *g, float *b, float *a)
{
/* Game uses ARGB color format */
*a = ((color & 0xFF000000) >> 24) / 255.0;
*r = ((color & 0x00FF0000) >> 16) / 255.0;
*g = ((color & 0x0000FF00) >> 8) / 255.0;
*b = ((color & 0x000000FF) ) / 255.0;
}

static inline void drawTriangles()
{
if (trianglesCount)
Expand Down Expand Up @@ -165,7 +160,7 @@ REALIGN STDCALL void grClipWindow(uint32_t minX, uint32_t minY, uint32_t maxX, u
REALIGN STDCALL void grBufferClear(GrColor_t color, GrAlpha_t alpha, uint16_t depth)
{
float r, g , b, a;
convertColor(color, &r, &g, &b, &a);
convertColor(color, &alpha, &r, &g, &b, &a);

// printf("grBufferClear: %X %X %X [%d]\n", color, alpha, depth, trianglesCount);

Expand All @@ -189,7 +184,23 @@ REALIGN STDCALL void grChromakeyValue(GrColor_t value)
REALIGN STDCALL void grBufferSwap(int swap_interval)
{
// printf("grBufferSwap: [%d]\n", trianglesCount);
swapBufferCommon();

if (windowResized)
{
grClipWindow(0, 0, 640, 480);
windowResized = false;
}
else
{
drawTriangles();
}

GLint scissorBox[4] = {};
glGetIntegerv(GL_SCISSOR_BOX, scissorBox);
if (clearUnusedArea(xOffset, yOffset, visibleWidth, visibleHeight))
glScissor(scissorBox[0], scissorBox[1], scissorBox[2], scissorBox[3]);

SDL_GL_SwapWindow(sdlWin);
}
REALIGN STDCALL void grColorCombine(GrCombineFunction_t function, GrCombineFactor_t factor, GrCombineLocal_t local, GrCombineOther_t other, BOOL invert)
{
Expand Down Expand Up @@ -237,7 +248,7 @@ REALIGN STDCALL void grDrawTriangle(const GrVertex *a, const GrVertex *b, const

glColor4f(grVertex->r / 255.0f, grVertex->g / 255.0f, grVertex->b / 255.0f, grVertex->a / 255.0f);
if (p_glFogCoordf)
p_glFogCoordf(fogTable[(uint16_t)(1.0f / grVertex->oow)] / 255.0f);
p_glFogCoordf(g_fogTable[(uint16_t)(1.0f / grVertex->oow)] / 255.0f);
glTexCoord4f(grVertex->tmuvtx[0].sow / 256.0f, grVertex->tmuvtx[0].tow / 256.0f, 0.0f, grVertex->oow);
glVertex3f(grVertex->x - VertexSnap, grVertex->y - VertexSnap, grVertex->oow);
}
Expand All @@ -254,7 +265,7 @@ REALIGN STDCALL void grDrawTriangle(const GrVertex *a, const GrVertex *b, const
colorValues[trianglesCount].vertex[i].b = grVertex->b / 255.0f;
colorValues[trianglesCount].vertex[i].a = grVertex->a / 255.0f;

fogCoord[trianglesCount].vertex[i] = fogTable[(uint16_t)(1.0f / grVertex->oow)] / 255.0f;
fogCoord[trianglesCount].vertex[i] = g_fogTable[(uint16_t)(1.0f / grVertex->oow)] / 255.0f;

textureCoord[trianglesCount].vertex[i].s = grVertex->tmuvtx[0].sow / 256.0f;
textureCoord[trianglesCount].vertex[i].t = grVertex->tmuvtx[0].tow / 256.0f;
Expand Down Expand Up @@ -286,8 +297,8 @@ REALIGN STDCALL void grDrawLine(const GrVertex *a, const GrVertex *b)
REALIGN STDCALL void grFogColorValue(GrColor_t fogcolor)
{
float fogColor[4];
convertColor(fogcolor, fogColor + 0, fogColor + 1, fogColor + 2, fogColor + 3);
drawTriangles();
convertColor(fogcolor, NULL, &fogColor[0], &fogColor[1], &fogColor[2], &fogColor[3]);
glFogfv(GL_FOG_COLOR, fogColor);
// printf("grFogColorValue: 0x%.8X [%d]\n", fogcolor, trianglesCount);
}
Expand Down Expand Up @@ -377,6 +388,12 @@ REALIGN STDCALL void grSstWinClose(void)
REALIGN STDCALL BOOL grSstWinOpen(uint32_t hWnd, GrScreenResolution_t screen_resolution, GrScreenRefresh_t refresh_rate, GrColorFormat_t color_format, GrOriginLocation_t origin_location, int nColBuffers, int nAuxBuffers)
{
glCtx = SDL_GL_CreateContext(sdlWin);
if (!glCtx)
{
contextError = true;
raise(SIGABRT);
}

handleDpr();

if (vSync >= 0)
Expand Down
Loading

0 comments on commit c3b29fa

Please sign in to comment.