summaryrefslogtreecommitdiff
path: root/src/GFX
diff options
context:
space:
mode:
Diffstat (limited to 'src/GFX')
-rw-r--r--src/GFX/Binder.hpp4
-rw-r--r--src/GFX/Camera.cpp10
-rw-r--r--src/GFX/Camera.hpp4
-rw-r--r--src/GFX/Image/PPMParser.cpp7
-rw-r--r--src/GFX/Image/PPMParser.hpp4
-rw-r--r--src/GFX/Image/RawImage.cpp24
-rw-r--r--src/GFX/Image/RawImage.hpp11
-rw-r--r--src/GFX/Mesh.hpp13
-rw-r--r--src/GFX/Mouse.hpp1
-rw-r--r--src/GFX/Shading/Program.hpp2
-rw-r--r--src/GFX/Shading/Shader.cpp4
-rw-r--r--src/GFX/Shading/Uniform.cpp4
-rw-r--r--src/GFX/Shading/Uniform.hpp7
-rw-r--r--src/GFX/Texture.cpp4
-rw-r--r--src/GFX/Texture.hpp6
-rw-r--r--src/GFX/Window.cpp14
-rw-r--r--src/GFX/Window.hpp7
17 files changed, 51 insertions, 75 deletions
diff --git a/src/GFX/Binder.hpp b/src/GFX/Binder.hpp
index 92faebe..773518d 100644
--- a/src/GFX/Binder.hpp
+++ b/src/GFX/Binder.hpp
@@ -21,7 +21,7 @@ private:
) : m_vao(vao),
m_vertex_count(vertex_count),
m_has_indices(vertex_count > 0),
- m_attribute_count(attribute_count) {};
+ m_attribute_count(attribute_count) {}
uint32_t m_vao;
size_t m_vertex_count;
@@ -33,7 +33,7 @@ private:
class Binder {
public:
- Binder() = default;;
+ Binder() = default;
static BindableMesh load(const Mesh& mesh);
diff --git a/src/GFX/Camera.cpp b/src/GFX/Camera.cpp
index 6b25347..dbc3444 100644
--- a/src/GFX/Camera.cpp
+++ b/src/GFX/Camera.cpp
@@ -2,7 +2,7 @@
namespace MC::GFX {
-Vector<3> Camera::position() {
+Vector<3> Camera::position() const {
return m_position;
}
@@ -10,18 +10,18 @@ void Camera::set_position(Vector<3> position) {
m_position = position;
}
-void Camera::move(Vector<3> vector) {
- m_position = m_position + vector;
+void Camera::move(Vector<3> by) {
+ m_position = m_position + by;
}
void Camera::move_relative(Vector<3> by) {
auto rotation = Matrix<4, 4>::rotation(m_angles);
auto result = rotation.transpose() * Vector<4>{by.x(), by.y(), by.z(), 1.0f};
- move(result.elements);
+ move(Vector<3>{result.elements});
}
-Rotation Camera::angles() {
+Rotation Camera::angles() const {
return m_angles;
}
diff --git a/src/GFX/Camera.hpp b/src/GFX/Camera.hpp
index 77a210f..2109749 100644
--- a/src/GFX/Camera.hpp
+++ b/src/GFX/Camera.hpp
@@ -8,12 +8,12 @@ class Camera {
public:
Camera() = default;
- Vector<3> position();
+ Vector<3> position() const;
void set_position(Vector<3> position);
void move(Vector<3> by);
void move_relative(Vector<3> by);
- Rotation angles();
+ Rotation angles() const;
void set_angles(Rotation angles);
void rotate(Rotation by);
diff --git a/src/GFX/Image/PPMParser.cpp b/src/GFX/Image/PPMParser.cpp
index cf1bf77..f002db8 100644
--- a/src/GFX/Image/PPMParser.cpp
+++ b/src/GFX/Image/PPMParser.cpp
@@ -66,7 +66,7 @@ RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) {
throw std::runtime_error("Sample can not be greater than Maxval.");
}
- auto map_to_range = [=](uint64_t s) -> uint8_t { return (s * 255) / max_color; };
+ auto map_to_range = [=](uint64_t s) -> uint8_t { return s * 255 / max_color; };
RawImage::Pixel pixel{};
pixel.r = map_to_range(r_sample);
@@ -120,9 +120,8 @@ std::string_view PPMParser::chomp_part() {
}
void PPMParser::skip_whitespace() {
- uint8_t c;
while (!is_eof()) {
- c = m_source[m_cursor];
+ uint8_t c = m_source[m_cursor];
if (c == '#') {
skip_comment();
continue;
@@ -147,7 +146,7 @@ void PPMParser::skip_comment() {
}
}
-bool PPMParser::is_eof() {
+bool PPMParser::is_eof() const {
return m_cursor >= m_source.size();
}
diff --git a/src/GFX/Image/PPMParser.hpp b/src/GFX/Image/PPMParser.hpp
index 3909cee..07ff3c2 100644
--- a/src/GFX/Image/PPMParser.hpp
+++ b/src/GFX/Image/PPMParser.hpp
@@ -8,7 +8,7 @@ namespace MC::GFX::Image {
class PPMParser {
public:
- explicit PPMParser(std::string_view source) : m_source(source) {};
+ explicit PPMParser(std::string_view source) : m_source(source) {}
RawImage parse();
private:
@@ -35,7 +35,7 @@ private:
void skip_whitespace();
void skip_comment();
- bool is_eof();
+ bool is_eof() const;
std::string_view m_source;
uint64_t m_cursor = 0;
diff --git a/src/GFX/Image/RawImage.cpp b/src/GFX/Image/RawImage.cpp
index 1222fab..6cb06b2 100644
--- a/src/GFX/Image/RawImage.cpp
+++ b/src/GFX/Image/RawImage.cpp
@@ -1,21 +1,9 @@
#include "RawImage.hpp"
+#include <sstream>
namespace MC::GFX::Image {
-RawImage::RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height)
- : m_pixels(), m_width(width), m_height(height)
-{
- m_pixels.reserve(width * height);
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- auto result = sampler.sample({(float)x, (float)y});
- auto intensity = (uint8_t)(result * 255);
- add({intensity, intensity, intensity});
- }
- }
-}
-
-void RawImage::add(RawImage::Pixel pixel) {
+void RawImage::add(Pixel pixel) {
m_pixels.push_back(pixel);
}
@@ -35,18 +23,14 @@ uint32_t RawImage::height() const {
return m_height;
}
-uint8_t RawImage::channels() const {
- return m_channels;
-}
-
std::string RawImage::string() const {
std::stringstream str{};
bool comma = false;
str << "[";
- for (const auto& pixel : m_pixels) {
+ for (const auto [r, g, b] : m_pixels) {
if (comma) { str << ", "; }
- str << "{r=" << (uint)pixel.r << ", g=" << (uint)pixel.g << ", b=" << (uint)pixel.r << "}";
+ str << "{r=" << (uint)r << ", g=" << (uint)g << ", b=" << (uint)r << "}";
comma = true;
}
str << "]";
diff --git a/src/GFX/Image/RawImage.hpp b/src/GFX/Image/RawImage.hpp
index 8a837e6..be3c83a 100644
--- a/src/GFX/Image/RawImage.hpp
+++ b/src/GFX/Image/RawImage.hpp
@@ -4,21 +4,17 @@
#include <cstddef>
#include <string>
#include <vector>
-#include "../../Util/Sampler.hpp"
namespace MC::GFX::Image {
class RawImage {
public:
- RawImage() : m_pixels(), m_width(0), m_height(0) {};
+ RawImage() : m_width(0), m_height(0) {}
- RawImage(uint32_t width, uint32_t height)
- : m_pixels(), m_width(width), m_height(height) {
+ RawImage(uint32_t width, uint32_t height) : m_width(width), m_height(height) {
m_pixels.reserve(width * height);
}
- RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height);
-
struct Pixel {
uint8_t r, g, b;
};
@@ -30,14 +26,11 @@ public:
uint32_t width() const;
uint32_t height() const;
- uint8_t channels() const;
std::string string() const;
private:
std::vector<Pixel> m_pixels;
-
uint32_t m_width, m_height;
- uint8_t m_channels = 3;
};
}
diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp
index 1d14ba2..ec21e53 100644
--- a/src/GFX/Mesh.hpp
+++ b/src/GFX/Mesh.hpp
@@ -17,18 +17,18 @@ public:
attribute_size(S),
type_size(sizeof(T)) {
data = copy(v.data(), v.size() * S * type_size);
- };
+ }
Attribute(
const Attribute& other
) : data(copy(other.data, other.data_size * other.attribute_size * other.type_size)),
data_size(other.data_size),
attribute_size(other.attribute_size),
- type_size(other.type_size) {};
+ type_size(other.type_size) {}
static void* copy(void* ptr, uint32_t size) {
auto* buffer = new uint8_t[size];
- std::copy((uint8_t*)ptr, (uint8_t*)ptr + size, buffer);
+ std::copy_n((uint8_t*)ptr, size, buffer);
return buffer;
}
@@ -42,12 +42,11 @@ public:
std::vector<Attribute> attributes,
std::vector<uint32_t> indices
) : m_attributes(std::move(attributes)),
- m_indices(std::move(indices)) {};
+ m_indices(std::move(indices)) {}
- Mesh(
+ explicit Mesh(
std::vector<Attribute> attributes
- ) : m_attributes(std::move(attributes)),
- m_indices() {};
+ ) : m_attributes(std::move(attributes)) {}
const std::vector<uint32_t>& indices() const;
const std::vector<Attribute>& attributes() const;
diff --git a/src/GFX/Mouse.hpp b/src/GFX/Mouse.hpp
index 3ed57a2..1e2111e 100644
--- a/src/GFX/Mouse.hpp
+++ b/src/GFX/Mouse.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include <cstdint>
#include <GLFW/glfw3.h>
#include "../Math/Vector.hpp"
diff --git a/src/GFX/Shading/Program.hpp b/src/GFX/Shading/Program.hpp
index 6b28de0..8a50617 100644
--- a/src/GFX/Shading/Program.hpp
+++ b/src/GFX/Shading/Program.hpp
@@ -1,9 +1,7 @@
#pragma once
#include <string>
-#include <vector>
#include "Shader.hpp"
-#include "../../Math/Common.hpp"
#include "Uniform.hpp"
namespace MC::GFX::Shading {
diff --git a/src/GFX/Shading/Shader.cpp b/src/GFX/Shading/Shader.cpp
index f1502c2..6dfac34 100644
--- a/src/GFX/Shading/Shader.cpp
+++ b/src/GFX/Shading/Shader.cpp
@@ -4,8 +4,8 @@
namespace MC::GFX::Shading {
-Shader::Shader(Shader::Type type, const char* source) {
- uint32_t gl_type;
+Shader::Shader(Type type, const char* source) {
+ uint32_t gl_type = 0;
switch (type) {
case Type::Vertex:
gl_type = GL_VERTEX_SHADER;
diff --git a/src/GFX/Shading/Uniform.cpp b/src/GFX/Shading/Uniform.cpp
index 9448574..71786ef 100644
--- a/src/GFX/Shading/Uniform.cpp
+++ b/src/GFX/Shading/Uniform.cpp
@@ -3,11 +3,11 @@
namespace MC::GFX::Shading {
-void Uniform::set(Matrix<4, 4> value) const {
+void Uniform::set(const Matrix<4, 4>& value) const {
glUniformMatrix4fv(m_index, 1, GL_TRUE, value.elements);
}
-void Uniform::set(Vector<3> value) const {
+void Uniform::set(const Vector<3>& value) const {
glUniform3f(m_index, value.x(), value.y(), value.z());
}
diff --git a/src/GFX/Shading/Uniform.hpp b/src/GFX/Shading/Uniform.hpp
index 3c14315..a270e65 100644
--- a/src/GFX/Shading/Uniform.hpp
+++ b/src/GFX/Shading/Uniform.hpp
@@ -2,7 +2,6 @@
#include <cstdint>
#include <string>
-#include <utility>
#include "../../Math/Common.hpp"
namespace MC::GFX::Shading {
@@ -10,10 +9,10 @@ namespace MC::GFX::Shading {
class Uniform {
public:
Uniform(std::string name, uint32_t index)
- : m_name(std::move(name)), m_index(index) {};
+ : m_name(std::move(name)), m_index(index) {}
- void set(Matrix<4, 4> value) const;
- void set(Vector<3> value) const;
+ void set(const Matrix<4, 4>& value) const;
+ void set(const Vector<3>& value) const;
private:
std::string m_name;
diff --git a/src/GFX/Texture.cpp b/src/GFX/Texture.cpp
index ef48631..64151a5 100644
--- a/src/GFX/Texture.cpp
+++ b/src/GFX/Texture.cpp
@@ -23,11 +23,11 @@ Texture::Texture(const Image::RawImage& image) {
glBindTexture(GL_TEXTURE_2D, 0);
}
-void Texture::bind() {
+void Texture::bind() const {
glBindTexture(GL_TEXTURE_2D, m_texture);
}
-void Texture::unbind() {
+void Texture::unbind() const {
glBindTexture(GL_TEXTURE_2D, 0);
}
} \ No newline at end of file
diff --git a/src/GFX/Texture.hpp b/src/GFX/Texture.hpp
index ff86634..2981b2c 100644
--- a/src/GFX/Texture.hpp
+++ b/src/GFX/Texture.hpp
@@ -8,10 +8,10 @@ class Texture {
public:
explicit Texture(const Image::RawImage& image);
- void bind();
- void unbind();
+ void bind() const;
+ void unbind() const;
private:
- uint32_t m_texture;
+ uint32_t m_texture = 0;
};
} \ No newline at end of file
diff --git a/src/GFX/Window.cpp b/src/GFX/Window.cpp
index 0a1828c..ea1fde4 100644
--- a/src/GFX/Window.cpp
+++ b/src/GFX/Window.cpp
@@ -23,11 +23,11 @@ Window::~Window() {
glfwDestroyWindow(m_window);
}
-bool Window::should_close() {
+bool Window::should_close() const {
return glfwWindowShouldClose(m_window);
}
-GLFWwindow* Window::get() {
+GLFWwindow* Window::get() const {
return m_window;
}
@@ -39,8 +39,12 @@ Vector<2> Window::mouse_delta() {
return m_mouse.update(m_window);
}
-bool Window::key(int key, int type) {
- return (glfwGetKey(m_window, key) == type);
+bool Window::key(int key, int type) const {
+ return glfwGetKey(m_window, key) == type;
+}
+
+bool Window::mouse(int key, int type) const {
+ return glfwGetMouseButton(m_window, key) == type;
}
void Window::start_frame() {
@@ -49,7 +53,7 @@ void Window::start_frame() {
}
void Window::on_size_change(void (callback)(GLFWwindow*, int, int)) {
- glfwSetFramebufferSizeCallback(m_window, static_cast<GLFWframebuffersizefun>(callback));
+ glfwSetFramebufferSizeCallback(m_window, callback);
}
} \ No newline at end of file
diff --git a/src/GFX/Window.hpp b/src/GFX/Window.hpp
index 63e8446..966925c 100644
--- a/src/GFX/Window.hpp
+++ b/src/GFX/Window.hpp
@@ -12,7 +12,7 @@ public:
Window(const char* title, uint32_t width, uint32_t height);
~Window();
- GLFWwindow* get();
+ GLFWwindow* get() const;
void on_size_change(void (* callback)(GLFWwindow*, int, int));
@@ -20,8 +20,9 @@ public:
void start_frame();
Vector<2> mouse_delta();
- bool key(int key, int type);
- bool should_close();
+ bool key(int key, int type) const;
+ bool mouse(int key, int type) const;
+ bool should_close() const;
private:
GLFWwindow* m_window;
Mouse m_mouse;