added wireframe cube

This commit is contained in:
Lauchmelder 2021-12-21 00:51:18 +01:00
parent 8f8592e0b5
commit 5e53c905a8
12 changed files with 233 additions and 59 deletions

View file

@ -19,7 +19,8 @@ if(NOT GLM_FOUND)
message(STATUS "Could not find GLM on system, including from source instead")
add_subdirectory("vendor/glm")
set(GLM_INCLUDE_DIRS glm::glm_static)
set(GLM_INCLUDE_DIRS glm)
set(GLM_LIBRARIES glm)
endif()
# Include sub-projects.

View file

@ -8,6 +8,9 @@
Application::~Application()
{
if (cube != nullptr)
delete cube;
if (window != nullptr)
{
glfwDestroyWindow(window);
@ -20,7 +23,7 @@ Application::~Application()
void Application::Init(int width, int height, const std::string& title)
{
// Initialize GLFW
if(window == nullptr)
if (window == nullptr)
glfwInit();
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
@ -59,9 +62,10 @@ void Application::Init(int width, int height, const std::string& title)
}
glViewport(0, 0, mode->width, mode->height);
glEnable(GL_DEPTH_TEST);
// Register GLFW callbacks
glfwSetFramebufferSizeCallback(window,
glfwSetFramebufferSizeCallback(window,
[](GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
@ -79,49 +83,7 @@ void Application::Init(int width, int height, const std::string& title)
}
);
// TODO: Remove, this should probably be done elsewhere
model = VAOFactory::Produce(
{
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
},
{
0, 1, 2
},
{
{ 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 }
}
);
shader = ShaderFactory::Produce(
R"(
#version 460 core
layout (location = 0) in vec2 aPos;
void main()
{
gl_Position = vec4(aPos, 0.0, 1.0);
}
)",
R"(
#version 460 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.5f, 0.0f, 0.8f, 1.0f);
}
)"
);
if (!shader->Good())
{
throw std::runtime_error("Shader creation failed");
}
cube = new Cuboid();
}
void Application::Launch()
@ -131,10 +93,9 @@ void Application::Launch()
glfwPollEvents();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader->Use();
model->Render();
cube->Render();
glfwSwapBuffers(window);
}

View file

@ -1,8 +1,7 @@
#pragma once
#include <string>
#include "Shader.hpp"
#include "VertexArrayObject.hpp"
#include "Cuboid.hpp"
struct GLFWwindow;
@ -35,6 +34,5 @@ public:
private:
GLFWwindow* window = nullptr;
Shader shader = nullptr;
VertexArrayObject model = nullptr;
Cuboid* cube;
};

View file

