Skip to content

Commit

Permalink
Attempt to use a separate shader for HUD
Browse files Browse the repository at this point in the history
I think that part is working just fine, though it seems like everything
is too bright despite not touching the main shader.
  • Loading branch information
rherriman committed Jan 9, 2024
1 parent 8bc7e6c commit 89384c6
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 25 deletions.
36 changes: 36 additions & 0 deletions rsrc/shaders/hud_frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 330 core

in vec4 fragmentColor;
in vec3 fragmentNormal;

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);

vec3 diffuse_light(vec3 light) {
return max(dot(fragmentNormal, light), 0.0) * lightColor;
}

vec3 diffuse() {
return diffuse_light(light0)
+ diffuse_light(light1);

}

vec4 light_color() {
return mix(
ambient * vec4(lightColor, 1.0) * fragmentColor,
vec4((ambient * lightColor) + diffuse(), 1.0) * fragmentColor,
lights_active
);
}

void main() {
color = light_color();
}

19 changes: 19 additions & 0 deletions rsrc/shaders/hud_vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec4 vertexColor;
layout(location = 2) in vec3 vertexNormal;

uniform mat4 view;
uniform mat4 proj;
uniform mat4 modelview;

out vec4 fragmentColor;
out vec3 fragmentNormal;

void main() {
vec4 pos = vec4(vertexPosition_modelspace, 1.0);
gl_Position = proj * (modelview * pos);
fragmentColor = vertexColor;
fragmentNormal = vertexNormal;
}
13 changes: 13 additions & 0 deletions src/assets/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ std::optional<std::string> AssetManager::GetResolvedAlfPath(std::string relative
return std::optional<std::string>{};
}

std::optional<std::string> AssetManager::GetShaderPath(std::string relativePath)
{
std::stringstream path;
path << baseStorage->GetRootPath() << PATHSEP << "shaders" << PATHSEP << relativePath;

std::ifstream file(path.str());
if (file.good()) {
return path.str();
}

return std::optional<std::string>{};
}

