Skip to content

Commit

Permalink
Merge pull request #384 from avaraline/hud-shader
Browse files Browse the repository at this point in the history
Allow ambient light tweaks to HUD shader
  • Loading branch information
rherriman authored Jan 11, 2024
2 parents 650d9e4 + cea11a4 commit eca42d5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion rsrc/shaders/hud_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
in vec4 fragmentColor;
in vec3 fragmentNormal;

uniform float ambient = 0.0;
uniform float lights_active = 1.0;

out vec4 color;

float ambient = 0.7;
vec3 light0 = vec3(-0.136808053, -0.282842726, 0.375877053);
vec3 light1 = vec3(0.102606051, -0.102606043, -0.281907797);
vec3 lightColor = vec3(1, 1, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/game/CAvaraGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ void CAvaraGame::EndScript() {
itsView->ambientLight = ReadFixedVar(iAmbient);
itsView->ambientLightColor = ARGBColor::Parse(ReadStringVar(iAmbientColor))
.value_or(DEFAULT_LIGHT_COLOR);
AvaraGLSetAmbient(ToFloat(itsView->ambientLight), itsView->ambientLightColor);
AvaraGLSetAmbient(ToFloat(itsView->ambientLight), itsView->ambientLightColor, Shader::World);

for (i = 0; i < 4; i++) {
intensity = ReadFixedVar(iLightsTable + 4 * i);
Expand Down
39 changes: 21 additions & 18 deletions src/util/AvaraGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ GLuint light2Loc, light2ColorLoc;
GLuint light3Loc, light3ColorLoc;

GLuint hudProgram;
GLuint hudViewLoc, hudProjLoc, hudMvLoc, hudLightsActiveLoc;
GLuint hudViewLoc, hudProjLoc, hudMvLoc, hudAmbientLoc, hudLightsActiveLoc;

GLuint skyProgram;
GLuint skyVertArray, skyBuffer;
Expand Down Expand Up @@ -189,15 +189,20 @@ void AvaraGLSetLight(int light_index, float intensity, float elevation, float az
}
}

void AvaraGLSetAmbient(float ambient, ARGBColor color) {
void AvaraGLSetAmbient(float ambient, ARGBColor color, Shader shader) {
if (!actuallyRender) return;

float rgb[3];
color.ExportGLFloats(rgb, 3);

glUseProgram(gProgram);
glUniform1f(ambLoc, ambient);
glUniform3fv(ambColorLoc, 1, rgb);
if (shader == Shader::HUD) {
glUseProgram(hudProgram);
glUniform1f(hudAmbientLoc, ambient);
} else {
glUseProgram(gProgram);
glUniform1f(ambLoc, ambient);
glUniform3fv(ambColorLoc, 1, rgb);
}
}

void ActivateLights(float active, Shader shader) {
Expand All @@ -217,7 +222,8 @@ void AvaraGLLightDefaults() {
AvaraGLSetLight(1, 0.3f, 20.0f, 200.0f, DEFAULT_LIGHT_COLOR);
AvaraGLSetLight(2, 0, 0, 0, DEFAULT_LIGHT_COLOR);
AvaraGLSetLight(3, 0, 0, 0, DEFAULT_LIGHT_COLOR);
AvaraGLSetAmbient(0.4f, DEFAULT_LIGHT_COLOR);
AvaraGLSetAmbient(0.4f, DEFAULT_LIGHT_COLOR, Shader::World);
AvaraGLSetAmbient(0.7f, DEFAULT_LIGHT_COLOR, Shader::HUD);
}

void SetTransforms(CBSPPart *part, Shader shader) {
Expand Down Expand Up @@ -292,6 +298,7 @@ void AvaraGLInitContext() {
hudViewLoc = glGetUniformLocation(hudProgram, "view");
hudProjLoc = glGetUniformLocation(hudProgram, "proj");
hudMvLoc = glGetUniformLocation(hudProgram, "modelview");
hudAmbientLoc = glGetUniformLocation(hudProgram, "ambient");
hudLightsActiveLoc = glGetUniformLocation(hudProgram, "lights_active");
glCheckErrors();

Expand Down Expand Up @@ -350,13 +357,11 @@ void AvaraGLDrawPolygons(CBSPPart* part, Shader shader) {
float extra_amb = ToFloat(part->extraAmbient);
float current_amb = ToFloat(part->currentView->ambientLight);

if (shader == Shader::World) {
if (part->privateAmbient != -1) {
AvaraGLSetAmbient(ToFloat(part->privateAmbient), part->currentView->ambientLightColor);
}
if (extra_amb > 0) {
AvaraGLSetAmbient(current_amb + extra_amb, part->currentView->ambientLightColor);
}
if (part->privateAmbient != -1) {
AvaraGLSetAmbient(ToFloat(part->privateAmbient), part->currentView->ambientLightColor, shader);
}
if (extra_amb > 0) {
AvaraGLSetAmbient(current_amb + extra_amb, part->currentView->ambientLightColor, shader);
}
if (part->ignoreDirectionalLights) {
ActivateLights(0, shader);
Expand All @@ -378,11 +383,9 @@ void AvaraGLDrawPolygons(CBSPPart* part, Shader shader) {
glDisableVertexAttribArray(2);

// restore previous lighting state
if (shader == Shader::World) {
if (part->privateAmbient != -1 || extra_amb > 0) {
AvaraGLSetAmbient(current_amb, part->currentView->ambientLightColor);
glCheckErrors();
}
if (part->privateAmbient != -1 || extra_amb > 0) {
AvaraGLSetAmbient(current_amb, part->currentView->ambientLightColor, shader);
glCheckErrors();
}
if (part->ignoreDirectionalLights) {
ActivateLights(1, shader);
Expand Down
4 changes: 2 additions & 2 deletions src/util/AvaraGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class CWorldShader;

#define GLAD_DEBUG

enum struct Shader { World, HUD, Sky };
enum struct Shader { World, HUD };


GLuint LoadShaders(std::optional<std::string> vertex_file_path, std::optional<std::string> fragment_file_path);
void AvaraGLSetLight(int light, float intensity, float elevation, float azimuth, ARGBColor color);
void AvaraGLSetDepthTest(bool doTest);
void AvaraGLSetAmbient(float ambient, ARGBColor color);
void AvaraGLSetAmbient(float ambient, ARGBColor color, Shader shader = Shader::World);
void AvaraGLSetView(glm::mat4 view);
void AvaraGLSetFOV(float fov);
void AvaraGLUpdateProjectionMatrix();
Expand Down

0 comments on commit eca42d5

Please sign in to comment.