Remove usage of SOIL2, just use stb_image.h

This commit is contained in:
SeanOMik 2022-09-17 19:41:38 -04:00
parent 2a72a30aa2
commit ec1d6aebe7
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
6 changed files with 7943 additions and 41 deletions

View File

@ -3,7 +3,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
project(SimpleEngine) project(SimpleEngine)
include(cmrc/CMakeRC.cmake) include(cmrc/CMakeRC.cmake)
include(CMake/Findsoil2.cmake) #include(CMake/Findsoil2.cmake)
# Add some CMake options: # Add some CMake options:
option(SIMPLE_ENGINE_BUILD_EXAMPLES "Build example projects" ON) option(SIMPLE_ENGINE_BUILD_EXAMPLES "Build example projects" ON)
@ -20,14 +20,29 @@ find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED) find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED) find_package(glm CONFIG REQUIRED)
if (WIN32) #if (WIN32)
find_package(soil2 CONFIG REQUIRED) # find_package(soil2 CONFIG REQUIRED)
else() #else()
find_package(soil2 REQUIRED) # find_package(soil2 REQUIRED)
endif() #endif()
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
#include(FetchContent)
#FetchContent_Declare(
# stbImage
# URL https://raw.githubusercontent.com/nothings/stb/5ba0baaa269b3fd681828e0e3b3ac0f1472eaf40/stb_image.h
# DOWNLOAD_NO_EXTRACT
#)
#FetchContent_MakeAvailable(stbImage)
#include(ExternalProject)
#ExternalProject_Add(stbImage
# URL https://raw.githubusercontent.com/nothings/stb/5ba0baaa269b3fd681828e0e3b3ac0f1472eaf40/stb_image.h
# URL_HASH MD5=d41d8cd98f00b204e9800998ecf8427e
# DOWNLOAD_NO_EXTRACT ON
#)
# Link sources # Link sources
file(GLOB_RECURSE source_list src/*.cpp) file(GLOB_RECURSE source_list src/*.cpp)
add_library(simpleengine STATIC ${source_list}) add_library(simpleengine STATIC ${source_list})
@ -49,7 +64,7 @@ cmrc_add_resource_library(
target_link_libraries(simpleengine PUBLIC GLEW::GLEW) target_link_libraries(simpleengine PUBLIC GLEW::GLEW)
target_link_libraries(simpleengine PUBLIC glfw) target_link_libraries(simpleengine PUBLIC glfw)
target_link_libraries(simpleengine PUBLIC ${GLM_LIBRARIES}) target_link_libraries(simpleengine PUBLIC ${GLM_LIBRARIES})
target_link_libraries(simpleengine PUBLIC soil2) #target_link_libraries(simpleengine PUBLIC soil2)
target_link_libraries(simpleengine PUBLIC ${OPENGL_LIBRARIES}) target_link_libraries(simpleengine PUBLIC ${OPENGL_LIBRARIES})
target_link_libraries(simpleengine PRIVATE simpleengine_resources) target_link_libraries(simpleengine PRIVATE simpleengine_resources)

View File

@ -1,3 +1,5 @@
*.blend *.blend
*.obj *.obj
*.png *.png
*.bin
*.gltf

View File

@ -31,12 +31,6 @@
#include <cmrc/cmrc.hpp> #include <cmrc/cmrc.hpp>
CMRC_DECLARE(resource_shaders); CMRC_DECLARE(resource_shaders);
#ifdef __linux__
#include <SOIL2.h>
#else
#include <SOIL2/SOIL2.h>
#endif
namespace se = simpleengine; namespace se = simpleengine;
class FPSCounterEvent : public se::Event { class FPSCounterEvent : public se::Event {
@ -179,7 +173,7 @@ int main(int argc, char *argv[]) {
auto entity = std::make_shared<simpleengine::Entity>(); auto entity = std::make_shared<simpleengine::Entity>();
se::gfx::Model model(material, "examples/dev_testing/resources/dragon.obj"); se::gfx::Model model(material, "examples/dev_testing/resources/dragon.obj");
entity->add_component<se::ModelComponent>(model); entity->add_component<se::ModelComponent>(model);
entity->translate(5.f, 0.f, 0.f); entity->translate(12.f, -4.f, 0.f);
// Create a renderer and submit the entity into it. // Create a renderer and submit the entity into it.
auto renderer = std::make_shared<se::gfx::Renderer>(game.get_window(), core_shader); auto renderer = std::make_shared<se::gfx::Renderer>(game.get_window(), core_shader);

View File

@ -12,12 +12,6 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#ifdef __linux__
#include <SOIL2.h>
#else
#include <SOIL2/SOIL2.h>
#endif
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
@ -75,6 +69,7 @@ namespace simpleengine::gfx {
Texture(std::vector<unsigned char> buffer, bool img_2d = true, bool mipmap = true); Texture(std::vector<unsigned char> buffer, bool img_2d = true, bool mipmap = true);
void bind() const; void bind() const;
void unbind() const;
unsigned int get_texture_id() const; unsigned int get_texture_id() const;
}; };

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,8 @@
#include "gfx/texture.h" #include "gfx/texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
namespace simpleengine::gfx { namespace simpleengine::gfx {
Texture::Texture(const char* path, bool img_2d, bool mipmap) { Texture::Texture(const char* path, bool img_2d, bool mipmap) {
image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D; image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D;
@ -12,17 +15,14 @@ namespace simpleengine::gfx {
glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img_data = SOIL_load_image // Read 4 channels (RGBA)
( img_data = stbi_load(path, &width, &height, &channels, 4);
path, if(!img_data) {
&width, &height, &channels, const char* failure = stbi_failure_reason();
SOIL_LOAD_RGBA std::cerr << "Failed to load texture! (" << failure << ")" << std::endl;
);
if (!img_data) {
std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl;
throw std::runtime_error("Failed to load texture from memory!"); throw std::runtime_error("Failed to load texture from memory!");
} }
std::cout << "Loaded image with a width of " << width << "px, a height of " << height << "px and " << channels << " channels" << std::endl;
glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data); glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data);
@ -30,7 +30,6 @@ namespace simpleengine::gfx {
glGenerateMipmap(image_type); glGenerateMipmap(image_type);
} }
SOIL_free_image_data(img_data);
glBindTexture(image_type, 0); glBindTexture(image_type, 0);
} }
@ -53,17 +52,14 @@ namespace simpleengine::gfx {
glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img_data = SOIL_load_image_from_memory // Read 4 channels (RGBA)
( img_data = stbi_load_from_memory(buffer, buffer_length, &width, &height, &channels, 4);
buffer, buffer_length, if(!img_data) {
&width, &height, &channels, const char* failure = stbi_failure_reason();
SOIL_LOAD_RGBA std::cerr << "Failed to load texture! (" << failure << ")" << std::endl;
);
if (!img_data) {
std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl;
throw std::runtime_error("Failed to load texture from memory!"); throw std::runtime_error("Failed to load texture from memory!");
} }
std::cout << "Loaded image with a width of " << width << "px, a height of " << height << "px and " << channels << " channels" << std::endl;
glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data); glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data);
@ -71,7 +67,6 @@ namespace simpleengine::gfx {
glGenerateMipmap(image_type); glGenerateMipmap(image_type);
} }
SOIL_free_image_data(img_data);
glBindTexture(image_type, 0); glBindTexture(image_type, 0);
} }
@ -91,6 +86,10 @@ namespace simpleengine::gfx {
glBindTexture(image_type, texture_id); glBindTexture(image_type, texture_id);
} }
void Texture::unbind() const {
glBindTexture(image_type, 0);
}
unsigned int Texture::get_texture_id() const { unsigned int Texture::get_texture_id() const {
return texture_id; return texture_id;
} }