Skip to content

Commit

Permalink
Add a shader class
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed Apr 7, 2018
1 parent ba15de0 commit 3e96a3a
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
16 changes: 15 additions & 1 deletion projectfiles/VC14/wesnoth.vcxproj
Expand Up @@ -2514,7 +2514,20 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)OGL\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\ogl\sprite.cpp" />
<ClCompile Include="..\..\src\ogl\shader.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)OGL\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\ogl\sprite.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)OGL\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\ogl\texture.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)OGL\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)OGL\</ObjectFileName>
Expand Down Expand Up @@ -3867,6 +3880,7 @@
<ClInclude Include="..\..\src\network_asio.hpp" />
<ClInclude Include="..\..\src\ogl\context.hpp" />
<ClInclude Include="..\..\src\ogl\draw_op.hpp" />
<ClInclude Include="..\..\src\ogl\shader.hpp" />
<ClInclude Include="..\..\src\ogl\sprite.hpp" />
<ClInclude Include="..\..\src\ogl\texture.hpp" />
<ClInclude Include="..\..\src\ogl\utils.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions projectfiles/VC14/wesnoth.vcxproj.filters
Expand Up @@ -1542,6 +1542,9 @@
<ClCompile Include="..\..\src\ogl\sprite.cpp">
<Filter>OGL</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ogl\shader.cpp">
<Filter>OGL</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
Expand Down Expand Up @@ -2997,6 +3000,9 @@
<ClInclude Include="..\..\src\ogl\sprite.hpp">
<Filter>OGL</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ogl\shader.hpp">
<Filter>OGL</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">
Expand Down
1 change: 1 addition & 0 deletions source_lists/libwesnoth_sdl
@@ -1,4 +1,5 @@
ogl/context.cpp
ogl/shader.cpp
ogl/sprite.cpp
ogl/texture.cpp
ogl/utils.cpp
Expand Down
110 changes: 110 additions & 0 deletions src/ogl/shader.cpp
@@ -0,0 +1,110 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "ogl/shader.hpp"

#include "log.hpp"
#include "ogl/vertex.hpp"

#include <array>
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <tuple>
#include <utility>

static lg::log_domain log_opengl("opengl");
#define LOG_GL LOG_STREAM(info, log_opengl)
#define ERR_GL LOG_STREAM(err, log_opengl)

namespace
{

std::pair<bool, std::string> compile_subshader(GLuint shader, const std::string& source_code)
{
GLint compile_status;
std::array<char, 1024> compile_log;

int src_length = source_code.length();
const char* src_char_array = source_code.data();
glShaderSource(shader, 1, &src_char_array, &src_length);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
glGetShaderInfoLog(shader, compile_log.size(), nullptr, compile_log.data());

return {compile_status == GL_TRUE, {compile_log.data()}};
}

}

namespace gl
{

shader::~shader()
{
std::array<GLuint, 2> subshaders;
glGetAttachedShaders(shader_program_, subshaders.size(), nullptr, subshaders.data());

glDeleteProgram(shader_program_);
for(const GLuint s : subshaders) {
glDeleteShader(s);
}
}

void shader::activate() const
{
glUseProgram(shader_program_);

GLint position_index = glGetAttribLocation(shader_program_, "position");
GLint texcoord_index = glGetAttribLocation(shader_program_, "tex_coord");

glVertexAttribPointer(position_index, 2, GL_FLOAT, false, sizeof(vertex),
reinterpret_cast<const void*>(offsetof(vertex, x)));
glVertexAttribPointer(texcoord_index, 2, GL_FLOAT, false, sizeof(vertex),
reinterpret_cast<const void*>(offsetof(vertex, u)));

glEnableVertexAttribArray(position_index);
glEnableVertexAttribArray(texcoord_index);
}

void shader::compile(const std::string& vertex_shader_src, const std::string& pixel_shader_src)
{
bool success;
std::string compile_log;

GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
std::tie(success, compile_log) = compile_subshader(vertex_shader, vertex_shader_src);
if(!success) {
ERR_GL << "Error compiling vertex shader\n" << compile_log << std::endl;
throw std::invalid_argument("error compiling vertex shader");
} else if(compile_log != "") {
LOG_GL << compile_log << std::endl;
}

GLuint pixel_shader = glCreateShader(GL_FRAGMENT_SHADER);
std::tie(success, compile_log) = compile_subshader(pixel_shader, pixel_shader_src);
if(!success) {
ERR_GL << "Error compiling pixel shader\n" << compile_log << std::endl;
throw std::invalid_argument("error compiling pixel shader");
} else if(compile_log != "") {
LOG_GL << compile_log << std::endl;
}

shader_program_ = glCreateProgram();
glAttachShader(shader_program_, vertex_shader);
glAttachShader(shader_program_, pixel_shader);

glLinkProgram(shader_program_);
}

}
49 changes: 49 additions & 0 deletions src/ogl/shader.hpp
@@ -0,0 +1,49 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#include <GL/glew.h>

#include <string>

namespace gl
{

class shader
{
public:
/** Constructor.
@param vertex_shader_src Vertex shader source code.
@param pixel_shader_src Pixel shader source code.
@throw std::invalid_argument if compiling the shader fails.
*/
shader(const std::string& vertex_shader_src, const std::string& pixel_shader_src)
{
compile(pixel_shader_src, vertex_shader_src);
}

/// Destructor.
~shader();

/// Makes the shader the active shader.
/// The right VBO and VAO must be already bound.
void activate() const;

private:
void compile(const std::string& vertex_shader_src, const std::string& pixel_shader_src);

GLuint shader_program_ = 0u;
};

}

0 comments on commit 3e96a3a

Please sign in to comment.