Skip to content
Snippets Groups Projects
Commit 2ca8e748 authored by Jonas Johan Solsvik's avatar Jonas Johan Solsvik
Browse files

resolved conflicts and made sure that everything works

parents 9ba1fac5 f6359348
Branches light
No related tags found
No related merge requests found
Showing
with 378 additions and 66 deletions
......@@ -65,6 +65,9 @@ target_sources(
PRIVATE ${SRCDIR}/src/VertexArray.cpp
PRIVATE ${SRCDIR}/src/VertexBuffer.cpp
PRIVATE ${SRCDIR}/src/Watcher.cpp
PRIVATE ${SRCDIR}/src/UniformBuffer.cpp
PRIVATE ${SRCDIR}/src/ShaderIntrospector.cpp
)
#
......
Use shader introspection find which shaders need to attach to the uniform block points, resulting in sets of shaders where binding point would be the key.
this way we can loop through them
\ No newline at end of file
maps: 2
maps: 3
mainTex: scifi
bumpTex: scifi_nrm
specTex: scifi_spec
mainTexture: brickwall
specularTex: brickwall-nrm
values: 1
test: 0.8
values: 4
opacity: 1
intensity: 1
bumpiness: 1
specularity: 0
......@@ -41,7 +41,6 @@ v: 1.0 1.0 -1.0 0.577 0.577 -0.577 1 0 255 255 255 80
v: -1.0 1.0 1.0 -0.577 0.577 0.577 0 1 255 255 255 80
v: 1.0 1.0 1.0 0.577 0.577 0.577 1 1 255 255 255 80
meshes: 2
mesh: innercube
......@@ -68,7 +67,7 @@ mesh: innercube
mesh: outercube
material: brick
shader: base
shader: edge
triangles: 12
t: 14 15 16
t: 16 17 14
......
#shader vertex
#version 410 core
#version 410
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
layout(location = 3) in vec4 vertex_color_from_program;
layout(location = 3) in vec4 vertex_color;
noperspective out vec2 texCoord;
smooth out vec4 vertex_color_out;
out vec2 texCoord;
out vec4 vertex_color_out;
out vec4 pos;
uniform mat4 projection = mat4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
uniform mat4 m2w;
uniform float time = 0;
uniform mat4 view = mat4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
layout(std140) uniform OK_Matrices{
mat4 projection;
mat4 view;
vec4 view_position;
};
uniform mat4 model = mat4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
uniform float time = 0;
mat4 rotate(float x, float y, float z) {
return mat4(
(cos(y + z) + cos(y - z)) / 2, (-sin(y + z) + sin(y - z)) / 2, -sin(y), 0,
(cos(x + y + z) - cos(x - y + z) + cos(x + y - z) - cos(x - y - z) + 2 * sin(x + z) - 2 * sin(x - z)) / 4, (2 * cos(x + z) + 2 * cos(x - z) - sin(x + y + z) + sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (-sin(x + y) - sin(x - y)) / 2, 0,
(-2 * cos(x + z) + 2 * cos(x - z) + sin(x + y + z) - sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (cos(x + y + z) - cos(x - y + z) - cos(x + y - z) + cos(x - y - z) + 2 * sin(x + z) + 2 * sin(x - z)) / 4, (cos(x + y) + cos(x - y)) / 2, 0,
0, 0, 0, 1
(cos(y + z) + cos(y - z)) / 2, (-sin(y + z) + sin(y - z)) / 2, -sin(y), 0,
(cos(x + y + z) - cos(x - y + z) + cos(x + y - z) - cos(x - y - z) + 2 * sin(x + z) - 2 * sin(x - z)) / 4, (2 * cos(x + z) + 2 * cos(x - z) - sin(x + y + z) + sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (-sin(x + y) - sin(x - y)) / 2, 0,
(-2 * cos(x + z) + 2 * cos(x - z) + sin(x + y + z) - sin(x - y + z) + sin(x + y - z) - sin(x - y - z)) / 4, (cos(x + y + z) - cos(x - y + z) - cos(x + y - z) + cos(x - y - z) + 2 * sin(x + z) + 2 * sin(x - z)) / 4, (cos(x + y) + cos(x - y)) / 2, 0,
0, 0, 0, 1
);
}
out vec3 fragVert;
out vec3 fragNormal;
vec4 MVP(in vec4 position) {
return projection * view * m2w * position;
}
void main() {
// model to world space transformations = transform
// translation * rotation * scale * vertexPos;
float F = sqrt(position.x*position.x + position.y*position.y + position.z*position.z) * 0.01;
mat4 rot = rotate(0, time*F, 0);
float F = sqrt(position.x*position.x + position.y*position.y + position.z*position.z);
gl_Position = projection * view * model * position;
vertex_color_out = vertex_color_from_program;
texCoord = uv;
pos = gl_Position;
}
vec4 rotatedNormal = rot * vec4(normal, 1);
// Pass some variables to the fragment shader
//fragNormal = vec3(rotatedNormal);
vertex_color_out = rotatedNormal;
texCoord = uv;
fragNormal = mat3(transpose(inverse(m2w))) * normal;
vec4 out_position = MVP(position);
gl_Position = out_position;
fragVert = vec3(m2w * position);
}
#shader fragment
#version 410
#version 140
#define MAX_LIGHTS 8
in vec4 gl_FragCoord;
// in vec2 gl_PointCoord; // @NOTE Not supported on [macos, openGL 4.1]
noperspective in vec2 texCoord;
smooth in vec4 vertex_color_out;
in vec4 pos;
in vec2 texCoord;
in vec3 fragNormal;
in vec3 fragVert;
out vec4 out_color;
uniform float time = 0;
uniform sampler2D mainTexture;
uniform float test = 1;
uniform sampler2D mainTex;
uniform sampler2D bumpTex;
uniform sampler2D specTex;
uniform float opacity = 0;
uniform float specularity = 1;
uniform float intensity = 1;
uniform float bumpiness = 1;
uniform mat4 m2w;
layout(std140) uniform OK_Matrices{
mat4 projection;
mat4 view;
vec4 view_position;
};
struct OK_Light {
vec4 position;
vec4 intensities;
};
layout(std140) uniform OK_Lights{
OK_Light light[MAX_LIGHTS];
//float spread;
//float constant;
//float linear;
//float quadratic;
};
vec3 OK_PointLight(in vec3 position, in vec3 intensities/*, in float constant, in float linear, in float quadratic*/) {
//Ambience
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * intensities;
vec3 bump = texture(bumpTex, texCoord).rgb*intensity;
// Diffussion
vec3 norm = normalize(fragNormal*bump);
vec3 lightDir = normalize(position - fragVert);
float diffusion = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diffusion * intensities;
// Specularity
//float specularStrength = 0.5;
//vec3 viewDir = normalize(view_position.xyz - fragVert);
//vec3 reflectDir = reflect(-lightDir, norm);
//float specPower = pow(max(dot(viewDir, reflectDir), 0.0), 32);
//vec3 specular = specularStrength * specPower * intensities;
// Attenuation
//float distance = length(position - fragVert);
float attenuation = 1.0; // /(constant + linear * distance + quadratic * (distance * distance));
return (ambient*attenuation + diffuse * attenuation /*+ specular*specularity*attenuation*/);
}
void main() {
out_color = test*vec4(texture(mainTexture, texCoord).rgb * vertex_color_out.rgb, vertex_color_out.a);
vec3 diff = texture(mainTex, texCoord).rgb;
vec3 bump = texture(bumpTex, texCoord).rgb;
vec3 spec = texture(specTex, texCoord).rgb;
vec3 light0 = OK_PointLight(light[0].position.xyz, light[0].intensities.rgb /*,light[0].constant,light[0].linear, light[0].quadratic*/);
vec3 light1 = OK_PointLight(light[1].position.xyz, light[1].intensities.rgb /*,light[1].constant,light[1].linear, light[1].quadratic*/);
vec3 light2 = OK_PointLight(light[2].position.xyz, light[2].intensities.rgb /*,light[1].constant,light[1].linear, light[1].quadratic*/);
//out_color = vec4(fragNormal, 1)*0.2 + 0.8*vec4(texture(mainTex, texCoord).rgb , vertex_color_out.a);
out_color = vec4((light0+ light1+ light2) * diff, 1);
}
//out_color = vec4(texture(mainTexture, texCoord).rgb * vertex_color_out.rgb, vertex_color_out.a);
//out_color = vec4(texture(mainTexture, texCoord).rgb * vertex_color_out.rgb, vertex_color_out.a);
\ No newline at end of file
......@@ -61,4 +61,4 @@ uniform float transparency = 1;
void main() {
out_color = transparency*vec4(texture(mainTexture, texCoord).rgb, 1);
}
}
\ No newline at end of file
assets/textures/example.jpg

32.5 KiB

assets/textures/example_nrm.jpg

91.3 KiB

assets/textures/nrm_test.jpg

37.5 KiB

assets/textures/scifi.jpg

111 KiB

assets/textures/scifi_nrm.jpg

206 KiB

assets/textures/scifi_spec.jpg

162 KiB

......@@ -22,7 +22,8 @@ namespace overkill::C
constexpr float NearClip = 0.1f;
constexpr float FarClip = 100.0f;
constexpr float CameraOffset = -70; //How far back the camera is from the center of the scene.
const glm::vec4 ClearColor { 0.05f, 0.06f, 0.075f, 1.0f };
const glm::vec4 ClearColor { 0.05f, 0.06f, 0.075f, 1.0f }; // Sexy blue
constexpr char PathBaseShader[] = "assets/shaders/base.shader";
constexpr char PathBaseTexture[] = "assets/textures/Checkers.jpg";
constexpr float PI = 3.14159265359f;
......@@ -33,5 +34,5 @@ namespace overkill::C
constexpr char ModelsFolder[] = "assets/models";
constexpr char ScenesFolder[] = "assets/scenes";
constexpr unsigned int MAX_LIGHTS = 8; //inject it into shaders
}
#pragma once
#include <string>
......@@ -34,4 +33,26 @@ public:
void update(float dt);
};
struct Transform
{
const glm::vec3 right{ 1.0f,0.0f,0.0f };
const glm::vec3 up{ 0.0f,1.0f,0.0f };
const glm::vec3 forward{ 0.0f,0.0f,1.0f };
const glm::vec3 one{ 1.0f, 1.0f, 1.0f };
const glm::vec3 zero{ 0.0f ,0.0f ,0.0f };
glm::vec3 m_position = zero;
glm::vec3 m_rotation = zero;
glm::vec3 m_scale = one;
auto modelToWorld()->glm::mat4;
};
struct Light : public Transform
{
glm::vec3 intensities = one; //a.k.a the color of the light
};
}
\ No newline at end of file
......@@ -10,6 +10,7 @@
namespace overkill
{
class Renderer;
class EntityModel : public Entity
{
......@@ -17,9 +18,9 @@ private:
int m_modelID; // What model with id will be used to draw Entity.
glm::vec3 m_scale;
public:
glm::mat4 getModelMatrix();
public:
EntityModel(C::Tag modelTag,
glm::vec3 pos = glm::vec3(0,0,0), glm::vec3 rot = glm::vec3(0,0,0),
glm::vec3 scale = glm::vec3(1,1,1), glm::vec3 vel = glm::vec3(0,0,0),
......@@ -34,4 +35,4 @@ public:
void draw();
};
}
\ No newline at end of file
}
......@@ -7,24 +7,23 @@
#include <overkill/ElementBuffer.hpp>
#include <overkill/ShaderProgram.hpp>
#include <overkill/Model.hpp>
#include <overkill/EntityModel.hpp>
namespace overkill
{
class EntityModel;
class Renderer
{
public:
static void clear();
static void draw(const VertexArray& va, const ElementBuffer& eb, const ShaderProgram& shader);
static void draw(const Model& model, glm::mat4 modelMatrix = glm::mat4(1)); // Model matrix is translation rotation and scale of the model.
static void draw( EntityModel& entity, float t );
//static void draw( EntityModel& entity, float t);
};
class EdgeRenderer : public Renderer
{
public:
void drawEdged(const VertexArray& va, const ElementBuffer& eb, const ShaderProgram& shader, const ShaderProgram& edgeShader) const;
};
}
#pragma once
#include <overkill/gl_caller.hpp>
#include <string>
#include <vector>
namespace overkill
{
class ShaderIntrospector
{
public:
static GLint getActiveBlockCount(const GLuint program);
static const std::vector<GLint> getUniformBlockUniformIndices(const GLuint program, const GLuint uBlockIndex);
static const std::string getUnifromBlockName(const GLuint program, const GLint uBlockIndex);
static GLuint getUniformBlockIndex(const GLuint program, const std::string& name);
static GLuint getUniformBufferMaxSize(const GLuint program);
static GLint getUniformBlockBinding(const GLuint program, GLuint uniformBlockIndex);
};
}
/*
map:
tag:
program: blockIndex
program: blockIndex
*/
\ No newline at end of file
......@@ -11,6 +11,7 @@
#include <overkill/gl_util.hpp>
#include <overkill/Texture.hpp>
#include <overkill/Material.hpp>
#include <overkill/ShaderIntrospector.hpp>
namespace overkill
{
......@@ -33,6 +34,7 @@ class ShaderProgram
};
private:
std::unordered_map<std::string, GLint> uniforms;
std::unordered_map<std::string, GLint> uniformBlocks;
void construct(const std::string& vert, const std::string& frag, const std::string& geom);
......@@ -58,7 +60,8 @@ public:
static ShaderSource ParseProgram(const std::string& file);
static GLuint CompileShader(GLuint type, const std::string& source);
GLuint getUniformBlockIndex(const std::string& blockName) const;
};
// @note unused functions
......
#pragma once
#include <set>
#include <vector>
#include <string>
#include <unordered_map>
#include <overkill/Config.hpp>
#include <overkill/ShaderProgram.hpp>
#include <overkill/UniformBuffer.hpp>
#include <overkill/ShaderIntrospector.hpp>
#include <overkill/Watcher.hpp>
namespace overkill
{
struct MVP {
glm::mat4 view;
glm::mat4 projection;
};
struct LightData {
glm::vec4 position;
glm::vec4 intensities;
float spread;
};
class ShaderSystem
{
......@@ -18,17 +32,27 @@ public:
using OnUpdate = void (*)(C::ID, C::ID, C::ID);
struct UpdateCallback
{
C::Tag tag;
C::ID modelID;
C::ID meshID;
OnUpdate callback;
};
private:
static std::vector<ShaderProgram> m_shaderPrograms;
static std::unordered_map<C::Tag, C::ID> m_mapShaderProgramID;
static std::vector<UpdateCallback> m_updateCallbacks;
static std::unordered_map<C::Tag, std::set<GLuint>> m_mapUniformBufferTargets;
static std::vector<UniformBuffer> m_uniformBuffers;
static std::unordered_map<C::Tag, C::ID> m_mapUniformBuffersID;
static std::vector<ShaderProgram> m_shaderPrograms;
static std::unordered_map<C::Tag, C::ID> m_mapShaderProgramID;
static std::vector<UpdateCallback> m_updateCallbacks;
static void push(const C::Tag tag, const std::string& filepath);
static void pushUniformBuffer(const C::Tag&& tag, GLuint size);
static void linkUniformBlocks();
static void linkUniformBlocksForAll();
public:
// <summary> Load all shader data onto GPU memory.
......@@ -41,7 +65,18 @@ public:
static auto copyByTag(const C::Tag& tag) -> ShaderProgram;
static auto copyById(C::ID shaderProgramID) -> ShaderProgram;
static void bindOnUpdate(const C::Tag& shaderTag, C::ID modelID, C::ID meshID, OnUpdate onUpdate);
static void unbindAll();
static auto getUniformBufferIdByTag(const C::Tag& tag)->C::ID;
static auto getUniformBufferByTag(const C::Tag& tag) -> const UniformBuffer&;
static auto getUniformBufferById(C::ID uBufferID) -> const UniformBuffer&;
GLuint getBlockUniformLocation(const C::Tag& uBlock, const C::Tag& uniform);
static auto updateUniformBlock(C::ID uBufferID, C::ID uniformLocation);
//void ShaderProgram::bindUniformBlockToAll(GLuint blockIndex);
};
}
#pragma once
#include <overkill/gl_caller.hpp>
#include <overkill/ShaderProgram.hpp>
#include <overkill/Config.hpp>
#include <vector>
#include <unordered_map>
namespace overkill
{
struct BlockLayout
{
private:
std::unordered_map<C::Tag, GLuint> m_vars;
//std::unordered_map<C::Tag, BlockLayout> m_blocks;
GLuint m_blockSize; //the combined size of this block
C::Tag m_name;
public:
BlockLayout(const char* name = "");
inline GLsizei size() const { return m_blockSize; }
explicit operator C::Tag() const;
GLuint indexOfUniform(const C::Tag& name) const
{
auto search = m_vars.find(name); //@TODO discuss usage of at() as it works just as well here
if (search != m_vars.end())
{
return search->second;
}
LOG_WARN("indexOfUniform: \"%s\" cannot be found in buffer!\n has it been added in the layout?", name.c_str());
return 0;
//return m_vars.at(name);
}
void pushBlock(const BlockLayout& block, const GLuint count = 1) //allows direct access to the inner-element indices
{
/* TESTING THE LOGIC BEHIND THIS:
auto block0 = BlockLayout("b0")
block0.push<16>("position");
block0.push<16>("color");
// block0.size = 32
block1.push<16>("somevec");
// block1.size = 16
block1.pushBlock(block0);
// somevec, 0
// position, 16
// color, 32
// block1.size = 48
*/
for (GLuint i = 0; i < count; i++)
{
for (const auto& var : block.m_vars)
{
m_vars.emplace(std::make_pair(block.m_name + '[' + std::to_string(i) + "]." + var.first, m_blockSize + var.second));
}
m_blockSize += block.m_blockSize;
}
}
void push(const C::Tag& name, GLuint size)
{
m_vars.emplace(std::make_pair(name, m_blockSize));
m_blockSize += size;
}
};
//struct UniformBufferLayout
//{
//
//private:
// std::unordered_map<C::Tag, GLuint> m_uniforms;
// GLuint m_blockSize;
// C::Tag m_name;
//public:
// UniformBufferLayout(const char* name = "") : m_blockSize(0), m_name(name) {}
//
// template<GLuint size>
// void push(const C::Tag& name, GLuint count)
// {
// /* m_uniforms.emplace(std::make_pair(name, m_blockSize));
// m_blockSize += size * componentCount;*/
// for (GLuint i = 0; i < count; i++)
// {
// push<size>(name + '[' + std::to_string(i) + ']');
// }
// }
//
// template<GLuint size>
// void push<size>(const C::Tag& name)
// {
// m_uniforms.emplace(std::make_pair(name, m_blockSize));
// m_blockSize += size;
// }
//
// inline GLsizei size() const { return m_blockSize; }
//
// inline GLuint indexOfUniform(const C::Tag& name) const
// {
// return m_uniforms.at(name);
//
// //auto search = m_uniforms.find(name); //@TODO discuss usage of at() as it works just as well here
// //if (search != m_uniforms.end())
// //{
// // return search->second;
// //}
// //LOG_ERROR("indexOfUniform: \"%s\" cannot be found in buffer!\n has it been added in the layout?", name.c_str());
// //return 0;
// }
//
//};
class UniformBuffer
{
private:
GLuint m_id;
C::Tag m_name;
BlockLayout m_blockLayout;
public:
UniformBuffer(const char *name, const BlockLayout& layout, const GLenum drawMode);
explicit operator C::Tag() const;
explicit operator GLuint() const;
void clean();
void bind() const;
void unbind() const;
inline GLuint blockSize()
{
return m_blockLayout.size();
}
GLuint getUniformIndex(const C::Tag& name) const;
void update(const C::ID index, GLsizeiptr size, const void *data);
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment