initial commit
This commit is contained in:
commit
739d8f55b7
20 changed files with 6472 additions and 0 deletions
13
src/Drawable.cpp
Normal file
13
src/Drawable.cpp
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "backend/Drawable.hpp"
|
||||
|
||||
void Drawable::Draw(const CameraBase& camera) const
|
||||
{
|
||||
shader->Use();
|
||||
PreRender(camera);
|
||||
vao->Render(static_cast<unsigned int>(type));
|
||||
}
|
||||
|
||||
void Drawable::SetPrimitiveType(PrimitiveType type)
|
||||
{
|
||||
this->type = type;
|
||||
}
|
97
src/Shader.cpp
Normal file
97
src/Shader.cpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
#include "backend/Shader.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glad/glad.h>
|
||||
|
||||
#define IMPLEMENT_UNIFORM_FUNCTION(type, func) \
|
||||
inline
|
||||
|
||||
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) :
|
||||
id(0)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||
const char* vertexShaderSource = vertexShader.c_str();
|
||||
glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL);
|
||||
glCompileShader(vertexShaderID);
|
||||
|
||||
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShaderID, 512, NULL, infoLog);
|
||||
std::cerr << "Vertex shader creation failed: \n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(vertexShaderID);
|
||||
return;
|
||||
}
|
||||
|
||||
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
const char* fragmentShaderSource = fragmentShader.c_str();
|
||||
glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, NULL);
|
||||
glCompileShader(fragmentShaderID);
|
||||
|
||||
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShaderID, 512, NULL, infoLog);
|
||||
std::cerr << "Fragment shader creation failed: \n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
return;
|
||||
}
|
||||
|
||||
id = glCreateProgram();
|
||||
glAttachShader(id, vertexShaderID);
|
||||
glAttachShader(id, fragmentShaderID);
|
||||
glLinkProgram(id);
|
||||
|
||||
glGetProgramiv(id, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(fragmentShaderID, 512, NULL, infoLog);
|
||||
std::cerr << "Shader program linking failed: \n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
|
||||
id = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
}
|
||||
|
||||
AbstractShader::~AbstractShader()
|
||||
{
|
||||
glDeleteProgram(id);
|
||||
}
|
||||
|
||||
void AbstractShader::Use()
|
||||
{
|
||||
glUseProgram(id);
|
||||
}
|
||||
|
||||
void AbstractShader::SetUniform(const std::string& name, const glm::mat4& value)
|
||||
{
|
||||
GLint location = glGetUniformLocation(id, name.c_str());
|
||||
if (location == -1)
|
||||
return;
|
||||
|
||||
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void AbstractShader::SetUniform(const std::string& name, const glm::vec4& value)
|
||||
{
|
||||
GLint location = glGetUniformLocation(id, name.c_str());
|
||||
if (location == -1)
|
||||
return;
|
||||
|
||||
glUniform4fv(location, 1, glm::value_ptr(value));
|
||||
}
|
82
src/Transformable.cpp
Normal file
82
src/Transformable.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include "backend/Transformable.hpp"
|
||||
|
||||
Transformable::Transformable() :
|
||||
position(0.0f), scale(1.0f), orientation(glm::vec3(0.0, 0.0, 0.0))
|
||||
{
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
const glm::vec3& Transformable::GetPosition() const
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
void Transformable::SetPosition(const glm::vec3& pos)
|
||||
{
|
||||
position = pos;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::Move(const glm::vec3& direction)
|
||||
{
|
||||
position += direction;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
const glm::vec3 Transformable::GetRotation() const
|
||||
{
|
||||
return glm::eulerAngles(orientation);
|
||||
}
|
||||
|
||||
const glm::quat& Transformable::GetQuaternion() const
|
||||
{
|
||||
return orientation;
|
||||
}
|
||||
|
||||
void Transformable::SetRotation(const glm::vec3& axis, float angle)
|
||||
{
|
||||
orientation = glm::quat(glm::radians(angle), axis);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::SetRotation(const glm::vec3& eulerAngles)
|
||||
{
|
||||
/*orientation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
orientation = glm::rotate(orientation, eulerAngles.x, glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
orientation = glm::rotate(orientation, eulerAngles.y, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
orientation = glm::rotate(orientation, eulerAngles.z, glm::vec3(0.0f, 0.0f, 1.0f));*/
|
||||
|
||||
orientation = glm::quat(eulerAngles);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::Rotate(const glm::vec3& axis, float angle)
|
||||
{
|
||||
orientation = glm::rotate(orientation, glm::radians(angle), axis);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
const glm::vec3& Transformable::GetScale() const
|
||||
{
|
||||
return scale;
|
||||
}
|
||||
|
||||
void Transformable::SetScale(const glm::vec3& scale)
|
||||
{
|
||||
this->scale = scale;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::Scale(const glm::vec3& factor)
|
||||
{
|
||||
this->scale *= scale; // I pray this is component-wise multiplication
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::CalculateTransformationMatrix()
|
||||
{
|
||||
transformation = glm::mat4(1.0f);
|
||||
transformation = glm::translate(transformation, position);
|
||||
transformation *= glm::mat4(orientation);
|
||||
transformation = glm::scale(transformation, scale);
|
||||
}
|
60
src/VertexArrayObject.cpp
Normal file
60
src/VertexArrayObject.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
#include "backend/VertexArrayObject.hpp"
|
||||
|
||||
#include <assert.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
AbstractVertexArrayObject::~AbstractVertexArrayObject()
|
||||
{
|
||||
glDeleteBuffers(1, &ebo);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
}
|
||||
|
||||
void AbstractVertexArrayObject::Render(GLenum mode)
|
||||
{
|
||||
assert(vao != 0);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
// GLenum result = glGetError();
|
||||
glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
|
||||
vao(0), vbo(0), ebo(0), indexCount(indices.size())
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
glGenBuffers(1, &ebo);
|
||||
|
||||
// Determing native OpenGL GLenum depending on specified usage
|
||||
GLenum bufferUsage;
|
||||
switch (usage)
|
||||
{
|
||||
case Usage::Static: bufferUsage = GL_STATIC_DRAW; break;
|
||||
case Usage::Dynamic: bufferUsage = GL_DYNAMIC_DRAW; break;
|
||||
case Usage::Stream: bufferUsage = GL_STREAM_DRAW; break;
|
||||
|
||||
default: // Forgot to add a usage case to this switch
|
||||
assert("Unknown buffer usage" == "");
|
||||
break;
|
||||
}
|
||||
|
||||
// Create VBO
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), (const void*)(vertices.data()), bufferUsage);
|
||||
|
||||
// Create EBO
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), (const void*)(indices.data()), bufferUsage);
|
||||
|
||||
// Set up pipeline layout
|
||||
unsigned int index = 0;
|
||||
for (const VertexAttribute& attribute : layout)
|
||||
{
|
||||
glVertexAttribPointer(index, attribute.size, attribute.type, attribute.normalized, attribute.stride, attribute.pointer);
|
||||
glEnableVertexAttribArray(index);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue