add assign3 shader classes
parent
28227d6105
commit
8d73163cc4
|
@ -182,49 +182,36 @@ namespace fp {
|
|||
glUniform4f(getUniformLocation(name), x, y, z, w);
|
||||
}
|
||||
};
|
||||
|
||||
class shader {
|
||||
|
||||
/**
|
||||
* a basic computer shader class, contains the functions and resources required to use compute shaders!
|
||||
*/
|
||||
class compute_shader : public shader_base {
|
||||
private:
|
||||
struct IntDefaultedToMinusOne {
|
||||
GLint i = -1;
|
||||
};
|
||||
// we can have shaders of many types in OpenGL
|
||||
unsigned int programID = 0;
|
||||
// but we will only make use of these two for now
|
||||
unsigned int vertexShaderID = 0;
|
||||
unsigned int fragmentShaderID = 0;
|
||||
GLuint shaderID = 0;
|
||||
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();
|
||||
};
|
||||
|
||||
class shader : public shader_base {
|
||||
private:
|
||||
GLuint vertexShaderID = 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!)
|
||||
unsigned int geometryShaderID = 0;
|
||||
GLuint geometryShaderID = 0;
|
||||
// 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 trying to keep the web and native versions the same though
|
||||
unsigned int tessellationShaderID = 0;
|
||||
std::unordered_map<std::string, IntDefaultedToMinusOne> uniformVars;
|
||||
|
||||
GLuint tessellationShaderID = 0;
|
||||
|
||||
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:
|
||||
/**
|
||||
* Creates a shader
|
||||
|
@ -243,56 +230,16 @@ namespace fp {
|
|||
// used to set location of shared UBOs like the perspective and view matrix
|
||||
void setUniformBlockLocation(const std::string &name, int location) const;
|
||||
|
||||
// set various data-types.
|
||||
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);
|
||||
}
|
||||
~shader();
|
||||
|
||||
static void updateProjectionMatrix(const blt::mat4x4& projectionMatrix);
|
||||
static void updateOrthographicMatrix(const blt::mat4x4& orthoMatrix);
|
||||
static void updateViewMatrix(const blt::mat4x4& viewMatrix);
|
||||
// returns the perspective view matrix which is calculated per frame. (This is for optimization)
|
||||
static const blt::mat4x4& getPVM();
|
||||
|
||||
~shader();
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
|
@ -166,7 +166,7 @@ namespace fp {
|
|||
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)
|
||||
return;
|
||||
chunk_storage.insert({chunk->getPos(), chunk});
|
||||
|
|
|
@ -65,6 +65,18 @@ namespace fp::_static {
|
|||
}
|
||||
|
||||
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() {
|
||||
glGenVertexArrays(1, &vaoID);
|
||||
}
|
||||
|
@ -96,7 +108,6 @@ namespace fp {
|
|||
VBOs.insert({-1, vbo});
|
||||
}
|
||||
|
||||
|
||||
unsigned int shader::createShader(const std::string& source, int type) {
|
||||
const char* shader_code = source.c_str();
|
||||
// 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) {
|
||||
// load shader sources
|
||||
// load shader sources
|
||||
bool load_geometry = !geometry.empty();
|
||||
std::string vertex_source = vertex;
|
||||
std::string fragment_source = fragment;
|
||||
|
@ -153,7 +164,7 @@ namespace fp {
|
|||
vertexShaderID = createShader(vertex_source, GL_VERTEX_SHADER);
|
||||
fragmentShaderID = createShader(fragment_source, GL_FRAGMENT_SHADER);
|
||||
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
|
||||
programID = glCreateProgram();
|
||||
|
@ -170,10 +181,10 @@ namespace fp {
|
|||
if (!success) {
|
||||
int log_length = 0;
|
||||
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &log_length);
|
||||
|
||||
|
||||
// scoped buffers will delete their memory when they go out of scope.
|
||||
blt::scoped_buffer<GLchar> infoLog{static_cast<unsigned long>(log_length + 1)};
|
||||
|
||||
|
||||
glGetProgramInfoLog(programID, log_length + 1, nullptr, infoLog.buffer);
|
||||
BLT_ERROR("--- --- --- --- --- --- --- --- ---");
|
||||
BLT_ERROR("Unable to link program of ID: %d", programID);
|
||||
|
@ -186,19 +197,18 @@ namespace fp {
|
|||
}
|
||||
|
||||
glValidateProgram(programID);
|
||||
use();
|
||||
bind();
|
||||
setUniformBlockLocation("StandardMatrices", 0);
|
||||
glUseProgram(0);
|
||||
|
||||
}
|
||||
|
||||
void shader::bindAttribute(int attribute, const std::string &name) const {
|
||||
use();
|
||||
bind();
|
||||
glBindAttribLocation(programID, attribute, name.c_str());
|
||||
}
|
||||
|
||||
void shader::setUniformBlockLocation(const std::string &name, int location) const {
|
||||
use();
|
||||
bind();
|
||||
glUniformBlockBinding(programID, glGetUniformBlockIndex(programID, name.c_str()), location);
|
||||
}
|
||||
|
||||
|
|
|
@ -184,7 +184,7 @@ namespace fp::graphics {
|
|||
|
||||
void render() {
|
||||
// generate planes
|
||||
plane_shader->use();
|
||||
plane_shader->bind();
|
||||
plane_vao->bind();
|
||||
glDisable(GL_CULL_FACE);
|
||||
while (!plane_render_queue.empty()) {
|
||||
|
@ -210,7 +210,7 @@ namespace fp::graphics {
|
|||
//glDisable(GL_CULL_FACE);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
text_shader->use();
|
||||
text_shader->bind();
|
||||
quad_vao->bind();
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
|
|
|
@ -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>
|
|
@ -125,19 +125,19 @@ std::queue<fp::chunk_pos> chunks_to_generate{};
|
|||
void fp::world::update() {
|
||||
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())
|
||||
break;
|
||||
const auto& front = chunks_to_generate.front();
|
||||
|
||||
insertChunk(generateChunk(front));
|
||||
spawnChunk(generateChunk(front));
|
||||
|
||||
chunks_to_generate.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void fp::world::render(fp::shader& shader) {
|
||||
shader.use();
|
||||
shader.bind();
|
||||
|
||||
if (fp::window::isKeyPressed(GLFW_KEY_F) && fp::window::keyState())
|
||||
fp::camera::isFrozen() ? fp::camera::unfreeze() : fp::camera::freeze();
|
||||
|
|
Loading…
Reference in New Issue