16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-18 03:09:18 +02:00

further pruning of legacy render code, now runs on opengl 3.3 core

profile
This commit is contained in:
milek7
2018-06-24 20:21:07 +02:00
parent c42afa2401
commit 0c5f990528
30 changed files with 400 additions and 1241 deletions

32
gl/object.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <GL/glew.h>
namespace gl
{
class object
{
private:
GLuint id = 0;
public:
inline operator GLuint() const
{
return id;
}
inline operator GLuint* const()
{
return &id;
}
inline operator const GLuint* const() const
{
return &id;
}
object() = default;
object(const object&) = delete;
object& operator=(const object&) = delete;
};
}

105
gl/shader.cpp Normal file
View File

@@ -0,0 +1,105 @@
#include "stdafx.h"
#include <fstream>
#include <sstream>
#include "shader.h"
inline bool strcend(std::string const &value, std::string const &ending)
{
if (ending.size() > value.size())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
gl::shader::shader(const std::string &filename)
{
std::stringstream stream;
std::ifstream f;
f.exceptions(std::ifstream::badbit);
f.open(filename);
stream << f.rdbuf();
f.close();
std::string str = stream.str();
const GLchar *cstr = str.c_str();
if (!cstr[0])
throw std::runtime_error("cannot read shader " + filename);
GLuint type;
if (strcend(filename, ".vert"))
type = GL_VERTEX_SHADER;
else if (strcend(filename, ".frag"))
type = GL_FRAGMENT_SHADER;
else
throw std::runtime_error("unknown shader " + filename);
**this = glCreateShader(type);
glShaderSource(*this, 1, &cstr, 0);
glCompileShader(*this);
GLint status;
glGetShaderiv(*this, GL_COMPILE_STATUS, &status);
if (!status)
{
GLchar info[512];
glGetShaderInfoLog(*this, 512, 0, info);
throw std::runtime_error("failed to compile " + filename + ": " + std::string(info));
}
}
gl::shader::~shader()
{
glDeleteShader(*this);
}
void gl::program::init()
{
bind();
glUniform1i(glGetUniformLocation(*this, "tex"), 3);
}
gl::program::program()
{
**this = glCreateProgram();
}
gl::program::program(std::vector<std::reference_wrapper<const gl::shader>> shaders) : program()
{
for (const gl::shader &s : shaders)
attach(s);
link();
}
void gl::program::attach(const gl::shader &s)
{
glAttachShader(*this, *s);
}
void gl::program::link()
{
glLinkProgram(*this);
GLint status;
glGetProgramiv(*this, GL_LINK_STATUS, &status);
if (!status)
{
GLchar info[512];
glGetProgramInfoLog(*this, 512, 0, info);
throw std::runtime_error("failed to link program: " + std::string(info));
}
init();
}
gl::program::~program()
{
glDeleteProgram(*this);
}
void gl::program::bind()
{
glUseProgram(*this);
}

32
gl/shader.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <string>
#include <vector>
#include <functional>
#include "object.h"
namespace gl
{
class shader : public object
{
public:
shader(const std::string &filename);
~shader();
};
class program : public object
{
public:
program();
program(std::vector<std::reference_wrapper<const gl::shader>>);
~program();
void bind();
void attach(const shader &);
void link();
virtual void init();
};
}

30
gl/shader_mvp.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "stdafx.h"
#include "shader_mvp.h"
void gl::program_mvp::init()
{
mv_uniform = glGetUniformLocation(*this, "modelview");
mvn_uniform = glGetUniformLocation(*this, "modelviewnormal");
p_uniform = glGetUniformLocation(*this, "projection");
}
void gl::program_mvp::set_mv(const glm::mat4 &m)
{
if (mvn_uniform != -1)
{
glm::mat3 mvn = glm::mat3(glm::transpose(glm::inverse(m)));
glUniformMatrix3fv(mvn_uniform, 1, GL_FALSE, glm::value_ptr(mvn));
}
glUniformMatrix4fv(mv_uniform, 1, GL_FALSE, glm::value_ptr(m));
}
void gl::program_mvp::set_p(const glm::mat4 &m)
{
glUniformMatrix4fv(p_uniform, 1, GL_FALSE, glm::value_ptr(m));
}
void gl::program_mvp::copy_gl_mvp()
{
set_mv(OpenGLMatrices.data(GL_MODELVIEW));
set_p(OpenGLMatrices.data(GL_PROJECTION));
}

22
gl/shader_mvp.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include "shader.h"
namespace gl
{
class program_mvp : public program
{
GLuint mv_uniform;
GLint mvn_uniform;
GLuint p_uniform;
public:
using program::program;
void set_mv(const glm::mat4 &);
void set_p(const glm::mat4 &);
void copy_gl_mvp();
virtual void init() override;
};
}