Switch to GLAD from GLEW

This commit is contained in:
illegitimate-egg 2024-10-06 18:11:28 +01:00
parent aac84fabcf
commit 3f6af200e0
7 changed files with 96 additions and 101 deletions

3
.gitmodules vendored
View File

@ -22,3 +22,6 @@
[submodule "include/CLog"] [submodule "include/CLog"]
path = include/CLog path = include/CLog
url = https://github.com/williamistGitHub/CLog.git url = https://github.com/williamistGitHub/CLog.git
[submodule "include/glad"]
path = include/glad
url = https://github.com/Dav1dde/glad.git

View File

@ -1,19 +1,18 @@
cmake_minimum_required(VERSION 3.11) cmake_minimum_required(VERSION 3.11)
project(fred) project(fred C CXX)
# if($<CONFIG:Debug>) # if($<CONFIG:Debug>)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # For my Clangd LSP set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # For my Clangd LSP
# endif() # endif()
set(GLFW_BUILD_WAYLAND OFF) # This should be detected and not forced set(GLFW_BUILD_WAYLAND OFF) # This should be detected and not forced
set(BUILD_SHARED_LIBS OFF) # Keep the project as one binary (glew, glm) set(BUILD_SHARED_LIBS OFF) # Keep the project as one binary (glew, glm)
set(GLM__BUILD_TESTS OFF) # Don't build GLM tests set(GLM__BUILD_TESTS OFF) # Don't build GLM tests
set(GLFW_BUILD_EXAMPLES OFF) # Don't build GLFW Examples set(GLFW_BUILD_EXAMPLES OFF) # Don't build GLFW Examples
set(GLFW_BUILD_TESTS OFF) # Don't build GLFW Tests set(GLFW_BUILD_TESTS OFF) # Don't build GLFW Tests
set(GLFW_INSTALL OFF) # We're not building a standalone set(GLFW_INSTALL OFF) # We're not building a standalone
# set(GLEW_BUILD_DOCS $<CONFIG:Debug>) # Build docs if debug # set(GLEW_BUILD_DOCS $<CONFIG:Debug>) # Build docs if debug
set(SOIL2_BUILD_TESTS OFF) # Don't build SOIL2 Tests set(SOIL2_BUILD_TESTS OFF) # Don't build SOIL2 Tests
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
@ -21,48 +20,12 @@ if(CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "You fucking smell fr\n") message(FATAL_ERROR "You fucking smell fr\n")
endif() endif()
if(CMAKE_SOURCE_DIR MATCHES " ") if(CMAKE_SOURCE_DIR MATCHES " ")
message(WARNING "Spaces in the source dir can cause errors, thou art been warned\n") message(
WARNING "Spaces in the source dir can cause errors, thou art been warned\n")
endif() endif()
if(CMAKE_BINARY_DIR MATCHES " ") if(CMAKE_BINARY_DIR MATCHES " ")
message(WARNING "Spaces in the build dir can cause errors, thou art been warned\n") message(
endif() WARNING "Spaces in the build dir can cause errors, thou art been warned\n")
if(UNIX)
message(STATUS "Configuring GLEW for *nix (Non-cmake)\n")
add_custom_command(
OUTPUT ${PROJECT_SOURCE_DIR}/include/glew/src/glew.c
${PROJECT_SOURCE_DIR}/include/glewinfo.c
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/include/glew/auto
COMMAND ${CMAKE_MAKE_PROGRAM})
add_subdirectory(include/glew/build/cmake)
if(NOT (EXISTS "${PROJECT_SOURCE_DIR}/include/glew/src/glew.c" AND EXISTS "${PROJECT_SOURCE_DIR}/include/glewinfo.c"))
message(FATAL_ERROR "GLEW failed to configure!")
endif()
endif()
if(WIN32)
message(STATUS "Download GLEW for Windows (Non-cmake)\n") # I wanted to make this work from source, but the auto config is make only, and requiring and/or shipping msys2 would be unreasonable so this is the best compromise
file(DOWNLOAD https://github.com/nigels-com/glew/releases/download/glew-2.2.0/glew-2.2.0-win32.zip ${CMAKE_BINARY_DIR}/glew-2.2.0.zip)
execute_process(COMMAND tar -xf ${CMAKE_BINARY_DIR}/glew-2.2.0.zip # bdstar
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
if(NOT EXISTS ${CMAKE_BINARY_DIR}/glew-2.2.0/include/GL/glew.h)
message(FATAL_ERROR "GLEW Failed to download/decompress")
endif()
include_directories(${CMAKE_BINARY_DIR}/glew-2.2.0/include)
add_library(glew STATIC # There aren't actually any binaries in here, so no lib is to be built
${CMAKE_BINARY_DIR}/glew-2.2.0/include/GL/eglew.h
${CMAKE_BINARY_DIR}/glew-2.2.0/include/GL/glew.h
${CMAKE_BINARY_DIR}/glew-2.2.0/include/GL/glxew.h
${CMAKE_BINARY_DIR}/glew-2.2.0/include/GL/wglew.h)
set_target_properties(glew PROPERTIES LINKER_LANGUAGE C)
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
target_link_libraries(glew ${CMAKE_BINARY_DIR}/glew-2.2.0/lib/Release/x64/glew32.lib ${CMAKE_BINARY_DIR}/glew-2.2.0/lib/Release/x64/glew32s.lib)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32-bit, if it's not either of these you're in trouble
target_link_libraries(glew ${CMAKE_BINARY_DIR}/glew-2.2.0/lib/Release/Win32/glew32.lib ${CMAKE_BINARY_DIR}/glew-2.2.0/lib/Release/Win32/glew32s.lib)
endif()
endif() endif()
add_subdirectory(include/glm) add_subdirectory(include/glm)
@ -71,7 +34,14 @@ add_subdirectory(include/SOIL2)
add_subdirectory(include/assimp) add_subdirectory(include/assimp)
add_subdirectory(include/CLog) add_subdirectory(include/CLog)
include_directories(include/imgui) # Glad is configured using cmake (unlike glew)
# https://github.com/Dav1dde/glad/wiki/C#cmake
set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/include/glad")
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
glad_add_library(glad_gl_core_33 REPRODUCIBLE API gl:core=3.3)
include_directories(include/imgui) # ImGui doesn't have a CMakeLists of its own
add_library( add_library(
imgui STATIC imgui STATIC
include/imgui/imgui.cpp include/imgui/imgui.cpp
@ -84,26 +54,26 @@ add_library(
target_link_libraries(imgui glfw) target_link_libraries(imgui glfw)
add_executable(fred engine.cpp shader.c) add_executable(fred engine.cpp shader.c)
if (UNIX) if(UNIX)
target_link_libraries( target_link_libraries(
fred fred
-lm -lm
glew glad_gl_core_33
glm glm
glfw glfw
soil2 soil2
assimp assimp
imgui imgui
clog) clog)
endif() endif()
if (WIN32) if(WIN32)
target_link_libraries( target_link_libraries(
fred fred
glew glad_gl_core_33
glm glm
glfw glfw
soil2 soil2
assimp assimp
imgui imgui
clog) clog)
endif() endif()

View File

@ -3,15 +3,15 @@
#include <string.h> #include <string.h>
#include <vector> #include <vector>
#include <GL/glew.h> #include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/trigonometric.hpp> #include <glm/trigonometric.hpp>
#include <clog/clog.h>
#include <backends/imgui_impl_glfw.h> #include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
#include <clog/clog.h>
#include <imgui.h> #include <imgui.h>
#include <SOIL2.h> #include <SOIL2.h>
@ -31,8 +31,9 @@ static void glfwErrorCallback(int e, const char *description) {
} }
static bool loadModel(const char *path, std::vector<unsigned short> &indices, static bool loadModel(const char *path, std::vector<unsigned short> &indices,
std::vector<glm::vec3> &vertices, std::vector<glm::vec2> &uvs, std::vector<glm::vec3> &vertices,
std::vector<glm::vec3> &normals) { std::vector<glm::vec2> &uvs,
std::vector<glm::vec3> &normals) {
Assimp::Importer importer; Assimp::Importer importer;
const aiScene *scene = importer.ReadFile( const aiScene *scene = importer.ReadFile(
@ -69,7 +70,6 @@ static bool loadModel(const char *path, std::vector<unsigned short> &indices,
static int initWindow() { static int initWindow() {
glfwSetErrorCallback(glfwErrorCallback); glfwSetErrorCallback(glfwErrorCallback);
glewExperimental = true;
if (!glfwInit()) { if (!glfwInit()) {
clog_log(CLOG_LEVEL_ERROR, "GLFW went shitty time (failed to init)"); clog_log(CLOG_LEVEL_ERROR, "GLFW went shitty time (failed to init)");
return 1; return 1;
@ -90,10 +90,14 @@ static int initWindow() {
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSwapInterval(1); glfwSwapInterval(1); // V-Sync
int code;
if ((code = glewInit()) != GLEW_OK) { int version;
clog_log(CLOG_LEVEL_ERROR, "Failed to init GLEW: %s", glewGetErrorString(code)); if ((version = gladLoadGL(glfwGetProcAddress))) {
clog_log(CLOG_LEVEL_DEBUG, "GL version: %d.%d", GLAD_VERSION_MAJOR(version),
GLAD_VERSION_MINOR(version));
} else {
clog_log(CLOG_LEVEL_ERROR, "Failed to init GL!");
return 1; return 1;
} }
@ -291,7 +295,7 @@ static int initWindow() {
// Disabled because unchanged // Disabled because unchanged
// glm_perspective(glm_rad(FOV), 4.0f/ 3.0f, 0.1f, 100.0f, projection); // glm_perspective(glm_rad(FOV), 4.0f/ 3.0f, 0.1f, 100.0f, projection);
glm::mat4 view = glm::lookAt(position, position + direction, up); glm::mat4 view = glm::lookAt(position, position + direction, up);
mvp = projection * view * model; // mvp = projection * view * model;
// Send mega sigma MVP to the vertex shader (transformations for the win) // Send mega sigma MVP to the vertex shader (transformations for the win)
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mvp[0][0]); glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mvp[0][0]);
@ -315,7 +319,8 @@ static int initWindow() {
// Index buffer // Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glDrawElements(GL_TRIANGLES, (GLsizei)indices.size(), GL_UNSIGNED_SHORT, (void *)0); glDrawElements(GL_TRIANGLES, (GLsizei)indices.size(), GL_UNSIGNED_SHORT,
(void *)0);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1); glDisableVertexAttribArray(1);

1
include/glad Submodule

@ -0,0 +1 @@
Subproject commit 73db193f853e2ee079bf3ca8a64aa2eaf6459043

@ -1 +0,0 @@
Subproject commit b323ebf9adeae6a3f26f91277d4f62df509037fc

View File

@ -1,18 +1,31 @@
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <GL/glew.h> #include <clog/clog.h>
#include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#ifdef __linux__ #ifdef __linux__
#include <unistd.h> #include <unistd.h>
typedef int errno_t;
errno_t fopen_s(FILE **f, const char *name,
const char *mode) {
errno_t ret = 0;
clog_assert(f);
*f = fopen(name, mode);
/* Can't be sure about 1-to-1 mapping of errno and MS' errno_t */
if (!*f)
ret = errno;
return ret;
} // God is here: https://stackoverflow.com/questions/1513209/is-there-a-way-to-use-fopen-s-with-gcc-or-at-least-create-a-define-about-it
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#include <io.h> #include <io.h>
#define lseek _lseek #define lseek _lseek
#endif #endif
#include <clog/clog.h>
GLuint loadShaders(const char *vertex_file_path, GLuint loadShaders(const char *vertex_file_path,
const char *fragment_file_path) { const char *fragment_file_path) {
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
@ -21,28 +34,27 @@ GLuint loadShaders(const char *vertex_file_path,
errno_t err; errno_t err;
FILE *vertexShaderFD; FILE *vertexShaderFD;
if (err = fopen_s(&vertexShaderFD, vertex_file_path, "rb")) { if ((err = fopen_s(&vertexShaderFD, vertex_file_path, "rb"))) {
clog_log(CLOG_LEVEL_ERROR, "Failed to open Vertex Shader \"%s\": %s", vertex_file_path, err); clog_log(CLOG_LEVEL_ERROR, "Failed to open Vertex Shader \"%s\": %d",
vertex_file_path, err);
return 0; return 0;
} }
int vertexShaderLength = lseek(fileno(vertexShaderFD), 0L, SEEK_END) + 1; int vertexShaderLength = lseek(fileno(vertexShaderFD), 0L, SEEK_END) + 1;
fseek(vertexShaderFD, 0L, SEEK_SET); fseek(vertexShaderFD, 0L, SEEK_SET);
char *vertexShaderCode = (char *)calloc(vertexShaderLength, sizeof(char)); char *vertexShaderCode = (char *)calloc(vertexShaderLength, sizeof(char));
clog_assert(vertexShaderCode != NULL);
clog_assert(7 != 7);
fread(vertexShaderCode, sizeof(*vertexShaderCode), vertexShaderLength, fread(vertexShaderCode, sizeof(*vertexShaderCode), vertexShaderLength,
vertexShaderFD); vertexShaderFD);
fclose(vertexShaderFD); fclose(vertexShaderFD);
FILE *fragmentShaderFD; FILE *fragmentShaderFD;
if (fopen_s(&fragmentShaderFD, fragment_file_path, "rb")) { if ((err = fopen_s(&fragmentShaderFD, fragment_file_path, "rb"))) {
clog_log(CLOG_LEVEL_ERROR, "Failed to open Vertex Shader \"%s\": %s", fragment_file_path, err); clog_log(CLOG_LEVEL_ERROR, "Failed to open Vertex Shader \"%s\": %d",
return 0; fragment_file_path, err);
return 0;
} }
int fragmentShaderLength = lseek(fileno(fragmentShaderFD), 0L, SEEK_END) + 1; int fragmentShaderLength = lseek(fileno(fragmentShaderFD), 0L, SEEK_END) + 1;
fseek(fragmentShaderFD, 0L, SEEK_SET); fseek(fragmentShaderFD, 0L, SEEK_SET);
char *fragmentShaderCode = (char *)calloc(fragmentShaderLength, sizeof(char)); char *fragmentShaderCode = (char *)calloc(fragmentShaderLength, sizeof(char));
clog_assert(fragmentShaderCode != NULL);
fread(fragmentShaderCode, sizeof(*fragmentShaderCode), fragmentShaderLength, fread(fragmentShaderCode, sizeof(*fragmentShaderCode), fragmentShaderLength,
fragmentShaderFD); fragmentShaderFD);
fclose(fragmentShaderFD); fclose(fragmentShaderFD);
@ -59,8 +71,10 @@ GLuint loadShaders(const char *vertex_file_path,
glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength); glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
if (infoLogLength > 0) { if (infoLogLength > 0) {
infoLogLength += 1; // Prevents a sub-expression overflow false positive infoLogLength += 1; // Prevents a sub-expression overflow false positive
char *vertexShaderErrorMessage = (char*)malloc(infoLogLength * sizeof(char)); // Not all compilers support VLAs, this will do char *vertexShaderErrorMessage = (char *)malloc(
glGetShaderInfoLog(vertexShaderID, infoLogLength-1, NULL, infoLogLength *
sizeof(char)); // Not all compilers support VLAs, this will do
glGetShaderInfoLog(vertexShaderID, infoLogLength - 1, NULL,
vertexShaderErrorMessage); vertexShaderErrorMessage);
clog_log(CLOG_LEVEL_ERROR, "%s", vertexShaderErrorMessage); clog_log(CLOG_LEVEL_ERROR, "%s", vertexShaderErrorMessage);
free(vertexShaderErrorMessage); free(vertexShaderErrorMessage);
@ -75,7 +89,8 @@ GLuint loadShaders(const char *vertex_file_path,
glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength); glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
if (infoLogLength > 0) { if (infoLogLength > 0) {
infoLogLength += 1; // Prevents a sub-expression overflow false positive infoLogLength += 1; // Prevents a sub-expression overflow false positive
char *fragmentShaderErrorMessage = (char*)malloc(infoLogLength * sizeof(char)); char *fragmentShaderErrorMessage =
(char *)malloc(infoLogLength * sizeof(char));
glGetShaderInfoLog(fragmentShaderID, infoLogLength - 1, NULL, glGetShaderInfoLog(fragmentShaderID, infoLogLength - 1, NULL,
fragmentShaderErrorMessage); fragmentShaderErrorMessage);
clog_log(CLOG_LEVEL_ERROR, "%s", fragmentShaderErrorMessage); clog_log(CLOG_LEVEL_ERROR, "%s", fragmentShaderErrorMessage);
@ -92,8 +107,10 @@ GLuint loadShaders(const char *vertex_file_path,
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength); glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength);
if (infoLogLength > 0) { if (infoLogLength > 0) {
infoLogLength += 1; // Prevents a sub-expression overflow false positive infoLogLength += 1; // Prevents a sub-expression overflow false positive
char *programErrorMessage = (char*)malloc(infoLogLength * sizeof(char));; char *programErrorMessage = (char *)malloc(infoLogLength * sizeof(char));
glGetProgramInfoLog(programID, infoLogLength - 1, NULL, programErrorMessage); ;
glGetProgramInfoLog(programID, infoLogLength - 1, NULL,
programErrorMessage);
clog_log(CLOG_LEVEL_ERROR, "%s", programErrorMessage); clog_log(CLOG_LEVEL_ERROR, "%s", programErrorMessage);
free(programErrorMessage); free(programErrorMessage);
} }

View File

@ -1,7 +1,7 @@
#ifndef LOAD_SHADERS_H #ifndef LOAD_SHADERS_H
#define LOAD_SHADERS_H #define LOAD_SHADERS_H
#include <GL/glew.h> #include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#ifdef __cplusplus #ifdef __cplusplus