summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-17 18:54:26 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-01-17 18:54:26 +0000
commit43a87590f45aa6e55724d30d0c2d0d34b407a57e (patch)
tree21ce8e8886f8aa58b159419b7d885f57d9a37580
parentInitial commit (diff)
downloadilt-43a87590f45aa6e55724d30d0c2d0d34b407a57e.tar.bz2
ilt-43a87590f45aa6e55724d30d0c2d0d34b407a57e.tar.xz
ilt-43a87590f45aa6e55724d30d0c2d0d34b407a57e.zip
First cut modernizing and sanitizing
-rw-r--r--Jamroot.jam38
-rw-r--r--camera.cpp46
-rw-r--r--camera.h49
-rw-r--r--cb.bmpbin54294 -> 0 bytes
-rw-r--r--debugTimer.h27
-rw-r--r--display.cpp19
-rw-r--r--display.h20
-rw-r--r--iwyu.json42
-rw-r--r--main.cpp72
-rw-r--r--math3d.h180
-rw-r--r--mesh.cpp63
-rw-r--r--mesh.h60
-rw-r--r--obj_loader.cpp118
-rw-r--r--obj_loader.h2
-rw-r--r--ptr.hpp25
-rw-r--r--shader.cpp100
-rw-r--r--shader.h37
-rw-r--r--stb_image.c10
-rw-r--r--texture.cpp11
-rw-r--r--texture.h13
-rw-r--r--transform.cpp27
-rw-r--r--transform.h66
-rw-r--r--util.h1
-rw-r--r--vertex.hpp36
24 files changed, 470 insertions, 592 deletions
diff --git a/Jamroot.jam b/Jamroot.jam
index 33c67c4..5058f6a 100644
--- a/Jamroot.jam
+++ b/Jamroot.jam
@@ -1,13 +1,49 @@
using gcc ;
using pkg-config ;
import pkg-config ;
+import type : register ;
+import generators : register-standard ;
pkg-config.import sdl2 ;
pkg-config.import glew ;
lib stb : : : : <include>/usr/include/stb ;
+project : requirements
+ <cxxstd>20
+ <variant>debug:<warnings>extra
+ <variant>debug:<warnings-as-errors>on
+ <variant>release:<lto>on
+ <toolset>tidy:<exclude>bin/res/fs-basicShader.h
+ <toolset>tidy:<exclude>bin/res/vs-basicShader.h
+ <toolset>tidy:<checkxx>boost-*
+ <toolset>tidy:<checkxx>bugprone-*
+ <toolset>tidy:<checkxx>clang-*
+ <toolset>tidy:<checkxx>misc-*
+ <toolset>tidy:<checkxx>modernize-*
+ <toolset>tidy:<xcheckxx>modernize-use-trailing-return-type
+ <toolset>tidy:<checkxx>hicpp-*
+ <toolset>tidy:<xcheckxx>hicpp-signed-bitwise
+ <toolset>tidy:<xcheckxx>hicpp-named-parameter
+ <toolset>tidy:<checkxx>performance-*
+ <toolset>tidy:<comments>no
+ <toolset>tidy:<mapping>iwyu.json
+ <toolset>tidy:<define>TIDY
+ ;
+
+type.register VERTEXSHADER : vs ;
+type.register FRAGMENTSHADER : fs ;
+
+generators.register-standard xxd.i : VERTEXSHADER : C(vs-%) H(vs-%) ;
+generators.register-standard xxd.i : FRAGMENTSHADER : C(fs-%) H(fs-%) ;
+
+actions xxd.i
+{
+ ( cd $(2:D) ; xxd -i $(2:B)$(2:S) ) | tee $(1[1]) | cproto -veo $(1[2])
+}
+IMPORT $(__name__) : xxd.i : : xxd.i ;
+
exe test :
- [ glob *.cpp *.c ]
+ [ glob-tree *.cpp *.c *.vs *.fs : bin ]
:
<library>sdl2
<library>glew
diff --git a/camera.cpp b/camera.cpp
new file mode 100644
index 0000000..b4a76d0
--- /dev/null
+++ b/camera.cpp
@@ -0,0 +1,46 @@
+#include "camera.h"
+#include <glm/gtx/transform.hpp>
+
+Camera::Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar) :
+ projection {glm::perspective(fov, aspect, zNear, zFar)}, pos {pos}, forward {0.0F, 0.0F, 1.0F}, up {0.0F, 1.0F,
+ 0.0F}
+{
+}
+
+glm::mat4
+Camera::GetViewProjection() const
+{
+ return projection * glm::lookAt(pos, pos + forward, up);
+}
+
+void
+Camera::MoveForward(float amt)
+{
+ pos += forward * amt;
+}
+
+void
+Camera::MoveRight(float amt)
+{
+ pos += glm::cross(up, forward) * amt;
+}
+
+void
+Camera::Pitch(float angle)
+{
+ const auto right = glm::normalize(glm::cross(up, forward));
+
+ forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0)));
+ up = glm::normalize(glm::cross(forward, right));
+}
+
+void
+Camera::RotateY(float angle)
+{
+ static constexpr glm::vec3 UP {0.0F, 1.0F, 0.0F};
+
+ const auto rotation = glm::rotate(angle, UP);
+
+ forward = glm::vec3(glm::normalize(rotation * glm::vec4(forward, 0.0)));
+ up = glm::vec3(glm::normalize(rotation * glm::vec4(up, 0.0)));
+}
diff --git a/camera.h b/camera.h
index 86b59d1..3c1e40c 100644
--- a/camera.h
+++ b/camera.h
@@ -2,53 +2,18 @@
#define CAMERA_INCLUDED_H
#include <glm/glm.hpp>
-#include <glm/gtx/transform.hpp>
-struct Camera {
+class Camera {
public:
- Camera(const glm::vec3 & pos, float fov, float aspect, float zNear, float zFar)
- {
- this->pos = pos;
- this->forward = glm::vec3(0.0f, 0.0f, 1.0f);
- this->up = glm::vec3(0.0f, 1.0f, 0.0f);
- this->projection = glm::perspective(fov, aspect, zNear, zFar);
- }
+ Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar);
- inline glm::mat4
- GetViewProjection() const
- {
- return projection * glm::lookAt(pos, pos + forward, up);
- }
+ glm::mat4 GetViewProjection() const;
- // void MoveForward(float amt)
- //{
- // pos += forward * amt;
- //}
+ void MoveForward(float amt);
+ void MoveRight(float amt);
+ void Pitch(float angle);
+ void RotateY(float angle);
- // void MoveRight(float amt)
- //{
- // pos += glm::cross(up, forward) * amt;
- //}
-
- // void Pitch(float angle)
- //{
- // glm::vec3 right = glm::normalize(glm::cross(up, forward));
-
- // forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0)));
- // up = glm::normalize(glm::cross(forward, right));
- //}
-
- // void RotateY(float angle)
- //{
- // static const glm::vec3 UP(0.0f, 1.0f, 0.0f);
-
- // glm::mat4 rotation = glm::rotate(angle, UP);
-
- // forward = glm::vec3(glm::normalize(rotation * glm::vec4(forward, 0.0)));
- // up = glm::vec3(glm::normalize(rotation * glm::vec4(up, 0.0)));
- //}
-
-protected:
private:
glm::mat4 projection;
glm::vec3 pos;
diff --git a/cb.bmp b/cb.bmp
deleted file mode 100644
index f912a9e..0000000
--- a/cb.bmp
+++ /dev/null
Binary files differ
diff --git a/debugTimer.h b/debugTimer.h
deleted file mode 100644
index fb95eec..0000000
--- a/debugTimer.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef DEBUGTIMER_H_INCLUDED
-#define DEBUGTIMER_H_INCLUDED
-
-#include <SDL2/SDL.h>
-#include <iostream>
-
-class DebugTimer {
-public:
- void
- Start()
- {
- startTime = SDL_GetTicks();
- }
-
- void
- End(const std::string & message)
- {
- unsigned int endTime = SDL_GetTicks();
- std::cout << message << (endTime - startTime) << "ms" << std::endl;
- }
-
-protected:
-private:
- unsigned int startTime;
-};
-
-#endif // DEBUGTIMER_H_INCLUDED
diff --git a/display.cpp b/display.cpp
index 4317933..b4a42d7 100644
--- a/display.cpp
+++ b/display.cpp
@@ -1,6 +1,6 @@
#include "display.h"
#include <GL/glew.h>
-#include <iostream>
+#include <stdexcept>
Display::Display(int width, int height, const std::string & title)
{
@@ -14,13 +14,12 @@ Display::Display(int width, int height, const std::string & title)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- m_window = SDL_CreateWindow(
- title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
- m_glContext = SDL_GL_CreateContext(m_window);
+ m_window = m_window.create(SDL_CreateWindow, SDL_DestroyWindow, title.c_str(), SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
+ m_glContext = m_glContext.create(SDL_GL_CreateContext, SDL_GL_DeleteContext, m_window);
- GLenum res = glewInit();
- if (res != GLEW_OK) {
- std::cerr << "Glew failed to initialize!" << std::endl;
+ if (glewInit() != GLEW_OK) {
+ throw std::runtime_error {"Glew failed to initialize!"};
}
glEnable(GL_DEPTH_TEST);
@@ -31,20 +30,18 @@ Display::Display(int width, int height, const std::string & title)
Display::~Display()
{
- SDL_GL_DeleteContext(m_glContext);
- SDL_DestroyWindow(m_window);
SDL_Quit();
}
void
-Display::Clear(float r, float g, float b, float a)
+Display::Clear(float r, float g, float b, float a) const
{
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void
-Display::SwapBuffers()
+Display::SwapBuffers() const
{
SDL_GL_SwapWindow(m_window);
}
diff --git a/display.h b/display.h
index 2573130..e5fcc73 100644
--- a/display.h
+++ b/display.h
@@ -1,28 +1,26 @@
#ifndef DISPLAY_INCLUDED_H
#define DISPLAY_INCLUDED_H
+#include "ptr.hpp"
#include <SDL2/SDL.h>
#include <string>
+#include <type_traits>
class Display {
public:
Display(int width, int height, const std::string & title);
- void Clear(float r, float g, float b, float a);
- void SwapBuffers();
+ Display(const Display &) = delete;
+ void operator=(const Display &) = delete;
virtual ~Display();
-protected:
-private:
- void
- operator=(const Display & display)
- {
- }
- Display(const Display & display) { }
+ void Clear(float r, float g, float b, float a) const;
+ void SwapBuffers() const;
- SDL_Window * m_window;
- SDL_GLContext m_glContext;
+private:
+ wrapped_ptr<SDL_Window> m_window;
+ wrapped_ptr<std::remove_pointer_t<SDL_GLContext>> m_glContext;
};
#endif
diff --git a/iwyu.json b/iwyu.json
new file mode 100644
index 0000000..ccf35c5
--- /dev/null
+++ b/iwyu.json
@@ -0,0 +1,42 @@
+[
+ {
+ "include": [
+ "<ext/alloc_traits.h>",
+ "private",
+ "<memory>",
+ "public"
+ ]
+ },
+ {
+ "include": [
+ "@\"SDL.*.h\"",
+ "private",
+ "<SDL2/SDL.h>",
+ "public"
+ ]
+ },
+ {
+ "include": [
+ "@<glm/detail/.*>",
+ "private",
+ "<glm/glm.hpp>",
+ "public"
+ ]
+ },
+ {
+ "include": [
+ "@<glm/ext/.*>",
+ "private",
+ "<glm/glm.hpp>",
+ "public"
+ ]
+ },
+ {
+ "include": [
+ "@<glm/gtx/transform.inl>",
+ "private",
+ "<glm/gtx/transform.hpp>",
+ "public"
+ ]
+ }
+]
diff --git a/main.cpp b/main.cpp
index 431610b..7f67ad4 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,84 +5,43 @@
#include "texture.h"
#include "transform.h"
#include <SDL2/SDL.h>
-#include <iostream>
+#include <cmath>
+#include <glm/glm.hpp>
+#include <memory>
+#include <numbers>
static const int DISPLAY_WIDTH = 800;
static const int DISPLAY_HEIGHT = 600;
int
-main(int argc, char ** argv)
+main(int, char **)
{
Display display(DISPLAY_WIDTH, DISPLAY_HEIGHT, "OpenGL");
- Vertex vertices[] = {
- Vertex(glm::vec3(-1, -1, -1), glm::vec2(1, 0), glm::vec3(0, 0, -1)),
- Vertex(glm::vec3(-1, 1, -1), glm::vec2(0, 0), glm::vec3(0, 0, -1)),
- Vertex(glm::vec3(1, 1, -1), glm::vec2(0, 1), glm::vec3(0, 0, -1)),
- Vertex(glm::vec3(1, -1, -1), glm::vec2(1, 1), glm::vec3(0, 0, -1)),
-
- Vertex(glm::vec3(-1, -1, 1), glm::vec2(1, 0), glm::vec3(0, 0, 1)),
- Vertex(glm::vec3(-1, 1, 1), glm::vec2(0, 0), glm::vec3(0, 0, 1)),
- Vertex(glm::vec3(1, 1, 1), glm::vec2(0, 1), glm::vec3(0, 0, 1)),
- Vertex(glm::vec3(1, -1, 1), glm::vec2(1, 1), glm::vec3(0, 0, 1)),
-
- Vertex(glm::vec3(-1, -1, -1), glm::vec2(0, 1), glm::vec3(0, -1, 0)),
- Vertex(glm::vec3(-1, -1, 1), glm::vec2(1, 1), glm::vec3(0, -1, 0)),
- Vertex(glm::vec3(1, -1, 1), glm::vec2(1, 0), glm::vec3(0, -1, 0)),
- Vertex(glm::vec3(1, -1, -1), glm::vec2(0, 0), glm::vec3(0, -1, 0)),
-
- Vertex(glm::vec3(-1, 1, -1), glm::vec2(0, 1), glm::vec3(0, 1, 0)),
- Vertex(glm::vec3(-1, 1, 1), glm::vec2(1, 1), glm::vec3(0, 1, 0)),
- Vertex(glm::vec3(1, 1, 1), glm::vec2(1, 0), glm::vec3(0, 1, 0)),
- Vertex(glm::vec3(1, 1, -1), glm::vec2(0, 0), glm::vec3(0, 1, 0)),
-
- Vertex(glm::vec3(-1, -1, -1), glm::vec2(1, 1), glm::vec3(-1, 0, 0)),
- Vertex(glm::vec3(-1, -1, 1), glm::vec2(1, 0), glm::vec3(-1, 0, 0)),
- Vertex(glm::vec3(-1, 1, 1), glm::vec2(0, 0), glm::vec3(-1, 0, 0)),
- Vertex(glm::vec3(-1, 1, -1), glm::vec2(0, 1), glm::vec3(-1, 0, 0)),
-
- Vertex(glm::vec3(1, -1, -1), glm::vec2(1, 1), glm::vec3(1, 0, 0)),
- Vertex(glm::vec3(1, -1, 1), glm::vec2(1, 0), glm::vec3(1, 0, 0)),
- Vertex(glm::vec3(1, 1, 1), glm::vec2(0, 0), glm::vec3(1, 0, 0)),
- Vertex(glm::vec3(1, 1, -1), glm::vec2(0, 1), glm::vec3(1, 0, 0)),
- };
-
- unsigned int indices[] = {0, 1, 2, 0, 2, 3,
-
- 6, 5, 4, 7, 6, 4,
-
- 10, 9, 8, 11, 10, 8,
-
- 12, 13, 14, 12, 14, 15,
-
- 16, 17, 18, 16, 18, 19,
-
- 22, 21, 20, 23, 22, 20};
-
- Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]), indices, sizeof(indices) / sizeof(indices[0]));
Mesh monkey("./res/monkey3.obj");
Shader shader("./res/basicShader");
Texture texture("./res/bricks.jpg");
Transform transform;
- Camera camera(glm::vec3(0.0f, 0.0f, -5.0f), 70.0f, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1f, 100.0f);
+ Camera camera(glm::vec3(0.0F, 0.0F, -5.0F), 70.0F, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1F, 100.0F);
SDL_Event e;
bool isRunning = true;
- float counter = 0.0f;
+ float counter = 0.0F;
while (isRunning) {
while (SDL_PollEvent(&e)) {
- if (e.type == SDL_QUIT)
+ if (e.type == SDL_QUIT) {
isRunning = false;
+ }
}
- display.Clear(0.0f, 0.0f, 0.0f, 1.0f);
+ display.Clear(0.0F, 0.0F, 0.0F, 1.0F);
- float sinCounter = sinf(counter);
- float absSinCounter = abs(sinCounter);
+ // float sinCounter = sinf(counter);
+ // float absSinCounter = abs(sinCounter);
// transform.GetPos()->x = sinCounter;
- transform.GetRot()->y = counter * 100;
- // transform.GetRot()->z = counter * 100;
+ transform.GetRot().y = std::numbers::pi_v<double> + sin(counter);
+ transform.GetRot().z = 0.3 * cos(counter * 10);
// transform.GetScale()->x = absSinCounter;
// transform.GetScale()->y = absSinCounter;
@@ -90,11 +49,10 @@ main(int argc, char ** argv)
texture.Bind();
shader.Update(transform, camera);
monkey.Draw();
- // mesh.Draw();
display.SwapBuffers();
SDL_Delay(1);
- counter += 0.01f;
+ counter += 0.01F;
}
return 0;
diff --git a/math3d.h b/math3d.h
deleted file mode 100644
index 8cdb756..0000000
--- a/math3d.h
+++ /dev/null
@@ -1,180 +0,0 @@
-#ifndef MATH3D_INCLUDED_H
-#define MATH3D_INCLUDED_H
-
-#include <glm.hpp>
-/*
-namespace Math3D
-{
- static const double PI = 3.1415926535897932384626433832795;
- static const double PIOVER2 = PI / 2.0;
- static const double PIOVER180 = PI / 180.0;
-
- static inline double ToRadians(double x) { return PIOVER180 * x; }
- static inline double ToDegrees(double x) { return 180.0 * x / PI; }
-};
-
-struct Vector3f
-{
-public:
- Vector3f(float x = 0.0f, float y = 0.0f, float z = 0.0f)
- {
- data.x = x;
- data.y = y;
- data.z = z;
- }
-
- inline float GetX() const { return data.x; }
- inline float GetY() const { return data.y; }
- inline float GetZ() const { return data.z; }
-
- inline void SetX(float x) { data.x = x; }
- inline void SetY(float y) { data.y = y; }
- inline void SetZ(float z) { data.z = z; }
-
- inline glm::vec3& GetRawVector() const { return glm::vec3(data); }
-
- inline float Length()
- {
- return sqrtf(this->Dot(*this));
- }
-
- inline Vector3f Normalized()
- {
- return *this / Length();
- }
-
- inline float Dot(const Vector3f& r) const
- {
- return glm::dot(data, r.GetRawVector());
- }
-
- inline Vector3f Cross(const Vector3f& r) const
- {
- return Vector3f(glm::cross(data, r.GetRawVector()));
- }
-
- inline Vector3f Rotate(const Vector3f& axis, float angle) const
- {
- float sinAngle = sinf(angle);
- float cosAngle = cosf(angle);
-
- return this->Cross(axis * sinAngle) + //Rotation on local X
- (*this * cosAngle) + //Rotation on local Z
- axis * this->Dot(axis * (1 - cosAngle)); //Rotation on local Y
- }
-
- inline void operator+=(const Vector3f& r)
- {
- data.x += r.GetX();
- data.y += r.GetY();
- data.z += r.GetZ();
- }
-
- inline Vector3f operator+(const Vector3f& r) const
- {
- return Vector3f(data.x + r.GetX(),
- data.y + r.GetY(),
- data.z + r.GetZ());
- }
-
- inline Vector3f operator*(float r) const
- {
- return Vector3f(data.x * r,
- data.y * r,
- data.z * r);
- }
-
- inline Vector3f operator/(float r) const
- {
- return Vector3f(data.x / r,
- data.y / r,
- data.z / r);
- }
-
-private:
- Vector3f(glm::vec3& data)
- {
- this->data.x = data.x;
- this->data.y = data.y;
- this->data.z = data.z;
- }
- glm::vec3 data;
-};
-
-struct Vector2f
-{
-public:
- Vector2f(float x = 0.0f, float y = 0.0f)
- {
- data.x = x;
- data.y = y;
- }
-
- inline float GetX() const { return data.x; }
- inline float GetY() const { return data.y; }
-
- inline void SetX(float x) { data.x = x; }
- inline void SetY(float y) { data.y = y; }
-
-private:
- DirectX::XMFLOAT2 data;
-};
-
-struct Matrix4f
-{
-public:
- static Matrix4f InitTranslation(const Vector3f& translation)
- {
- return Matrix4f(DirectX::XMMatrixTranslation(translation.GetX(), translation.GetY(), translation.GetZ()));
- }
-
- static Matrix4f InitRotation(const Vector3f& eulerAngles)
- {
- return Matrix4f(DirectX::XMMatrixRotationRollPitchYaw(eulerAngles.GetX(), eulerAngles.GetY(),
-eulerAngles.GetZ()));
- }
-
- static Matrix4f InitScale(const Vector3f& scale)
- {
- return Matrix4f(DirectX::XMMatrixScaling(scale.GetX(), scale.GetY(), scale.GetZ()));
- }
-
- static Matrix4f InitLookTo(const Vector3f& eye, const Vector3f& direction, const Vector3f& up)
- {
- return Matrix4f(DirectX::XMMatrixLookToLH(eye.GetDXVector(), direction.GetDXVector(), up.GetDXVector()));
- }
-
- static Matrix4f InitPerspective(float angle, float aspect, float zNear, float zFar)
- {
- return Matrix4f(DirectX::XMMatrixPerspectiveFovLH(angle, aspect, zNear, zFar));
- }
-
- Matrix4f()
- {
- m_Data = DirectX::XMMatrixIdentity();
- }
-
- Matrix4f Inverse() const
- {
- return Matrix4f(DirectX::XMMatrixInverse(NULL, m_Data));
- }
-
- Matrix4f Transpose() const
- {
- return Matrix4f(DirectX::XMMatrixTranspose(m_Data));
- }
-
- inline Matrix4f operator*(const Matrix4f& r) const
- {
- return Matrix4f(m_Data * r.m_Data);
- }
-private:
- Matrix4f(const DirectX::XMMATRIX& data)
- {
- m_Data = data;
- }
-
- DirectX::XMMATRIX m_Data;
-};
-*/
-#endif \ No newline at end of file
diff --git a/mesh.cpp b/mesh.cpp
index 19f3a82..8c304b3 100644
--- a/mesh.cpp
+++ b/mesh.cpp
@@ -1,43 +1,36 @@
#include "mesh.h"
-#include "debugTimer.h"
-#include "util.h"
-#include <algorithm>
-#include <fstream>
-#include <iostream>
-#include <map>
-#include <stdlib.h>
-
-Mesh::Mesh(const std::string & fileName)
-{
- InitMesh(OBJModel(fileName).ToIndexedModel());
-}
+#include "obj_loader.h"
+#include "vertex.hpp"
+#include <glm/glm.hpp>
+#include <memory>
+#include <vector>
-void
-Mesh::InitMesh(const IndexedModel & model)
-{
- m_numIndices = model.indices.size();
+Mesh::Mesh(const std::string & fileName) : Mesh(OBJModel(fileName).ToIndexedModel()) { }
+Mesh::Mesh(const IndexedModel & model) :
+ m_vertexArrayObject {}, m_vertexArrayBuffers {}, m_numIndices {model.indices.size()}
+{
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
- glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
+ glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers.data());
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(
GL_ARRAY_BUFFER, sizeof(model.positions[0]) * model.positions.size(), &model.positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
glBufferData(
GL_ARRAY_BUFFER, sizeof(model.texCoords[0]) * model.texCoords.size(), &model.texCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[NORMAL_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(model.normals[0]) * model.normals.size(), &model.normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[INDEX_VB]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(model.indices[0]) * model.indices.size(), &model.indices[0],
@@ -46,25 +39,28 @@ Mesh::InitMesh(const IndexedModel & model)
glBindVertexArray(0);
}
-Mesh::Mesh(Vertex * vertices, unsigned int numVertices, unsigned int * indices, unsigned int numIndices)
-{
- IndexedModel model;
+Mesh::Mesh(Vertex * vertices, unsigned int numVertices, unsigned int * indices, unsigned int numIndices) :
+ Mesh {[vertices, numVertices, indices, numIndices]() {
+ IndexedModel model;
- for (unsigned int i = 0; i < numVertices; i++) {
- model.positions.push_back(*vertices[i].GetPos());
- model.texCoords.push_back(*vertices[i].GetTexCoord());
- model.normals.push_back(*vertices[i].GetNormal());
- }
+ for (unsigned int i = 0; i < numVertices; i++) {
+ model.positions.push_back(vertices[i].GetPos());
+ model.texCoords.push_back(vertices[i].GetTexCoord());
+ model.normals.push_back(vertices[i].GetNormal());
+ }
- for (unsigned int i = 0; i < numIndices; i++)
- model.indices.push_back(indices[i]);
+ for (unsigned int i = 0; i < numIndices; i++) {
+ model.indices.push_back(indices[i]);
+ }
- InitMesh(model);
+ return model;
+ }()}
+{
}
Mesh::~Mesh()
{
- glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
+ glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers.data());
glDeleteVertexArrays(1, &m_vertexArrayObject);
}
@@ -73,8 +69,7 @@ Mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
- // glDrawElements(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, 0);
- glDrawElementsBaseVertex(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, 0, 0);
+ glDrawElementsBaseVertex(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, nullptr, 0);
glBindVertexArray(0);
}
diff --git a/mesh.h b/mesh.h
index aceaafe..69f41b9 100644
--- a/mesh.h
+++ b/mesh.h
@@ -1,68 +1,34 @@
#ifndef MESH_INCLUDED_H
#define MESH_INCLUDED_H
-#include "obj_loader.h"
#include <GL/glew.h>
-#include <glm/glm.hpp>
+#include <array>
+#include <cstddef>
#include <string>
-#include <vector>
-struct Vertex {
-public:
- Vertex(const glm::vec3 & pos, const glm::vec2 & texCoord, const glm::vec3 & normal)
- {
- this->pos = pos;
- this->texCoord = texCoord;
- this->normal = normal;
- }
-
- glm::vec3 *
- GetPos()
- {
- return &pos;
- }
- glm::vec2 *
- GetTexCoord()
- {
- return &texCoord;
- }
- glm::vec3 *
- GetNormal()
- {
- return &normal;
- }
-
-private:
- glm::vec3 pos;
- glm::vec2 texCoord;
- glm::vec3 normal;
-};
+class IndexedModel;
+class Vertex;
enum MeshBufferPositions { POSITION_VB, TEXCOORD_VB, NORMAL_VB, INDEX_VB };
class Mesh {
public:
- Mesh(const std::string & fileName);
+ explicit Mesh(const std::string & fileName);
+ explicit Mesh(const IndexedModel & model);
Mesh(Vertex * vertices, unsigned int numVertices, unsigned int * indices, unsigned int numIndices);
-
- void Draw();
+ Mesh(const Mesh &) = delete;
+ void operator=(const Mesh &) = delete;
virtual ~Mesh();
-protected:
-private:
- static const unsigned int NUM_BUFFERS = 4;
- void
- operator=(const Mesh & mesh)
- {
- }
- Mesh(const Mesh & mesh) { }
+ void Draw();
- void InitMesh(const IndexedModel & model);
+private:
+ static constexpr unsigned int NUM_BUFFERS {4};
GLuint m_vertexArrayObject;
- GLuint m_vertexArrayBuffers[NUM_BUFFERS];
- unsigned int m_numIndices;
+ std::array<GLuint, NUM_BUFFERS> m_vertexArrayBuffers;
+ size_t m_numIndices;
};
#endif
diff --git a/obj_loader.cpp b/obj_loader.cpp
index b3cb517..7611a2c 100644
--- a/obj_loader.cpp
+++ b/obj_loader.cpp
@@ -1,8 +1,10 @@
#include "obj_loader.h"
#include <algorithm>
#include <fstream>
-#include <iostream>
#include <map>
+#include <memory>
+#include <stdexcept>
+#include <utility>
static bool CompareOBJIndexPtr(const OBJIndex * a, const OBJIndex * b);
static inline unsigned int FindNextChar(unsigned int start, const char * str, unsigned int length, char token);
@@ -24,19 +26,25 @@ OBJModel::OBJModel(const std::string & fileName)
unsigned int lineLength = line.length();
- if (lineLength < 2)
+ if (lineLength < 2) {
continue;
+ }
const char * lineCStr = line.c_str();
switch (lineCStr[0]) {
case 'v':
- if (lineCStr[1] == 't')
- this->uvs.push_back(ParseOBJVec2(line));
- else if (lineCStr[1] == 'n')
- this->normals.push_back(ParseOBJVec3(line));
- else if (lineCStr[1] == ' ' || lineCStr[1] == '\t')
- this->vertices.push_back(ParseOBJVec3(line));
+ switch (lineCStr[1]) {
+ case 't':
+ this->uvs.push_back(ParseOBJVec2(line));
+ break;
+ case 'n':
+ this->normals.push_back(ParseOBJVec3(line));
+ break;
+ case ' ':
+ case '\t':
+ this->vertices.push_back(ParseOBJVec3(line));
+ }
break;
case 'f':
CreateOBJFace(line);
@@ -47,7 +55,7 @@ OBJModel::OBJModel(const std::string & fileName)
}
}
else {
- std::cerr << "Unable to load mesh: " << fileName << std::endl;
+ throw std::runtime_error {"Unable to load mesh: " + fileName};
}
}
@@ -69,8 +77,9 @@ IndexedModel::CalcNormals()
normals[i2] += normal;
}
- for (unsigned int i = 0; i < positions.size(); i++)
+ for (unsigned int i = 0; i < positions.size(); i++) {
normals[i] = glm::normalize(normals[i]);
+ }
}
IndexedModel
@@ -83,8 +92,9 @@ OBJModel::ToIndexedModel()
std::vector<OBJIndex *> indexLookup;
- for (unsigned int i = 0; i < numIndices; i++)
+ for (unsigned int i = 0; i < numIndices; i++) {
indexLookup.push_back(&OBJIndices[i]);
+ }
std::sort(indexLookup.begin(), indexLookup.end(), CompareOBJIndexPtr);
@@ -98,21 +108,25 @@ OBJModel::ToIndexedModel()
glm::vec2 currentTexCoord;
glm::vec3 currentNormal;
- if (hasUVs)
+ if (hasUVs) {
currentTexCoord = uvs[currentIndex->uvIndex];
- else
+ }
+ else {
currentTexCoord = glm::vec2(0, 0);
+ }
- if (hasNormals)
+ if (hasNormals) {
currentNormal = normals[currentIndex->normalIndex];
- else
+ }
+ else {
currentNormal = glm::vec3(0, 0, 0);
+ }
unsigned int normalModelIndex;
unsigned int resultModelIndex;
// Create model to properly generate normals on
- std::map<OBJIndex, unsigned int>::iterator it = normalModelIndexMap.find(*currentIndex);
+ const auto it = normalModelIndexMap.find(*currentIndex);
if (it == normalModelIndexMap.end()) {
normalModelIndex = normalModel.positions.size();
@@ -121,8 +135,9 @@ OBJModel::ToIndexedModel()
normalModel.texCoords.push_back(currentTexCoord);
normalModel.normals.push_back(currentNormal);
}
- else
+ else {
normalModelIndex = it->second;
+ }
// Create model which properly separates texture coordinates
unsigned int previousVertexLocation = FindLastVertexIndex(indexLookup, currentIndex, result);
@@ -134,8 +149,9 @@ OBJModel::ToIndexedModel()
result.texCoords.push_back(currentTexCoord);
result.normals.push_back(currentNormal);
}
- else
+ else {
resultModelIndex = previousVertexLocation;
+ }
normalModel.indices.push_back(normalModelIndex);
result.indices.push_back(resultModelIndex);
@@ -145,8 +161,9 @@ OBJModel::ToIndexedModel()
if (!hasNormals) {
normalModel.CalcNormals();
- for (unsigned int i = 0; i < result.positions.size(); i++)
+ for (unsigned int i = 0; i < result.positions.size(); i++) {
result.normals[i] = normalModel.normals[indexMap[i]];
+ }
}
return result;
@@ -170,11 +187,13 @@ OBJModel::FindLastVertexIndex(
for (unsigned int i = 0; i < current; i++) {
OBJIndex * possibleIndex = indexLookup[current - i];
- if (possibleIndex == currentIndex)
+ if (possibleIndex == currentIndex) {
continue;
+ }
- if (possibleIndex->vertexIndex != currentIndex->vertexIndex)
+ if (possibleIndex->vertexIndex != currentIndex->vertexIndex) {
break;
+ }
countStart--;
}
@@ -182,26 +201,32 @@ OBJModel::FindLastVertexIndex(
for (unsigned int i = countStart; i < indexLookup.size() - countStart; i++) {
OBJIndex * possibleIndex = indexLookup[current + i];
- if (possibleIndex == currentIndex)
+ if (possibleIndex == currentIndex) {
continue;
+ }
- if (possibleIndex->vertexIndex != currentIndex->vertexIndex)
+ if (possibleIndex->vertexIndex != currentIndex->vertexIndex) {
break;
+ }
else if ((!hasUVs || possibleIndex->uvIndex == currentIndex->uvIndex)
&& (!hasNormals || possibleIndex->normalIndex == currentIndex->normalIndex)) {
glm::vec3 currentPosition = vertices[currentIndex->vertexIndex];
glm::vec2 currentTexCoord;
glm::vec3 currentNormal;
- if (hasUVs)
+ if (hasUVs) {
currentTexCoord = uvs[currentIndex->uvIndex];
- else
+ }
+ else {
currentTexCoord = glm::vec2(0, 0);
+ }
- if (hasNormals)
+ if (hasNormals) {
currentNormal = normals[currentIndex->normalIndex];
- else
+ }
+ else {
currentNormal = glm::vec3(0, 0, 0);
+ }
for (unsigned int j = 0; j < result.positions.size(); j++) {
if (currentPosition == result.positions[j]
@@ -216,10 +241,12 @@ OBJModel::FindLastVertexIndex(
return -1;
}
else {
- if (testIndex->vertexIndex < currentIndex->vertexIndex)
+ if (testIndex->vertexIndex < currentIndex->vertexIndex) {
start = current;
- else
+ }
+ else {
end = current;
+ }
}
previous = current;
@@ -254,13 +281,15 @@ OBJModel::ParseOBJIndex(const std::string & token, bool * hasUVs, bool * hasNorm
unsigned int vertIndexStart = 0;
unsigned int vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, '/');
- OBJIndex result;
- result.vertexIndex = ParseOBJIndexValue(token, vertIndexStart, vertIndexEnd);
- result.uvIndex = 0;
- result.normalIndex = 0;
+ OBJIndex result {
+ .vertexIndex = ParseOBJIndexValue(token, vertIndexStart, vertIndexEnd),
+ .uvIndex = 0,
+ .normalIndex = 0,
+ };
- if (vertIndexEnd >= tokenLength)
+ if (vertIndexEnd >= tokenLength) {
return result;
+ }
vertIndexStart = vertIndexEnd + 1;
vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, '/');
@@ -268,8 +297,9 @@ OBJModel::ParseOBJIndex(const std::string & token, bool * hasUVs, bool * hasNorm
result.uvIndex = ParseOBJIndexValue(token, vertIndexStart, vertIndexEnd);
*hasUVs = true;
- if (vertIndexEnd >= tokenLength)
+ if (vertIndexEnd >= tokenLength) {
return result;
+ }
vertIndexStart = vertIndexEnd + 1;
vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, '/');
@@ -289,8 +319,9 @@ OBJModel::ParseOBJVec3(const std::string & line)
unsigned int vertIndexStart = 2;
while (vertIndexStart < tokenLength) {
- if (tokenString[vertIndexStart] != ' ')
+ if (tokenString[vertIndexStart] != ' ') {
break;
+ }
vertIndexStart++;
}
@@ -309,8 +340,6 @@ OBJModel::ParseOBJVec3(const std::string & line)
float z = ParseOBJFloatValue(line, vertIndexStart, vertIndexEnd);
return glm::vec3(x, y, z);
-
- // glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()))
}
glm::vec2
@@ -322,8 +351,9 @@ OBJModel::ParseOBJVec2(const std::string & line)
unsigned int vertIndexStart = 3;
while (vertIndexStart < tokenLength) {
- if (tokenString[vertIndexStart] != ' ')
+ if (tokenString[vertIndexStart] != ' ') {
break;
+ }
vertIndexStart++;
}
@@ -351,8 +381,9 @@ FindNextChar(unsigned int start, const char * str, unsigned int length, char tok
unsigned int result = start;
while (result < length) {
result++;
- if (str[result] == token)
+ if (str[result] == token) {
break;
+ }
}
return result;
@@ -361,13 +392,13 @@ FindNextChar(unsigned int start, const char * str, unsigned int length, char tok
static inline unsigned int
ParseOBJIndexValue(const std::string & token, unsigned int start, unsigned int end)
{
- return atoi(token.substr(start, end - start).c_str()) - 1;
+ return std::stoul(token.substr(start, end - start)) - 1;
}
static inline float
ParseOBJFloatValue(const std::string & token, unsigned int start, unsigned int end)
{
- return atof(token.substr(start, end - start).c_str());
+ return std::stof(token.substr(start, end - start));
}
static inline std::vector<std::string>
@@ -382,8 +413,9 @@ SplitString(const std::string & s, char delim)
while (end <= strLength) {
while (end <= strLength) {
- if (cstr[end] == delim)
+ if (cstr[end] == delim) {
break;
+ }
end++;
}
diff --git a/obj_loader.h b/obj_loader.h
index 5a1eabf..11c6b38 100644
--- a/obj_loader.h
+++ b/obj_loader.h
@@ -36,7 +36,7 @@ public:
bool hasUVs;
bool hasNormals;
- OBJModel(const std::string & fileName);
+ explicit OBJModel(const std::string & fileName);
IndexedModel ToIndexedModel();
diff --git a/ptr.hpp b/ptr.hpp
new file mode 100644
index 0000000..b92b63e
--- /dev/null
+++ b/ptr.hpp
@@ -0,0 +1,25 @@
+#ifndef PTR_H
+#define PTR_H
+
+#include <memory>
+
+template<typename Obj> class wrapped_ptr : public std::unique_ptr<Obj, void (*)(Obj *)> {
+public:
+ using std::unique_ptr<Obj, void (*)(Obj *)>::unique_ptr;
+ wrapped_ptr() : std::unique_ptr<Obj, void (*)(Obj *)> {{}, {}} { }
+
+ inline
+ operator Obj *() const
+ {
+ return this->get();
+ }
+
+ template<typename... Args, typename... Params>
+ static auto
+ create(Obj * (*factory)(Args...), void (*deleter)(Obj *), Params &&... params)
+ {
+ return wrapped_ptr<Obj> {factory(std::forward<Params>(params)...), deleter};
+ }
+};
+
+#endif
diff --git a/shader.cpp b/shader.cpp
index 21098bd..4d9f43b 100644
--- a/shader.cpp
+++ b/shader.cpp
@@ -1,15 +1,20 @@
#include "shader.h"
-#include <fstream>
-#include <iostream>
-
-Shader::Shader(const std::string & fileName)
+#include "transform.h"
+#include <glm/glm.hpp>
+#include <res/fs-basicShader.h>
+#include <res/vs-basicShader.h>
+#include <stdexcept>
+
+Shader::Shader(const std::string &) :
+ m_program {glCreateProgram()}, m_shaders {CreateShader(
+ (GLchar *)(basicShader_vs), basicShader_vs_len, GL_VERTEX_SHADER),
+ CreateShader(
+ (GLchar *)basicShader_fs, basicShader_fs_len, GL_FRAGMENT_SHADER)},
+ m_uniforms {}
{
- m_program = glCreateProgram();
- m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
- m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);
-
- for (unsigned int i = 0; i < NUM_SHADERS; i++)
- glAttachShader(m_program, m_shaders[i]);
+ for (auto m_shader : m_shaders) {
+ glAttachShader(m_program, m_shader);
+ }
glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");
@@ -19,97 +24,74 @@ Shader::Shader(const std::string & fileName)
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program");
glValidateProgram(m_program);
- CheckShaderError(m_program, GL_LINK_STATUS, true, "Invalid shader program");
+ CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program");
- m_uniforms[0] = glGetUniformLocation(m_program, "MVP");
- m_uniforms[1] = glGetUniformLocation(m_program, "Normal");
- m_uniforms[2] = glGetUniformLocation(m_program, "lightDirection");
+ m_uniforms = {glGetUniformLocation(m_program, "MVP"), glGetUniformLocation(m_program, "Normal"),
+ glGetUniformLocation(m_program, "lightDirection")};
}
Shader::~Shader()
{
- for (unsigned int i = 0; i < NUM_SHADERS; i++) {
- glDetachShader(m_program, m_shaders[i]);
- glDeleteShader(m_shaders[i]);
+ for (auto m_shader : m_shaders) {
+ glDetachShader(m_program, m_shader);
+ glDeleteShader(m_shader);
}
glDeleteProgram(m_program);
}
void
-Shader::Bind()
+Shader::Bind() const
{
glUseProgram(m_program);
}
void
-Shader::Update(const Transform & transform, const Camera & camera)
+Shader::Update(const Transform & transform, const Camera & camera) const
{
glm::mat4 MVP = transform.GetMVP(camera);
glm::mat4 Normal = transform.GetModel();
glUniformMatrix4fv(m_uniforms[0], 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(m_uniforms[1], 1, GL_FALSE, &Normal[0][0]);
- glUniform3f(m_uniforms[2], 0.0f, 0.0f, 1.0f);
-}
-
-std::string
-Shader::LoadShader(const std::string & fileName)
-{
- std::ifstream file;
- file.open((fileName).c_str());
-
- std::string output;
- std::string line;
-
- if (file.is_open()) {
- while (file.good()) {
- getline(file, line);
- output.append(line + "\n");
- }
- }
- else {
- std::cerr << "Unable to load shader: " << fileName << std::endl;
- }
-
- return output;
+ glUniform3f(m_uniforms[2], 0.0F, 0.0F, 1.0F);
}
void
Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage)
{
GLint success = 0;
- GLchar error[1024] = {0};
+ std::array<GLchar, 1024> error {};
- if (isProgram)
+ if (isProgram) {
glGetProgramiv(shader, flag, &success);
- else
+ }
+ else {
glGetShaderiv(shader, flag, &success);
+ }
if (success == GL_FALSE) {
- if (isProgram)
- glGetProgramInfoLog(shader, sizeof(error), NULL, error);
- else
- glGetShaderInfoLog(shader, sizeof(error), NULL, error);
+ if (isProgram) {
+ glGetProgramInfoLog(shader, error.size(), nullptr, error.data());
+ }
+ else {
+ glGetShaderInfoLog(shader, error.size(), nullptr, error.data());
+ }
- std::cerr << errorMessage << ": '" << error << "'" << std::endl;
+ throw std::runtime_error {errorMessage + ": '" + std::string {error.data(), error.size()} + "'"};
}
}
GLuint
-Shader::CreateShader(const std::string & text, unsigned int type)
+Shader::CreateShader(const GLchar * text, GLint len, unsigned int type)
{
GLuint shader = glCreateShader(type);
- if (shader == 0)
- std::cerr << "Error compiling shader type " << type << std::endl;
-
- const GLchar * p[1];
- p[0] = text.c_str();
- GLint lengths[1];
- lengths[0] = text.length();
+ if (shader == 0) {
+ throw std::runtime_error {"Error compiling shader type " + std::to_string(type)};
+ }
- glShaderSource(shader, 1, p, lengths);
+ glShaderSource(shader, 1, &text, &len);
glCompileShader(shader);
CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");
diff --git a/shader.h b/shader.h
index bd3ac38..85a7d58 100644
--- a/shader.h
+++ b/shader.h
@@ -1,36 +1,33 @@
#ifndef SHADER_INCLUDED_H
#define SHADER_INCLUDED_H
-#include "transform.h"
#include <GL/glew.h>
+#include <array>
#include <string>
+class Camera;
+class Transform;
+
class Shader {
public:
- Shader(const std::string & fileName);
-
- void Bind();
- void Update(const Transform & transform, const Camera & camera);
-
+ explicit Shader(const std::string & fileName);
virtual ~Shader();
+ void operator=(const Shader &) = delete;
+ Shader(const Shader &) = delete;
+
+ void Bind() const;
+ void Update(const Transform & transform, const Camera & camera) const;
-protected:
private:
- static const unsigned int NUM_SHADERS = 2;
- static const unsigned int NUM_UNIFORMS = 3;
- void
- operator=(const Shader & shader)
- {
- }
- Shader(const Shader & shader) { }
-
- std::string LoadShader(const std::string & fileName);
- void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage);
- GLuint CreateShader(const std::string & text, unsigned int type);
+ static constexpr unsigned int NUM_SHADERS = 2;
+ static constexpr unsigned int NUM_UNIFORMS = 3;
+
+ static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage);
+ static GLuint CreateShader(const GLchar * text, GLint len, unsigned int type);
GLuint m_program;
- GLuint m_shaders[NUM_SHADERS];
- GLuint m_uniforms[NUM_UNIFORMS];
+ std::array<GLuint, NUM_SHADERS> m_shaders;
+ std::array<GLint, NUM_UNIFORMS> m_uniforms;
};
#endif
diff --git a/stb_image.c b/stb_image.c
index 8ddfd1f..a839b61 100644
--- a/stb_image.c
+++ b/stb_image.c
@@ -1,2 +1,8 @@
-#define STB_IMAGE_IMPLEMENTATION
-#include "stb_image.h"
+#ifndef TIDY
+# define STB_IMAGE_IMPLEMENTATION
+# pragma GCC diagnostic ignored "-Wsign-compare"
+# ifndef __clang__
+# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
+# endif
+# include "stb_image.h"
+#endif
diff --git a/texture.cpp b/texture.cpp
index aae16b3..a388b32 100644
--- a/texture.cpp
+++ b/texture.cpp
@@ -1,14 +1,15 @@
#include "texture.h"
#include "stb_image.h"
-#include <iostream>
+#include <stdexcept>
-Texture::Texture(const std::string & fileName)
+Texture::Texture(const std::string & fileName) : m_texture {}
{
int width, height, numComponents;
unsigned char * data = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);
- if (data == NULL)
- std::cerr << "Unable to load texture: " << fileName << std::endl;
+ if (!data) {
+ throw std::runtime_error {"Unable to load texture: " + fileName};
+ }
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
@@ -28,7 +29,7 @@ Texture::~Texture()
}
void
-Texture::Bind()
+Texture::Bind() const
{
glBindTexture(GL_TEXTURE_2D, m_texture);
}
diff --git a/texture.h b/texture.h
index df9f287..9955f01 100644
--- a/texture.h
+++ b/texture.h
@@ -6,20 +6,17 @@
class Texture {
public:
- Texture(const std::string & fileName);
+ explicit Texture(const std::string & fileName);
- void Bind();
+ Texture(const Texture &) = delete;
+ void operator=(const Texture &) = delete;
virtual ~Texture();
+ void Bind() const;
+
protected:
private:
- Texture(const Texture & texture) { }
- void
- operator=(const Texture & texture)
- {
- }
-
GLuint m_texture;
};
diff --git a/transform.cpp b/transform.cpp
new file mode 100644
index 0000000..7b256af
--- /dev/null
+++ b/transform.cpp
@@ -0,0 +1,27 @@
+#include "transform.h"
+#include "camera.h"
+#include <glm/gtx/transform.hpp>
+
+Transform::Transform(glm::vec3 pos, glm::vec3 rot, glm::vec3 scale) : pos {pos}, rot {rot}, scale {scale} { }
+
+glm::mat4
+Transform::GetModel() const
+{
+ const auto posMat = glm::translate(pos);
+ const auto scaleMat = glm::scale(scale);
+ const auto rotX = glm::rotate(rot.x, glm::vec3(1.0, 0.0, 0.0));
+ const auto rotY = glm::rotate(rot.y, glm::vec3(0.0, 1.0, 0.0));
+ const auto rotZ = glm::rotate(rot.z, glm::vec3(0.0, 0.0, 1.0));
+ const auto rotMat = rotX * rotY * rotZ;
+
+ return posMat * rotMat * scaleMat;
+}
+
+glm::mat4
+Transform::GetMVP(const Camera & camera) const
+{
+ const auto VP = camera.GetViewProjection();
+ const auto M = GetModel();
+
+ return VP * M;
+}
diff --git a/transform.h b/transform.h
index ed9b703..783ef31 100644
--- a/transform.h
+++ b/transform.h
@@ -1,75 +1,55 @@
#ifndef TRANSFORM_INCLUDED_H
#define TRANSFORM_INCLUDED_H
-#include "camera.h"
#include <glm/glm.hpp>
-#include <glm/gtx/transform.hpp>
+#include <utility>
-struct Transform {
-public:
- Transform(const glm::vec3 & pos = glm::vec3(), const glm::vec3 & rot = glm::vec3(),
- const glm::vec3 & scale = glm::vec3(1.0f, 1.0f, 1.0f))
- {
- this->pos = pos;
- this->rot = rot;
- this->scale = scale;
- }
+class Camera;
- inline glm::mat4
- GetModel() const
- {
- glm::mat4 posMat = glm::translate(pos);
- glm::mat4 scaleMat = glm::scale(scale);
- glm::mat4 rotX = glm::rotate(rot.x, glm::vec3(1.0, 0.0, 0.0));
- glm::mat4 rotY = glm::rotate(rot.y, glm::vec3(0.0, 1.0, 0.0));
- glm::mat4 rotZ = glm::rotate(rot.z, glm::vec3(0.0, 0.0, 1.0));
- glm::mat4 rotMat = rotX * rotY * rotZ;
+class Transform {
+public:
+ Transform(glm::vec3 pos = {}, glm::vec3 rot = {}, glm::vec3 scale = {1.0f, 1.0f, 1.0f});
- return posMat * rotMat * scaleMat;
- }
+ glm::mat4 GetModel() const;
- inline glm::mat4
- GetMVP(const Camera & camera) const
- {
- glm::mat4 VP = camera.GetViewProjection();
- glm::mat4 M = GetModel();
+ glm::mat4 GetMVP(const Camera & camera) const;
- return VP * M; // camera.GetViewProjection() * GetModel();
- }
-
- inline glm::vec3 *
+ inline glm::vec3 &
GetPos()
{
- return &pos;
+ return pos;
}
- inline glm::vec3 *
+
+ inline glm::vec3 &
GetRot()
{
- return &rot;
+ return rot;
}
- inline glm::vec3 *
+
+ inline glm::vec3 &
GetScale()
{
- return &scale;
+ return scale;
}
inline void
- SetPos(glm::vec3 & pos)
+ SetPos(glm::vec3 && pos)
{
- this->pos = pos;
+ this->pos = std::move(pos);
}
+
inline void
- SetRot(glm::vec3 & rot)
+ SetRot(glm::vec3 && rot)
{
- this->rot = rot;
+ this->rot = std::move(rot);
}
+
inline void
- SetScale(glm::vec3 & scale)
+ SetScale(glm::vec3 && scale)
{
- this->scale = scale;
+ this->scale = std::move(scale);
}
-protected:
private:
glm::vec3 pos;
glm::vec3 rot;
diff --git a/util.h b/util.h
index cbabdb8..83de626 100644
--- a/util.h
+++ b/util.h
@@ -1,7 +1,6 @@
#ifndef UTIL_H_INCLUDED
#define UTIL_H_INCLUDED
-#include "debugTimer.h"
#include <sstream>
#include <vector>
diff --git a/vertex.hpp b/vertex.hpp
new file mode 100644
index 0000000..3f6514b
--- /dev/null
+++ b/vertex.hpp
@@ -0,0 +1,36 @@
+#ifndef VERTEX_H
+#define VERTEX_H
+
+#include <glm/glm.hpp>
+
+class Vertex {
+public:
+ Vertex(glm::vec3 pos, glm::vec2 texCoord, glm::vec3 normal) :
+ pos {std::move(pos)}, texCoord {std::move(texCoord)}, normal {std::move(normal)}
+ {
+ }
+
+ glm::vec3 &
+ GetPos()
+ {
+ return pos;
+ }
+
+ glm::vec2 &
+ GetTexCoord()
+ {
+ return texCoord;
+ }
+
+ glm::vec3 &
+ GetNormal()
+ {
+ return normal;
+ }
+
+private:
+ glm::vec3 pos;
+ glm::vec2 texCoord;
+ glm::vec3 normal;
+};
+#endif