Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a possibility to build with CMake #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
submodules/
4 changes: 4 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
InlayHints:
Enabled: No
ParameterNames: Yes
DeducedTypes: No
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.cache/
.vscode/
build/
build_*/
bazel-*
/compile_commands.json
/opengl_tutorial.sublime-workspace
21 changes: 21 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[submodule "submodules/glfw"]
path = submodules/glfw
url = https://github.com/glfw/glfw.git
[submodule "submodules/eigen"]
path = submodules/eigen
url = https://github.com/libigl/eigen.git
[submodule "submodules/abseil"]
path = submodules/abseil
url = https://github.com/abseil/abseil-cpp.git
[submodule "submodules/stb"]
path = submodules/stb
url = https://github.com/nothings/stb.git
[submodule "submodules/googletest"]
path = submodules/googletest
url = https://github.com/google/googletest.git
[submodule "submodules/glog"]
path = submodules/glog
url = https://github.com/google/glog.git
[submodule "submodules/glad"]
path = submodules/glad
url = https://github.com/Dav1dde/glad.git
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.16...3.23)

project(igloo VERSION 0.0.1 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(submodules)
add_subdirectory(utils)
add_subdirectory(gl)
5 changes: 2 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ pip_install(

http_archive(
name = "glfw",
urls = ["https://github.com/glfw/glfw/releases/download/3.3.2/glfw-3.3.2.zip"],
sha256 = "08a33a512f29d7dbf78eab39bd7858576adcc95228c9efe8e4bc5f0f3261efc7",
strip_prefix = "glfw-3.3.2",
urls = ["https://github.com/glfw/glfw/releases/download/3.3.8/glfw-3.3.8.zip"],
strip_prefix = "glfw-3.3.8",
build_file = "@//:third_party/glfw/glfw.BUILD",
)

Expand Down
8 changes: 5 additions & 3 deletions examples/3d_viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include "gl/scene/drawables/all.h"
#include "gl/viewer/viewer.h"

#include <Eigen/Core>
#include <Eigen/Geometry>
#include <GLFW/glfw3.h>

#include <iostream>
#include <ostream>
#include <vector>
#include <cmath>

namespace {

Expand Down Expand Up @@ -93,15 +95,15 @@ int main(int argc, char* argv[]) {
texture_3d_drawable,
Eigen::Translation3f{5.0F, 0.0F, 0.0F} *
Eigen::AngleAxisf(0.0F, Eigen::Vector3f::UnitX()) *
Eigen::AngleAxisf(-0.5F * M_PIf32, Eigen::Vector3f::UnitY()) *
Eigen::AngleAxisf(-0.5F * M_PIf32, Eigen::Vector3f::UnitZ()));
Eigen::AngleAxisf(-0.5F * M_PI, Eigen::Vector3f::UnitY()) *
Eigen::AngleAxisf(-0.5F * M_PI, Eigen::Vector3f::UnitZ()));
viewer.Attach(
viewer.world_key(),
texture_3d_drawable,
Eigen::Translation3f{-1.0F, -1.0F, 2.0F} *
Eigen::AngleAxisf(0.0F, Eigen::Vector3f::UnitX()) *
Eigen::AngleAxisf(0.0, Eigen::Vector3f::UnitY()) *
Eigen::AngleAxisf(-0.5F * M_PIf32, Eigen::Vector3f::UnitZ()));
Eigen::AngleAxisf(-0.5F * M_PI, Eigen::Vector3f::UnitZ()));
viewer.AttachToScreen(texture_2d_drawable, {0.5f, 0.5f});

viewer.camera().LookAt({0.0f, 0.0f, 0.0f}, {-10.0f, 0.0f, 3.0f});
Expand Down
2 changes: 2 additions & 0 deletions gl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(ui)
add_subdirectory(core)
30 changes: 30 additions & 0 deletions gl/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
add_library(core
shader.cpp
uniform.cpp
texture.cpp
program.cpp
)
target_link_libraries(core PUBLIC
file_utils
type_traits
macro_utils
image
glad_gl_core_33
glog::glog
absl::strings
absl::str_format
)

add_executable(core_test
buffer_test.cpp
shader_test.cpp
program_test.cpp
uniform_test.cpp
main_test.cpp
)
target_link_libraries(core_test PRIVATE
core
glfw_viewer
eigen_utils
GTest::gtest
)
57 changes: 37 additions & 20 deletions gl/core/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,52 @@
#ifndef CODE_OPENGL_TUTORIALS_GL_CORE_INIT_H_
#define CODE_OPENGL_TUTORIALS_GL_CORE_INIT_H_

#include "glad/gl.h"
#include "glog/logging.h"
#include "third_party/glad/glad.h"

#include <functional>

namespace gl {

void GLAPIENTRY MessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam) {
if (type == GL_DEBUG_TYPE_ERROR) {
LOG(FATAL) << "GL ERROR message: '" << message << "'";
inline std::string ErrorDescription(GLenum err) {
switch (err) {
case GL_NO_ERROR:
return "GL_NO_ERROR: No error has been recorded. The value of this "
"symbolic constant is guaranteed to be 0. ";
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM: An unacceptable value is specified for an "
"enumerated argument. The offending command is ignored and has no "
"other side effect than to set the error flag. ";
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE: A numeric argument is out of range. The "
"offending command is ignored and has no other side effect than "
"to set the error flag. ";
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION: The specified operation is not allowed in "
"the current state. The offending command is ignored and has no "
"other side effect than to set the error flag. ";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION: The framebuffer object is not "
"complete."
"The offending command is ignored and has no other side effect "
"than to set the error flag.";
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY: There is not enough memory left to execute the "
"command. The state of the GL is undefined, except for the state "
"of the error flags, after this error is recorded. . ";
case GL_STACK_UNDERFLOW:
return "GL_STACK_UNDERFLOW: An attempt has been made to perform an "
"operation that would cause an internal stack to underflow. ";
case GL_STACK_OVERFLOW:
return "GL_STACK_OVERFLOW: An attempt has been made to perform an "
"operation that would cause an internal stack to overflow. ";
default: return "No Description";
}
}

template <typename FunctionT>
void InitializeGlContext(FunctionT intialization_function) {
if (!gladLoadGLLoader(
reinterpret_cast<GLADloadproc>(intialization_function))) {
LOG(FATAL) << "Cannot initialize OpenGL context with GLAD.";
}
#ifndef NDEBUG
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, 0);
#endif
inline void CheckGlError() {
const auto err = glGetError();
if (err != GL_NO_ERROR) { LOG(FATAL) << ErrorDescription(err); }
}

} // namespace gl
Expand Down
2 changes: 1 addition & 1 deletion gl/core/opengl_object.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CODE_OPENGL_TUTORIALS_GL_CORE_OPENGL_OBJECT_H_
#define CODE_OPENGL_TUTORIALS_GL_CORE_OPENGL_OBJECT_H_

#include "third_party/glad/glad.h"
#include "glad/gl.h"

#include <cstdint>

Expand Down
1 change: 1 addition & 0 deletions gl/core/test_shaders/shader.vert
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#version 330 core

uniform float dummy_value_dim_1;
uniform vec2 dummy_value_dim_2;
uniform vec3 dummy_value_dim_3;
Expand Down
5 changes: 4 additions & 1 deletion gl/core/uniform.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENGL_TUTORIALS_CORE_GL_UNIFORM_H_
#define OPENGL_TUTORIALS_CORE_GL_UNIFORM_H_

#include "gl/core/init.h"
#include "gl/core/opengl_object.h"
#include "gl/core/traits.h"
#include "utils/type_traits.h"
Expand All @@ -17,7 +18,9 @@ class Uniform : public OpenGlObject {
Uniform(const std::string& name, std::uint32_t program_id)
: OpenGlObject{0},
name_{name},
location_{glGetUniformLocation(program_id, name_.c_str())} {}
location_{glGetUniformLocation(program_id, name_.c_str())} {
CheckGlError();
}

GLint location() const noexcept { return location_; }

Expand Down
53 changes: 38 additions & 15 deletions gl/core/uniform_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,71 @@ class UniformTest : public ::testing::Test {
TEST_F(UniformTest, Init) {
Uniform uniform{"dummy_value_dim_1", program_->id()};
EXPECT_EQ("dummy_value_dim_1", uniform.name());
EXPECT_EQ(uniform.location(), 0);
EXPECT_EQ(uniform.location(),
glGetUniformLocation(program_->id(), "dummy_value_dim_1"));
}

TEST_F(UniformTest, UpdateValueFromPack) {
Uniform uniform_1{"dummy_value_dim_1", program_->id()};
EXPECT_EQ(uniform_1.location(), 0);
uniform_1.UpdateValue(1.0f);
float value[4] = {42.0f, 42.0f, 42.0f, 42.0f};
glGetUniformfv(program_->id(), uniform_1.location(), value);
EXPECT_FLOAT_EQ(value[0], 1.0f);
EXPECT_FLOAT_EQ(value[1], 42.0f);
Uniform uniform_2{"dummy_value_dim_2", program_->id()};
EXPECT_EQ(uniform_2.location(), 1);
uniform_2.UpdateValue(1.0f, 2.0f);
uniform_2.UpdateValue(1.1f, 2.2f);
glGetUniformfv(program_->id(), uniform_2.location(), value);
EXPECT_FLOAT_EQ(value[0], 1.1f);
EXPECT_FLOAT_EQ(value[1], 2.2f);
EXPECT_FLOAT_EQ(value[2], 42.0f);
Uniform uniform_3{"dummy_value_dim_3", program_->id()};
EXPECT_EQ(uniform_3.location(), 2);
uniform_3.UpdateValue(1.0f, 2.0f, 3.0f);
uniform_3.UpdateValue(1.11f, 2.22f, 3.33f);
glGetUniformfv(program_->id(), uniform_3.location(), value);
EXPECT_FLOAT_EQ(value[0], 1.11f);
EXPECT_FLOAT_EQ(value[1], 2.22f);
EXPECT_FLOAT_EQ(value[2], 3.33f);
EXPECT_FLOAT_EQ(value[3], 42.0f);
Uniform uniform_4{"dummy_value_dim_4", program_->id()};
EXPECT_EQ(uniform_4.location(), 3);
uniform_4.UpdateValue(1.0f, 2.0f, 3.0f, 4.0f);
glGetUniformfv(program_->id(), uniform_4.location(), value);
EXPECT_FLOAT_EQ(value[0], 1.0f);
EXPECT_FLOAT_EQ(value[1], 2.0f);
EXPECT_FLOAT_EQ(value[2], 3.0f);
EXPECT_FLOAT_EQ(value[3], 4.0f);
}

TEST_F(UniformTest, UpdateValueFromEigenMat) {
Uniform uniform_2{"dummy_value_dim_2", program_->id()};
EXPECT_EQ(uniform_2.location(), 1);
uniform_2.UpdateValue(Eigen::Vector2f{1.0f, 2.0f});
float values_2[2] = {42.0f, 42.0f};
glGetUniformfv(program_->id(), uniform_2.location(), values_2);
EXPECT_FLOAT_EQ(values_2[0], 1.0f);
EXPECT_FLOAT_EQ(values_2[1], 2.0f);
Uniform uniform_3{"dummy_value_dim_3", program_->id()};
EXPECT_EQ(uniform_3.location(), 2);
uniform_3.UpdateValue(Eigen::Vector3f{1.0f, 2.0f, 3.0f});
float values_3[3] = {42.0f, 42.0f, 42.0f};
glGetUniformfv(program_->id(), uniform_3.location(), values_3);
EXPECT_FLOAT_EQ(values_3[0], 1.0f);
EXPECT_FLOAT_EQ(values_3[1], 2.0f);
EXPECT_FLOAT_EQ(values_3[2], 3.0f);
Uniform uniform_4{"dummy_value_dim_4", program_->id()};
EXPECT_EQ(uniform_4.location(), 3);
uniform_4.UpdateValue(Eigen::Vector4f{1.0f, 2.0f, 3.0f, 4.0f});
float values_4[4] = {42.0f, 42.0f, 42.0f, 42.0f};
glGetUniformfv(program_->id(), uniform_4.location(), values_4);
EXPECT_FLOAT_EQ(values_4[0], 1.0f);
EXPECT_FLOAT_EQ(values_4[1], 2.0f);
EXPECT_FLOAT_EQ(values_4[2], 3.0f);
EXPECT_FLOAT_EQ(values_4[3], 4.0f);
}

TEST_F(UniformTest, UpdateValueFromPackOfIntegers) {
Uniform uniform_1{"i_dummy_value_dim_1", program_->id()};
EXPECT_EQ(uniform_1.location(), 4);
uniform_1.UpdateValue(1);
Uniform uniform_2{"i_dummy_value_dim_2", program_->id()};
EXPECT_EQ(uniform_2.location(), 5);
uniform_2.UpdateValue(1, 2);
Uniform uniform_3{"i_dummy_value_dim_3", program_->id()};
EXPECT_EQ(uniform_3.location(), 6);
uniform_3.UpdateValue(1, 2, 3);
Uniform uniform_4{"i_dummy_value_dim_4", program_->id()};
EXPECT_EQ(uniform_4.location(), 7);
uniform_4.UpdateValue(1, 2, 3, 4);
}

Expand Down Expand Up @@ -134,5 +157,5 @@ TEST_F(UniformTest, EverythingIsPossibleWithNonExistingUniform) {

TEST(UniformDeathTest, InitWithoutProgram) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
EXPECT_DEBUG_DEATH(Uniform("some_name", 0), ".*GL_INVALID_VALUE.*");
EXPECT_DEBUG_DEATH(Uniform("some_name", 666), ".*GL_INVALID_VALUE.*");
}
1 change: 1 addition & 0 deletions gl/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(glfw)
1 change: 1 addition & 0 deletions gl/ui/core/user_input_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define OPENGL_TUTORIALS_GL_UI_CORE_UX_INPUT_INTERFACE_H_

#include <functional>
#include <vector>
#include <set>

namespace gl {
Expand Down
26 changes: 26 additions & 0 deletions gl/ui/glfw/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
add_library(glfw_viewer
glfw_user_input_handler.cpp
viewer.cpp
)
target_include_directories(glfw_viewer PUBLIC ${PROJECT_SOURCE_DIR})
target_link_libraries(glfw_viewer PUBLIC
glfw
glad_gl_core_33
glog::glog)

# cc_library(
# name = "viewer",
# srcs = [
# "glfw_user_input_handler.cpp",
# "viewer.cpp",
# ],
# hdrs = [
# "glfw_user_input_handler.h",
# "viewer.h"
# ],
# deps = [
# "//gl/core:core",
# "//gl/ui/core:user_input_handler",
# "@glfw//:glfw",
# ],
# )
4 changes: 3 additions & 1 deletion gl/ui/glfw/glfw_user_input_handler.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef OPENGL_TUTORIALS_GL_UI_GLFW_MOUSE_EVENT_HANDLER_H_
#define OPENGL_TUTORIALS_GL_UI_GLFW_MOUSE_EVENT_HANDLER_H_

#include "GLFW/glfw3.h"
#include "gl/ui/core/user_input_handler.h"

#include "glad/gl.h"
#include "GLFW/glfw3.h"
#include "glog/logging.h"

#include <array>
Expand Down
3 changes: 1 addition & 2 deletions gl/ui/glfw/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ bool Viewer::Initialize(const WindowSize& window_size,
nullptr);
if (!window_) { return false; }
glfwMakeContextCurrent(window_);

InitializeGlContext(glfwGetProcAddress);
gladLoadGL(glfwGetProcAddress);

glfwSetFramebufferSizeCallback(window_, Viewer::OnResize);
Resize(window_size);
Expand Down
Loading