Skip to content

Commit

Permalink
fix compiler issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lindemeier committed Jun 11, 2024
1 parent ed72593 commit d771ce3
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 38 deletions.
40 changes: 19 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ project(prgl)

find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package (Threads REQUIRED)
find_package(Threads REQUIRED)

# glew
if (UNIX)
if(UNIX)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLEW REQUIRED glew)
else ()
else()
file(TO_CMAKE_PATH "$ENV{GLEW_HOME}" GLEW_HOME)
set(GLEW_INCLUDE_DIR "${GLEW_HOME}/include")
find_library(GLEW_LIBRARY NAMES GLEW glew32 glew glew32s PATHS "${GLEW_HOME}/lib" NO_DEFAULT_PATH)
set(GLEW_LIBRARIES ${GLEW_LIBRARY})
endif (UNIX)
endif(UNIX)

add_library(${PROJECT_NAME} STATIC
src/ContextImplementation.cxx
Expand All @@ -28,24 +28,23 @@ add_library(${PROJECT_NAME} STATIC
src/GlslComputeShader.cxx
src/VertexBufferObject.cxx
src/VertexArrayObject.cxx

)

set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON)
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "d")
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# using Clang
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Weverything -Wno-c++98-compat -Wno-padded -Wno-documentation -Werror -Wno-global-constructors -Wno-exit-time-destructors)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using GCC
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Werror)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# using Visual Studio C++
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
endif()