@ -1,15 +1,18 @@
add_executable(visualizer
"main.cpp" "Application.cpp"
"VertexArrayObject.cpp" "Shader.cpp")
"backend/VertexArrayObject.cpp" "backend/Shader.cpp" "Cuboid.cpp")
target_sources(visualizer PUBLIC
${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c
)
target_include_directories(visualizer PUBLIC
target_include_directories(visualizer PRIVATE
${GLFW3_INCLUDE_DIRS}
${GLM_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/vendor/glad/include
)
target_link_libraries(visualizer PUBLIC ${GLFW3_INCLUDE_DIRS})
target_link_libraries(visualizer PRIVATE
${GLFW3_LIBRARIES}
${GLM_LIBRARIES}
)

95
src/Cuboid.cpp Normal file
View file

@ -0,0 +1,95 @@
#include "Cuboid.hpp"
#include <stdexcept>
#include <glad/glad.h>
#include <glm/gtc/matrix_transform.hpp>
#include "backend/ObjectManager.hpp"
#include "Util.hpp"
Cuboid::Cuboid()
{
vao = VAOManager::GetInstance().Get(CUBOID_ID);
if (vao == nullptr)
{
vao = VAOFactory::Produce(
{
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f
},
{
0, 1, 1, 2, 2, 3, 3, 0, // Front
4, 5, 5, 6, 6, 7, 7, 4, // Back
0, 4, 1, 5, 2, 6, 3, 7
},
{
{ 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 }
}
);
VAOManager::GetInstance().Register(CUBOID_ID, vao);
}
shader = ShaderManager::GetInstance().Get(CUBOID_ID);
if (shader == nullptr)
{
shader = ShaderFactory::Produce(
R"(
#version 460 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;msp
uniform mat4 view;
uniform mat4 perspective;
void main()
{
gl_Position = perspective * view * model * vec4(aPos, 1.0f);
}
)",
R"(
#version 460 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
)"
);
if (!shader->Good())
{
throw std::runtime_error("Shader creation failed");
}
ShaderManager::GetInstance().Register(CUBOID_ID, shader);
}
model = glm::mat4(1.0);
view = glm::lookAt(glm::vec3(2.0f, 2.0f, -3.5f), glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
perspective = glm::perspective(glm::radians(100.0f), 16.0f / 9.0f, 0.01f, 100.0f);
}
void Cuboid::Render()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
shader->Use();
shader->SetUniform("model", model);
shader->SetUniform("view", view);
shader->SetUniform("perspective", perspective);
vao->Render(GL_LINES);
}

24
src/Cuboid.hpp Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <glm/glm.hpp>
#include "backend/VertexArrayObject.hpp"
#include "backend/Shader.hpp"
/**
* A cuboid that sits at a position and expands into all
* three spatial directions
*/
class Cuboid
{
public:
Cuboid();
void Render();
private:
VertexArrayObject vao;
Shader shader;
// TODO: Remove view and projection matrices from cube class
glm::mat4 model, view, perspective;
};

3
src/Util.hpp Normal file
View file

@ -0,0 +1,3 @@
#pragma once
#define CUBOID_ID 0x1

View file

@ -0,0 +1,71 @@
#pragma once
#include <map>
#include <memory>
/**
* Some objects should only exist once but be available to multiple objects (e.g. multiple
* models sharing the same VAO and shaders. Any object can register objects here and other
* objects can then retrieve them if they have the ID.
*
* Objects are stored and returned as shared_ptr's, so even if a part of the program deletes
* an object from the manager, any part of the program that has the objects stored in them will
* not break and still work. But realistically this shouldn't happen in the first place.
* As a consequence, even if no objects are using an object stored in here it will continue to
* exist.
*/
template<typename Type>
class ObjectManager
{
public:
static ObjectManager<Type>& GetInstance()
{
static ObjectManager<Type> instance;
return instance;
}
public:
ObjectManager(const ObjectManager<Type>&) = delete;
void operator=(const ObjectManager<Type>&) = delete;
/**
* Add new (existing) object to manager
*/
inline void Register(unsigned int id, std::shared_ptr<Type> obj)
{
objects.insert(std::make_pair(id, obj));
}
/**
* Remove object from manager
*/
inline void Delete(unsigned int id)
{
objects.erase(id);
}
/**
* Retrieve object from manager
*/
inline std::shared_ptr<Type> Get(unsigned int id)
{
auto it = objects.find(id);
if (it == objects.end())
return nullptr;
return it->second;
}
private:
ObjectManager() {}
private:
std::map<unsigned int, std::shared_ptr<Type>> objects;
};
class AbstractShader;
class AbstractVertexArrayObject;
typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
typedef ObjectManager<AbstractShader> ShaderManager;

View file

@ -1,8 +1,13 @@
#include "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)
{
@ -72,3 +77,12 @@ 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));
}

View file

@ -1,5 +1,7 @@
#pragma once
#include <glm/glm.hpp>
#include <string>
#include <memory>
@ -15,6 +17,8 @@ public:
inline bool Good() { return id != 0; }
void Use();
void SetUniform(const std::string& name, const glm::mat4& value);
private:
unsigned int id;
};

View file

@ -10,13 +10,13 @@ AbstractVertexArrayObject::~AbstractVertexArrayObject()
glDeleteVertexArrays(1, &vao);
}
void AbstractVertexArrayObject::Render()
void AbstractVertexArrayObject::Render(GLenum mode)
{
assert(vao != 0);
glBindVertexArray(vao);
// GLenum result = glGetError();
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0);
}
AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :

View file

@ -34,7 +34,7 @@ public:
AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage);
~AbstractVertexArrayObject();
void Render();
void Render(unsigned int mode = 4);
private:
unsigned int vao, vbo, ebo;