std::optional<std::shared_ptr<PackageManifest>> AssetManager::GetManifest(MaybePackage package)
{
if (manifestCache.count(package) > 0) {
Expand Down
8 changes: 8 additions & 0 deletions src/assets/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class AssetManager {
*/
static std::optional<std::string> GetResolvedAlfPath(std::string relativePath);

/**
* Get the full filesystem path for a shader file, if it is available.
*
* @param relativePath The relative path to look for.
* @return the full path to the shader file
*/
static std::optional<std::string> GetShaderPath(std::string relativePath);

/**
* Get the manifest for the specified package.
*
Expand Down
4 changes: 2 additions & 2 deletions src/game/CAvaraGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,11 @@ void CAvaraGame::Render(NVGcontext *ctx) {

//if (gameStatus == kPlayingStatus || gameStatus == kPauseStatus || gameStatus == kWinStatus || gameStatus == kLoseStatus) {
ViewControl();
AvaraGLSwitchShader(Shader::World);
itsWorld->Render(itsView);
AvaraGLSetAmbient(.7, UINT_MAX);
AvaraGLSwitchShader(Shader::HUD);
AvaraGLSetDepthTest(false);
hudWorld->Render(itsView);
AvaraGLSetAmbient(ToFloat(itsView->ambientLight), itsView->ambientLightColor);

if (showNewHUD) {
hud->RenderNewHUD(itsView, ctx);
Expand Down
89 changes: 67 additions & 22 deletions src/util/AvaraGL.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
#include "AvaraGL.h"
#include "AssetManager.h"
#include "FastMat.h"
#include "Resource.h"
#include "CViewParameters.h"
#include "ARGBColor.h"
#include "CBSPPart.h"

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>

#define OBJ_VERT "rsrc/shaders/avara_vert.glsl"
#define OBJ_FRAG "rsrc/shaders/avara_frag.glsl"
#define OBJ_VERT "avara_vert.glsl"
#define OBJ_FRAG "avara_frag.glsl"

#define SKY_VERT "rsrc/shaders/sky_vert.glsl"
#define SKY_FRAG "rsrc/shaders/sky_frag.glsl"
#define HUD_VERT "hud_vert.glsl"
#define HUD_FRAG "hud_frag.glsl"

#define SKY_VERT "sky_vert.glsl"
#define SKY_FRAG "sky_frag.glsl"

bool actuallyRender = true;
bool ready = false;
Expand Down Expand Up @@ -83,10 +85,15 @@ GLuint light1Loc, light1ColorLoc;
GLuint light2Loc, light2ColorLoc;
GLuint light3Loc, light3ColorLoc;

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

GLuint skyProgram;
GLuint skyVertArray, skyBuffer;
GLuint skyViewLoc, skyProjLoc, groundColorLoc, horizonColorLoc, skyColorLoc;

Shader currentProgram;

const char* glGetErrorString(GLenum error)
{
switch (error)
Expand Down Expand Up @@ -126,6 +133,9 @@ void AvaraGLSetView(glm::mat4 view) {
glUseProgram(gProgram);
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glCheckErrors();
glUseProgram(hudProgram);
glUniformMatrix4fv(hudViewLoc, 1, GL_FALSE, glm::value_ptr(view));
glCheckErrors();
}


Expand All @@ -134,6 +144,10 @@ void AvaraGLSetFOV(float fov) {
AvaraGLUpdateProjectionMatrix();
}

void AvaraGLSwitchShader(Shader shader) {
currentProgram = shader;
}

void AvaraGLUpdateProjectionMatrix() {
proj = glm::scale(glm::perspective(
glm::radians(current_fov),
Expand All @@ -144,6 +158,9 @@ void AvaraGLUpdateProjectionMatrix() {
glUseProgram(gProgram);
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
glCheckErrors();
glUseProgram(hudProgram);
glUniformMatrix4fv(hudProjLoc, 1, GL_FALSE, glm::value_ptr(proj));
glCheckErrors();
}

void AvaraGLSetLight(int light_index, float intensity, float elevation, float azimuth, ARGBColor color) {
Expand Down Expand Up @@ -192,6 +209,8 @@ void AvaraGLSetAmbient(float ambient, ARGBColor color) {
void ActivateLights(float active) {
glUseProgram(gProgram);
glUniform1f(lights_activeLoc, active);
glUseProgram(hudProgram);
glUniform1f(hudLightsActiveLoc, active);
}

void AvaraGLLightDefaults() {
Expand All @@ -214,16 +233,19 @@ void SetTransforms(CBSPPart *part) {
);
mv = glm::scale(mv, sc);
}
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mv));
glm::mat3 normal_mat = glm::mat3(1.0f);

glm::mat3 normal_mat = glm::mat3(1.0f);
for (int i = 0; i < 3; i ++) {
normal_mat[0][i] = ToFloat((part->itsTransform)[0][i]);
normal_mat[1][i] = ToFloat((part->itsTransform)[1][i]);
normal_mat[2][i] = ToFloat((part->itsTransform)[2][i]);
}

glUseProgram(gProgram);
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mv));
glUniformMatrix3fv(ntLoc, 1, GL_TRUE, glm::value_ptr(normal_mat));
glUseProgram(hudProgram);
glUniformMatrix4fv(hudMvLoc, 1, GL_FALSE, glm::value_ptr(mv));
}

void AvaraGLSetDepthTest(bool doTest) {
Expand All @@ -234,10 +256,8 @@ void AvaraGLSetDepthTest(bool doTest) {
void AvaraGLInitContext() {
//glEnable(GL_DEBUG_OUTPUT);
if (!actuallyRender) return;
char vertPath[PATH_MAX];
char fragPath[PATH_MAX];
BundlePath(OBJ_VERT, vertPath);
BundlePath(OBJ_FRAG, fragPath);
std::optional<std::string> vertPath = AssetManager::GetShaderPath(OBJ_VERT);
std::optional<std::string> fragPath = AssetManager::GetShaderPath(OBJ_FRAG);
gProgram = LoadShaders(vertPath, fragPath);
glUseProgram(gProgram);

Expand All @@ -263,9 +283,20 @@ void AvaraGLInitContext() {

AvaraGLLightDefaults();
glCheckErrors();
char skyVertPath[PATH_MAX], skyFragPath[PATH_MAX];
BundlePath(SKY_VERT, skyVertPath);
BundlePath(SKY_FRAG, skyFragPath);

std::optional<std::string> hudVertPath = AssetManager::GetShaderPath(HUD_VERT);
std::optional<std::string> hudFragPath = AssetManager::GetShaderPath(HUD_FRAG);
hudProgram = LoadShaders(hudVertPath, hudFragPath);
glUseProgram(hudProgram);

hudViewLoc = glGetUniformLocation(hudProgram, "view");
hudProjLoc = glGetUniformLocation(hudProgram, "proj");
hudMvLoc = glGetUniformLocation(hudProgram, "modelview");
hudLightsActiveLoc = glGetUniformLocation(hudProgram, "lights_active");
glCheckErrors();

std::optional<std::string> skyVertPath = AssetManager::GetShaderPath(SKY_VERT);
std::optional<std::string> skyFragPath = AssetManager::GetShaderPath(SKY_FRAG);
skyProgram = LoadShaders(skyVertPath, skyFragPath);
glGenVertexArrays(1, &skyVertArray);
glGenBuffers(1, &skyBuffer);
Expand All @@ -291,7 +322,11 @@ void AvaraGLDrawPolygons(CBSPPart* part) {
glCheckErrors();
if(!actuallyRender || !ready) return;
// Bind the vertex array and buffer that we set up earlier
glUseProgram(gProgram);
if (currentProgram == Shader::HUD) {
glUseProgram(hudProgram);
} else {
glUseProgram(gProgram);
}
glBindVertexArray(part->vertexArray);
glBindBuffer(GL_ARRAY_BUFFER, part->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, part->glDataSize, part->glData.get(), GL_STREAM_DRAW);
Expand Down Expand Up @@ -346,6 +381,12 @@ void AvaraGLDrawPolygons(CBSPPart* part) {
glCheckErrors();
}

if (currentProgram == Shader::HUD) {
glUseProgram(hudProgram);
} else {
glUseProgram(gProgram);
}

glBindVertexArray(0);
glCheckErrors();

Expand Down Expand Up @@ -493,40 +534,44 @@ void AvaraGLShadeWorld(CWorldShader *theShader, CViewParameters *theView) {
glEnable(GL_DEPTH_TEST);
}

GLuint LoadShaders(const char *vertex_file_path, const char *fragment_file_path) {
GLuint LoadShaders(std::optional<std::string> vertex_file_path, std::optional<std::string> fragment_file_path) {
// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
std::ifstream VertexShaderStream(*vertex_file_path, std::ios::in);
if (VertexShaderStream.is_open()) {
std::stringstream sstr;
sstr << VertexShaderStream.rdbuf();
VertexShaderCode = sstr.str();
VertexShaderStream.close();
} else {
printf("Impossible to open %s. Are you in the right directory? \n", vertex_file_path);
printf("Impossible to open %s. Are you in the right directory? \n", (*vertex_file_path).c_str());
getchar();
return 0;
}

// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
std::ifstream FragmentShaderStream(*fragment_file_path, std::ios::in);
if (FragmentShaderStream.is_open()) {
std::stringstream sstr;
sstr << FragmentShaderStream.rdbuf();
FragmentShaderCode = sstr.str();
FragmentShaderStream.close();
} else {
printf("Impossible to open %s. Are you in the right directory? \n", (*fragment_file_path).c_str());
getchar();
return 0;
}

GLint Result = GL_FALSE;
int InfoLogLength;

// Compile Vertex Shader
fprintf(stderr, "Compiling shader : %s\n", vertex_file_path);
fprintf(stderr, "Compiling shader : %s\n", (*vertex_file_path).c_str());
char const *VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
glCompileShader(VertexShaderID);
Expand All @@ -541,7 +586,7 @@ GLuint LoadShaders(const char *vertex_file_path, const char *fragment_file_path)
}

// Compile Fragment Shader
fprintf(stderr, "Compiling shader : %s\n", fragment_file_path);
fprintf(stderr, "Compiling shader : %s\n", (*fragment_file_path).c_str());
char const *FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
glCompileShader(FragmentShaderID);
Expand Down
17 changes: 16 additions & 1 deletion src/util/AvaraGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
#include "FastMat.h"
#include "Memory.h"

#include <string>

#ifdef __has_include
# if __has_include(<optional>) // Check for a standard library
# include <optional>
# elif __has_include(<experimental/optional>) // Check for an experimental version
# include <experimental/optional> // Check if __has_include is present
# else // Not found at all
# error "Missing <optional>"
# endif
#endif

// Forward declaration of Avara fundamental drawing classes
class CBSPPart;
#include "CBSPPart.h"
Expand All @@ -19,13 +31,16 @@ class CWorldShader;

#define GLAD_DEBUG

enum struct Shader { World, HUD, Sky };


GLuint LoadShaders(const char *vertex_file_path, const char *fragment_file_path);
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 AvaraGLSetView(glm::mat4 view);
void AvaraGLSetFOV(float fov);
void AvaraGLSwitchShader(Shader shader);
void AvaraGLUpdateProjectionMatrix();
void AvaraGLLightDefaults();
void AvaraGLInitContext();
Expand Down

0 comments on commit 89384c6

Please sign in to comment.