target_include_directories(${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/>
PUBLIC $<INSTALL_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/>
Expand All @@ -54,16 +53,14 @@ target_include_directories(${PROJECT_NAME}
PUBLIC ${GLEW_INCLUDE_DIRS}
)


target_link_libraries(${PROJECT_NAME}
OpenGL::GL
glfw
${GLEW_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)


## testing
# # testing

# executable
add_executable(${PROJECT_NAME}_test
Expand All @@ -79,14 +76,15 @@ set_property(TARGET ${PROJECT_NAME}_test PROPERTY CXX_STANDARD_REQUIRED ON)

include(FetchContent)
option(RUN_TESTS "Build and run the tests" ON)

if(RUN_TESTS)
# get google test
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_subdirectory(test)
# get google test
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_subdirectory(test)
endif()
2 changes: 2 additions & 0 deletions prgl/FrameBufferObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class FrameBufferObject final {
const std::shared_ptr<Texture2d>& getTarget() const;
const std::shared_ptr<Texture2d>& getDepth() const;

void clear(const std::array<float, 4UL>& clearColor, double clearDepth);

private:
FrameBufferObject(const FrameBufferObject&) = delete;
FrameBufferObject& operator=(const FrameBufferObject&) = delete;
Expand Down
7 changes: 6 additions & 1 deletion prgl/VertexArrayObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ class VertexArrayObject final {
const std::shared_ptr<VertexBufferObject>& vbo);

void render(const DrawMode& mode, uint32_t first, uint32_t count);
void renderAll(const DrawMode& mode);

private:
VertexArrayObject(const VertexArrayObject&) = delete;
VertexArrayObject(const VertexArrayObject&) = delete;
VertexArrayObject& operator=(const VertexArrayObject&) = delete;

protected:
uint32_t mVao;

std::size_t mIndicesCount;

std::map<uint32_t, std::shared_ptr<VertexBufferObject>> mVboMap;
};

} // namespace prgl

#endif // PRGL_VERTEX_ARRAY_OBJECT_H
4 changes: 3 additions & 1 deletion prgl/VertexBufferObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ class VertexBufferObject final {

DataType getVertexComponentDataType() const;
uint32_t getVertexComponentDataColumns() const;
size_t getVerticesCount() const;
std::size_t getVerticesCount() const;

std::size_t getSize() const;

private:
VertexBufferObject(const VertexBufferObject&) = delete;
Expand Down
8 changes: 8 additions & 0 deletions src/ContextImplementation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void checkGLError(const char* file, const char* function, int line) {
error = "GL_INVALID_FRAMEBUFFER_OPERATION";
break;
// case GL_CONTEXT_LOST: error = "GL_CONTEXT_LOST"; break;
default:
break;
}

std::cerr << "ERROR::" << error.c_str() << "\n\tfile:\t" << file
Expand Down Expand Up @@ -82,6 +84,9 @@ static void APIENTRY openGlDebugCallback(GLenum /*unused*/, GLenum type,
case GL_DEBUG_TYPE_OTHER:
std::cerr << "OTHER";
break;
default:
std::cerr << "UNKNOWN";
break;
}
std::cerr << std::endl;

Expand All @@ -97,6 +102,9 @@ static void APIENTRY openGlDebugCallback(GLenum /*unused*/, GLenum type,
case GL_DEBUG_SEVERITY_HIGH:
std::cerr << "HIGH";
break;
default:
std::cerr << "UNKNOWN";
break;
}
std::cerr << std::endl;
std::cerr << "---------------------opengl-callback-end--------------"
Expand Down
42 changes: 34 additions & 8 deletions src/FrameBufferObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,77 @@ const std::shared_ptr<Texture2d>& FrameBufferObject::getDepth() const {
return mDepth;
}

void FrameBufferObject::clear(const std::array<float, 4UL>& clearColor,
double clearDepth) {
if (mTarget != nullptr) {
mTarget->bind(true);
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
glClear(GL_COLOR_BUFFER_BIT);
mTarget->bind(false);
}

if (mDepth != nullptr) {
mDepth->bind(true);
glClearDepth(clearDepth);
glClear(GL_DEPTH_BUFFER_BIT);
mDepth->bind(false);
}
}

bool FrameBufferObject::checkStatus() const {
bind(true);

// glReadBuffer(GL_NONE);

const auto status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
bool result = false;
bool result = false;

switch (status) {
case GL_FRAMEBUFFER_COMPLETE_EXT:
// std::cerr << "FRAMEBUFFER::Complete" << std::endl;
result = true;
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Attachment is NOT complete"
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: "
"Attachment is NOT complete"
<< std::endl;
result = false;
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: No image is attached to FBO"
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: No "
"image is attached to FBO"
<< std::endl;
result = false;
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Attached images have "
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: "
"Attached images have "
"different dimensions"
<< std::endl;
result = false;
break;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
std::cout << "[ERROR]FRAMEBUFFER::incomplete: Color attached images have "
std::cout << "[ERROR]FRAMEBUFFER::incomplete: "
"Color attached images have "
"different internal formats"
<< std::endl;
result = false;
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Draw buffer" << std::endl;
std::cerr << "[ERROR]FRAMEBUFFER::"
"incomplete: Draw buffer"
<< std::endl;
result = false;
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
std::cerr << "[ERROR]FRAMEBUFFER::incomplete: Read buffer" << std::endl;
std::cerr << "[ERROR]FRAMEBUFFER::"
"incomplete: Read buffer"
<< std::endl;
result = false;
break;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
std::cerr << "[ERROR]FRAMEBUFFER::Unsupported by FBO implementation"
std::cerr << "[ERROR]FRAMEBUFFER::Unsupported by "
"FBO implementation"
<< std::endl;
result = false;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Texture2d.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ void Texture2d::render(float posX, float posY, float width, float height,

glDisable(GL_DEPTH_TEST);

int32_t iViewport[4];
glGetIntegerv(GL_VIEWPORT, iViewport);
std::array<int32_t, 4> iViewport;
glGetIntegerv(GL_VIEWPORT, iViewport.data());

glMatrixMode(GL_PROJECTION);
glPushMatrix();
Expand Down
37 changes: 32 additions & 5 deletions src/VertexArrayObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ std::shared_ptr<VertexArrayObject> VertexArrayObject::Create() {
return std::make_shared<VertexArrayObject>();
}

VertexArrayObject::VertexArrayObject() : mVao(INVALID_HANDLE) {
VertexArrayObject::VertexArrayObject()
: mVao(INVALID_HANDLE),
mIndicesCount(0) {
glGenVertexArrays(1, &mVao);
}

VertexArrayObject::~VertexArrayObject() {
glDeleteVertexArrays(1, &mVao);
mVao = INVALID_HANDLE;
if (mVao != INVALID_HANDLE) {
glDeleteVertexArrays(1, &mVao);
mVao = INVALID_HANDLE;
}
}

/**
Expand All @@ -50,8 +54,31 @@ void VertexArrayObject::bind(bool bind) const {
*/
void VertexArrayObject::render(const DrawMode& mode, const uint32_t first,
const uint32_t count) {
glDrawArrays(static_cast<GLenum>(mode), static_cast<GLint>(first),
static_cast<GLint>(count));
glDrawArrays(static_cast<GLenum>(mode), static_cast<GLint>(first),
static_cast<GLint>(count));
}

/**
* @brief
* https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml
*
* @param mode Specifies what kind of primitives to render.
* @param first Specifies the starting index in the enabled arrays.
* @param count Specifies the number of indices to be rendered.
*/
void VertexArrayObject::renderAll(const DrawMode& mode) {
auto numEl = 1U;
if (mode == DrawMode::Triangles) {
numEl = 3U;
} else if (mode == DrawMode::Points) {
numEl = 1U;
} else {
throw std::runtime_error("not implemented");
}

glDrawArrays(
static_cast<GLenum>(mode), static_cast<GLint>(0),
static_cast<GLint>(mVboMap.begin()->second->getVerticesCount() / numEl));
}

void VertexArrayObject::addVertexBufferObject(
Expand Down
22 changes: 22 additions & 0 deletions src/VertexBufferObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ size_t VertexBufferObject::getVerticesCount() const {
return mVerticesCount;
}

std::size_t VertexBufferObject::getSize() const {
auto depth = 1U;
if (mDataType == DataType::Float) {
depth = sizeof(float);
} else if (mDataType == DataType::Double) {
depth = sizeof(double);
} else if (mDataType == DataType::Int) {
depth = sizeof(int32_t);
} else if (mDataType == DataType::UnsignedInt) {
depth = sizeof(uint32_t);
} else if (mDataType == DataType::Short) {
depth = sizeof(int16_t);
} else if (mDataType == DataType::UnsignedShort) {
depth = sizeof(uint16_t);
} else if (mDataType == DataType::Byte) {
depth = sizeof(int8_t);
} else if (mDataType == DataType::UnsignedByte) {
depth = sizeof(uint8_t);
}
return (mVerticesCount * depth) * mDataColumns;
}

} // namespace prgl

0 comments on commit d771ce3

Please sign in to comment.