Merge branch 'feature/texture-import' into 'main'

Start load texture logic

See merge request jack/terravisor!1
This commit is contained in:
Jack Christensen 2024-08-25 20:35:09 +00:00
commit 0097ab2641
16 changed files with 1290 additions and 34 deletions

11
.gitignore vendored
View File

@ -26,6 +26,12 @@ rules.ninja
*.log *.log
*.pch *.pch
# Exclude FreeImage binaries from being ignored
!dependencies/FreeImage/lib/UNIX/*.a
!dependencies/FreeImage/lib/UNIX/*.so
!dependencies/FreeImage/lib/WIN32/*.lib
!dependencies/FreeImage/bin/WIN32/*.dll
# Visual Studio Code settings # Visual Studio Code settings
.vscode/ .vscode/
@ -51,7 +57,4 @@ Thumbs.db
# Output directories # Output directories
/output/ /output/
/dist/ /dist/
# Libraries and dependencies
/lib/

View File

@ -14,7 +14,7 @@ build:
# Set the VCPKG_TOOLCHAIN environment variable # Set the VCPKG_TOOLCHAIN environment variable
- export VCPKG_TOOLCHAIN=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake - export VCPKG_TOOLCHAIN=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
- mkdir -p build - mkdir -p build
- cmake -B build -S . -G Ninja --preset release - cmake -B build -S . -G Ninja --preset debug
- cmake --build build - cmake --build build
artifacts: artifacts:
paths: paths:

View File

@ -10,15 +10,6 @@ find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED) find_package(glm CONFIG REQUIRED)
find_package(assimp CONFIG REQUIRED) find_package(assimp CONFIG REQUIRED)
# Direct linking to FreeImage libraries for now
if (WIN32)
set(FreeImage_INCLUDE_DIR $ENV{VCPKG_ROOT}/packages/freeimage_x64-windows/include)
set(FreeImage_LIBRARIES $ENV{VCPKG_ROOT}/packages/freeimage_x64-windows/lib/FreeImage.lib)
elseif (UNIX)
set(FreeImage_INCLUDE_DIR $ENV{VCPKG_ROOT}/packages/freeimage_x64-linux/include)
set(FreeImage_LIBRARIES $ENV{VCPKG_ROOT}/packages/freeimage_x64-linux/lib/libFreeImage.a)
endif()
# Set Debug and Release directories # Set Debug and Release directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}")
@ -32,20 +23,44 @@ file(GLOB IMGUI_SOURCES
"${CMAKE_SOURCE_DIR}/imgui/backends/imgui_impl_glfw.cpp" "${CMAKE_SOURCE_DIR}/imgui/backends/imgui_impl_glfw.cpp"
"${CMAKE_SOURCE_DIR}/imgui/backends/imgui_impl_opengl3.cpp" "${CMAKE_SOURCE_DIR}/imgui/backends/imgui_impl_opengl3.cpp"
) )
file(GLOB FREEIMAGE_HEADER "${CMAKE_SOURCE_DIR}/dependencies/FreeImage/include/*.h")
# Add the executable # Add the executable
add_executable(TerraVisor ${SOURCES} ${HEADERS} ${IMGUI_SOURCES}) add_executable(TerraVisor ${SOURCES} ${HEADERS} ${IMGUI_SOURCES} ${FREEIMAGE_HEADER})
# Include directories (handled by find_package now) # Include directories (handled by find_package now)
include_directories( include_directories(
${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/imgui ${CMAKE_SOURCE_DIR}/imgui
${CMAKE_SOURCE_DIR}/imgui/backends ${CMAKE_SOURCE_DIR}/imgui/backends
${FreeImage_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/dependencies/FreeImage/include
) )
link_directories(${CMAKE_SOURCE_DIR}/dependencies/FreeImage/lib)
# Link libraries using vcpkg # Link libraries using vcpkg
target_link_libraries(TerraVisor PRIVATE GLEW::GLEW glfw glm::glm assimp::assimp ${FreeImage_LIBRARIES}) if (UNIX)
target_link_libraries(TerraVisor PRIVATE
GLEW::GLEW
glfw
glm::glm
assimp::assimp
${CMAKE_SOURCE_DIR}/dependencies/FreeImage/lib/UNIX/libfreeimage.a
/vcpkg/installed/x64-linux/debug/lib/libtiffd.a
/vcpkg/installed/x64-linux/debug/lib/libjpeg.a
/vcpkg/installed/x64-linux/debug/lib/libz.a
/vcpkg/installed/x64-linux/debug/lib/liblzma.a
/usr/lib/x86_64-linux-gnu/libGL.so
/usr/lib/x86_64-linux-gnu/libGLU.so
/usr/lib/x86_64-linux-gnu/libX11.so
/usr/lib/x86_64-linux-gnu/libXext.so
-lm
-ldl
-lrt
)
else()
target_link_libraries(TerraVisor PRIVATE GLEW::GLEW glfw glm::glm assimp::assimp ${CMAKE_SOURCE_DIR}/dependencies/FreeImage/lib/WIN32/FreeImage.lib)
endif()
# Custom targets # Custom targets
add_custom_target(copy_shaders ALL add_custom_target(copy_shaders ALL
@ -56,6 +71,15 @@ add_custom_target(copy_hgt ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/hgt ${CMAKE_BINARY_DIR}/hgt ${CMAKE_SOURCE_DIR}/hgt ${CMAKE_BINARY_DIR}/hgt
) )
# Set paths for FreeImage DLL
set(FREEIMAGE_DLL_PATH "${CMAKE_SOURCE_DIR}/dependencies/FreeImage/bin/FreeImage.dll")
# Add custom command to copy FreeImage.dll to the output directory for both Debug and Release
add_custom_command(TARGET TerraVisor POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${FREEIMAGE_DLL_PATH}
$<TARGET_FILE_DIR:TerraVisor>
)
# Set subsystem based on build type # Set subsystem based on build type
if (WIN32) if (WIN32)

View File

@ -671,4 +671,10 @@ into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>. <https://www.gnu.org/licenses/why-not-lgpl.html>.
---
This project also includes third-party software with different licenses:
- FreeImage is licensed under the FreeImage Public License (FIPL). See `licenses/LICENSE-FreeImage.txt` for details.

BIN
dependencies/FreeImage/bin/FreeImage.dll vendored Normal file

Binary file not shown.

1163
dependencies/FreeImage/include/FreeImage.h vendored Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,8 +4,8 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Terrain Controls] [Window][Terrain Controls]
Pos=863,19 Pos=925,19
Size=417,701 Size=403,701
Collapsed=0 Collapsed=0
DockId=0x00000002,0 DockId=0x00000002,0
@ -19,17 +19,17 @@ Collapsed=0
[Window][TerraVisor] [Window][TerraVisor]
Pos=0,0 Pos=0,0
Size=1280,720 Size=1328,720
Collapsed=0 Collapsed=0
[Window][Scene Window] [Window][Scene Window]
Pos=0,19 Pos=0,19
Size=861,701 Size=923,701
Collapsed=0 Collapsed=0
DockId=0x00000001,0 DockId=0x00000001,0
[Docking][Data] [Docking][Data]
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 Split=X Selected=0x9F2D9299 DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1328,701 Split=X Selected=0x9F2D9299
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=861,701 CentralNode=1 Selected=0x9F2D9299 DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=875,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=417,701 Selected=0xF69494A7 DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=403,701 Selected=0xF69494A7

10
include/load_texture.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef LOAD_TEXTURE_H_
#define LOAD_TEXTURE_H_
#include <GL/glew.h>
namespace Texture {
GLuint LoadTexture(const char* filePath);
}
#endif // LOAD_TEXTURE_H_

View File

@ -1,6 +1,7 @@
#version 450 core #version 450 core
layout(binding = 0) uniform sampler2D heightTexture; layout(binding = 0) uniform sampler2D heightTexture;
layout(binding = 1) uniform sampler2D colorTexture;
in TE_OUT { in TE_OUT {
vec3 position; vec3 position;
@ -53,7 +54,7 @@ vec3 calculateNormalsFromHeightTexture() {
} }
void main() { void main() {
albedoGbuffer = vec4(vec3(texture(heightTexture, fs_in.texCoord).r), 1.0); albedoGbuffer = texture(colorTexture, vec2(fs_in.texCoord.x, -fs_in.texCoord.y));
positionGbuffer = vec4(fs_in.position, 1.0); positionGbuffer = vec4(fs_in.position, 1.0f);
normalGbuffer = vec4(fs_in.normal, 1.0); normalGbuffer = vec4(fs_in.normal, 1.0f);
} }

View File

@ -23,7 +23,7 @@ vec3 calculateDisneySpecular(vec3 N, vec3 V, vec3 L, vec3 albedo) {
vec3 F = F0 + (1.0f - F0) * pow(1.0f - dot(H, V), 5.0f); vec3 F = F0 + (1.0f - F0) * pow(1.0f - dot(H, V), 5.0f);
// Normal Distribution Function (GGX) // Normal Distribution Function (GGX)
float roughness = 0.5f; float roughness = 0.7f;
float alpha = roughness * roughness; float alpha = roughness * roughness;
float alpha2 = alpha * alpha; float alpha2 = alpha * alpha;
@ -47,7 +47,7 @@ vec3 calculateDisneySpecular(vec3 N, vec3 V, vec3 L, vec3 albedo) {
void main() void main()
{ {
vec4 albedo = texture(albedoGbuffer, tex_coords); vec4 albedo = texture(albedoGbuffer, tex_coords);
albedo = vec4(vec3(0.18f), texture(albedoGbuffer, tex_coords).a); // albedo = vec4(vec3(0.18f), texture(albedoGbuffer, tex_coords).a);
vec4 position = texture(positionGbuffer, tex_coords); vec4 position = texture(positionGbuffer, tex_coords);
vec4 normal = texture(normalGbuffer, tex_coords); vec4 normal = texture(normalGbuffer, tex_coords);
@ -76,7 +76,7 @@ void main()
vec4 specular = vec4(calculateDisneySpecular(N, V, L, albedo.rgb), albedo.a); vec4 specular = vec4(calculateDisneySpecular(N, V, L, albedo.rgb), albedo.a);
// Light Intensity // Light Intensity
vec4 lightColor = vec4(vec3(50.0f), 1.0f); vec4 lightColor = vec4(vec3(25.0f), 1.0f);
vec4 finalDiffuse = (diffuse + specular) * lightColor + (ambientLight * 0.1f); vec4 finalDiffuse = (diffuse + specular) * lightColor + (ambientLight * 0.1f);

38
source/load_texture.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "load_texture.h"
#include <iostream>
#include <FreeImage.h>
namespace Texture {
GLuint LoadTexture(const char* filePath) {
FIBITMAP* bitmap = FreeImage_Load(FIF_TIFF, filePath, TIFF_DEFAULT);
if(!bitmap) {
std::cerr << "Failed to load TIFF image: " << filePath << std::endl;
}
FIBITMAP* bitmap32 = FreeImage_ConvertTo32Bits(bitmap);
FreeImage_Unload(bitmap);
int width = FreeImage_GetWidth(bitmap32);
int height = FreeImage_GetHeight(bitmap32);
BYTE* pixels = FreeImage_GetBits(bitmap32);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
FreeImage_Unload(bitmap32);
return textureID;
}
}

View File

@ -35,6 +35,8 @@
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#include <FreeImage.h>
#include "callbacks.h" #include "callbacks.h"
#include "scene.h" #include "scene.h"
@ -146,6 +148,7 @@ int main() {
Callbacks::Register(window, &scene); Callbacks::Register(window, &scene);
scene.Init(); scene.Init();
InitializeImGui(window); InitializeImGui(window);
FreeImage_Initialise();
// Check if we're in test mode // Check if we're in test mode
const char* testEnv = std::getenv("TEST_MODE"); const char* testEnv = std::getenv("TEST_MODE");

View File

@ -28,6 +28,7 @@
#include "Uniforms.h" #include "Uniforms.h"
#include "InitShader.h" #include "InitShader.h"
#include "DebugCallback.h" #include "DebugCallback.h"
#include "load_texture.h"
namespace { namespace {
@ -40,7 +41,9 @@ const std::string kQuadVertexPath = "shaders/quad_vertex.glsl";
const std::string kQuadFragmentPath = "shaders/quad_fragment.glsl"; const std::string kQuadFragmentPath = "shaders/quad_fragment.glsl";
const std::string kHGTPath = "hgt/N02E016.hgt"; const std::string kHGTPath = "hgt/N02E016.hgt";
GLuint tex_id = -1; const std::string kColPath = "hgt/color/N02E16_col.tiff";
GLuint tex_id = 0;
GLuint col_id = 0;
float tessellationFactor = 32.0f; float tessellationFactor = 32.0f;
@ -83,6 +86,7 @@ void Scene::Init() {
std::vector<int16_t> heightData = LoadHGT(kHGTPath, 3601, 3601); std::vector<int16_t> heightData = LoadHGT(kHGTPath, 3601, 3601);
tex_id = CreateHeightmapTexture(heightData, 3601, 3601); tex_id = CreateHeightmapTexture(heightData, 3601, 3601);
col_id = Texture::LoadTexture(kColPath.c_str());
InitBuffers(); InitBuffers();
ReloadShader(); ReloadShader();
@ -229,9 +233,13 @@ void Scene::Display(GLFWwindow* window) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_id); glBindTexture(GL_TEXTURE_2D, tex_id);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, col_id);
GLint heightTextureLoc = glGetUniformLocation(shader_program_, "heightTexture"); GLint heightTextureLoc = glGetUniformLocation(shader_program_, "heightTexture");
glUniform1i(heightTextureLoc, 0); glUniform1i(heightTextureLoc, 0);
GLint colorTextureLoc = glGetUniformLocation(shader_program_, "colorTexture");
glUniform1i(colorTextureLoc, 1);
GLint tessFactorLoc = glGetUniformLocation(shader_program_, "tessellationFactor"); GLint tessFactorLoc = glGetUniformLocation(shader_program_, "tessellationFactor");
glUniform1f(tessFactorLoc, tessellationFactor); glUniform1f(tessFactorLoc, tessellationFactor);
@ -254,8 +262,8 @@ void Scene::Display(GLFWwindow* window) {
glActiveTexture(GL_TEXTURE2); glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, geo_fbo_.normalTexture); glBindTexture(GL_TEXTURE_2D, geo_fbo_.normalTexture);
GLint colorTextureLoc = glGetUniformLocation(quad_shader_program_, "albedoGbuffer"); GLint albedoTextureLoc = glGetUniformLocation(quad_shader_program_, "albedoGbuffer");
glUniform1i(colorTextureLoc, 0); glUniform1i(albedoTextureLoc, 0);
GLint positionTextureLoc = glGetUniformLocation(quad_shader_program_, "positionGbuffer"); GLint positionTextureLoc = glGetUniformLocation(quad_shader_program_, "positionGbuffer");
glUniform1i(positionTextureLoc, 1); glUniform1i(positionTextureLoc, 1);
GLint normalTextureLoc = glGetUniformLocation(quad_shader_program_, "normalGbuffer"); GLint normalTextureLoc = glGetUniformLocation(quad_shader_program_, "normalGbuffer");