add assign3 shader classes

main
Brett 2023-04-22 20:35:46 -04:00
parent 28227d6105
commit 8d73163cc4
7 changed files with 81 additions and 95 deletions

View File

@ -183,48 +183,35 @@ namespace fp {
} }
}; };
class shader { /**
* a basic computer shader class, contains the functions and resources required to use compute shaders!
*/
class compute_shader : public shader_base {
private: private:
struct IntDefaultedToMinusOne { GLuint shaderID = 0;
GLint i = -1; public:
explicit compute_shader(const std::string& shader_source, bool loadAsString = true);
inline void execute(int x, int y, int z) const {
bind();
glDispatchCompute(x, y, z);
}
~compute_shader();
}; };
// we can have shaders of many types in OpenGL
unsigned int programID = 0; class shader : public shader_base {
// but we will only make use of these two for now private:
unsigned int vertexShaderID = 0; GLuint vertexShaderID = 0;
unsigned int fragmentShaderID = 0; GLuint fragmentShaderID = 0;
// while these will remain unused. (Webgl2 apparently doesn't support them despite being based on GL4.3? that's a TODO!) // while these will remain unused. (Webgl2 apparently doesn't support them despite being based on GL4.3? that's a TODO!)
unsigned int geometryShaderID = 0; GLuint geometryShaderID = 0;
// this would be very useful however it is highly unlikely webgl will support it // this would be very useful however it is highly unlikely webgl will support it
// im leaving some of this stuff in here because I might expand the native application to use some of it. // im leaving some of this stuff in here because I might expand the native application to use some of it.
// im trying to keep the web and native versions the same though // im trying to keep the web and native versions the same though
unsigned int tessellationShaderID = 0; GLuint tessellationShaderID = 0;
std::unordered_map<std::string, IntDefaultedToMinusOne> uniformVars;
static unsigned int createShader(const std::string& source, int type); static unsigned int createShader(const std::string& source, int type);
inline GLint getUniformLocation(const std::string &name) {
if (uniformVars[name].i != -1)
return uniformVars[name].i;
// caching the result is a lot faster since it won't change after the shader is created.
// TODO: look into this: https://webglfundamentals.org/webgl/lessons/webgl-qna-how-can-i-get-all-the-uniforms-and-uniformblocks.html
int loc = glGetUniformLocation(programID, name.c_str());
uniformVars[name].i = loc;
return loc;
}
static inline std::string removeEmptyFirstLines(const std::string& string){
auto lines = blt::string::split(string, "\n");
std::string new_source_string;
for (const auto& line : lines) {
if (!line.empty() && !blt::string::contains(line, "\"")) {
new_source_string += line;
new_source_string += "\n";
}
}
return new_source_string;
}
public: public:
/** /**
* Creates a shader * Creates a shader
@ -243,56 +230,16 @@ namespace fp {
// used to set location of shared UBOs like the perspective and view matrix // used to set location of shared UBOs like the perspective and view matrix
void setUniformBlockLocation(const std::string &name, int location) const; void setUniformBlockLocation(const std::string &name, int location) const;
// set various data-types. ~shader();
inline void setBool(const std::string &name, bool value) {
glUniform1i(getUniformLocation(name), (int) value);
}
inline void setInt(const std::string &name, int value) {
glUniform1i(getUniformLocation(name), value);
}
inline void setFloat(const std::string &name, float value) {
glUniform1f(getUniformLocation(name), value);
}
inline void setMatrix(const std::string &name, blt::mat4x4 &matrix) {
glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, matrix.ptr());
}
inline void setVec3(const std::string &name, const blt::vec3 &vec) {
glUniform3f(getUniformLocation(name), vec.x(), vec.y(), vec.z());
}
inline void setVec4(const std::string &name, const blt::vec4 &vec) {
// TODO: edit BLT to include a w component
glUniform4f(getUniformLocation(name), vec.x(), vec.y(), vec.z(), vec[3]);
}
inline void setVec2(const std::string &name, float x, float y) {
glUniform2f(getUniformLocation(name), x, y);
}
inline void setVec3(const std::string &name, float x, float y, float z) {
glUniform3f(getUniformLocation(name), x, y, z);
}
inline void setVec4(const std::string &name, float x, float y, float z, float w) {
glUniform4f(getUniformLocation(name), x, y, z, w);
}
inline void use() const {
glUseProgram(programID);
}
static void updateProjectionMatrix(const blt::mat4x4& projectionMatrix); static void updateProjectionMatrix(const blt::mat4x4& projectionMatrix);
static void updateOrthographicMatrix(const blt::mat4x4& orthoMatrix); static void updateOrthographicMatrix(const blt::mat4x4& orthoMatrix);
static void updateViewMatrix(const blt::mat4x4& viewMatrix); static void updateViewMatrix(const blt::mat4x4& viewMatrix);
// returns the perspective view matrix which is calculated per frame. (This is for optimization) // returns the perspective view matrix which is calculated per frame. (This is for optimization)
static const blt::mat4x4& getPVM(); static const blt::mat4x4& getPVM();
~shader();
}; };
} }

23
include/util/threadpool.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Created by Brett on 22/04/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef FINALPROJECT_THREADPOOL_H
#define FINALPROJECT_THREADPOOL_H
#include <thread>
#include <mutex>
namespace blt {
class thread_pool {
private:
public:
};
}
#endif //FINALPROJECT_THREADPOOL_H

View File

@ -166,7 +166,7 @@ namespace fp {
neighbours[Z_NEG] = getChunk(chunk_pos{pos.x, pos.y, pos.z - 1}); neighbours[Z_NEG] = getChunk(chunk_pos{pos.x, pos.y, pos.z - 1});
} }
inline void insertChunk(chunk* chunk) { inline void spawnChunk(chunk* chunk) {
if (chunk == nullptr) if (chunk == nullptr)
return; return;
chunk_storage.insert({chunk->getPos(), chunk}); chunk_storage.insert({chunk->getPos(), chunk});

View File

@ -65,6 +65,18 @@ namespace fp::_static {
} }
namespace fp { namespace fp {
static inline std::string removeEmptyFirstLines(const std::string& string){
auto lines = blt::string::split(string, "\n");
std::string new_source_string;
for (const auto& line : lines) {
if (!line.empty() && !blt::string::contains(line, "\"")) {
new_source_string += line;
new_source_string += "\n";
}
}
return new_source_string;
}
VAO::VAO() { VAO::VAO() {
glGenVertexArrays(1, &vaoID); glGenVertexArrays(1, &vaoID);
} }
@ -96,7 +108,6 @@ namespace fp {
VBOs.insert({-1, vbo}); VBOs.insert({-1, vbo});
} }
unsigned int shader::createShader(const std::string& source, int type) { unsigned int shader::createShader(const std::string& source, int type) {
const char* shader_code = source.c_str(); const char* shader_code = source.c_str();
// creates a Shader // creates a Shader
@ -132,7 +143,7 @@ namespace fp {
} }
shader::shader(const std::string& vertex, const std::string& fragment, const std::string& geometry, bool load_as_string) { shader::shader(const std::string& vertex, const std::string& fragment, const std::string& geometry, bool load_as_string) {
// load shader sources // load shader sources
bool load_geometry = !geometry.empty(); bool load_geometry = !geometry.empty();
std::string vertex_source = vertex; std::string vertex_source = vertex;
std::string fragment_source = fragment; std::string fragment_source = fragment;
@ -153,7 +164,7 @@ namespace fp {
vertexShaderID = createShader(vertex_source, GL_VERTEX_SHADER); vertexShaderID = createShader(vertex_source, GL_VERTEX_SHADER);
fragmentShaderID = createShader(fragment_source, GL_FRAGMENT_SHADER); fragmentShaderID = createShader(fragment_source, GL_FRAGMENT_SHADER);
if (load_geometry) if (load_geometry)
BLT_ERROR("Unable to load geometry shader because webgl doesn't support it!"); geometryShaderID = createShader(geometry_source, GL_GEOMETRY_SHADER);
// bind them to a program // bind them to a program
programID = glCreateProgram(); programID = glCreateProgram();
@ -186,19 +197,18 @@ namespace fp {
} }
glValidateProgram(programID); glValidateProgram(programID);
use(); bind();
setUniformBlockLocation("StandardMatrices", 0); setUniformBlockLocation("StandardMatrices", 0);
glUseProgram(0); glUseProgram(0);
} }
void shader::bindAttribute(int attribute, const std::string &name) const { void shader::bindAttribute(int attribute, const std::string &name) const {
use(); bind();
glBindAttribLocation(programID, attribute, name.c_str()); glBindAttribLocation(programID, attribute, name.c_str());
} }
void shader::setUniformBlockLocation(const std::string &name, int location) const { void shader::setUniformBlockLocation(const std::string &name, int location) const {
use(); bind();
glUniformBlockBinding(programID, glGetUniformBlockIndex(programID, name.c_str()), location); glUniformBlockBinding(programID, glGetUniformBlockIndex(programID, name.c_str()), location);
} }

View File

@ -184,7 +184,7 @@ namespace fp::graphics {
void render() { void render() {
// generate planes // generate planes
plane_shader->use(); plane_shader->bind();
plane_vao->bind(); plane_vao->bind();
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
while (!plane_render_queue.empty()) { while (!plane_render_queue.empty()) {
@ -210,7 +210,7 @@ namespace fp::graphics {
//glDisable(GL_CULL_FACE); //glDisable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
text_shader->use(); text_shader->bind();
quad_vao->bind(); quad_vao->bind();
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);

6
src/util/threadpool.cpp Normal file
View File

@ -0,0 +1,6 @@
/*
* Created by Brett on 22/04/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#include <util/threadpool.h>

View File

@ -125,19 +125,19 @@ std::queue<fp::chunk_pos> chunks_to_generate{};
void fp::world::update() { void fp::world::update() {
auto target_delta = 1000000000 / std::stoi(fp::settings::get("FPS")); auto target_delta = 1000000000 / std::stoi(fp::settings::get("FPS"));
while (fp::window::getCurrentDelta() < target_delta) { while (fp::window::getCurrentDelta() < target_delta/2) {
if (chunks_to_generate.empty()) if (chunks_to_generate.empty())
break; break;
const auto& front = chunks_to_generate.front(); const auto& front = chunks_to_generate.front();
insertChunk(generateChunk(front)); spawnChunk(generateChunk(front));
chunks_to_generate.pop(); chunks_to_generate.pop();
} }
} }
void fp::world::render(fp::shader& shader) { void fp::world::render(fp::shader& shader) {
shader.use(); shader.bind();
if (fp::window::isKeyPressed(GLFW_KEY_F) && fp::window::keyState()) if (fp::window::isKeyPressed(GLFW_KEY_F) && fp::window::keyState())
fp::camera::isFrozen() ? fp::camera::unfreeze() : fp::camera::freeze(); fp::camera::isFrozen() ? fp::camera::unfreeze() : fp::camera::freeze();