Skip to content

Commit c2c375f

Browse files
authored
Merge pull request #141 from inguin/opengl-nightsight
Implement night sight hardware effect with OpenGL
2 parents 6ed78f8 + d0e33fa commit c2c375f

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

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
}

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
};
@@ -204,10 +206,12 @@ static int CreateShader(const char *vertexShaderFile, const char *fragmentShader
204206
glAttachShader(shaderProgram, vertShader);
205207
glAttachShader(shaderProgram, fragShader);
206208
glLinkProgram(shaderProgram);
209+
glUseProgram(shaderProgram);
207210

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

@@ -393,6 +397,10 @@ static void updatePalette(SDL_Palette *palette, bool transparent) {
393397
}
394398
}
395399

400+
static bool nightsight_active() {
401+
return WareActive(player_struct.hardwarez_status[HARDWARE_GOGGLE_INFRARED]);
402+
}
403+
396404
void opengl_start_frame() {
397405
SDL_GL_MakeCurrent(window, context);
398406

@@ -426,6 +434,8 @@ void opengl_swap_and_restore() {
426434
GLint tcAttrib = textureShaderProgram.tcAttrib;
427435
GLint lightAttrib = textureShaderProgram.lightAttrib;
428436

437+
glUniform1i(textureShaderProgram.uniNightSight, nightsight_active());
438+
429439
glUniformMatrix4fv(textureShaderProgram.uniView, 1, false, IdentityMatrix);
430440
glUniformMatrix4fv(textureShaderProgram.uniProj, 1, false, IdentityMatrix);
431441

@@ -450,6 +460,8 @@ void opengl_swap_and_restore() {
450460

451461
glFlush();
452462

463+
glUniform1i(textureShaderProgram.uniNightSight, false);
464+
453465
// check OpenGL error
454466
GLenum err = glGetError();
455467
if (err != GL_NO_ERROR)
@@ -642,19 +654,21 @@ static void set_texture(grs_bitmap *bm) {
642654
static void draw_vertex(const g3s_point& vertex, GLint tcAttrib, GLint lightAttrib) {
643655

644656
// Default, per-vertex lighting
645-
float light = vertex.i / 4096.0f;
657+
float light = 1.0f - (vertex.i / 4096.0f);
646658

647-
// Could be a CLUT color instead, use that for lighting
648-
if(gr_get_fill_type() == FILL_CLUT) {
659+
if (nightsight_active()) {
660+
light = 1.0f;
661+
} else if (gr_get_fill_type() == FILL_CLUT) {
662+
// Could be a CLUT color instead, use that for lighting
649663
// Ugly hack: We don't get the original light value, so we have to
650664
// recalculate it from the offset into the global lighting lookup
651665
// table.
652666
uchar* clut = (uchar*)gr_get_fill_parm();
653-
light = (clut - grd_screen->ltab) / 4096.0f;
667+
light = 1.0f - (clut - grd_screen->ltab) / 4096.0f;
654668
}
655669

656670
glVertexAttrib2f(tcAttrib, vertex.uv.u / 256.0, vertex.uv.v / 256.0);
657-
glVertexAttrib1f(lightAttrib, 1.0f - light);
671+
glVertexAttrib1f(lightAttrib, light);
658672
glVertex3f(vertex.x / 65536.0f, vertex.y / 65536.0f, -vertex.z / 65536.0f);
659673
}
660674

@@ -729,7 +743,7 @@ int opengl_bitmap(grs_bitmap *bm, int n, grs_vertex **vpl, grs_tmap_info *ti) {
729743
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
730744

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

0 commit comments

Comments
 (0)