X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fshader.cpp;h=b2ccb8b6dc7fcfbc98c44028b9c97263d562b274;hb=e53a0e2e711a7d8bd9b0ddacd1360aa14370643f;hp=134f710c9684b2c690a3b71b58219872a02d419f;hpb=e74f1ad236429f05db90c0ace825277e2a3fbc05;p=blank.git diff --git a/src/shader.cpp b/src/shader.cpp index 134f710..b2ccb8b 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -42,27 +42,27 @@ Shader::~Shader() { } } -Shader::Shader(Shader &&other) +Shader::Shader(Shader &&other) noexcept : handle(other.handle) { other.handle = 0; } -Shader &Shader::operator =(Shader &&other) { +Shader &Shader::operator =(Shader &&other) noexcept { std::swap(handle, other.handle); return *this; } -void Shader::Source(const GLchar *src) { +void Shader::Source(const GLchar *src) noexcept { const GLchar* src_arr[] = { src }; glShaderSource(handle, 1, src_arr, nullptr); } -void Shader::Compile() { +void Shader::Compile() noexcept { glCompileShader(handle); } -bool Shader::Compiled() const { +bool Shader::Compiled() const noexcept { GLint compiled = GL_FALSE; glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled); return compiled == GL_TRUE; @@ -77,7 +77,7 @@ void Shader::Log(std::ostream &out) const { } -void Shader::AttachToProgram(GLuint id) const { +void Shader::AttachToProgram(GLuint id) const noexcept { glAttachShader(id, handle); } @@ -109,15 +109,15 @@ const Shader &Program::LoadShader(GLenum type, const GLchar *src) { return shader; } -void Program::Attach(Shader &shader) { +void Program::Attach(Shader &shader) noexcept { shader.AttachToProgram(handle); } -void Program::Link() { +void Program::Link() noexcept { glLinkProgram(handle); } -bool Program::Linked() const { +bool Program::Linked() const noexcept { GLint linked = GL_FALSE; glGetProgramiv(handle, GL_LINK_STATUS, &linked); return linked == GL_TRUE; @@ -132,7 +132,11 @@ void Program::Log(std::ostream &out) const { } -GLint Program::UniformLocation(const GLchar *name) const { +GLint Program::AttributeLocation(const GLchar *name) const noexcept { + return glGetAttribLocation(handle, name); +} + +GLint Program::UniformLocation(const GLchar *name) const noexcept { return glGetUniformLocation(handle, name); } @@ -205,7 +209,7 @@ DirectionalLighting::DirectionalLighting() } -void DirectionalLighting::Activate() { +void DirectionalLighting::Activate() noexcept { GLContext::EnableDepthTest(); GLContext::EnableBackfaceCulling(); program.Use(); @@ -214,7 +218,7 @@ void DirectionalLighting::Activate() { glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z); } -void DirectionalLighting::SetM(const glm::mat4 &m) { +void DirectionalLighting::SetM(const glm::mat4 &m) noexcept { glm::mat4 mv(view * m); glm::mat4 mvp(vp * m); glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]); @@ -222,33 +226,33 @@ void DirectionalLighting::SetM(const glm::mat4 &m) { glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); } -void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) { +void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept { light_direction = -dir; glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z); } -void DirectionalLighting::SetFogDensity(float f) { +void DirectionalLighting::SetFogDensity(float f) noexcept { fog_density = f; glUniform1f(fog_density_handle, fog_density); } -void DirectionalLighting::SetProjection(const glm::mat4 &p) { +void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept { projection = p; vp = p * view; } -void DirectionalLighting::SetView(const glm::mat4 &v) { +void DirectionalLighting::SetView(const glm::mat4 &v) noexcept { view = v; vp = projection * v; } -void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) { +void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { projection = p; view = v; vp = p * v; } -void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) { +void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept { SetVP(v, p); SetM(m); } @@ -257,7 +261,6 @@ void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const g BlockLighting::BlockLighting() : program() , vp(1.0f) -, m_handle(0) , mv_handle(0) , mvp_handle(0) , fog_density_handle(0) { @@ -266,20 +269,16 @@ BlockLighting::BlockLighting() "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\n" "layout(location = 1) in vec3 vtx_color;\n" - "layout(location = 2) in vec3 vtx_normal;\n" - "layout(location = 3) in float vtx_light;\n" - "uniform mat4 M;\n" + "layout(location = 2) in float vtx_light;\n" "uniform mat4 MV;\n" "uniform mat4 MVP;\n" "out vec3 frag_color;\n" "out vec3 vtx_viewspace;\n" - "out vec3 normal;\n" "out float frag_light;\n" "void main() {\n" "gl_Position = MVP * vec4(vtx_position, 1);\n" "frag_color = vtx_color;\n" "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" - "normal = (M * vec4(vtx_normal, 0)).xyz;\n" "frag_light = vtx_light;\n" "}\n" ); @@ -294,8 +293,6 @@ BlockLighting::BlockLighting() "void main() {\n" "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n" "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n" - //"float light_power = clamp(frag_light / 15, 0, 1);\n" - // this should be the same as the clear color, otherwise looks really weird "vec3 fog_color = vec3(0, 0, 0);\n" "float e = 2.718281828;\n" //"vec3 reflect_color = ambient + frag_color * light_power;\n" @@ -310,49 +307,47 @@ BlockLighting::BlockLighting() throw std::runtime_error("link program"); } - m_handle = program.UniformLocation("M"); mv_handle = program.UniformLocation("MV"); mvp_handle = program.UniformLocation("MVP"); fog_density_handle = program.UniformLocation("fog_density"); } -void BlockLighting::Activate() { +void BlockLighting::Activate() noexcept { GLContext::EnableDepthTest(); GLContext::EnableBackfaceCulling(); program.Use(); } -void BlockLighting::SetM(const glm::mat4 &m) { +void BlockLighting::SetM(const glm::mat4 &m) noexcept { glm::mat4 mv(view * m); glm::mat4 mvp(vp * m); - glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]); glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]); glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); } -void BlockLighting::SetFogDensity(float f) { +void BlockLighting::SetFogDensity(float f) noexcept { fog_density = f; glUniform1f(fog_density_handle, fog_density); } -void BlockLighting::SetProjection(const glm::mat4 &p) { +void BlockLighting::SetProjection(const glm::mat4 &p) noexcept { projection = p; vp = p * view; } -void BlockLighting::SetView(const glm::mat4 &v) { +void BlockLighting::SetView(const glm::mat4 &v) noexcept { view = v; vp = projection * v; } -void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) { +void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { projection = p; view = v; vp = p * v; } -void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) { +void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept { SetVP(v, p); SetM(m); }