Compare commits

...

6 Commits

Author SHA1 Message Date
Abdi Ibrahim
9662813b32
Merge 4f50603b8f into 902ff3b0a9 2024-12-30 23:56:03 -08:00
Stowy
902ff3b0a9
Fixed compilation using clang on windows (#134)
Some checks failed
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 12s
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 10s
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
2024-12-31 17:51:18 +13:00
SuperOpt
2938c00dc8
[Renderers/Cairo] Add FindCairo.cmake (#122) 2024-12-31 13:32:56 +13:00
SuperOpt
ba78b35604
C++ projects should use CXX flags (#136) 2024-12-31 13:29:49 +13:00
Abdi Ibrahim
4f50603b8f updated render 2024-12-29 09:39:59 -05:00
Abdi Ibrahim
275c9d98cd precompute once per frame 2024-12-29 09:21:40 -05:00
6 changed files with 72 additions and 28 deletions

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.27) cmake_minimum_required(VERSION 3.27)
project(clay) project(clay)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_subdirectory("examples/cpp-project-example") add_subdirectory("examples/cpp-project-example")
# Don't try to compile C99 projects using MSVC # Don't try to compile C99 projects using MSVC

2
clay.h
View File

@ -134,7 +134,7 @@ CLAY__ALIGNMENT_STRUCT(bool);
CLAY__ALIGNMENT_STRUCT(uint8_t); CLAY__ALIGNMENT_STRUCT(uint8_t);
CLAY__ALIGNMENT_STRUCT(int32_t); CLAY__ALIGNMENT_STRUCT(int32_t);
#ifdef _MSC_VER #if defined(_MSC_VER) && !defined(__clang__)
#define CLAY_PACKED_ENUM __pragma(pack(push, 1)) enum __pragma(pack(pop)) #define CLAY_PACKED_ENUM __pragma(pack(push, 1)) enum __pragma(pack(pop))
#else #else
#define CLAY_PACKED_ENUM enum __attribute__((__packed__)) #define CLAY_PACKED_ENUM enum __attribute__((__packed__))

32
cmake/FindCairo.cmake Normal file
View File

@ -0,0 +1,32 @@
# Defines:
# CAIRO_FOUND - System has Cairo
# CAIRO_INCLUDE_DIRS - Cairo include directories
# CAIRO_LIBRARY - Cairo library
# Cairo::Cairo - Imported target
find_path(CAIRO_INCLUDE_DIRS
NAMES cairo/cairo.h
PATHS ${CAIRO_ROOT_DIR}
PATH_SUFFIXES include
)
find_library(CAIRO_LIBRARY
NAMES cairo
PATHS ${CAIRO_ROOT_DIR}
PATH_SUFFIXES lib lib64
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Cairo
REQUIRED_VARS CAIRO_LIBRARY CAIRO_INCLUDE_DIRS
)
if(Cairo_FOUND AND NOT TARGET Cairo::Cairo)
add_library(Cairo::Cairo UNKNOWN IMPORTED)
set_target_properties(Cairo::Cairo PROPERTIES
IMPORTED_LOCATION "${CAIRO_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${CAIRO_INCLUDE_DIRS}"
)
endif()
mark_as_advanced(CAIRO_INCLUDE_DIRS CAIRO_LIBRARY)

View File

@ -2,13 +2,17 @@ cmake_minimum_required(VERSION 3.27)
project(clay_examples_cairo_pdf_rendering C) project(clay_examples_cairo_pdf_rendering C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake")
add_executable(clay_examples_cairo_pdf_rendering main.c) add_executable(clay_examples_cairo_pdf_rendering main.c)
find_package(Cairo REQUIRED)
target_compile_options(clay_examples_cairo_pdf_rendering PUBLIC) target_compile_options(clay_examples_cairo_pdf_rendering PUBLIC)
target_include_directories(clay_examples_cairo_pdf_rendering PUBLIC .) target_include_directories(clay_examples_cairo_pdf_rendering PUBLIC . ${CAIRO_INCLUDE_DIRS})
target_link_libraries(clay_examples_cairo_pdf_rendering PUBLIC cairo)
target_link_libraries(clay_examples_cairo_pdf_rendering PUBLIC Cairo::Cairo)
set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces") set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces")
set(CMAKE_C_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_RELEASE "-O3")

View File

@ -8,5 +8,5 @@ add_executable(clay_examples_cpp_project_example main.cpp)
target_include_directories(clay_examples_cpp_project_example PUBLIC .) target_include_directories(clay_examples_cpp_project_example PUBLIC .)
set(CMAKE_C_FLAGS_DEBUG "-Werror -Wall -Wno-error=missing-braces") set(CMAKE_CXX_FLAGS_DEBUG "-Werror -Wall -Wno-error=missing-braces")
set(CMAKE_C_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS_RELEASE "-O3")

View File

@ -38,8 +38,28 @@ typedef struct
}; };
} CustomLayoutElement; } CustomLayoutElement;
// Precompute the matrices at start of the frame
Matrix PrecomputeViewMatrix(const Camera camera){
return MatrixLookAt(camera.position, camera.target, camera.up);
}
Matrix PrecomputeProjectionMatrix(const Camera camera, int screenWidth, int screenHeight, float zDistance) {
Matrix matProj = MatrixIdentity();
if (camera.projection == CAMERA_PERSPECTIVE) {
// Calculate projection matrix from perspective
matProj = MatrixPerspective(camera.fovy * DEG2RAD, ((double)screenWidth / (double)screenHeight), 0.01f, zDistance);
} else if (camera.projection == CAMERA_ORTHOGRAPHIC) {
double aspect = (double)screenWidth / (double)screenHeight;
double top = camera.fovy / 2.0;
double right = top * aspect;
// Calculate projection matrix from orthographic
matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0);
}
return matProj;
}
// Get a ray trace from the screen position (i.e mouse) within a specific section of the screen // Get a ray trace from the screen position (i.e mouse) within a specific section of the screen
Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int screenWidth, int screenHeight, float zDistance) Ray GetScreenToWorldPointWithZDistance(Vector2 position, const Matrix matView, const Matrix matProj, int screenWidth, int screenHeight) {
{ {
Ray ray = { 0 }; Ray ray = { 0 };
@ -52,26 +72,6 @@ Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int scre
// Store values in a vector // Store values in a vector
Vector3 deviceCoords = { x, y, z }; Vector3 deviceCoords = { x, y, z };
// Calculate view matrix from camera look at
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
Matrix matProj = MatrixIdentity();
if (camera.projection == CAMERA_PERSPECTIVE)
{
// Calculate projection matrix from perspective
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)screenWidth/(double)screenHeight), 0.01f, zDistance);
}
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
{
double aspect = (double)screenWidth/(double)screenHeight;
double top = camera.fovy/2.0;
double right = top*aspect;
// Calculate projection matrix from orthographic
matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0);
}
// Unproject far/near points // Unproject far/near points
Vector3 nearPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 0.0f }, matProj, matView); Vector3 nearPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 0.0f }, matProj, matView);
Vector3 farPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView); Vector3 farPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView);
@ -129,6 +129,12 @@ void Clay_Raylib_Initialize(int width, int height, const char *title, unsigned i
void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands) void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands)
{ {
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
Matrix matView = PrecomputeViewMatrix(Raylib_camera);
Matrix matProj = PrecomputeProjectionMatrix(Raylib_camera, screenWidth, screenHeight, 140.0f);
measureCalls = 0; measureCalls = 0;
for (int j = 0; j < renderCommands.length; j++) for (int j = 0; j < renderCommands.length; j++)
{ {