feat: Added basic lighting and rotations to 3d visualizer model

This commit is contained in:
WerWolv
2023-01-20 23:32:51 +01:00
parent 8e759d9b5f
commit d4967018c2
4 changed files with 141 additions and 40 deletions

View File

@@ -32,31 +32,7 @@
#ifndef __gl3w_h_
#define __gl3w_h_
// Adapted from KHR/khrplatform.h to avoid including entire file.
typedef float khronos_float_t;
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
#ifdef _WIN64
typedef signed long long int khronos_intptr_t;
typedef signed long long int khronos_ssize_t;
#else
typedef signed long int khronos_intptr_t;
typedef signed long int khronos_ssize_t;
#endif
#if defined(_MSC_VER) && !defined(__clang__)
typedef signed __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
#elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100)
#include <stdint.h>
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#else
typedef signed long long khronos_int64_t;
typedef unsigned long long khronos_uint64_t;
#endif
#include <KHR/khrplatform.h>
#ifndef __gl_glcorearb_h_
#define __gl_glcorearb_h_ 1

View File

@@ -4,6 +4,8 @@
#include <hex/helpers/concepts.hpp>
#include <cmath>
#include <map>
#include <span>
#include <string>
@@ -33,11 +35,14 @@ namespace hex::gl {
void bind() const;
void unbind() const;
void setUniform(const std::string &name, const float &value);
private:
void compile(GLuint shader, const std::string &source);
private:
GLuint m_program;
std::map<std::string, GLint> m_uniforms;
};
enum class BufferType {
@@ -72,9 +77,10 @@ namespace hex::gl {
~VertexArray();
template<typename T>
void addBuffer(const Buffer<T> &buffer) const {
glVertexAttribPointer(0, buffer.getSize() / sizeof(T), getType<T>(), GL_FALSE, 3 * sizeof(T), nullptr);
glEnableVertexAttribArray(0);
void addBuffer(u32 index, const Buffer<T> &buffer) const {
glEnableVertexAttribArray(index);
buffer.bind();
glVertexAttribPointer(index, 3, getType<T>(), GL_FALSE, 3 * sizeof(T), nullptr);
}
void bind() const;
@@ -116,5 +122,57 @@ namespace hex::gl {
GLuint m_frameBuffer, m_renderBuffer;
};
template<typename T, size_t Size>
class Vector {
public:
Vector() = default;
Vector(std::array<T, Size> data) : m_data(data) { }
T &operator[](size_t index) { return this->m_data[index]; }
const T &operator[](size_t index) const { return this->m_data[index]; }
T *data() { return this->m_data.data(); }
const T *data() const { return this->m_data.data(); }
size_t size() const { return this->m_data.size(); }
auto operator+(const Vector<T, Size>& other) {
auto copy = *this;
for (size_t i = 0; i < Size; i++)
copy[i] += other[i];
return copy;
}
auto operator-(const Vector<T, Size>& other) {
auto copy = *this;
for (size_t i = 0; i < Size; i++)
copy[i] -= other[i];
return copy;
}
auto dot(const Vector<T, Size>& other) {
T result = 0;
for (size_t i = 0; i < Size; i++)
result += this->m_data[i] * other[i];
return result;
}
auto cross(const Vector<T, Size>& other) {
static_assert(Size == 3, "Cross product is only defined for 3D vectors");
return Vector<T, Size>({ this->m_data[1] * other[2] - this->m_data[2] * other[1], this->m_data[2] * other[0] - this->m_data[0] * other[2], this->m_data[0] * other[1] - this->m_data[1] * other[0] });
}
auto normalize() {
auto copy = *this;
auto length = std::sqrt(copy.dot(copy));
for (size_t i = 0; i < Size; i++)
copy[i] /= length;
return copy;
}
private:
std::array<T, Size> m_data;
};
}

View File

@@ -41,6 +41,22 @@ namespace hex::gl {
glUseProgram(0);
}
void Shader::setUniform(const std::string &name, const float &value) {
auto uniform = this->m_uniforms.find(name);
if (uniform == this->m_uniforms.end()) {
auto location = glGetUniformLocation(this->m_program, name.c_str());
if (location == -1) {
log::warn("Uniform '{}' not found in shader", name);
return;
}
this->m_uniforms[name] = location;
uniform = this->m_uniforms.find(name);
}
glUniform1f(uniform->second, value);
}
void Shader::compile(GLuint shader, const std::string &source) {
auto sourcePtr = source.c_str();
@@ -86,7 +102,14 @@ namespace hex::gl {
template<typename T>
void Buffer<T>::draw() const {
glDrawElements(GL_TRIANGLES, this->m_size, getType<T>(), nullptr);
switch (this->m_type) {
case GL_ARRAY_BUFFER:
glDrawArrays(GL_TRIANGLES, 0, this->m_size);
break;
case GL_ELEMENT_ARRAY_BUFFER:
glDrawElements(GL_TRIANGLES, this->m_size, getType<T>(), nullptr);
break;
}
}
template class Buffer<float>;