Skip to content

Commit d0e33fa

Browse files
committed
Implement night sight hardware effect with OpenGL
When the Night Sight hardware is enabled, let the shader draw everything at full light level and in black and white.
1 parent 21d5f58 commit d0e33fa

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Diff for: shaders/texture.frag

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ varying vec2 TexCoords;
99
varying float Light;
1010

1111
uniform sampler2D tex;
12+
uniform bool nightsight;
1213

1314
void main() {
1415
vec4 t = texture2D(tex, TexCoords);
1516

1617
// Alpha values > 0.5 are emissive
1718
float alpha = t.a > 0.5 ? 1.0 : t.a * 2.0;
18-
float emissive = t.a > 0.5 ? 1.0 : 0.0;
19-
float light = max(Light, emissive);
2019

21-
gl_FragColor = vec4(t.r * light, t.g * light, t.b * light, alpha);
20+
if (nightsight) {
21+
// approximate the blue tint of palette colors 0xb0..0xbf
22+
float gray = 0.40 * t.r + 0.59 * t.g + 0.11 * t.b;
23+
float rg = 0.7 * gray;
24+
float b = 0.75 * gray + 0.1;
25+
gl_FragColor = vec4(rg, rg, b, alpha);
26+
} else {
27+
float emissive = t.a > 0.5 ? 1.0 : 0.0;
28+
float light = max(Light, emissive);
29+
gl_FragColor = vec4(t.r * light, t.g * light, t.b * light, alpha);
30+
}
2231
}

Diff for: src/MacSrc/OpenGL.cc

+20-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern "C" {
3333
#include "Shock.h"
3434
#include "faketime.h"
3535
#include "render.h"
36+
#include "wares.h"
3637

3738
extern SDL_Renderer *renderer;
3839
extern SDL_Palette *sdlPalette;
@@ -54,6 +55,7 @@ struct Shader {
5455
GLuint shaderProgram;
5556
GLint uniView;
5657
GLint uniProj;
58+
GLint uniNightSight;
5759
GLint tcAttrib;
5860
GLint lightAttrib;
5961
};
@@ -203,10 +205,12 @@ static int CreateShader(const char *vertexShaderFile, const char *fragmentShader
203205
glAttachShader(shaderProgram, vertShader);
204206
glAttachShader(shaderProgram, fragShader);
205207
glLinkProgram(shaderProgram);
208+
glUseProgram(shaderProgram);
206209

207210
outShader->shaderProgram = shaderProgram;
208211
outShader->uniView = glGetUniformLocation(shaderProgram, "view");
209212
outShader->uniProj = glGetUniformLocation(shaderProgram, "proj");
213+
outShader->uniNightSight = glGetUniformLocation(shaderProgram, "nightsight");
210214
outShader->tcAttrib = glGetAttribLocation(shaderProgram, "texcoords");
211215
outShader->lightAttrib = glGetAttribLocation(shaderProgram, "light");
212216

@@ -391,6 +395,10 @@ static void updatePalette(SDL_Palette *palette, bool transparent) {
391395
}
392396
}
393397

398+
static bool nightsight_active() {
399+
return WareActive(player_struct.hardwarez_status[HARDWARE_GOGGLE_INFRARED]);
400+
}
401+
394402
void opengl_start_frame() {
395403
SDL_GL_MakeCurrent(window, context);
396404

@@ -424,6 +432,8 @@ void opengl_swap_and_restore() {
424432
GLint tcAttrib = textureShaderProgram.tcAttrib;
425433
GLint lightAttrib = textureShaderProgram.lightAttrib;
426434

435+
glUniform1i(textureShaderProgram.uniNightSight, nightsight_active());
436+
427437
glUniformMatrix4fv(textureShaderProgram.uniView, 1, false, IdentityMatrix);
428438
glUniformMatrix4fv(textureShaderProgram.uniProj, 1, false, IdentityMatrix);
429439

@@ -448,6 +458,8 @@ void opengl_swap_and_restore() {
448458

449459
glFlush();
450460

461+
glUniform1i(textureShaderProgram.uniNightSight, false);
462+
451463
// check OpenGL error
452464
GLenum err = glGetError();
453465
if (err != GL_NO_ERROR)
@@ -640,19 +652,21 @@ static void set_texture(grs_bitmap *bm) {
640652
static void draw_vertex(const g3s_point& vertex, GLint tcAttrib, GLint lightAttrib) {
641653

642654
// Default, per-vertex lighting
643-
float light = vertex.i / 4096.0f;
655+
float light = 1.0f - (vertex.i / 4096.0f);
644656

645-
// Could be a CLUT color instead, use that for lighting
646-
if(gr_get_fill_type() == FILL_CLUT) {
657+
if (nightsight_active()) {
658+
light = 1.0f;
659+
} else if (gr_get_fill_type() == FILL_CLUT) {
660+
// Could be a CLUT color instead, use that for lighting
647661
// Ugly hack: We don't get the original light value, so we have to
648662
// recalculate it from the offset into the global lighting lookup
649663
// table.
650664
uchar* clut = (uchar*)gr_get_fill_parm();
651-
light = (clut - grd_screen->ltab) / 4096.0f;
665+
light = 1.0f - (clut - grd_screen->ltab) / 4096.0f;
652666
}
653667

654668
glVertexAttrib2f(tcAttrib, vertex.uv.u / 256.0, vertex.uv.v / 256.0);
655-
glVertexAttrib1f(lightAttrib, 1.0f - light);
669+
glVertexAttrib1f(lightAttrib, light);
656670
glVertex3f(vertex.x / 65536.0f, vertex.y / 65536.0f, -vertex.z / 65536.0f);
657671
}
658672

@@ -727,7 +741,7 @@ int opengl_bitmap(grs_bitmap *bm, int n, grs_vertex **vpl, grs_tmap_info *ti) {
727741
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
728742

729743
float light = 1.0f;
730-
if (ti->flags & TMF_CLUT) {
744+
if ((ti->flags & TMF_CLUT) && !nightsight_active()) {
731745
// Ugly hack: We don't get the original 'i' value, so we have to
732746
// recalculate it from the offset into the global lighting lookup
733747
// table.

0 commit comments

Comments
 (0)