initial commit (lol)
This commit is contained in:
commit
4da7be3982
.clang-format.gitignoreCMakeLists.txtcompile_commands.json
marble_bust
mons_3d
mons_collections
mons_exe
mons_gltf
mons_image
mons_json
mons_math
18
.clang-format
Normal file
18
.clang-format
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
# TabWidth (unsigned)
|
||||
# The number of columns used for tab stops.
|
||||
TabWidth: 4
|
||||
|
||||
# IndentWidth (unsigned)
|
||||
# The number of columns to use for indentation.
|
||||
IndentWidth: 4
|
||||
|
||||
# UseTab (UseTabStyle)
|
||||
# The way to use tab characters in the resulting file.
|
||||
# Possible values:
|
||||
# UT_Never (in configuration: Never) Never use tab.
|
||||
# UT_ForIndentation (in configuration: ForIndentation) Use tabs only for indentation.
|
||||
# UT_Always (in configuration: Always) Use tabs whenever we need to fill whitespace that spans at least from one tab stop to the next one.
|
||||
UseTab: Never
|
||||
|
||||
SortIncludes: false
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
build
|
||||
.ccls-cache
|
||||
|
14
CMakeLists.txt
Normal file
14
CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_math LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_subdirectory(./mons_math)
|
||||
add_subdirectory(./mons_collections)
|
||||
add_subdirectory(./mons_json)
|
||||
add_subdirectory(./mons_image)
|
||||
add_subdirectory(./mons_3d)
|
||||
add_subdirectory(./mons_gltf)
|
||||
add_subdirectory(./mons_exe)
|
||||
|
1
compile_commands.json
Symbolic link
1
compile_commands.json
Symbolic link
@ -0,0 +1 @@
|
||||
build/compile_commands.json
|
BIN
marble_bust/marble_bust_01.bin
Normal file
BIN
marble_bust/marble_bust_01.bin
Normal file
Binary file not shown.
161
marble_bust/marble_bust_01_4k.gltf
Normal file
161
marble_bust/marble_bust_01_4k.gltf
Normal file
@ -0,0 +1,161 @@
|
||||
{
|
||||
"asset": {
|
||||
"generator": "Khronos glTF Blender I/O v1.6.16",
|
||||
"version": "2.0"
|
||||
},
|
||||
"scene": 0,
|
||||
"scenes": [
|
||||
{
|
||||
"name": "Scene",
|
||||
"nodes": [
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"nodes": [
|
||||
{
|
||||
"mesh": 0,
|
||||
"name": "marble_bust_01",
|
||||
"translation": [
|
||||
0,
|
||||
0.028335653245449066,
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"doubleSided": true,
|
||||
"name": "marble_bust_01",
|
||||
"normalTexture": {
|
||||
"index": 0
|
||||
},
|
||||
"pbrMetallicRoughness": {
|
||||
"baseColorTexture": {
|
||||
"index": 1
|
||||
},
|
||||
"metallicFactor": 0,
|
||||
"metallicRoughnessTexture": {
|
||||
"index": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"meshes": [
|
||||
{
|
||||
"name": "marble_bust_01",
|
||||
"primitives": [
|
||||
{
|
||||
"attributes": {
|
||||
"POSITION": 0,
|
||||
"NORMAL": 1,
|
||||
"TEXCOORD_0": 2
|
||||
},
|
||||
"indices": 3,
|
||||
"material": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"textures": [
|
||||
{
|
||||
"sampler": 0,
|
||||
"source": 0
|
||||
},
|
||||
{
|
||||
"sampler": 0,
|
||||
"source": 1
|
||||
},
|
||||
{
|
||||
"sampler": 0,
|
||||
"source": 2
|
||||
}
|
||||
],
|
||||
"images": [
|
||||
{
|
||||
"mimeType": "image/qoi",
|
||||
"name": "marble_bust_01_nor_gl",
|
||||
"uri": "textures/marble_bust_01_nor_gl_4k.qoi"
|
||||
},
|
||||
{
|
||||
"mimeType": "image/qoi",
|
||||
"name": "marble_bust_01_diff",
|
||||
"uri": "textures/marble_bust_01_diff_4k.qoi"
|
||||
},
|
||||
{
|
||||
"mimeType": "image/qoi",
|
||||
"name": "marble_bust_01_arm",
|
||||
"uri": "textures/marble_bust_01_rough_4k.qoi"
|
||||
}
|
||||
],
|
||||
"accessors": [
|
||||
{
|
||||
"bufferView": 0,
|
||||
"componentType": 5126,
|
||||
"count": 9746,
|
||||
"max": [
|
||||
0.14886942505836487,
|
||||
0.48668384552001953,
|
||||
0.1551172435283661
|
||||
],
|
||||
"min": [
|
||||
-0.12288019061088562,
|
||||
-0.028259359300136566,
|
||||
-0.1445964276790619
|
||||
],
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 1,
|
||||
"componentType": 5126,
|
||||
"count": 9746,
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 2,
|
||||
"componentType": 5126,
|
||||
"count": 9746,
|
||||
"type": "VEC2"
|
||||
},
|
||||
{
|
||||
"bufferView": 3,
|
||||
"componentType": 5123,
|
||||
"count": 52368,
|
||||
"type": "SCALAR"
|
||||
}
|
||||
],
|
||||
"bufferViews": [
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 116952,
|
||||
"byteOffset": 0
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 116952,
|
||||
"byteOffset": 116952
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 77968,
|
||||
"byteOffset": 233904
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 104736,
|
||||
"byteOffset": 311872
|
||||
}
|
||||
],
|
||||
"samplers": [
|
||||
{
|
||||
"magFilter": 9729,
|
||||
"minFilter": 9987
|
||||
}
|
||||
],
|
||||
"buffers": [
|
||||
{
|
||||
"byteLength": 416608,
|
||||
"uri": "marble_bust_01.bin"
|
||||
}
|
||||
]
|
||||
}
|
BIN
marble_bust/textures/marble_bust_01_diff_4k.qoi
Normal file
BIN
marble_bust/textures/marble_bust_01_diff_4k.qoi
Normal file
Binary file not shown.
BIN
marble_bust/textures/marble_bust_01_nor_gl_4k.qoi
Normal file
BIN
marble_bust/textures/marble_bust_01_nor_gl_4k.qoi
Normal file
Binary file not shown.
BIN
marble_bust/textures/marble_bust_01_rough_4k.qoi
Normal file
BIN
marble_bust/textures/marble_bust_01_rough_4k.qoi
Normal file
Binary file not shown.
41
mons_3d/CMakeLists.txt
Normal file
41
mons_3d/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_3d LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_library(mons_3d
|
||||
SHARED
|
||||
./src/mesh.c
|
||||
./src/color.c
|
||||
./src/model.c
|
||||
./src/shader.c
|
||||
./src/texture.c
|
||||
./src/transform.c
|
||||
./src/projection.c
|
||||
./external/glad/src/gl.c
|
||||
./external/glad/src/glx.c
|
||||
./external/mikktspace/src/mikktspace.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_3d PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/external/glad/include"
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/external/mikktspace/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_3d PRIVATE -coverage)
|
||||
target_link_options(mons_3d PRIVATE -coverage)
|
||||
target_link_libraries(mons_3d PUBLIC mons_collections mons_math mons_image)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_3d)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
311
mons_3d/external/glad/include/KHR/khrplatform.h
vendored
Normal file
311
mons_3d/external/glad/include/KHR/khrplatform.h
vendored
Normal file
@ -0,0 +1,311 @@
|
||||
#ifndef __khrplatform_h_
|
||||
#define __khrplatform_h_
|
||||
|
||||
/*
|
||||
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||
**
|
||||
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||
** copy of this software and/or associated documentation files (the
|
||||
** "Materials"), to deal in the Materials without restriction, including
|
||||
** without limitation the rights to use, copy, modify, merge, publish,
|
||||
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||
** permit persons to whom the Materials are furnished to do so, subject to
|
||||
** the following conditions:
|
||||
**
|
||||
** The above copyright notice and this permission notice shall be included
|
||||
** in all copies or substantial portions of the Materials.
|
||||
**
|
||||
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
*/
|
||||
|
||||
/* Khronos platform-specific types and definitions.
|
||||
*
|
||||
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||
* The last semantic modification to khrplatform.h was at commit ID:
|
||||
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||
*
|
||||
* Adopters may modify this file to suit their platform. Adopters are
|
||||
* encouraged to submit platform specific modifications to the Khronos
|
||||
* group so that they can be included in future versions of this file.
|
||||
* Please submit changes by filing pull requests or issues on
|
||||
* the EGL Registry repository linked above.
|
||||
*
|
||||
*
|
||||
* See the Implementer's Guidelines for information about where this file
|
||||
* should be located on your system and for more details of its use:
|
||||
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||
*
|
||||
* This file should be included as
|
||||
* #include <KHR/khrplatform.h>
|
||||
* by Khronos client API header files that use its types and defines.
|
||||
*
|
||||
* The types in khrplatform.h should only be used to define API-specific types.
|
||||
*
|
||||
* Types defined in khrplatform.h:
|
||||
* khronos_int8_t signed 8 bit
|
||||
* khronos_uint8_t unsigned 8 bit
|
||||
* khronos_int16_t signed 16 bit
|
||||
* khronos_uint16_t unsigned 16 bit
|
||||
* khronos_int32_t signed 32 bit
|
||||
* khronos_uint32_t unsigned 32 bit
|
||||
* khronos_int64_t signed 64 bit
|
||||
* khronos_uint64_t unsigned 64 bit
|
||||
* khronos_intptr_t signed same number of bits as a pointer
|
||||
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||
* khronos_ssize_t signed size
|
||||
* khronos_usize_t unsigned size
|
||||
* khronos_float_t signed 32 bit floating point
|
||||
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||
* nanoseconds
|
||||
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||
* only be used as a base type when a client API's boolean type is
|
||||
* an enum. Client APIs which use an integer or other type for
|
||||
* booleans cannot use this as the base type for their boolean.
|
||||
*
|
||||
* Tokens defined in khrplatform.h:
|
||||
*
|
||||
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||
*
|
||||
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||
*
|
||||
* Calling convention macros defined in this file:
|
||||
* KHRONOS_APICALL
|
||||
* KHRONOS_APIENTRY
|
||||
* KHRONOS_APIATTRIBUTES
|
||||
*
|
||||
* These may be used in function prototypes as:
|
||||
*
|
||||
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||
* int arg1,
|
||||
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||
*/
|
||||
|
||||
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||
# define KHRONOS_STATIC 1
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APICALL
|
||||
*-------------------------------------------------------------------------
|
||||
* This precedes the return type of the function in the function prototype.
|
||||
*/
|
||||
#if defined(KHRONOS_STATIC)
|
||||
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||
* header compatible with static linking. */
|
||||
# define KHRONOS_APICALL
|
||||
#elif defined(_WIN32)
|
||||
# define KHRONOS_APICALL __declspec(dllimport)
|
||||
#elif defined (__SYMBIAN32__)
|
||||
# define KHRONOS_APICALL IMPORT_C
|
||||
#elif defined(__ANDROID__)
|
||||
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||
#else
|
||||
# define KHRONOS_APICALL
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIENTRY
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the return type of the function and precedes the function
|
||||
* name in the function prototype.
|
||||
*/
|
||||
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||
/* Win32 but not WinCE */
|
||||
# define KHRONOS_APIENTRY __stdcall
|
||||
#else
|
||||
# define KHRONOS_APIENTRY
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIATTRIBUTES
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the closing parenthesis of the function prototype arguments.
|
||||
*/
|
||||
#if defined (__ARMCC_2__)
|
||||
#define KHRONOS_APIATTRIBUTES __softfp
|
||||
#else
|
||||
#define KHRONOS_APIATTRIBUTES
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* basic type definitions
|
||||
*-----------------------------------------------------------------------*/
|
||||
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||
|
||||
|
||||
/*
|
||||
* Using <stdint.h>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
/*
|
||||
* To support platform where unsigned long cannot be used interchangeably with
|
||||
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
|
||||
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
|
||||
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
|
||||
* unsigned long long or similar (this results in different C++ name mangling).
|
||||
* To avoid changes for existing platforms, we restrict usage of intptr_t to
|
||||
* platforms where the size of a pointer is larger than the size of long.
|
||||
*/
|
||||
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
|
||||
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
|
||||
#define KHRONOS_USE_INTPTR_T
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#elif defined(__VMS ) || defined(__sgi)
|
||||
|
||||
/*
|
||||
* Using <inttypes.h>
|
||||
*/
|
||||
#include <inttypes.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||
|
||||
/*
|
||||
* Win32
|
||||
*/
|
||||
typedef __int32 khronos_int32_t;
|
||||
typedef unsigned __int32 khronos_uint32_t;
|
||||
typedef __int64 khronos_int64_t;
|
||||
typedef unsigned __int64 khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__sun__) || defined(__digital__)
|
||||
|
||||
/*
|
||||
* Sun or Digital
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#if defined(__arch64__) || defined(_LP64)
|
||||
typedef long int khronos_int64_t;
|
||||
typedef unsigned long int khronos_uint64_t;
|
||||
#else
|
||||
typedef long long int khronos_int64_t;
|
||||
typedef unsigned long long int khronos_uint64_t;
|
||||
#endif /* __arch64__ */
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif 0
|
||||
|
||||
/*
|
||||
* Hypothetical platform with no float or int64 support
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#define KHRONOS_SUPPORT_INT64 0
|
||||
#define KHRONOS_SUPPORT_FLOAT 0
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
* Generic fallback
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Types that are (so far) the same on all platforms
|
||||
*/
|
||||
typedef signed char khronos_int8_t;
|
||||
typedef unsigned char khronos_uint8_t;
|
||||
typedef signed short int khronos_int16_t;
|
||||
typedef unsigned short int khronos_uint16_t;
|
||||
|
||||
/*
|
||||
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||
* to be the only LLP64 architecture in current use.
|
||||
*/
|
||||
#ifdef KHRONOS_USE_INTPTR_T
|
||||
typedef intptr_t khronos_intptr_t;
|
||||
typedef uintptr_t khronos_uintptr_t;
|
||||
#elif defined(_WIN64)
|
||||
typedef signed long long int khronos_intptr_t;
|
||||
typedef unsigned long long int khronos_uintptr_t;
|
||||
#else
|
||||
typedef signed long int khronos_intptr_t;
|
||||
typedef unsigned long int khronos_uintptr_t;
|
||||
#endif
|
||||
|
||||
#if defined(_WIN64)
|
||||
typedef signed long long int khronos_ssize_t;
|
||||
typedef unsigned long long int khronos_usize_t;
|
||||
#else
|
||||
typedef signed long int khronos_ssize_t;
|
||||
typedef unsigned long int khronos_usize_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_FLOAT
|
||||
/*
|
||||
* Float type
|
||||
*/
|
||||
typedef float khronos_float_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_INT64
|
||||
/* Time types
|
||||
*
|
||||
* These types can be used to represent a time interval in nanoseconds or
|
||||
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||
* time the system booted). The Unadjusted System Time is an unsigned
|
||||
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||
* may be either signed or unsigned.
|
||||
*/
|
||||
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Dummy value used to pad enum types to 32 bits.
|
||||
*/
|
||||
#ifndef KHRONOS_MAX_ENUM
|
||||
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Enumerated boolean type
|
||||
*
|
||||
* Values other than zero should be considered to be true. Therefore
|
||||
* comparisons should not be made against KHRONOS_TRUE.
|
||||
*/
|
||||
typedef enum {
|
||||
KHRONOS_FALSE = 0,
|
||||
KHRONOS_TRUE = 1,
|
||||
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||
} khronos_boolean_enum_t;
|
||||
|
||||
#endif /* __khrplatform_h_ */
|
3644
mons_3d/external/glad/include/glad/gl.h
vendored
Normal file
3644
mons_3d/external/glad/include/glad/gl.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
566
mons_3d/external/glad/include/glad/glx.h
vendored
Normal file
566
mons_3d/external/glad/include/glad/glx.h
vendored
Normal file
@ -0,0 +1,566 @@
|
||||
/**
|
||||
* Loader generated by glad 2.0.8 on Sat Feb 1 22:26:11 2025
|
||||
*
|
||||
* SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
|
||||
*
|
||||
* Generator: C/C++
|
||||
* Specification: glx
|
||||
* Extensions: 0
|
||||
*
|
||||
* APIs:
|
||||
* - glx=1.4
|
||||
*
|
||||
* Options:
|
||||
* - ALIAS = False
|
||||
* - DEBUG = False
|
||||
* - HEADER_ONLY = False
|
||||
* - LOADER = True
|
||||
* - MX = False
|
||||
* - ON_DEMAND = False
|
||||
*
|
||||
* Commandline:
|
||||
* --api='glx=1.4' --extensions='' c --loader
|
||||
*
|
||||
* Online:
|
||||
* http://glad.sh/#api=glx%3D1.4&extensions=&generator=c&options=LOADER
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef GLAD_GLX_H_
|
||||
#define GLAD_GLX_H_
|
||||
|
||||
#ifdef GLX_H
|
||||
#error GLX header already included (API: glx), remove previous include!
|
||||
#endif
|
||||
#define GLX_H 1
|
||||
|
||||
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
#define GLAD_GLX
|
||||
#define GLAD_OPTION_GLX_LOADER
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef GLAD_PLATFORM_H_
|
||||
#define GLAD_PLATFORM_H_
|
||||
|
||||
#ifndef GLAD_PLATFORM_WIN32
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__)
|
||||
#define GLAD_PLATFORM_WIN32 1
|
||||
#else
|
||||
#define GLAD_PLATFORM_WIN32 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef GLAD_PLATFORM_APPLE
|
||||
#ifdef __APPLE__
|
||||
#define GLAD_PLATFORM_APPLE 1
|
||||
#else
|
||||
#define GLAD_PLATFORM_APPLE 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef GLAD_PLATFORM_EMSCRIPTEN
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#define GLAD_PLATFORM_EMSCRIPTEN 1
|
||||
#else
|
||||
#define GLAD_PLATFORM_EMSCRIPTEN 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef GLAD_PLATFORM_UWP
|
||||
#if defined(_MSC_VER) && !defined(GLAD_INTERNAL_HAVE_WINAPIFAMILY)
|
||||
#ifdef __has_include
|
||||
#if __has_include(<winapifamily.h>)
|
||||
#define GLAD_INTERNAL_HAVE_WINAPIFAMILY 1
|
||||
#endif
|
||||
#elif _MSC_VER >= 1700 && !_USING_V110_SDK71_
|
||||
#define GLAD_INTERNAL_HAVE_WINAPIFAMILY 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef GLAD_INTERNAL_HAVE_WINAPIFAMILY
|
||||
#include <winapifamily.h>
|
||||
#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
|
||||
#define GLAD_PLATFORM_UWP 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef GLAD_PLATFORM_UWP
|
||||
#define GLAD_PLATFORM_UWP 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define GLAD_GNUC_EXTENSION __extension__
|
||||
#else
|
||||
#define GLAD_GNUC_EXTENSION
|
||||
#endif
|
||||
|
||||
#define GLAD_UNUSED(x) (void)(x)
|
||||
|
||||
#ifndef GLAD_API_CALL
|
||||
#if defined(GLAD_API_CALL_EXPORT)
|
||||
#if GLAD_PLATFORM_WIN32 || defined(__CYGWIN__)
|
||||
#if defined(GLAD_API_CALL_EXPORT_BUILD)
|
||||
#if defined(__GNUC__)
|
||||
#define GLAD_API_CALL __attribute__ ((dllexport)) extern
|
||||
#else
|
||||
#define GLAD_API_CALL __declspec(dllexport) extern
|
||||
#endif
|
||||
#else
|
||||
#if defined(__GNUC__)
|
||||
#define GLAD_API_CALL __attribute__ ((dllimport)) extern
|
||||
#else
|
||||
#define GLAD_API_CALL __declspec(dllimport) extern
|
||||
#endif
|
||||
#endif
|
||||
#elif defined(__GNUC__) && defined(GLAD_API_CALL_EXPORT_BUILD)
|
||||
#define GLAD_API_CALL __attribute__ ((visibility ("default"))) extern
|
||||
#else
|
||||
#define GLAD_API_CALL extern
|
||||
#endif
|
||||
#else
|
||||
#define GLAD_API_CALL extern
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef APIENTRY
|
||||
#define GLAD_API_PTR APIENTRY
|
||||
#elif GLAD_PLATFORM_WIN32
|
||||
#define GLAD_API_PTR __stdcall
|
||||
#else
|
||||
#define GLAD_API_PTR
|
||||
#endif
|
||||
|
||||
#ifndef GLAPI
|
||||
#define GLAPI GLAD_API_CALL
|
||||
#endif
|
||||
|
||||
#ifndef GLAPIENTRY
|
||||
#define GLAPIENTRY GLAD_API_PTR
|
||||
#endif
|
||||
|
||||
#define GLAD_MAKE_VERSION(major, minor) (major * 10000 + minor)
|
||||
#define GLAD_VERSION_MAJOR(version) (version / 10000)
|
||||
#define GLAD_VERSION_MINOR(version) (version % 10000)
|
||||
|
||||
#define GLAD_GENERATOR_VERSION "2.0.8"
|
||||
|
||||
typedef void (*GLADapiproc)(void);
|
||||
|
||||
typedef GLADapiproc (*GLADloadfunc)(const char *name);
|
||||
typedef GLADapiproc (*GLADuserptrloadfunc)(void *userptr, const char *name);
|
||||
|
||||
typedef void (*GLADprecallback)(const char *name, GLADapiproc apiproc, int len_args, ...);
|
||||
typedef void (*GLADpostcallback)(void *ret, const char *name, GLADapiproc apiproc, int len_args, ...);
|
||||
|
||||
#endif /* GLAD_PLATFORM_H_ */
|
||||
|
||||
#define GLX_ACCUM_ALPHA_SIZE 17
|
||||
#define GLX_ACCUM_BLUE_SIZE 16
|
||||
#define GLX_ACCUM_BUFFER_BIT 0x00000080
|
||||
#define GLX_ACCUM_GREEN_SIZE 15
|
||||
#define GLX_ACCUM_RED_SIZE 14
|
||||
#define GLX_ALPHA_SIZE 11
|
||||
#define GLX_AUX_BUFFERS 7
|
||||
#define GLX_AUX_BUFFERS_BIT 0x00000010
|
||||
#define GLX_BACK_LEFT_BUFFER_BIT 0x00000004
|
||||
#define GLX_BACK_RIGHT_BUFFER_BIT 0x00000008
|
||||
#define GLX_BAD_ATTRIBUTE 2
|
||||
#define GLX_BAD_CONTEXT 5
|
||||
#define GLX_BAD_ENUM 7
|
||||
#define GLX_BAD_SCREEN 1
|
||||
#define GLX_BAD_VALUE 6
|
||||
#define GLX_BAD_VISUAL 4
|
||||
#define GLX_BLUE_SIZE 10
|
||||
#define GLX_BUFFER_SIZE 2
|
||||
#define GLX_BufferSwapComplete 1
|
||||
#define GLX_COLOR_INDEX_BIT 0x00000002
|
||||
#define GLX_COLOR_INDEX_TYPE 0x8015
|
||||
#define GLX_CONFIG_CAVEAT 0x20
|
||||
#define GLX_DAMAGED 0x8020
|
||||
#define GLX_DEPTH_BUFFER_BIT 0x00000020
|
||||
#define GLX_DEPTH_SIZE 12
|
||||
#define GLX_DIRECT_COLOR 0x8003
|
||||
#define GLX_DONT_CARE 0xFFFFFFFF
|
||||
#define GLX_DOUBLEBUFFER 5
|
||||
#define GLX_DRAWABLE_TYPE 0x8010
|
||||
#define GLX_EVENT_MASK 0x801F
|
||||
#define GLX_EXTENSIONS 0x3
|
||||
#define GLX_EXTENSION_NAME "GLX"
|
||||
#define GLX_FBCONFIG_ID 0x8013
|
||||
#define GLX_FRONT_LEFT_BUFFER_BIT 0x00000001
|
||||
#define GLX_FRONT_RIGHT_BUFFER_BIT 0x00000002
|
||||
#define GLX_GRAY_SCALE 0x8006
|
||||
#define GLX_GREEN_SIZE 9
|
||||
#define GLX_HEIGHT 0x801E
|
||||
#define GLX_LARGEST_PBUFFER 0x801C
|
||||
#define GLX_LEVEL 3
|
||||
#define GLX_MAX_PBUFFER_HEIGHT 0x8017
|
||||
#define GLX_MAX_PBUFFER_PIXELS 0x8018
|
||||
#define GLX_MAX_PBUFFER_WIDTH 0x8016
|
||||
#define GLX_NONE 0x8000
|
||||
#define GLX_NON_CONFORMANT_CONFIG 0x800D
|
||||
#define GLX_NO_EXTENSION 3
|
||||
#define GLX_PBUFFER 0x8023
|
||||
#define GLX_PBUFFER_BIT 0x00000004
|
||||
#define GLX_PBUFFER_CLOBBER_MASK 0x08000000
|
||||
#define GLX_PBUFFER_HEIGHT 0x8040
|
||||
#define GLX_PBUFFER_WIDTH 0x8041
|
||||
#define GLX_PIXMAP_BIT 0x00000002
|
||||
#define GLX_PRESERVED_CONTENTS 0x801B
|
||||
#define GLX_PSEUDO_COLOR 0x8004
|
||||
#define GLX_PbufferClobber 0
|
||||
#define GLX_RED_SIZE 8
|
||||
#define GLX_RENDER_TYPE 0x8011
|
||||
#define GLX_RGBA 4
|
||||
#define GLX_RGBA_BIT 0x00000001
|
||||
#define GLX_RGBA_TYPE 0x8014
|
||||
#define GLX_SAMPLES 100001
|
||||
#define GLX_SAMPLE_BUFFERS 100000
|
||||
#define GLX_SAVED 0x8021
|
||||
#define GLX_SCREEN 0x800C
|
||||
#define GLX_SLOW_CONFIG 0x8001
|
||||
#define GLX_STATIC_COLOR 0x8005
|
||||
#define GLX_STATIC_GRAY 0x8007
|
||||
#define GLX_STENCIL_BUFFER_BIT 0x00000040
|
||||
#define GLX_STENCIL_SIZE 13
|
||||
#define GLX_STEREO 6
|
||||
#define GLX_TRANSPARENT_ALPHA_VALUE 0x28
|
||||
#define GLX_TRANSPARENT_BLUE_VALUE 0x27
|
||||
#define GLX_TRANSPARENT_GREEN_VALUE 0x26
|
||||
#define GLX_TRANSPARENT_INDEX 0x8009
|
||||
#define GLX_TRANSPARENT_INDEX_VALUE 0x24
|
||||
#define GLX_TRANSPARENT_RED_VALUE 0x25
|
||||
#define GLX_TRANSPARENT_RGB 0x8008
|
||||
#define GLX_TRANSPARENT_TYPE 0x23
|
||||
#define GLX_TRUE_COLOR 0x8002
|
||||
#define GLX_USE_GL 1
|
||||
#define GLX_VENDOR 0x1
|
||||
#define GLX_VERSION 0x2
|
||||
#define GLX_VISUAL_ID 0x800B
|
||||
#define GLX_WIDTH 0x801D
|
||||
#define GLX_WINDOW 0x8022
|
||||
#define GLX_WINDOW_BIT 0x00000001
|
||||
#define GLX_X_RENDERABLE 0x8012
|
||||
#define GLX_X_VISUAL_TYPE 0x22
|
||||
#define __GLX_NUMBER_EVENTS 17
|
||||
|
||||
|
||||
#ifndef GLEXT_64_TYPES_DEFINED
|
||||
/* This code block is duplicated in glext.h, so must be protected */
|
||||
#define GLEXT_64_TYPES_DEFINED
|
||||
/* Define int32_t, int64_t, and uint64_t types for UST/MSC */
|
||||
/* (as used in the GLX_OML_sync_control extension). */
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
||||
#include <inttypes.h>
|
||||
#elif defined(__sun__) || defined(__digital__)
|
||||
#include <inttypes.h>
|
||||
#if defined(__STDC__)
|
||||
#if defined(__arch64__) || defined(_LP64)
|
||||
typedef long int int64_t;
|
||||
typedef unsigned long int uint64_t;
|
||||
#else
|
||||
typedef long long int int64_t;
|
||||
typedef unsigned long long int uint64_t;
|
||||
#endif /* __arch64__ */
|
||||
#endif /* __STDC__ */
|
||||
#elif defined( __VMS ) || defined(__sgi)
|
||||
#include <inttypes.h>
|
||||
#elif defined(__SCO__) || defined(__USLC__)
|
||||
#include <stdint.h>
|
||||
#elif defined(__UNIXOS2__) || defined(__SOL64__)
|
||||
typedef long int int32_t;
|
||||
typedef long long int int64_t;
|
||||
typedef unsigned long long int uint64_t;
|
||||
#elif defined(_WIN32) && defined(__GNUC__)
|
||||
#include <stdint.h>
|
||||
#elif defined(_WIN32)
|
||||
typedef __int32 int32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
/* Fallback if nothing above works */
|
||||
#include <inttypes.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > 1060)
|
||||
|
||||
#else
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ > 1060)
|
||||
|
||||
#else
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef XID GLXFBConfigID;
|
||||
typedef struct __GLXFBConfigRec *GLXFBConfig;
|
||||
typedef XID GLXContextID;
|
||||
typedef struct __GLXcontextRec *GLXContext;
|
||||
typedef XID GLXPixmap;
|
||||
typedef XID GLXDrawable;
|
||||
typedef XID GLXWindow;
|
||||
typedef XID GLXPbuffer;
|
||||
typedef void (GLAD_API_PTR *__GLXextFuncPtr)(void);
|
||||
typedef XID GLXVideoCaptureDeviceNV;
|
||||
typedef unsigned int GLXVideoDeviceNV;
|
||||
typedef XID GLXVideoSourceSGIX;
|
||||
typedef XID GLXFBConfigIDSGIX;
|
||||
typedef struct __GLXFBConfigRec *GLXFBConfigSGIX;
|
||||
typedef XID GLXPbufferSGIX;
|
||||
typedef struct {
|
||||
int event_type; /* GLX_DAMAGED or GLX_SAVED */
|
||||
int draw_type; /* GLX_WINDOW or GLX_PBUFFER */
|
||||
unsigned long serial; /* # of last request processed by server */
|
||||
Bool send_event; /* true if this came for SendEvent request */
|
||||
Display *display; /* display the event was read from */
|
||||
GLXDrawable drawable; /* XID of Drawable */
|
||||
unsigned int buffer_mask; /* mask indicating which buffers are affected */
|
||||
unsigned int aux_buffer; /* which aux buffer was affected */
|
||||
int x, y;
|
||||
int width, height;
|
||||
int count; /* if nonzero, at least this many more */
|
||||
} GLXPbufferClobberEvent;
|
||||
typedef struct {
|
||||
int type;
|
||||
unsigned long serial; /* # of last request processed by server */
|
||||
Bool send_event; /* true if this came from a SendEvent request */
|
||||
Display *display; /* Display the event was read from */
|
||||
GLXDrawable drawable; /* drawable on which event was requested in event mask */
|
||||
int event_type;
|
||||
int64_t ust;
|
||||
int64_t msc;
|
||||
int64_t sbc;
|
||||
} GLXBufferSwapComplete;
|
||||
typedef union __GLXEvent {
|
||||
GLXPbufferClobberEvent glxpbufferclobber;
|
||||
GLXBufferSwapComplete glxbufferswapcomplete;
|
||||
long pad[24];
|
||||
} GLXEvent;
|
||||
typedef struct {
|
||||
int type;
|
||||
unsigned long serial;
|
||||
Bool send_event;
|
||||
Display *display;
|
||||
int extension;
|
||||
int evtype;
|
||||
GLXDrawable window;
|
||||
Bool stereo_tree;
|
||||
} GLXStereoNotifyEventEXT;
|
||||
typedef struct {
|
||||
int type;
|
||||
unsigned long serial; /* # of last request processed by server */
|
||||
Bool send_event; /* true if this came for SendEvent request */
|
||||
Display *display; /* display the event was read from */
|
||||
GLXDrawable drawable; /* i.d. of Drawable */
|
||||
int event_type; /* GLX_DAMAGED_SGIX or GLX_SAVED_SGIX */
|
||||
int draw_type; /* GLX_WINDOW_SGIX or GLX_PBUFFER_SGIX */
|
||||
unsigned int mask; /* mask indicating which buffers are affected*/
|
||||
int x, y;
|
||||
int width, height;
|
||||
int count; /* if nonzero, at least this many more */
|
||||
} GLXBufferClobberEventSGIX;
|
||||
typedef struct {
|
||||
char pipeName[80]; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
|
||||
int networkId;
|
||||
} GLXHyperpipeNetworkSGIX;
|
||||
typedef struct {
|
||||
char pipeName[80]; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
|
||||
int channel;
|
||||
unsigned int participationType;
|
||||
int timeSlice;
|
||||
} GLXHyperpipeConfigSGIX;
|
||||
typedef struct {
|
||||
char pipeName[80]; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
|
||||
int srcXOrigin, srcYOrigin, srcWidth, srcHeight;
|
||||
int destXOrigin, destYOrigin, destWidth, destHeight;
|
||||
} GLXPipeRect;
|
||||
typedef struct {
|
||||
char pipeName[80]; /* Should be [GLX_HYPERPIPE_PIPE_NAME_LENGTH_SGIX] */
|
||||
int XOrigin, YOrigin, maxHeight, maxWidth;
|
||||
} GLXPipeRectLimits;
|
||||
|
||||
|
||||
#define GLX_VERSION_1_0 1
|
||||
GLAD_API_CALL int GLAD_GLX_VERSION_1_0;
|
||||
#define GLX_VERSION_1_1 1
|
||||
GLAD_API_CALL int GLAD_GLX_VERSION_1_1;
|
||||
#define GLX_VERSION_1_2 1
|
||||
GLAD_API_CALL int GLAD_GLX_VERSION_1_2;
|
||||
#define GLX_VERSION_1_3 1
|
||||
GLAD_API_CALL int GLAD_GLX_VERSION_1_3;
|
||||
#define GLX_VERSION_1_4 1
|
||||
GLAD_API_CALL int GLAD_GLX_VERSION_1_4;
|
||||
|
||||
|
||||
typedef GLXFBConfig * (GLAD_API_PTR *PFNGLXCHOOSEFBCONFIGPROC)(Display * dpy, int screen, const int * attrib_list, int * nelements);
|
||||
typedef XVisualInfo * (GLAD_API_PTR *PFNGLXCHOOSEVISUALPROC)(Display * dpy, int screen, int * attribList);
|
||||
typedef void (GLAD_API_PTR *PFNGLXCOPYCONTEXTPROC)(Display * dpy, GLXContext src, GLXContext dst, unsigned long mask);
|
||||
typedef GLXContext (GLAD_API_PTR *PFNGLXCREATECONTEXTPROC)(Display * dpy, XVisualInfo * vis, GLXContext shareList, Bool direct);
|
||||
typedef GLXPixmap (GLAD_API_PTR *PFNGLXCREATEGLXPIXMAPPROC)(Display * dpy, XVisualInfo * visual, Pixmap pixmap);
|
||||
typedef GLXContext (GLAD_API_PTR *PFNGLXCREATENEWCONTEXTPROC)(Display * dpy, GLXFBConfig config, int render_type, GLXContext share_list, Bool direct);
|
||||
typedef GLXPbuffer (GLAD_API_PTR *PFNGLXCREATEPBUFFERPROC)(Display * dpy, GLXFBConfig config, const int * attrib_list);
|
||||
typedef GLXPixmap (GLAD_API_PTR *PFNGLXCREATEPIXMAPPROC)(Display * dpy, GLXFBConfig config, Pixmap pixmap, const int * attrib_list);
|
||||
typedef GLXWindow (GLAD_API_PTR *PFNGLXCREATEWINDOWPROC)(Display * dpy, GLXFBConfig config, Window win, const int * attrib_list);
|
||||
typedef void (GLAD_API_PTR *PFNGLXDESTROYCONTEXTPROC)(Display * dpy, GLXContext ctx);
|
||||
typedef void (GLAD_API_PTR *PFNGLXDESTROYGLXPIXMAPPROC)(Display * dpy, GLXPixmap pixmap);
|
||||
typedef void (GLAD_API_PTR *PFNGLXDESTROYPBUFFERPROC)(Display * dpy, GLXPbuffer pbuf);
|
||||
typedef void (GLAD_API_PTR *PFNGLXDESTROYPIXMAPPROC)(Display * dpy, GLXPixmap pixmap);
|
||||
typedef void (GLAD_API_PTR *PFNGLXDESTROYWINDOWPROC)(Display * dpy, GLXWindow win);
|
||||
typedef const char * (GLAD_API_PTR *PFNGLXGETCLIENTSTRINGPROC)(Display * dpy, int name);
|
||||
typedef int (GLAD_API_PTR *PFNGLXGETCONFIGPROC)(Display * dpy, XVisualInfo * visual, int attrib, int * value);
|
||||
typedef GLXContext (GLAD_API_PTR *PFNGLXGETCURRENTCONTEXTPROC)(void);
|
||||
typedef Display * (GLAD_API_PTR *PFNGLXGETCURRENTDISPLAYPROC)(void);
|
||||
typedef GLXDrawable (GLAD_API_PTR *PFNGLXGETCURRENTDRAWABLEPROC)(void);
|
||||
typedef GLXDrawable (GLAD_API_PTR *PFNGLXGETCURRENTREADDRAWABLEPROC)(void);
|
||||
typedef int (GLAD_API_PTR *PFNGLXGETFBCONFIGATTRIBPROC)(Display * dpy, GLXFBConfig config, int attribute, int * value);
|
||||
typedef GLXFBConfig * (GLAD_API_PTR *PFNGLXGETFBCONFIGSPROC)(Display * dpy, int screen, int * nelements);
|
||||
typedef __GLXextFuncPtr (GLAD_API_PTR *PFNGLXGETPROCADDRESSPROC)(const GLubyte * procName);
|
||||
typedef void (GLAD_API_PTR *PFNGLXGETSELECTEDEVENTPROC)(Display * dpy, GLXDrawable draw, unsigned long * event_mask);
|
||||
typedef XVisualInfo * (GLAD_API_PTR *PFNGLXGETVISUALFROMFBCONFIGPROC)(Display * dpy, GLXFBConfig config);
|
||||
typedef Bool (GLAD_API_PTR *PFNGLXISDIRECTPROC)(Display * dpy, GLXContext ctx);
|
||||
typedef Bool (GLAD_API_PTR *PFNGLXMAKECONTEXTCURRENTPROC)(Display * dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx);
|
||||
typedef Bool (GLAD_API_PTR *PFNGLXMAKECURRENTPROC)(Display * dpy, GLXDrawable drawable, GLXContext ctx);
|
||||
typedef int (GLAD_API_PTR *PFNGLXQUERYCONTEXTPROC)(Display * dpy, GLXContext ctx, int attribute, int * value);
|
||||
typedef void (GLAD_API_PTR *PFNGLXQUERYDRAWABLEPROC)(Display * dpy, GLXDrawable draw, int attribute, unsigned int * value);
|
||||
typedef Bool (GLAD_API_PTR *PFNGLXQUERYEXTENSIONPROC)(Display * dpy, int * errorb, int * event);
|
||||
typedef const char * (GLAD_API_PTR *PFNGLXQUERYEXTENSIONSSTRINGPROC)(Display * dpy, int screen);
|
||||
typedef const char * (GLAD_API_PTR *PFNGLXQUERYSERVERSTRINGPROC)(Display * dpy, int screen, int name);
|
||||
typedef Bool (GLAD_API_PTR *PFNGLXQUERYVERSIONPROC)(Display * dpy, int * maj, int * min);
|
||||
typedef void (GLAD_API_PTR *PFNGLXSELECTEVENTPROC)(Display * dpy, GLXDrawable draw, unsigned long event_mask);
|
||||
typedef void (GLAD_API_PTR *PFNGLXSWAPBUFFERSPROC)(Display * dpy, GLXDrawable drawable);
|
||||
typedef void (GLAD_API_PTR *PFNGLXUSEXFONTPROC)(Font font, int first, int count, int list);
|
||||
typedef void (GLAD_API_PTR *PFNGLXWAITGLPROC)(void);
|
||||
typedef void (GLAD_API_PTR *PFNGLXWAITXPROC)(void);
|
||||
|
||||
GLAD_API_CALL PFNGLXCHOOSEFBCONFIGPROC glad_glXChooseFBConfig;
|
||||
#define glXChooseFBConfig glad_glXChooseFBConfig
|
||||
GLAD_API_CALL PFNGLXCHOOSEVISUALPROC glad_glXChooseVisual;
|
||||
#define glXChooseVisual glad_glXChooseVisual
|
||||
GLAD_API_CALL PFNGLXCOPYCONTEXTPROC glad_glXCopyContext;
|
||||
#define glXCopyContext glad_glXCopyContext
|
||||
GLAD_API_CALL PFNGLXCREATECONTEXTPROC glad_glXCreateContext;
|
||||
#define glXCreateContext glad_glXCreateContext
|
||||
GLAD_API_CALL PFNGLXCREATEGLXPIXMAPPROC glad_glXCreateGLXPixmap;
|
||||
#define glXCreateGLXPixmap glad_glXCreateGLXPixmap
|
||||
GLAD_API_CALL PFNGLXCREATENEWCONTEXTPROC glad_glXCreateNewContext;
|
||||
#define glXCreateNewContext glad_glXCreateNewContext
|
||||
GLAD_API_CALL PFNGLXCREATEPBUFFERPROC glad_glXCreatePbuffer;
|
||||
#define glXCreatePbuffer glad_glXCreatePbuffer
|
||||
GLAD_API_CALL PFNGLXCREATEPIXMAPPROC glad_glXCreatePixmap;
|
||||
#define glXCreatePixmap glad_glXCreatePixmap
|
||||
GLAD_API_CALL PFNGLXCREATEWINDOWPROC glad_glXCreateWindow;
|
||||
#define glXCreateWindow glad_glXCreateWindow
|
||||
GLAD_API_CALL PFNGLXDESTROYCONTEXTPROC glad_glXDestroyContext;
|
||||
#define glXDestroyContext glad_glXDestroyContext
|
||||
GLAD_API_CALL PFNGLXDESTROYGLXPIXMAPPROC glad_glXDestroyGLXPixmap;
|
||||
#define glXDestroyGLXPixmap glad_glXDestroyGLXPixmap
|
||||
GLAD_API_CALL PFNGLXDESTROYPBUFFERPROC glad_glXDestroyPbuffer;
|
||||
#define glXDestroyPbuffer glad_glXDestroyPbuffer
|
||||
GLAD_API_CALL PFNGLXDESTROYPIXMAPPROC glad_glXDestroyPixmap;
|
||||
#define glXDestroyPixmap glad_glXDestroyPixmap
|
||||
GLAD_API_CALL PFNGLXDESTROYWINDOWPROC glad_glXDestroyWindow;
|
||||
#define glXDestroyWindow glad_glXDestroyWindow
|
||||
GLAD_API_CALL PFNGLXGETCLIENTSTRINGPROC glad_glXGetClientString;
|
||||
#define glXGetClientString glad_glXGetClientString
|
||||
GLAD_API_CALL PFNGLXGETCONFIGPROC glad_glXGetConfig;
|
||||
#define glXGetConfig glad_glXGetConfig
|
||||
GLAD_API_CALL PFNGLXGETCURRENTCONTEXTPROC glad_glXGetCurrentContext;
|
||||
#define glXGetCurrentContext glad_glXGetCurrentContext
|
||||
GLAD_API_CALL PFNGLXGETCURRENTDISPLAYPROC glad_glXGetCurrentDisplay;
|
||||
#define glXGetCurrentDisplay glad_glXGetCurrentDisplay
|
||||
GLAD_API_CALL PFNGLXGETCURRENTDRAWABLEPROC glad_glXGetCurrentDrawable;
|
||||
#define glXGetCurrentDrawable glad_glXGetCurrentDrawable
|
||||
GLAD_API_CALL PFNGLXGETCURRENTREADDRAWABLEPROC glad_glXGetCurrentReadDrawable;
|
||||
#define glXGetCurrentReadDrawable glad_glXGetCurrentReadDrawable
|
||||
GLAD_API_CALL PFNGLXGETFBCONFIGATTRIBPROC glad_glXGetFBConfigAttrib;
|
||||
#define glXGetFBConfigAttrib glad_glXGetFBConfigAttrib
|
||||
GLAD_API_CALL PFNGLXGETFBCONFIGSPROC glad_glXGetFBConfigs;
|
||||
#define glXGetFBConfigs glad_glXGetFBConfigs
|
||||
GLAD_API_CALL PFNGLXGETPROCADDRESSPROC glad_glXGetProcAddress;
|
||||
#define glXGetProcAddress glad_glXGetProcAddress
|
||||
GLAD_API_CALL PFNGLXGETSELECTEDEVENTPROC glad_glXGetSelectedEvent;
|
||||
#define glXGetSelectedEvent glad_glXGetSelectedEvent
|
||||
GLAD_API_CALL PFNGLXGETVISUALFROMFBCONFIGPROC glad_glXGetVisualFromFBConfig;
|
||||
#define glXGetVisualFromFBConfig glad_glXGetVisualFromFBConfig
|
||||
GLAD_API_CALL PFNGLXISDIRECTPROC glad_glXIsDirect;
|
||||
#define glXIsDirect glad_glXIsDirect
|
||||
GLAD_API_CALL PFNGLXMAKECONTEXTCURRENTPROC glad_glXMakeContextCurrent;
|
||||
#define glXMakeContextCurrent glad_glXMakeContextCurrent
|
||||
GLAD_API_CALL PFNGLXMAKECURRENTPROC glad_glXMakeCurrent;
|
||||
#define glXMakeCurrent glad_glXMakeCurrent
|
||||
GLAD_API_CALL PFNGLXQUERYCONTEXTPROC glad_glXQueryContext;
|
||||
#define glXQueryContext glad_glXQueryContext
|
||||
GLAD_API_CALL PFNGLXQUERYDRAWABLEPROC glad_glXQueryDrawable;
|
||||
#define glXQueryDrawable glad_glXQueryDrawable
|
||||
GLAD_API_CALL PFNGLXQUERYEXTENSIONPROC glad_glXQueryExtension;
|
||||
#define glXQueryExtension glad_glXQueryExtension
|
||||
GLAD_API_CALL PFNGLXQUERYEXTENSIONSSTRINGPROC glad_glXQueryExtensionsString;
|
||||
#define glXQueryExtensionsString glad_glXQueryExtensionsString
|
||||
GLAD_API_CALL PFNGLXQUERYSERVERSTRINGPROC glad_glXQueryServerString;
|
||||
#define glXQueryServerString glad_glXQueryServerString
|
||||
GLAD_API_CALL PFNGLXQUERYVERSIONPROC glad_glXQueryVersion;
|
||||
#define glXQueryVersion glad_glXQueryVersion
|
||||
GLAD_API_CALL PFNGLXSELECTEVENTPROC glad_glXSelectEvent;
|
||||
#define glXSelectEvent glad_glXSelectEvent
|
||||
GLAD_API_CALL PFNGLXSWAPBUFFERSPROC glad_glXSwapBuffers;
|
||||
#define glXSwapBuffers glad_glXSwapBuffers
|
||||
GLAD_API_CALL PFNGLXUSEXFONTPROC glad_glXUseXFont;
|
||||
#define glXUseXFont glad_glXUseXFont
|
||||
GLAD_API_CALL PFNGLXWAITGLPROC glad_glXWaitGL;
|
||||
#define glXWaitGL glad_glXWaitGL
|
||||
GLAD_API_CALL PFNGLXWAITXPROC glad_glXWaitX;
|
||||
#define glXWaitX glad_glXWaitX
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
GLAD_API_CALL int gladLoadGLXUserPtr(Display *display, int screen, GLADuserptrloadfunc load, void *userptr);
|
||||
GLAD_API_CALL int gladLoadGLX(Display *display, int screen, GLADloadfunc load);
|
||||
|
||||
#ifdef GLAD_GLX
|
||||
|
||||
GLAD_API_CALL int gladLoaderLoadGLX(Display *display, int screen);
|
||||
|
||||
GLAD_API_CALL void gladLoaderUnloadGLX(void);
|
||||
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
1786
mons_3d/external/glad/src/gl.c
vendored
Normal file
1786
mons_3d/external/glad/src/gl.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
352
mons_3d/external/glad/src/glx.c
vendored
Normal file
352
mons_3d/external/glad/src/glx.c
vendored
Normal file
@ -0,0 +1,352 @@
|
||||
/**
|
||||
* SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <glad/glx.h>
|
||||
|
||||
#ifndef GLAD_IMPL_UTIL_C_
|
||||
#define GLAD_IMPL_UTIL_C_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define GLAD_IMPL_UTIL_SSCANF sscanf_s
|
||||
#else
|
||||
#define GLAD_IMPL_UTIL_SSCANF sscanf
|
||||
#endif
|
||||
|
||||
#endif /* GLAD_IMPL_UTIL_C_ */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
int GLAD_GLX_VERSION_1_0 = 0;
|
||||
int GLAD_GLX_VERSION_1_1 = 0;
|
||||
int GLAD_GLX_VERSION_1_2 = 0;
|
||||
int GLAD_GLX_VERSION_1_3 = 0;
|
||||
int GLAD_GLX_VERSION_1_4 = 0;
|
||||
|
||||
|
||||
|
||||
PFNGLXCHOOSEFBCONFIGPROC glad_glXChooseFBConfig = NULL;
|
||||
PFNGLXCHOOSEVISUALPROC glad_glXChooseVisual = NULL;
|
||||
PFNGLXCOPYCONTEXTPROC glad_glXCopyContext = NULL;
|
||||
PFNGLXCREATECONTEXTPROC glad_glXCreateContext = NULL;
|
||||
PFNGLXCREATEGLXPIXMAPPROC glad_glXCreateGLXPixmap = NULL;
|
||||
PFNGLXCREATENEWCONTEXTPROC glad_glXCreateNewContext = NULL;
|
||||
PFNGLXCREATEPBUFFERPROC glad_glXCreatePbuffer = NULL;
|
||||
PFNGLXCREATEPIXMAPPROC glad_glXCreatePixmap = NULL;
|
||||
PFNGLXCREATEWINDOWPROC glad_glXCreateWindow = NULL;
|
||||
PFNGLXDESTROYCONTEXTPROC glad_glXDestroyContext = NULL;
|
||||
PFNGLXDESTROYGLXPIXMAPPROC glad_glXDestroyGLXPixmap = NULL;
|
||||
PFNGLXDESTROYPBUFFERPROC glad_glXDestroyPbuffer = NULL;
|
||||
PFNGLXDESTROYPIXMAPPROC glad_glXDestroyPixmap = NULL;
|
||||
PFNGLXDESTROYWINDOWPROC glad_glXDestroyWindow = NULL;
|
||||
PFNGLXGETCLIENTSTRINGPROC glad_glXGetClientString = NULL;
|
||||
PFNGLXGETCONFIGPROC glad_glXGetConfig = NULL;
|
||||
PFNGLXGETCURRENTCONTEXTPROC glad_glXGetCurrentContext = NULL;
|
||||
PFNGLXGETCURRENTDISPLAYPROC glad_glXGetCurrentDisplay = NULL;
|
||||
PFNGLXGETCURRENTDRAWABLEPROC glad_glXGetCurrentDrawable = NULL;
|
||||
PFNGLXGETCURRENTREADDRAWABLEPROC glad_glXGetCurrentReadDrawable = NULL;
|
||||
PFNGLXGETFBCONFIGATTRIBPROC glad_glXGetFBConfigAttrib = NULL;
|
||||
PFNGLXGETFBCONFIGSPROC glad_glXGetFBConfigs = NULL;
|
||||
PFNGLXGETPROCADDRESSPROC glad_glXGetProcAddress = NULL;
|
||||
PFNGLXGETSELECTEDEVENTPROC glad_glXGetSelectedEvent = NULL;
|
||||
PFNGLXGETVISUALFROMFBCONFIGPROC glad_glXGetVisualFromFBConfig = NULL;
|
||||
PFNGLXISDIRECTPROC glad_glXIsDirect = NULL;
|
||||
PFNGLXMAKECONTEXTCURRENTPROC glad_glXMakeContextCurrent = NULL;
|
||||
PFNGLXMAKECURRENTPROC glad_glXMakeCurrent = NULL;
|
||||
PFNGLXQUERYCONTEXTPROC glad_glXQueryContext = NULL;
|
||||
PFNGLXQUERYDRAWABLEPROC glad_glXQueryDrawable = NULL;
|
||||
PFNGLXQUERYEXTENSIONPROC glad_glXQueryExtension = NULL;
|
||||
PFNGLXQUERYEXTENSIONSSTRINGPROC glad_glXQueryExtensionsString = NULL;
|
||||
PFNGLXQUERYSERVERSTRINGPROC glad_glXQueryServerString = NULL;
|
||||
PFNGLXQUERYVERSIONPROC glad_glXQueryVersion = NULL;
|
||||
PFNGLXSELECTEVENTPROC glad_glXSelectEvent = NULL;
|
||||
PFNGLXSWAPBUFFERSPROC glad_glXSwapBuffers = NULL;
|
||||
PFNGLXUSEXFONTPROC glad_glXUseXFont = NULL;
|
||||
PFNGLXWAITGLPROC glad_glXWaitGL = NULL;
|
||||
PFNGLXWAITXPROC glad_glXWaitX = NULL;
|
||||
|
||||
|
||||
static void glad_glx_load_GLX_VERSION_1_0( GLADuserptrloadfunc load, void* userptr) {
|
||||
if(!GLAD_GLX_VERSION_1_0) return;
|
||||
glad_glXChooseVisual = (PFNGLXCHOOSEVISUALPROC) load(userptr, "glXChooseVisual");
|
||||
glad_glXCopyContext = (PFNGLXCOPYCONTEXTPROC) load(userptr, "glXCopyContext");
|
||||
glad_glXCreateContext = (PFNGLXCREATECONTEXTPROC) load(userptr, "glXCreateContext");
|
||||
glad_glXCreateGLXPixmap = (PFNGLXCREATEGLXPIXMAPPROC) load(userptr, "glXCreateGLXPixmap");
|
||||
glad_glXDestroyContext = (PFNGLXDESTROYCONTEXTPROC) load(userptr, "glXDestroyContext");
|
||||
glad_glXDestroyGLXPixmap = (PFNGLXDESTROYGLXPIXMAPPROC) load(userptr, "glXDestroyGLXPixmap");
|
||||
glad_glXGetConfig = (PFNGLXGETCONFIGPROC) load(userptr, "glXGetConfig");
|
||||
glad_glXGetCurrentContext = (PFNGLXGETCURRENTCONTEXTPROC) load(userptr, "glXGetCurrentContext");
|
||||
glad_glXGetCurrentDrawable = (PFNGLXGETCURRENTDRAWABLEPROC) load(userptr, "glXGetCurrentDrawable");
|
||||
glad_glXIsDirect = (PFNGLXISDIRECTPROC) load(userptr, "glXIsDirect");
|
||||
glad_glXMakeCurrent = (PFNGLXMAKECURRENTPROC) load(userptr, "glXMakeCurrent");
|
||||
glad_glXQueryExtension = (PFNGLXQUERYEXTENSIONPROC) load(userptr, "glXQueryExtension");
|
||||
glad_glXQueryVersion = (PFNGLXQUERYVERSIONPROC) load(userptr, "glXQueryVersion");
|
||||
glad_glXSwapBuffers = (PFNGLXSWAPBUFFERSPROC) load(userptr, "glXSwapBuffers");
|
||||
glad_glXUseXFont = (PFNGLXUSEXFONTPROC) load(userptr, "glXUseXFont");
|
||||
glad_glXWaitGL = (PFNGLXWAITGLPROC) load(userptr, "glXWaitGL");
|
||||
glad_glXWaitX = (PFNGLXWAITXPROC) load(userptr, "glXWaitX");
|
||||
}
|
||||
static void glad_glx_load_GLX_VERSION_1_1( GLADuserptrloadfunc load, void* userptr) {
|
||||
if(!GLAD_GLX_VERSION_1_1) return;
|
||||
glad_glXGetClientString = (PFNGLXGETCLIENTSTRINGPROC) load(userptr, "glXGetClientString");
|
||||
glad_glXQueryExtensionsString = (PFNGLXQUERYEXTENSIONSSTRINGPROC) load(userptr, "glXQueryExtensionsString");
|
||||
glad_glXQueryServerString = (PFNGLXQUERYSERVERSTRINGPROC) load(userptr, "glXQueryServerString");
|
||||
}
|
||||
static void glad_glx_load_GLX_VERSION_1_2( GLADuserptrloadfunc load, void* userptr) {
|
||||
if(!GLAD_GLX_VERSION_1_2) return;
|
||||
glad_glXGetCurrentDisplay = (PFNGLXGETCURRENTDISPLAYPROC) load(userptr, "glXGetCurrentDisplay");
|
||||
}
|
||||
static void glad_glx_load_GLX_VERSION_1_3( GLADuserptrloadfunc load, void* userptr) {
|
||||
if(!GLAD_GLX_VERSION_1_3) return;
|
||||
glad_glXChooseFBConfig = (PFNGLXCHOOSEFBCONFIGPROC) load(userptr, "glXChooseFBConfig");
|
||||
glad_glXCreateNewContext = (PFNGLXCREATENEWCONTEXTPROC) load(userptr, "glXCreateNewContext");
|
||||
glad_glXCreatePbuffer = (PFNGLXCREATEPBUFFERPROC) load(userptr, "glXCreatePbuffer");
|
||||
glad_glXCreatePixmap = (PFNGLXCREATEPIXMAPPROC) load(userptr, "glXCreatePixmap");
|
||||
glad_glXCreateWindow = (PFNGLXCREATEWINDOWPROC) load(userptr, "glXCreateWindow");
|
||||
glad_glXDestroyPbuffer = (PFNGLXDESTROYPBUFFERPROC) load(userptr, "glXDestroyPbuffer");
|
||||
glad_glXDestroyPixmap = (PFNGLXDESTROYPIXMAPPROC) load(userptr, "glXDestroyPixmap");
|
||||
glad_glXDestroyWindow = (PFNGLXDESTROYWINDOWPROC) load(userptr, "glXDestroyWindow");
|
||||
glad_glXGetCurrentReadDrawable = (PFNGLXGETCURRENTREADDRAWABLEPROC) load(userptr, "glXGetCurrentReadDrawable");
|
||||
glad_glXGetFBConfigAttrib = (PFNGLXGETFBCONFIGATTRIBPROC) load(userptr, "glXGetFBConfigAttrib");
|
||||
glad_glXGetFBConfigs = (PFNGLXGETFBCONFIGSPROC) load(userptr, "glXGetFBConfigs");
|
||||
glad_glXGetSelectedEvent = (PFNGLXGETSELECTEDEVENTPROC) load(userptr, "glXGetSelectedEvent");
|
||||
glad_glXGetVisualFromFBConfig = (PFNGLXGETVISUALFROMFBCONFIGPROC) load(userptr, "glXGetVisualFromFBConfig");
|
||||
glad_glXMakeContextCurrent = (PFNGLXMAKECONTEXTCURRENTPROC) load(userptr, "glXMakeContextCurrent");
|
||||
glad_glXQueryContext = (PFNGLXQUERYCONTEXTPROC) load(userptr, "glXQueryContext");
|
||||
glad_glXQueryDrawable = (PFNGLXQUERYDRAWABLEPROC) load(userptr, "glXQueryDrawable");
|
||||
glad_glXSelectEvent = (PFNGLXSELECTEVENTPROC) load(userptr, "glXSelectEvent");
|
||||
}
|
||||
static void glad_glx_load_GLX_VERSION_1_4( GLADuserptrloadfunc load, void* userptr) {
|
||||
if(!GLAD_GLX_VERSION_1_4) return;
|
||||
glad_glXGetProcAddress = (PFNGLXGETPROCADDRESSPROC) load(userptr, "glXGetProcAddress");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int glad_glx_has_extension(Display *display, int screen, const char *ext) {
|
||||
#ifndef GLX_VERSION_1_1
|
||||
GLAD_UNUSED(display);
|
||||
GLAD_UNUSED(screen);
|
||||
GLAD_UNUSED(ext);
|
||||
#else
|
||||
const char *terminator;
|
||||
const char *loc;
|
||||
const char *extensions;
|
||||
|
||||
if (glXQueryExtensionsString == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
extensions = glXQueryExtensionsString(display, screen);
|
||||
|
||||
if(extensions == NULL || ext == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
loc = strstr(extensions, ext);
|
||||
if(loc == NULL)
|
||||
break;
|
||||
|
||||
terminator = loc + strlen(ext);
|
||||
if((loc == extensions || *(loc - 1) == ' ') &&
|
||||
(*terminator == ' ' || *terminator == '\0')) {
|
||||
return 1;
|
||||
}
|
||||
extensions = terminator;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static GLADapiproc glad_glx_get_proc_from_userptr(void *userptr, const char* name) {
|
||||
return (GLAD_GNUC_EXTENSION (GLADapiproc (*)(const char *name)) userptr)(name);
|
||||
}
|
||||
|
||||
static int glad_glx_find_extensions(Display *display, int screen) {
|
||||
GLAD_UNUSED(glad_glx_has_extension);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int glad_glx_find_core_glx(Display **display, int *screen) {
|
||||
int major = 0, minor = 0;
|
||||
if(*display == NULL) {
|
||||
#ifdef GLAD_GLX_NO_X11
|
||||
GLAD_UNUSED(screen);
|
||||
return 0;
|
||||
#else
|
||||
*display = XOpenDisplay(0);
|
||||
if (*display == NULL) {
|
||||
return 0;
|
||||
}
|
||||
*screen = XScreenNumberOfScreen(XDefaultScreenOfDisplay(*display));
|
||||
#endif
|
||||
}
|
||||
glXQueryVersion(*display, &major, &minor);
|
||||
GLAD_GLX_VERSION_1_0 = (major == 1 && minor >= 0) || major > 1;
|
||||
GLAD_GLX_VERSION_1_1 = (major == 1 && minor >= 1) || major > 1;
|
||||
GLAD_GLX_VERSION_1_2 = (major == 1 && minor >= 2) || major > 1;
|
||||
GLAD_GLX_VERSION_1_3 = (major == 1 && minor >= 3) || major > 1;
|
||||
GLAD_GLX_VERSION_1_4 = (major == 1 && minor >= 4) || major > 1;
|
||||
return GLAD_MAKE_VERSION(major, minor);
|
||||
}
|
||||
|
||||
int gladLoadGLXUserPtr(Display *display, int screen, GLADuserptrloadfunc load, void *userptr) {
|
||||
int version;
|
||||
glXQueryVersion = (PFNGLXQUERYVERSIONPROC) load(userptr, "glXQueryVersion");
|
||||
if(glXQueryVersion == NULL) return 0;
|
||||
version = glad_glx_find_core_glx(&display, &screen);
|
||||
|
||||
glad_glx_load_GLX_VERSION_1_0(load, userptr);
|
||||
glad_glx_load_GLX_VERSION_1_1(load, userptr);
|
||||
glad_glx_load_GLX_VERSION_1_2(load, userptr);
|
||||
glad_glx_load_GLX_VERSION_1_3(load, userptr);
|
||||
glad_glx_load_GLX_VERSION_1_4(load, userptr);
|
||||
|
||||
if (!glad_glx_find_extensions(display, screen)) return 0;
|
||||
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
int gladLoadGLX(Display *display, int screen, GLADloadfunc load) {
|
||||
return gladLoadGLXUserPtr(display, screen, glad_glx_get_proc_from_userptr, GLAD_GNUC_EXTENSION (void*) load);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef GLAD_GLX
|
||||
|
||||
#ifndef GLAD_LOADER_LIBRARY_C_
|
||||
#define GLAD_LOADER_LIBRARY_C_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if GLAD_PLATFORM_WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
|
||||
static void* glad_get_dlopen_handle(const char *lib_names[], int length) {
|
||||
void *handle = NULL;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < length; ++i) {
|
||||
#if GLAD_PLATFORM_WIN32
|
||||
#if GLAD_PLATFORM_UWP
|
||||
size_t buffer_size = (strlen(lib_names[i]) + 1) * sizeof(WCHAR);
|
||||
LPWSTR buffer = (LPWSTR) malloc(buffer_size);
|
||||
if (buffer != NULL) {
|
||||
int ret = MultiByteToWideChar(CP_ACP, 0, lib_names[i], -1, buffer, buffer_size);
|
||||
if (ret != 0) {
|
||||
handle = (void*) LoadPackagedLibrary(buffer, 0);
|
||||
}
|
||||
free((void*) buffer);
|
||||
}
|
||||
#else
|
||||
handle = (void*) LoadLibraryA(lib_names[i]);
|
||||
#endif
|
||||
#else
|
||||
handle = dlopen(lib_names[i], RTLD_LAZY | RTLD_LOCAL);
|
||||
#endif
|
||||
if (handle != NULL) {
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void glad_close_dlopen_handle(void* handle) {
|
||||
if (handle != NULL) {
|
||||
#if GLAD_PLATFORM_WIN32
|
||||
FreeLibrary((HMODULE) handle);
|
||||
#else
|
||||
dlclose(handle);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static GLADapiproc glad_dlsym_handle(void* handle, const char *name) {
|
||||
if (handle == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if GLAD_PLATFORM_WIN32
|
||||
return (GLADapiproc) GetProcAddress((HMODULE) handle, name);
|
||||
#else
|
||||
return GLAD_GNUC_EXTENSION (GLADapiproc) dlsym(handle, name);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* GLAD_LOADER_LIBRARY_C_ */
|
||||
|
||||
typedef void* (GLAD_API_PTR *GLADglxprocaddrfunc)(const char*);
|
||||
|
||||
static GLADapiproc glad_glx_get_proc(void *userptr, const char *name) {
|
||||
return GLAD_GNUC_EXTENSION ((GLADapiproc (*)(const char *name)) userptr)(name);
|
||||
}
|
||||
|
||||
static void* _glx_handle;
|
||||
|
||||
static void* glad_glx_dlopen_handle(void) {
|
||||
static const char *NAMES[] = {
|
||||
#if defined __CYGWIN__
|
||||
"libGL-1.so",
|
||||
#endif
|
||||
"libGL.so.1",
|
||||
"libGL.so"
|
||||
};
|
||||
|
||||
if (_glx_handle == NULL) {
|
||||
_glx_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
|
||||
}
|
||||
|
||||
return _glx_handle;
|
||||
}
|
||||
|
||||
int gladLoaderLoadGLX(Display *display, int screen) {
|
||||
int version = 0;
|
||||
void *handle = NULL;
|
||||
int did_load = 0;
|
||||
GLADglxprocaddrfunc loader;
|
||||
|
||||
did_load = _glx_handle == NULL;
|
||||
handle = glad_glx_dlopen_handle();
|
||||
if (handle != NULL) {
|
||||
loader = (GLADglxprocaddrfunc) glad_dlsym_handle(handle, "glXGetProcAddressARB");
|
||||
if (loader != NULL) {
|
||||
version = gladLoadGLXUserPtr(display, screen, glad_glx_get_proc, GLAD_GNUC_EXTENSION (void*) loader);
|
||||
}
|
||||
|
||||
if (!version && did_load) {
|
||||
gladLoaderUnloadGLX();
|
||||
}
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
|
||||
void gladLoaderUnloadGLX() {
|
||||
if (_glx_handle != NULL) {
|
||||
glad_close_dlopen_handle(_glx_handle);
|
||||
_glx_handle = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* GLAD_GLX */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
145
mons_3d/external/mikktspace/include/mikktspace.h
vendored
Normal file
145
mons_3d/external/mikktspace/include/mikktspace.h
vendored
Normal file
@ -0,0 +1,145 @@
|
||||
/** \file mikktspace/mikktspace.h
|
||||
* \ingroup mikktspace
|
||||
*/
|
||||
/**
|
||||
* Copyright (C) 2011 by Morten S. Mikkelsen
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef __MIKKTSPACE_H__
|
||||
#define __MIKKTSPACE_H__
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Author: Morten S. Mikkelsen
|
||||
* Version: 1.0
|
||||
*
|
||||
* The files mikktspace.h and mikktspace.c are designed to be
|
||||
* stand-alone files and it is important that they are kept this way.
|
||||
* Not having dependencies on structures/classes/libraries specific
|
||||
* to the program, in which they are used, allows them to be copied
|
||||
* and used as is into any tool, program or plugin.
|
||||
* The code is designed to consistently generate the same
|
||||
* tangent spaces, for a given mesh, in any tool in which it is used.
|
||||
* This is done by performing an internal welding step and subsequently an order-independent evaluation
|
||||
* of tangent space for meshes consisting of triangles and quads.
|
||||
* This means faces can be received in any order and the same is true for
|
||||
* the order of vertices of each face. The generated result will not be affected
|
||||
* by such reordering. Additionally, whether degenerate (vertices or texture coordinates)
|
||||
* primitives are present or not will not affect the generated results either.
|
||||
* Once tangent space calculation is done the vertices of degenerate primitives will simply
|
||||
* inherit tangent space from neighboring non degenerate primitives.
|
||||
* The analysis behind this implementation can be found in my master's thesis
|
||||
* which is available for download --> http://image.diku.dk/projects/media/morten.mikkelsen.08.pdf
|
||||
* Note that though the tangent spaces at the vertices are generated in an order-independent way,
|
||||
* by this implementation, the interpolated tangent space is still affected by which diagonal is
|
||||
* chosen to split each quad. A sensible solution is to have your tools pipeline always
|
||||
* split quads by the shortest diagonal. This choice is order-independent and works with mirroring.
|
||||
* If these have the same length then compare the diagonals defined by the texture coordinates.
|
||||
* XNormal which is a tool for baking normal maps allows you to write your own tangent space plugin
|
||||
* and also quad triangulator plugin.
|
||||
*/
|
||||
|
||||
|
||||
typedef int tbool;
|
||||
typedef struct SMikkTSpaceContext SMikkTSpaceContext;
|
||||
|
||||
typedef struct {
|
||||
// Returns the number of faces (triangles/quads) on the mesh to be processed.
|
||||
int (*m_getNumFaces)(const SMikkTSpaceContext * pContext);
|
||||
|
||||
// Returns the number of vertices on face number iFace
|
||||
// iFace is a number in the range {0, 1, ..., getNumFaces()-1}
|
||||
int (*m_getNumVerticesOfFace)(const SMikkTSpaceContext * pContext, const int iFace);
|
||||
|
||||
// returns the position/normal/texcoord of the referenced face of vertex number iVert.
|
||||
// iVert is in the range {0,1,2} for triangles and {0,1,2,3} for quads.
|
||||
void (*m_getPosition)(const SMikkTSpaceContext * pContext, float fvPosOut[], const int iFace, const int iVert);
|
||||
void (*m_getNormal)(const SMikkTSpaceContext * pContext, float fvNormOut[], const int iFace, const int iVert);
|
||||
void (*m_getTexCoord)(const SMikkTSpaceContext * pContext, float fvTexcOut[], const int iFace, const int iVert);
|
||||
|
||||
// either (or both) of the two setTSpace callbacks can be set.
|
||||
// The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
|
||||
|
||||
// This function is used to return the tangent and fSign to the application.
|
||||
// fvTangent is a unit length vector.
|
||||
// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
|
||||
// bitangent = fSign * cross(vN, tangent);
|
||||
// Note that the results are returned unindexed. It is possible to generate a new index list
|
||||
// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
|
||||
// DO NOT! use an already existing index list.
|
||||
void (*m_setTSpaceBasic)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert);
|
||||
|
||||
// This function is used to return tangent space results to the application.
|
||||
// fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
|
||||
// true magnitudes which can be used for relief mapping effects.
|
||||
// fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
|
||||
// However, both are perpendicular to the vertex normal.
|
||||
// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
|
||||
// fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
|
||||
// bitangent = fSign * cross(vN, tangent);
|
||||
// Note that the results are returned unindexed. It is possible to generate a new index list
|
||||
// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
|
||||
// DO NOT! use an already existing index list.
|
||||
void (*m_setTSpace)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
|
||||
const tbool bIsOrientationPreserving, const int iFace, const int iVert);
|
||||
} SMikkTSpaceInterface;
|
||||
|
||||
struct SMikkTSpaceContext
|
||||
{
|
||||
SMikkTSpaceInterface * m_pInterface; // initialized with callback functions
|
||||
void * m_pUserData; // pointer to client side mesh data etc. (passed as the first parameter with every interface call)
|
||||
};
|
||||
|
||||
// these are both thread safe!
|
||||
tbool genTangSpaceDefault(const SMikkTSpaceContext * pContext); // Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
|
||||
tbool genTangSpace(const SMikkTSpaceContext * pContext, const float fAngularThreshold);
|
||||
|
||||
|
||||
// To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal maps, the
|
||||
// normal map sampler must use the exact inverse of the pixel shader transformation.
|
||||
// The most efficient transformation we can possibly do in the pixel shader is
|
||||
// achieved by using, directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
|
||||
// pixel shader (fast transform out)
|
||||
// vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
|
||||
// where vNt is the tangent space normal. The normal map sampler must likewise use the
|
||||
// interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the pixel shader.
|
||||
// sampler does (exact inverse of pixel shader):
|
||||
// float3 row0 = cross(vB, vN);
|
||||
// float3 row1 = cross(vN, vT);
|
||||
// float3 row2 = cross(vT, vB);
|
||||
// float fSign = dot(vT, row0)<0 ? -1 : 1;
|
||||
// vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
|
||||
// where vNout is the sampled normal in some chosen 3D space.
|
||||
//
|
||||
// Should you choose to reconstruct the bitangent in the pixel shader instead
|
||||
// of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler also.
|
||||
// Finally, beware of quad triangulations. If the normal map sampler doesn't use the same triangulation of
|
||||
// quads as your renderer then problems will occur since the interpolated tangent spaces will differ
|
||||
// eventhough the vertex level tangent spaces match. This can be solved either by triangulating before
|
||||
// sampling/exporting or by using the order-independent choice of diagonal for splitting quads suggested earlier.
|
||||
// However, this must be used both by the sampler and your tools/rendering pipeline.
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
1899
mons_3d/external/mikktspace/src/mikktspace.c
vendored
Normal file
1899
mons_3d/external/mikktspace/src/mikktspace.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
12
mons_3d/include/camera.h
Normal file
12
mons_3d/include/camera.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef MONS_CAMERA_H
|
||||
#define MONS_CAMERA_H
|
||||
|
||||
#include "projection.h"
|
||||
#include "transform.h"
|
||||
|
||||
typedef struct mons_camera {
|
||||
mons_transform transform;
|
||||
mons_projection projection;
|
||||
} mons_camera;
|
||||
|
||||
#endif
|
12
mons_3d/include/color.h
Normal file
12
mons_3d/include/color.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef MONS_COLOR_H
|
||||
#define MONS_COLOR_H
|
||||
|
||||
#include "mons_math/vec4.h"
|
||||
|
||||
#define MONS_COLOR_BLACK (mons_vec4){0,0,0,1}
|
||||
#define MONS_COLOR_WHITE MONS_VEC4_ONE
|
||||
#define MONS_COLOR_RED (mons_vec4){1,0,0,1}
|
||||
#define MONS_COLOR_GREEN (mons_vec4){0,1,0,1}
|
||||
#define MONS_COLOR_BLUE (mons_vec4){0,0,1,1}
|
||||
|
||||
#endif
|
12
mons_3d/include/light.h
Normal file
12
mons_3d/include/light.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef MONS_LIGHT_H
|
||||
#define MONS_LIGHT_H
|
||||
|
||||
#include "transform.h"
|
||||
#include "mons_math/vec4.h"
|
||||
|
||||
typedef struct mons_directional_light {
|
||||
mons_transform transform;
|
||||
mons_vec4 color;
|
||||
} mons_directional_light;
|
||||
|
||||
#endif
|
14
mons_3d/include/mesh.h
Normal file
14
mons_3d/include/mesh.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef MONS_MESH_H
|
||||
#define MONS_MESH_H
|
||||
|
||||
struct mons_vertex;
|
||||
typedef unsigned int mons_vao;
|
||||
|
||||
typedef struct mons_mesh {
|
||||
mons_vao vao;
|
||||
unsigned int element_count;
|
||||
} mons_mesh;
|
||||
|
||||
mons_mesh mons_create_mesh(struct mons_vertex *vertex_array, int vertex_count, int *index_array, int index_count);
|
||||
|
||||
#endif
|
19
mons_3d/include/model.h
Normal file
19
mons_3d/include/model.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef MONS_MODEL_H
|
||||
#define MONS_MODEL_H
|
||||
|
||||
#include "mesh.h"
|
||||
#include "shader.h"
|
||||
#include "transform.h"
|
||||
#include "texture.h"
|
||||
|
||||
typedef struct mons_model {
|
||||
mons_mesh mesh;
|
||||
mons_program shader;
|
||||
mons_transform transform;
|
||||
mons_texture *textures;
|
||||
unsigned int textures_len;
|
||||
} mons_model;
|
||||
|
||||
void mons_model_draw(mons_model model);
|
||||
|
||||
#endif
|
13
mons_3d/include/projection.h
Normal file
13
mons_3d/include/projection.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef MONS_PROJECTION_H
|
||||
#define MONS_PROJECTION_H
|
||||
|
||||
typedef struct mons_projection {
|
||||
float near;
|
||||
float far;
|
||||
float fov;
|
||||
float aspect_ratio;
|
||||
} mons_projection;
|
||||
|
||||
struct mons_mat4 mons_projection_matrix(mons_projection projection);
|
||||
|
||||
#endif
|
35
mons_3d/include/shader.h
Normal file
35
mons_3d/include/shader.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef MONS_SHADER_H
|
||||
#define MONS_SHADER_H
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef unsigned int mons_shader;
|
||||
typedef unsigned int mons_program;
|
||||
|
||||
typedef enum mons_shader_type {
|
||||
MONS_SHADER_TYPE_VERTEX = GL_VERTEX_SHADER,
|
||||
MONS_SHADER_TYPE_FRAGMENT = GL_FRAGMENT_SHADER,
|
||||
} mons_shader_type;
|
||||
|
||||
struct mons_vec2;
|
||||
struct mons_vec3;
|
||||
struct mons_vec4;
|
||||
struct mons_mat4;
|
||||
|
||||
mons_shader mons_create_shader(mons_shader_type type, char *source, int len);
|
||||
mons_program mons_create_program(mons_shader vertex_shader, mons_shader fragment_shader);
|
||||
void mons_shader_set_float(mons_program shader, char *uniform_name, float value);
|
||||
void mons_shader_set_vec2(mons_program shader, char *uniform_name, struct mons_vec2 value);
|
||||
void mons_shader_set_vec3(mons_program shader, char *uniform_name, struct mons_vec3 value);
|
||||
void mons_shader_set_vec4(mons_program shader, char *uniform_name, struct mons_vec4 value);
|
||||
void mons_shader_set_mat4(mons_program shader, char *uniform_name, struct mons_mat4 value, bool transpose);
|
||||
|
||||
void mons_shader_set_float_global(char *uniform_name, float value);
|
||||
void mons_shader_set_vec2_global(char *uniform_name, struct mons_vec2 value);
|
||||
void mons_shader_set_vec3_global(char *uniform_name, struct mons_vec3 value);
|
||||
void mons_shader_set_vec4_global(char *uniform_name, struct mons_vec4 value);
|
||||
void mons_shader_set_mat4_global(char *uniform_name, struct mons_mat4 value, bool transpose);
|
||||
void mons_shader_apply_global_uniforms(mons_program shader);
|
||||
|
||||
#endif
|
19
mons_3d/include/texture.h
Normal file
19
mons_3d/include/texture.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef MONS_TEXTURE_H
|
||||
#define MONS_TEXTURE_H
|
||||
|
||||
#include "image.h"
|
||||
#include "shader.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct mons_texture {
|
||||
unsigned int id;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
} mons_texture;
|
||||
|
||||
mons_texture mons_texture_from_image(mons_image image);
|
||||
mons_texture mons_texture_load(FILE *stream);
|
||||
|
||||
void mons_texture_bind(mons_program shader, unsigned int unit, mons_texture texture);
|
||||
|
||||
#endif
|
36
mons_3d/include/transform.h
Normal file
36
mons_3d/include/transform.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef MONS_TRANSFORM_H
|
||||
#define MONS_TRANSFORM_H
|
||||
|
||||
#include "mons_math/mat4.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/quat.h"
|
||||
|
||||
typedef struct mons_transform {
|
||||
mons_vec3 translation;
|
||||
mons_vec3 scale;
|
||||
mons_quat rotation;
|
||||
} mons_transform;
|
||||
|
||||
void mons_transform_translate(mons_transform *transform, struct mons_vec3 translation);
|
||||
void mons_transform_rotate(mons_transform *transform, struct mons_quat rotation);
|
||||
void mons_transform_scale(mons_transform *transform, struct mons_vec3 scale);
|
||||
|
||||
mons_mat4 mons_transform_matrix(mons_transform transform);
|
||||
|
||||
void mons_transform_set_translation(mons_transform *transform, struct mons_vec3 translation);
|
||||
void mons_transform_set_rotation(mons_transform *transform, struct mons_quat rotation);
|
||||
void mons_transform_set_scale(mons_transform *transform, struct mons_vec3 scale);
|
||||
|
||||
struct mons_vec3 mons_transform_forward(mons_transform transform);
|
||||
struct mons_vec3 mons_transform_up(mons_transform transform);
|
||||
struct mons_vec3 mons_transform_right(mons_transform transform);
|
||||
|
||||
void mons_transform_look_at(mons_transform *transform, mons_vec3 point, mons_vec3 up);
|
||||
|
||||
#define MONS_TRANSFORM_IDENTITY (mons_transform) {\
|
||||
MONS_VEC3_ZERO,\
|
||||
MONS_VEC3_ONE,\
|
||||
MONS_QUAT_IDENTITY,\
|
||||
}
|
||||
|
||||
#endif
|
15
mons_3d/include/vertex.h
Normal file
15
mons_3d/include/vertex.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef MONS_VERTEX_H
|
||||
#define MONS_VERTEX_H
|
||||
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/vec2.h"
|
||||
|
||||
typedef struct mons_vertex {
|
||||
mons_vec3 position;
|
||||
mons_vec3 normal;
|
||||
mons_vec4 tangent;
|
||||
mons_vec2 texture_coords;
|
||||
} mons_vertex;
|
||||
|
||||
#endif
|
1
mons_3d/src/color.c
Normal file
1
mons_3d/src/color.c
Normal file
@ -0,0 +1 @@
|
||||
#include "color.h"
|
124
mons_3d/src/mesh.c
Normal file
124
mons_3d/src/mesh.c
Normal file
@ -0,0 +1,124 @@
|
||||
#include "mesh.h"
|
||||
#include "vertex.h"
|
||||
#include "mikktspace.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void compute_tangents(mons_vertex *vertex_array, int vertex_count, int *index_array, int index_count);
|
||||
|
||||
mons_mesh mons_create_mesh(mons_vertex *vertex_array, int vertex_count, int *index_array, int index_count) {
|
||||
compute_tangents(vertex_array, vertex_count, index_array, index_count);
|
||||
// VAO & VBO
|
||||
unsigned int vao;
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
unsigned int vbo;
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(mons_vertex) * vertex_count, vertex_array, GL_STATIC_DRAW);
|
||||
unsigned int ebo;
|
||||
glGenBuffers(1, &ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * index_count, index_array, GL_STATIC_DRAW);
|
||||
// Set up vertex attributes
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(mons_vertex), 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(mons_vertex), (void*)(sizeof(mons_vec3)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(mons_vertex), (void*)(sizeof(mons_vec3) * 2));
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(mons_vertex), (void*)(sizeof(mons_vec3) * 2 + sizeof(mons_vec4)));
|
||||
glEnableVertexAttribArray(3);
|
||||
return (mons_mesh) {
|
||||
.vao = vao,
|
||||
.element_count = index_count,
|
||||
};
|
||||
}
|
||||
|
||||
struct mesh_data {
|
||||
mons_vertex *vertex_array;
|
||||
int vertex_count;
|
||||
int *index_array;
|
||||
int index_count;
|
||||
};
|
||||
|
||||
int get_vertex_index(const SMikkTSpaceContext *context, int i_face, int i_vert) {
|
||||
struct mesh_data *mesh_data = context->m_pUserData;
|
||||
int index_index = (i_face * 3) + i_vert;
|
||||
int index = mesh_data->index_array[index_index];
|
||||
return index;
|
||||
}
|
||||
|
||||
int get_num_faces(const SMikkTSpaceContext *context) {
|
||||
struct mesh_data *mesh_data = context->m_pUserData;
|
||||
return mesh_data->index_count / 3;
|
||||
}
|
||||
|
||||
int get_num_vertices_of_face(const SMikkTSpaceContext *context, int i_face) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
void get_position(const SMikkTSpaceContext *context, float *outpos, int i_face, int i_vert) {
|
||||
struct mesh_data *mesh_data = context->m_pUserData;
|
||||
int index = get_vertex_index(context, i_face, i_vert);
|
||||
mons_vertex vertex = mesh_data->vertex_array[index];
|
||||
|
||||
outpos[0] = vertex.position.x;
|
||||
outpos[1] = vertex.position.y;
|
||||
outpos[2] = vertex.position.z;
|
||||
}
|
||||
|
||||
void get_normal(const SMikkTSpaceContext *context, float *outnormal, int i_face, int i_vert) {
|
||||
struct mesh_data *mesh_data = context->m_pUserData;
|
||||
int index = get_vertex_index(context, i_face, i_vert);
|
||||
mons_vertex vertex = mesh_data->vertex_array[index];
|
||||
|
||||
outnormal[0] = vertex.normal.x;
|
||||
outnormal[1] = vertex.normal.y;
|
||||
outnormal[2] = vertex.normal.z;
|
||||
}
|
||||
|
||||
void get_tex_coords(const SMikkTSpaceContext *context, float *outuv, int i_face, int i_vert) {
|
||||
struct mesh_data *mesh_data = context->m_pUserData;
|
||||
int index = get_vertex_index(context, i_face, i_vert);
|
||||
mons_vertex vertex = mesh_data->vertex_array[index];
|
||||
|
||||
outuv[0] = vertex.texture_coords.x;
|
||||
outuv[1] = vertex.texture_coords.y;
|
||||
}
|
||||
|
||||
void set_tspace_basic(const SMikkTSpaceContext *context, const float *tangentu, float f_sign, int i_face, int i_vert) {
|
||||
struct mesh_data *mesh_data = context->m_pUserData;
|
||||
int index = get_vertex_index(context, i_face, i_vert);
|
||||
mons_vertex *vertex = &mesh_data->vertex_array[index];
|
||||
|
||||
vertex->tangent.x = tangentu[0];
|
||||
vertex->tangent.y = tangentu[1];
|
||||
vertex->tangent.z = tangentu[2];
|
||||
vertex->tangent.w = f_sign;
|
||||
}
|
||||
|
||||
void compute_tangents(mons_vertex *vertex_array, int vertex_count, int *index_array, int index_count) {
|
||||
static SMikkTSpaceInterface iface = {
|
||||
.m_getNumFaces = get_num_faces,
|
||||
.m_getNumVerticesOfFace = get_num_vertices_of_face,
|
||||
.m_getNormal = get_normal,
|
||||
.m_getPosition = get_position,
|
||||
.m_getTexCoord = get_tex_coords,
|
||||
.m_setTSpaceBasic = set_tspace_basic,
|
||||
};
|
||||
struct mesh_data mesh_data = {
|
||||
vertex_array,
|
||||
vertex_count,
|
||||
index_array,
|
||||
index_count,
|
||||
};
|
||||
SMikkTSpaceContext context = {
|
||||
.m_pInterface = &iface,
|
||||
.m_pUserData = &mesh_data,
|
||||
};
|
||||
|
||||
genTangSpaceDefault(&context);
|
||||
}
|
||||
|
15
mons_3d/src/model.c
Normal file
15
mons_3d/src/model.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include "model.h"
|
||||
#include "texture.h"
|
||||
#include "camera.h"
|
||||
#include <glad/gl.h>
|
||||
|
||||
void mons_model_draw(mons_model model) {
|
||||
glUseProgram(model.shader);
|
||||
mons_shader_apply_global_uniforms(model.shader);
|
||||
mons_shader_set_mat4(model.shader, "transform", mons_transform_matrix(model.transform), GL_TRUE);
|
||||
for (int i = 0; i < model.textures_len; i++) {
|
||||
mons_texture_bind(model.shader, i, model.textures[i]);
|
||||
}
|
||||
glBindVertexArray(model.mesh.vao);
|
||||
glDrawElements(GL_TRIANGLES, model.mesh.element_count, GL_UNSIGNED_INT, 0);
|
||||
}
|
15
mons_3d/src/projection.c
Normal file
15
mons_3d/src/projection.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include "projection.h"
|
||||
#include <math.h>
|
||||
#include "mons_math/mat4.h"
|
||||
|
||||
mons_mat4 mons_projection_matrix(mons_projection projection) {
|
||||
float tangent = tanf(projection.fov / 2.0f);
|
||||
float top = projection.near * tangent;
|
||||
float right = top * projection.aspect_ratio;
|
||||
return (mons_mat4){
|
||||
{projection.near / right, 0, 0, 0},
|
||||
{0, projection.near / top, 0, 0},
|
||||
{0, 0, -(projection.far + projection.near) / (projection.far - projection.near), -1},
|
||||
{0, 0, -(2.0f * projection.far * projection.near) / (projection.far - projection.near), 0},
|
||||
};
|
||||
}
|
185
mons_3d/src/shader.c
Normal file
185
mons_3d/src/shader.c
Normal file
@ -0,0 +1,185 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "shader.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/vec2.h"
|
||||
#include "mons_math/mat4.h"
|
||||
#include "hashmap.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef enum mons_uniform_type {
|
||||
MONS_UNIFORM_INT,
|
||||
MONS_UNIFORM_FLOAT,
|
||||
MONS_UNIFORM_VEC2,
|
||||
MONS_UNIFORM_VEC3,
|
||||
MONS_UNIFORM_VEC4,
|
||||
MONS_UNIFORM_MAT4,
|
||||
} mons_uniform_type;
|
||||
|
||||
typedef struct mons_uniform_m4 {
|
||||
mons_mat4 matrix;
|
||||
bool transpose;
|
||||
} mons_uniform_m4;
|
||||
|
||||
typedef union mons_uniform_data {
|
||||
int i;
|
||||
float f;
|
||||
mons_vec2 v2;
|
||||
mons_vec3 v3;
|
||||
mons_vec4 v4;
|
||||
mons_uniform_m4 m4;
|
||||
} mons_uniform_data;
|
||||
|
||||
typedef struct mons_uniform {
|
||||
mons_uniform_type type;
|
||||
mons_uniform_data data;
|
||||
} mons_uniform;
|
||||
|
||||
static mons_hashmap *GLOBAL_UNIFORMS = NULL;
|
||||
mons_hashmap *get_global_uniforms() {
|
||||
if (GLOBAL_UNIFORMS == NULL) {
|
||||
GLOBAL_UNIFORMS = malloc(sizeof(mons_hashmap));
|
||||
*GLOBAL_UNIFORMS = mons_hashmap_new(sizeof(mons_uniform), 10);
|
||||
}
|
||||
return GLOBAL_UNIFORMS;
|
||||
}
|
||||
|
||||
void mons_shader_apply_global_uniforms(mons_program shader) {
|
||||
mons_hashmap *global_uniforms = get_global_uniforms();
|
||||
for (int i = 0; i < global_uniforms->len; i++) {
|
||||
mons_hashmap_pair pair;
|
||||
mons_hashmap_at(*global_uniforms, i, &pair);
|
||||
char *name = pair.key;
|
||||
mons_uniform uniform = *(mons_uniform *)pair.value;
|
||||
switch (uniform.type) {
|
||||
case MONS_UNIFORM_INT:
|
||||
// TODO:
|
||||
break;
|
||||
case MONS_UNIFORM_FLOAT:
|
||||
mons_shader_set_float(shader, name, uniform.data.f);
|
||||
break;
|
||||
case MONS_UNIFORM_VEC2:
|
||||
mons_shader_set_vec2(shader, name, uniform.data.v2);
|
||||
break;
|
||||
case MONS_UNIFORM_VEC3:
|
||||
mons_shader_set_vec3(shader, name, uniform.data.v3);
|
||||
break;
|
||||
case MONS_UNIFORM_VEC4:
|
||||
mons_shader_set_vec4(shader, name, uniform.data.v4);
|
||||
break;
|
||||
case MONS_UNIFORM_MAT4:
|
||||
mons_shader_set_mat4(shader, name, uniform.data.m4.matrix, uniform.data.m4.transpose);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mons_shader_set_float_global(char *uniform_name, float value) {
|
||||
mons_hashmap *global_uniforms = get_global_uniforms();
|
||||
mons_uniform uniform = {
|
||||
.type = MONS_UNIFORM_FLOAT,
|
||||
.data.f = value,
|
||||
};
|
||||
mons_hashmap_insert(global_uniforms, uniform_name, &uniform);
|
||||
}
|
||||
|
||||
void mons_shader_set_vec2_global(char *uniform_name, mons_vec2 value) {
|
||||
mons_hashmap *global_uniforms = get_global_uniforms();
|
||||
mons_uniform uniform = {
|
||||
.type = MONS_UNIFORM_VEC2,
|
||||
.data.v2 = value,
|
||||
};
|
||||
mons_hashmap_insert(global_uniforms, uniform_name, &uniform);
|
||||
}
|
||||
|
||||
void mons_shader_set_vec3_global(char *uniform_name, mons_vec3 value) {
|
||||
mons_hashmap *global_uniforms = get_global_uniforms();
|
||||
mons_uniform uniform = {
|
||||
.type = MONS_UNIFORM_VEC3,
|
||||
.data.v3 = value,
|
||||
};
|
||||
mons_hashmap_insert(global_uniforms, uniform_name, &uniform);
|
||||
}
|
||||
|
||||
void mons_shader_set_vec4_global(char *uniform_name, mons_vec4 value) {
|
||||
mons_hashmap *global_uniforms = get_global_uniforms();
|
||||
mons_uniform uniform = {
|
||||
.type = MONS_UNIFORM_VEC4,
|
||||
.data.v4 = value,
|
||||
};
|
||||
mons_hashmap_insert(global_uniforms, uniform_name, &uniform);
|
||||
}
|
||||
|
||||
void mons_shader_set_mat4_global(char *uniform_name, mons_mat4 value, bool transpose) {
|
||||
mons_hashmap *global_uniforms = get_global_uniforms();
|
||||
mons_uniform uniform = {
|
||||
.type = MONS_UNIFORM_MAT4,
|
||||
.data.m4 = {
|
||||
.matrix = value,
|
||||
.transpose = transpose,
|
||||
},
|
||||
};
|
||||
mons_hashmap_insert(global_uniforms, uniform_name, &uniform);
|
||||
}
|
||||
|
||||
mons_shader mons_create_shader(mons_shader_type shader_type, char *source,
|
||||
int source_length) {
|
||||
unsigned int shader = glCreateShader(shader_type);
|
||||
const char *shader_source = (const char *)source;
|
||||
int shader_len = (int)source_length;
|
||||
glShaderSource(shader, 1, &shader_source, &shader_len);
|
||||
glCompileShader(shader);
|
||||
int success;
|
||||
char info_log[512];
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(shader, 512, NULL, info_log);
|
||||
printf("Shader Compilation Failed: %s\n", info_log);
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
|
||||
mons_program mons_create_program(mons_shader vertex_shader,
|
||||
mons_shader fragment_shader) {
|
||||
unsigned int shader_program = glCreateProgram();
|
||||
glAttachShader(shader_program, vertex_shader);
|
||||
glAttachShader(shader_program, fragment_shader);
|
||||
glLinkProgram(shader_program);
|
||||
int success;
|
||||
char info_log[512];
|
||||
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetProgramInfoLog(shader_program, 512, NULL, info_log);
|
||||
}
|
||||
return shader_program;
|
||||
}
|
||||
|
||||
void mons_shader_set_float(mons_program shader, char *uniform_name,
|
||||
float value) {
|
||||
glUniform1f(glGetUniformLocation(shader, uniform_name), value);
|
||||
}
|
||||
|
||||
void mons_shader_set_vec2(mons_program shader, char *uniform_name,
|
||||
mons_vec2 value) {
|
||||
glUniform2fv(glGetUniformLocation(shader, uniform_name), 1,
|
||||
(float *)&value);
|
||||
}
|
||||
|
||||
void mons_shader_set_vec3(mons_program shader, char *uniform_name,
|
||||
mons_vec3 value) {
|
||||
glUniform3fv(glGetUniformLocation(shader, uniform_name), 1,
|
||||
(float *)&value);
|
||||
}
|
||||
|
||||
void mons_shader_set_vec4(mons_program shader, char *uniform_name,
|
||||
mons_vec4 value) {
|
||||
glUniform4fv(glGetUniformLocation(shader, uniform_name), 1,
|
||||
(float *)&value);
|
||||
}
|
||||
|
||||
void mons_shader_set_mat4(mons_program shader, char *uniform_name,
|
||||
mons_mat4 value, bool transpose) {
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader, uniform_name), 1, transpose,
|
||||
(float *)&value);
|
||||
}
|
55
mons_3d/src/texture.c
Normal file
55
mons_3d/src/texture.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include "texture.h"
|
||||
#include "qoi.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
|
||||
mons_texture mons_texture_load(FILE *stream) {
|
||||
mons_image image = mons_load_qoi(stream);
|
||||
return mons_texture_from_image(image);
|
||||
}
|
||||
|
||||
mons_texture mons_texture_from_image(mons_image image) {
|
||||
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_NEAREST_MIPMAP_LINEAR);
|
||||
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
unsigned int texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, image.data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
mons_texture result = {
|
||||
.id = texture,
|
||||
.width = image.width,
|
||||
.height = image.height,
|
||||
};
|
||||
mons_image_free(&image);
|
||||
return result;
|
||||
}
|
||||
|
||||
char *texure_uniforms[16] = {
|
||||
"base_texture",
|
||||
"normal_texture",
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
};
|
||||
|
||||
void mons_texture_bind(mons_program shader, unsigned int unit, mons_texture texture) {
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
glUniform1i(glGetUniformLocation(shader, texure_uniforms[unit]), unit);
|
||||
glBindTexture(GL_TEXTURE_2D, texture.id);
|
||||
}
|
112
mons_3d/src/transform.c
Normal file
112
mons_3d/src/transform.c
Normal file
@ -0,0 +1,112 @@
|
||||
#include <math.h>
|
||||
#include "transform.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/quat.h"
|
||||
#include <stdio.h>
|
||||
|
||||
mons_mat4 translation_to_mat4(mons_vec3 translation) {
|
||||
mons_mat4 result = MONS_MAT4_IDENTITY;
|
||||
result.m1.w = translation.x;
|
||||
result.m2.w = translation.y;
|
||||
result.m3.w = translation.z;
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat4 scale_to_mat4(mons_vec3 scale) {
|
||||
mons_mat4 result = MONS_MAT4_IDENTITY;
|
||||
result.m1.x = scale.x;
|
||||
result.m2.y = scale.y;
|
||||
result.m3.z = scale.z;
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_transform_translate(mons_transform *transform,
|
||||
mons_vec3 translation) {
|
||||
mons_vec3_add_inplace(&transform->translation, translation);
|
||||
}
|
||||
|
||||
void mons_transform_rotate(mons_transform *transform, mons_quat rotation) {
|
||||
mons_quat_mul_inplace(&transform->rotation, rotation);
|
||||
}
|
||||
|
||||
void mons_transform_scale(mons_transform *transform, mons_vec3 scale) {
|
||||
mons_vec3_mul_memberwise_inplace(&transform->scale, scale);
|
||||
}
|
||||
|
||||
mons_mat4 mons_transform_matrix(mons_transform transform) {
|
||||
mons_mat4 translation = translation_to_mat4(transform.translation);
|
||||
mons_mat4 result = translation;
|
||||
mons_mat4_mul_inplace(&result, mons_quat_to_mat4(transform.rotation));
|
||||
mons_mat4_mul_inplace(&result, scale_to_mat4(transform.scale));
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec3 mons_transform_forward(mons_transform transform) {
|
||||
return mons_quat_transform_vec3(transform.rotation, MONS_VEC3_Z);
|
||||
}
|
||||
|
||||
mons_vec3 mons_transform_up(mons_transform transform) {
|
||||
return mons_quat_transform_vec3(transform.rotation, MONS_VEC3_Y);
|
||||
}
|
||||
|
||||
mons_vec3 mons_transform_right(mons_transform transform) {
|
||||
return mons_quat_transform_vec3(transform.rotation, MONS_VEC3_X);
|
||||
}
|
||||
|
||||
void mons_transform_look_at(mons_transform *transform, mons_vec3 point, mons_vec3 up) {
|
||||
mons_vec3 forward = mons_vec3_normalize(mons_vec3_sub(transform->translation, point));
|
||||
mons_vec3 right = mons_vec3_normalize(mons_vec3_cross(up, forward));
|
||||
mons_vec3 new_up = mons_vec3_cross(forward, right);
|
||||
float m00 = right.x;
|
||||
float m01 = right.y;
|
||||
float m02 = right.z;
|
||||
float m10 = new_up.x;
|
||||
float m11 = new_up.y;
|
||||
float m12 = new_up.z;
|
||||
float m20 = forward.x;
|
||||
float m21 = forward.y;
|
||||
float m22 = forward.z;
|
||||
|
||||
float num8 = (m00 + m11) + m22;
|
||||
mons_quat quaternion = {0};
|
||||
if (num8 > 0.0f) {
|
||||
float num = sqrt(num8 + 1.0f);
|
||||
quaternion.w = num * 0.5f;
|
||||
num = 0.5f / num;
|
||||
quaternion.x = (m12 - m21) * num;
|
||||
quaternion.y = (m20 - m02) * num;
|
||||
quaternion.z = (m01 - m10) * num;
|
||||
transform->rotation = quaternion;
|
||||
return;
|
||||
}
|
||||
if ((m00 >= m11) && (m00 >= m22))
|
||||
{
|
||||
float num7 = (float)sqrt(((1.0f + m00) - m11) - m22);
|
||||
float num4 = 0.5f / num7;
|
||||
quaternion.x = 0.5f * num7;
|
||||
quaternion.y = (m01 + m10) * num4;
|
||||
quaternion.z = (m02 + m20) * num4;
|
||||
quaternion.w = (m12 - m21) * num4;
|
||||
transform->rotation = quaternion;
|
||||
return;
|
||||
}
|
||||
if (m11 > m22)
|
||||
{
|
||||
float num6 = (float)sqrt(((1.0f + m11) - m00) - m22);
|
||||
float num3 = 0.5f / num6;
|
||||
quaternion.x = (m10 + m01) * num3;
|
||||
quaternion.y = 0.5f * num6;
|
||||
quaternion.z = (m21 + m12) * num3;
|
||||
quaternion.w = (m20 - m02) * num3;
|
||||
transform->rotation = quaternion;
|
||||
return;
|
||||
}
|
||||
float num5 = (float)sqrt(((1.0f + m22) - m00) - m11);
|
||||
float num2 = 0.5f / num5;
|
||||
quaternion.x = (m20 + m02) * num2;
|
||||
quaternion.y = (m21 + m12) * num2;
|
||||
quaternion.z = 0.5f * num5;
|
||||
quaternion.w = (m01 - m10) * num2;
|
||||
transform->rotation = quaternion;
|
||||
return;
|
||||
}
|
29
mons_collections/CMakeLists.txt
Normal file
29
mons_collections/CMakeLists.txt
Normal file
@ -0,0 +1,29 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_collections LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_library(mons_collections
|
||||
SHARED
|
||||
./src/hashmap.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_collections PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_collections PRIVATE -coverage)
|
||||
target_link_options(mons_collections PRIVATE -coverage)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_collections)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
23
mons_collections/include/hashmap.h
Normal file
23
mons_collections/include/hashmap.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef MONS_HASHMAP_H
|
||||
#define MONS_HASHMAP_H
|
||||
|
||||
typedef struct mons_hashmap_pair {
|
||||
char *key;
|
||||
void *value;
|
||||
struct mons_hashmap_pair *next;
|
||||
} mons_hashmap_pair;
|
||||
|
||||
typedef struct mons_hashmap {
|
||||
mons_hashmap_pair **data;
|
||||
unsigned int bucket_count;
|
||||
unsigned int member_size;
|
||||
unsigned int len;
|
||||
} mons_hashmap;
|
||||
|
||||
mons_hashmap mons_hashmap_new(unsigned int member_size, unsigned int buckets);
|
||||
void mons_hashmap_free(mons_hashmap *hashmap);
|
||||
int mons_hashmap_insert(mons_hashmap *map, char *key, void *value);
|
||||
int mons_hashmap_get(mons_hashmap map, char *key, void *out);
|
||||
int mons_hashmap_remove(mons_hashmap *map, char *key);
|
||||
int mons_hashmap_at(mons_hashmap map, unsigned int index, mons_hashmap_pair *out);
|
||||
#endif
|
141
mons_collections/src/hashmap.c
Normal file
141
mons_collections/src/hashmap.c
Normal file
@ -0,0 +1,141 @@
|
||||
#include "hashmap.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define MONS_HASHMAP_MAX_LOAD_FACTOR 3.0f
|
||||
|
||||
unsigned int djb2_hash(unsigned char *str, unsigned int bucket_count) {
|
||||
unsigned long hash = 5381;
|
||||
int c;
|
||||
|
||||
while ((c = *str++)) {
|
||||
hash = ((hash << 5) + hash) + c;
|
||||
}
|
||||
|
||||
return hash % bucket_count;
|
||||
}
|
||||
|
||||
mons_hashmap mons_hashmap_new(unsigned int member_size, unsigned int buckets) {
|
||||
mons_hashmap result = {
|
||||
.member_size = member_size,
|
||||
.bucket_count = buckets,
|
||||
.data = calloc(buckets, sizeof(mons_hashmap_pair *)),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
int mons_hashmap_rehash(mons_hashmap *map) {
|
||||
// TODO: Expand number of buckets, re-hash all entries
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int mons_hashmap_insert(mons_hashmap *map, char *key, void *value) {
|
||||
unsigned int bucket = djb2_hash((unsigned char*)key, map->bucket_count);
|
||||
mons_hashmap_pair *tail = map->data[bucket];
|
||||
if (tail == NULL) {
|
||||
map->data[bucket] = calloc(1, sizeof(mons_hashmap_pair *));
|
||||
tail = map->data[bucket];
|
||||
} else {
|
||||
mons_hashmap_pair *node = tail;
|
||||
while(1) {
|
||||
if(strcmp(key, node->key) == 0) {
|
||||
memcpy(node->value, value, map->member_size);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if(node->next == NULL) {
|
||||
tail = node;
|
||||
break;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
tail->next = calloc(1, sizeof(mons_hashmap_pair *));
|
||||
tail = tail->next;
|
||||
}
|
||||
tail->key = calloc(strlen(key) + 1, 1);
|
||||
strcpy(tail->key, key);
|
||||
tail->value = calloc(1, map->member_size);
|
||||
memcpy(tail->value, value, map->member_size);
|
||||
map->len++;
|
||||
if (map->len / map->bucket_count > MONS_HASHMAP_MAX_LOAD_FACTOR) {
|
||||
mons_hashmap_rehash(map);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int mons_hashmap_get(mons_hashmap map, char *key, void *out) {
|
||||
unsigned int bucket = djb2_hash((unsigned char*)key, map.bucket_count);
|
||||
mons_hashmap_pair *tail = map.data[bucket];
|
||||
while(tail != NULL) {
|
||||
if(strcmp(key, tail->key) == 0) {
|
||||
memcpy(out, tail->value, map.member_size);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
tail = tail->next;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int mons_hashmap_remove(mons_hashmap *map, char *key) {
|
||||
unsigned int bucket = djb2_hash((unsigned char*)key, map->bucket_count);
|
||||
mons_hashmap_pair *tail = map->data[bucket];
|
||||
mons_hashmap_pair *prev = NULL;
|
||||
while(tail != NULL) {
|
||||
if(strcmp(key, tail->key) == 0) {
|
||||
if (prev == NULL) {
|
||||
map->data[bucket] = tail->next;
|
||||
} else {
|
||||
prev->next = tail->next;
|
||||
}
|
||||
free(tail->key);
|
||||
free(tail->value);
|
||||
free(tail);
|
||||
map->len--;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
prev = tail;
|
||||
tail = tail->next;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
void mons_hashmap_free(mons_hashmap *hashmap) {
|
||||
for (int i = 0; i < hashmap->bucket_count; i++) {
|
||||
mons_hashmap_pair *node = hashmap->data[i];
|
||||
while(node != NULL) {
|
||||
mons_hashmap_pair *next = node->next;
|
||||
free(node->key);
|
||||
free(node->value);
|
||||
free(node);
|
||||
node = next;
|
||||
}
|
||||
}
|
||||
free(hashmap->data);
|
||||
hashmap->data = NULL;
|
||||
hashmap->bucket_count = 0;
|
||||
hashmap->member_size = 0;
|
||||
hashmap->len = 0;
|
||||
}
|
||||
|
||||
int mons_hashmap_at(mons_hashmap map, unsigned int index, mons_hashmap_pair *out) {
|
||||
mons_hashmap_pair **bucket = map.data;
|
||||
mons_hashmap_pair *node = *bucket;
|
||||
while(node == NULL) {
|
||||
bucket++;
|
||||
node = *bucket;
|
||||
}
|
||||
for(int i = 0; i < index; i++) {
|
||||
if (node == NULL || node->next == NULL) {
|
||||
bucket++;
|
||||
node = *bucket;
|
||||
while(node == NULL) {
|
||||
bucket++;
|
||||
node = *bucket;
|
||||
}
|
||||
} else {
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
*out = *node;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
36
mons_exe/CMakeLists.txt
Normal file
36
mons_exe/CMakeLists.txt
Normal file
@ -0,0 +1,36 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_exe LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_custom_target(hexdump_embeds
|
||||
./embed_headers.sh
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
)
|
||||
|
||||
add_executable(mons_exe
|
||||
./src/main.c
|
||||
./src/input.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_exe PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_exe PRIVATE -coverage)
|
||||
target_link_options(mons_exe PRIVATE -coverage)
|
||||
target_link_libraries(mons_exe PUBLIC mons_3d mons_gltf X11)
|
||||
add_dependencies(mons_exe hexdump_embeds)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_exe)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
26
mons_exe/embed/shaders/basic.vert.glsl
Normal file
26
mons_exe/embed/shaders/basic.vert.glsl
Normal file
@ -0,0 +1,26 @@
|
||||
#version 460 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec4 aTangent;
|
||||
layout (location = 3) in vec2 aTexCoord;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec2 TexCoord;
|
||||
out vec3 FragPos;
|
||||
out mat3 TBN;
|
||||
|
||||
uniform mat4 transform;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection * view * transform * vec4(aPos, 1.0);
|
||||
FragPos = vec3(transform * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(transform))) * aNormal;
|
||||
TexCoord = aTexCoord;
|
||||
|
||||
vec3 T = normalize(vec3(transform * vec4(aTangent.xyz, 0.0)));
|
||||
vec3 N = normalize(vec3(transform * vec4(aNormal, 0.0)));
|
||||
vec3 B = aTangent.w * cross(N, T);
|
||||
TBN = mat3(T,B,N);
|
||||
}
|
27
mons_exe/embed/shaders/basic_lit.frag.glsl
Normal file
27
mons_exe/embed/shaders/basic_lit.frag.glsl
Normal file
@ -0,0 +1,27 @@
|
||||
#version 460 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoord;
|
||||
in vec3 FragPos;
|
||||
in mat3 TBN;
|
||||
|
||||
uniform float ambient_strength;
|
||||
uniform sampler2D base_texture;
|
||||
uniform sampler2D normal_texture;
|
||||
|
||||
uniform vec4 light_color;
|
||||
uniform vec3 light_position;
|
||||
|
||||
void main() {
|
||||
vec4 base_color = texture(base_texture, TexCoord);
|
||||
vec3 normal = texture(normal_texture, TexCoord).rgb;
|
||||
normal = normal * 2.0 - 1.0;
|
||||
normal = normalize(TBN * normal);
|
||||
vec3 light_direction = normalize(light_position - FragPos);
|
||||
float diff = max(dot(normal, light_direction), 0.0);
|
||||
vec4 diffuse_color = diff * light_color;
|
||||
vec4 ambient_color = ambient_strength * light_color;
|
||||
|
||||
FragColor = (ambient_color + diffuse_color) * base_color;
|
||||
}
|
10
mons_exe/embed_headers.sh
Executable file
10
mons_exe/embed_headers.sh
Executable file
@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
rm -rf ./include/embedded/
|
||||
for file in $(find ./embed -type f); do
|
||||
basepath="${file#*/*/}"
|
||||
basedir="${basepath%/*}"
|
||||
mkdir -p "./include/embedded/$basedir"
|
||||
echo "Generating ./include/embedded/$basepath.h"
|
||||
xxd -i "$file" "./include/embedded/$basepath.h"
|
||||
done
|
64
mons_exe/include/embedded/shaders/basic.vert.glsl.h
Normal file
64
mons_exe/include/embedded/shaders/basic.vert.glsl.h
Normal file
@ -0,0 +1,64 @@
|
||||
unsigned char __embed_shaders_basic_vert_glsl[] = {
|
||||
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x34, 0x36, 0x30,
|
||||
0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74,
|
||||
0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d,
|
||||
0x20, 0x30, 0x29, 0x20, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20,
|
||||
0x61, 0x50, 0x6f, 0x73, 0x3b, 0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74,
|
||||
0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d,
|
||||
0x20, 0x31, 0x29, 0x20, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20,
|
||||
0x61, 0x4e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x3b, 0x0a, 0x6c, 0x61, 0x79,
|
||||
0x6f, 0x75, 0x74, 0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x20, 0x3d, 0x20, 0x32, 0x29, 0x20, 0x69, 0x6e, 0x20, 0x76, 0x65,
|
||||
0x63, 0x34, 0x20, 0x61, 0x54, 0x61, 0x6e, 0x67, 0x65, 0x6e, 0x74, 0x3b,
|
||||
0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74, 0x20, 0x28, 0x6c, 0x6f, 0x63,
|
||||
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x33, 0x29, 0x20, 0x69,
|
||||
0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x54, 0x65, 0x78, 0x43,
|
||||
0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76,
|
||||
0x65, 0x63, 0x33, 0x20, 0x4e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x3b, 0x0a,
|
||||
0x6f, 0x75, 0x74, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x54, 0x65, 0x78,
|
||||
0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76,
|
||||
0x65, 0x63, 0x33, 0x20, 0x46, 0x72, 0x61, 0x67, 0x50, 0x6f, 0x73, 0x3b,
|
||||
0x0a, 0x6f, 0x75, 0x74, 0x20, 0x6d, 0x61, 0x74, 0x33, 0x20, 0x54, 0x42,
|
||||
0x4e, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20,
|
||||
0x6d, 0x61, 0x74, 0x34, 0x20, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x6f,
|
||||
0x72, 0x6d, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20,
|
||||
0x6d, 0x61, 0x74, 0x34, 0x20, 0x76, 0x69, 0x65, 0x77, 0x3b, 0x0a, 0x75,
|
||||
0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20,
|
||||
0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a,
|
||||
0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29,
|
||||
0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f,
|
||||
0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x70, 0x72, 0x6f,
|
||||
0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x2a, 0x20, 0x76, 0x69,
|
||||
0x65, 0x77, 0x20, 0x2a, 0x20, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x6f,
|
||||
0x72, 0x6d, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x61, 0x50,
|
||||
0x6f, 0x73, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20,
|
||||
0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x50, 0x6f, 0x73, 0x20, 0x3d, 0x20,
|
||||
0x76, 0x65, 0x63, 0x33, 0x28, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x6f,
|
||||
0x72, 0x6d, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x61, 0x50,
|
||||
0x6f, 0x73, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x29, 0x3b, 0x0a, 0x20,
|
||||
0x20, 0x20, 0x20, 0x4e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x20, 0x3d, 0x20,
|
||||
0x6d, 0x61, 0x74, 0x33, 0x28, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f,
|
||||
0x73, 0x65, 0x28, 0x69, 0x6e, 0x76, 0x65, 0x72, 0x73, 0x65, 0x28, 0x74,
|
||||
0x72, 0x61, 0x6e, 0x73, 0x66, 0x6f, 0x72, 0x6d, 0x29, 0x29, 0x29, 0x20,
|
||||
0x2a, 0x20, 0x61, 0x4e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x3b, 0x0a, 0x20,
|
||||
0x20, 0x20, 0x20, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x20,
|
||||
0x3d, 0x20, 0x61, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b,
|
||||
0x0a, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65,
|
||||
0x63, 0x33, 0x20, 0x54, 0x20, 0x3d, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61,
|
||||
0x6c, 0x69, 0x7a, 0x65, 0x28, 0x76, 0x65, 0x63, 0x33, 0x28, 0x74, 0x72,
|
||||
0x61, 0x6e, 0x73, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x2a, 0x20, 0x76, 0x65,
|
||||
0x63, 0x34, 0x28, 0x61, 0x54, 0x61, 0x6e, 0x67, 0x65, 0x6e, 0x74, 0x2e,
|
||||
0x78, 0x79, 0x7a, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x29, 0x29, 0x29, 0x3b,
|
||||
0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x4e, 0x20,
|
||||
0x3d, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x28,
|
||||
0x76, 0x65, 0x63, 0x33, 0x28, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x6f,
|
||||
0x72, 0x6d, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x61, 0x4e,
|
||||
0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x29, 0x29,
|
||||
0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20,
|
||||
0x42, 0x20, 0x3d, 0x20, 0x61, 0x54, 0x61, 0x6e, 0x67, 0x65, 0x6e, 0x74,
|
||||
0x2e, 0x77, 0x20, 0x2a, 0x20, 0x63, 0x72, 0x6f, 0x73, 0x73, 0x28, 0x4e,
|
||||
0x2c, 0x20, 0x54, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x54, 0x42,
|
||||
0x4e, 0x20, 0x3d, 0x20, 0x6d, 0x61, 0x74, 0x33, 0x28, 0x54, 0x2c, 0x42,
|
||||
0x2c, 0x4e, 0x29, 0x3b, 0x0a, 0x7d, 0x0a
|
||||
};
|
||||
unsigned int __embed_shaders_basic_vert_glsl_len = 727;
|
65
mons_exe/include/embedded/shaders/basic_lit.frag.glsl.h
Normal file
65
mons_exe/include/embedded/shaders/basic_lit.frag.glsl.h
Normal file
@ -0,0 +1,65 @@
|
||||
unsigned char __embed_shaders_basic_lit_frag_glsl[] = {
|
||||
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x34, 0x36, 0x30,
|
||||
0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76, 0x65,
|
||||
0x63, 0x34, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72,
|
||||
0x3b, 0x0a, 0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x4e,
|
||||
0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x3b, 0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65,
|
||||
0x63, 0x32, 0x20, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b,
|
||||
0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x46, 0x72, 0x61,
|
||||
0x67, 0x50, 0x6f, 0x73, 0x3b, 0x0a, 0x69, 0x6e, 0x20, 0x6d, 0x61, 0x74,
|
||||
0x33, 0x20, 0x54, 0x42, 0x4e, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x66,
|
||||
0x6f, 0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x61, 0x6d,
|
||||
0x62, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x6e, 0x67,
|
||||
0x74, 0x68, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20,
|
||||
0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x32, 0x44, 0x20, 0x62, 0x61,
|
||||
0x73, 0x65, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x3b, 0x0a,
|
||||
0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x73, 0x61, 0x6d, 0x70,
|
||||
0x6c, 0x65, 0x72, 0x32, 0x44, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c,
|
||||
0x5f, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x3b, 0x0a, 0x0a, 0x75,
|
||||
0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20,
|
||||
0x6c, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b,
|
||||
0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x76, 0x65, 0x63,
|
||||
0x33, 0x20, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x70, 0x6f, 0x73, 0x69,
|
||||
0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20,
|
||||
0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20,
|
||||
0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x62, 0x61, 0x73, 0x65, 0x5f, 0x63,
|
||||
0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, 0x74, 0x75,
|
||||
0x72, 0x65, 0x28, 0x62, 0x61, 0x73, 0x65, 0x5f, 0x74, 0x65, 0x78, 0x74,
|
||||
0x75, 0x72, 0x65, 0x2c, 0x20, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72,
|
||||
0x64, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x33,
|
||||
0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x20, 0x3d, 0x20, 0x74, 0x65,
|
||||
0x78, 0x74, 0x75, 0x72, 0x65, 0x28, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c,
|
||||
0x5f, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x54, 0x65,
|
||||
0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x29, 0x2e, 0x72, 0x67, 0x62, 0x3b,
|
||||
0x0a, 0x20, 0x20, 0x20, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x20,
|
||||
0x3d, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x20, 0x2a, 0x20, 0x32,
|
||||
0x2e, 0x30, 0x20, 0x2d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x20, 0x20,
|
||||
0x20, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x20, 0x3d, 0x20, 0x6e,
|
||||
0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x28, 0x54, 0x42, 0x4e,
|
||||
0x20, 0x2a, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x29, 0x3b, 0x0a,
|
||||
0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x6c, 0x69, 0x67,
|
||||
0x68, 0x74, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
|
||||
0x20, 0x3d, 0x20, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x69, 0x7a, 0x65,
|
||||
0x28, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74,
|
||||
0x69, 0x6f, 0x6e, 0x20, 0x2d, 0x20, 0x46, 0x72, 0x61, 0x67, 0x50, 0x6f,
|
||||
0x73, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6c, 0x6f, 0x61,
|
||||
0x74, 0x20, 0x64, 0x69, 0x66, 0x66, 0x20, 0x3d, 0x20, 0x6d, 0x61, 0x78,
|
||||
0x28, 0x64, 0x6f, 0x74, 0x28, 0x6e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x2c,
|
||||
0x20, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63,
|
||||
0x74, 0x69, 0x6f, 0x6e, 0x29, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x29, 0x3b,
|
||||
0x0a, 0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x64, 0x69,
|
||||
0x66, 0x66, 0x75, 0x73, 0x65, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x20,
|
||||
0x3d, 0x20, 0x64, 0x69, 0x66, 0x66, 0x20, 0x2a, 0x20, 0x6c, 0x69, 0x67,
|
||||
0x68, 0x74, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x20, 0x20,
|
||||
0x20, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x61, 0x6d, 0x62, 0x69, 0x65,
|
||||
0x6e, 0x74, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x61,
|
||||
0x6d, 0x62, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x65, 0x6e,
|
||||
0x67, 0x74, 0x68, 0x20, 0x2a, 0x20, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x5f,
|
||||
0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20,
|
||||
0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20,
|
||||
0x28, 0x61, 0x6d, 0x62, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x63, 0x6f, 0x6c,
|
||||
0x6f, 0x72, 0x20, 0x2b, 0x20, 0x64, 0x69, 0x66, 0x66, 0x75, 0x73, 0x65,
|
||||
0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x29, 0x20, 0x2a, 0x20, 0x62, 0x61,
|
||||
0x73, 0x65, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x7d, 0x0a
|
||||
};
|
||||
unsigned int __embed_shaders_basic_lit_frag_glsl_len = 744;
|
113
mons_exe/include/input.h
Normal file
113
mons_exe/include/input.h
Normal file
@ -0,0 +1,113 @@
|
||||
#ifndef MONS_INPUT_H
|
||||
#define MONS_INPUT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum mons_keycode {
|
||||
MONS_KEY_A = 38,
|
||||
MONS_KEY_B = 56,
|
||||
MONS_KEY_C = 54,
|
||||
MONS_KEY_D = 40,
|
||||
MONS_KEY_E = 26,
|
||||
MONS_KEY_F = 41,
|
||||
MONS_KEY_G = 42,
|
||||
MONS_KEY_H = 43,
|
||||
MONS_KEY_I = 31,
|
||||
MONS_KEY_J = 44,
|
||||
MONS_KEY_K = 45,
|
||||
MONS_KEY_L = 46,
|
||||
MONS_KEY_M = 58,
|
||||
MONS_KEY_N = 57,
|
||||
MONS_KEY_O = 32,
|
||||
MONS_KEY_P = 33,
|
||||
MONS_KEY_Q = 24,
|
||||
MONS_KEY_R = 27,
|
||||
MONS_KEY_S = 39,
|
||||
MONS_KEY_T = 28,
|
||||
MONS_KEY_U = 30,
|
||||
MONS_KEY_V = 55,
|
||||
MONS_KEY_W = 25,
|
||||
MONS_KEY_X = 53,
|
||||
MONS_KEY_Y = 29,
|
||||
MONS_KEY_Z = 52,
|
||||
MONS_KEY_1 = 10,
|
||||
MONS_KEY_2 = 11,
|
||||
MONS_KEY_3 = 12,
|
||||
MONS_KEY_4 = 13,
|
||||
MONS_KEY_5 = 14,
|
||||
MONS_KEY_6 = 15,
|
||||
MONS_KEY_7 = 16,
|
||||
MONS_KEY_8 = 17,
|
||||
MONS_KEY_9 = 18,
|
||||
MONS_KEY_0 = 19,
|
||||
MONS_KEY_F1 = 67,
|
||||
MONS_KEY_F2 = 68,
|
||||
MONS_KEY_F3 = 69,
|
||||
MONS_KEY_F4 = 70,
|
||||
MONS_KEY_F5 = 71,
|
||||
MONS_KEY_F6 = 72,
|
||||
MONS_KEY_F7 = 73,
|
||||
MONS_KEY_F8 = 74,
|
||||
MONS_KEY_F9 = 75,
|
||||
MONS_KEY_F10 = 76,
|
||||
MONS_KEY_F11 = 95,
|
||||
MONS_KEY_F12 = 96,
|
||||
MONS_KEY_ESCAPE = 9,
|
||||
MONS_KEY_UP = 111,
|
||||
MONS_KEY_DOWN = 116,
|
||||
MONS_KEY_LEFT = 113,
|
||||
MONS_KEY_RIGHT = 114,
|
||||
MONS_KEY_GRAVE = 49,
|
||||
MONS_KEY_TAB = 23,
|
||||
MONS_KEY_CAPSLOCK = 66,
|
||||
MONS_KEY_LSHIFT = 50,
|
||||
MONS_KEY_RSHIFT = 62,
|
||||
MONS_KEY_LCTRL = 37,
|
||||
MONS_KEY_RCTRL = 105,
|
||||
MONS_KEY_LSUPER = 133,
|
||||
MONS_KEY_RSUPER = 134,
|
||||
MONS_KEY_LMETA = 64,
|
||||
MONS_KEY_RMETA = 108,
|
||||
MONS_KEY_SPACE = 65,
|
||||
MONS_KEY_MENU = 135,
|
||||
MONS_KEY_ENTER = 36,
|
||||
MONS_KEY_BACKSLASH = 51,
|
||||
MONS_KEY_SLASH = 61,
|
||||
MONS_KEY_COMMA = 59,
|
||||
MONS_KEY_PERIOD = 60,
|
||||
MONS_KEY_SEMICOLON = 47,
|
||||
MONS_KEY_QUOTE = 48,
|
||||
MONS_KEY_LBRACKET = 34,
|
||||
MONS_KEY_RBRACKET = 35,
|
||||
MONS_KEY_MINUS = 20,
|
||||
MONS_KEY_EQUAL = 21,
|
||||
MONS_KEY_BACKSPACE = 22,
|
||||
MONS_KEY_PRINT = 107,
|
||||
MONS_KEY_SCROLLLOCK = 78,
|
||||
MONS_KEY_PAUSE = 127,
|
||||
MONS_KEY_INSERT = 118,
|
||||
MONS_KEY_DELETE = 119,
|
||||
MONS_KEY_HOME = 110,
|
||||
MONS_KEY_END = 115,
|
||||
MONS_KEY_PREV = 112,
|
||||
MONS_KEY_NEXT = 117,
|
||||
MONS_KEY_NUMLOCK = 77,
|
||||
MONS_KEY_NUMPAD_1 = 87,
|
||||
MONS_KEY_NUMPAD_2 = 88,
|
||||
MONS_KEY_NUMPAD_3 = 89,
|
||||
MONS_KEY_NUMPAD_4 = 83,
|
||||
MONS_KEY_NUMPAD_5 = 84,
|
||||
MONS_KEY_NUMPAD_6 = 85,
|
||||
MONS_KEY_NUMPAD_7 = 79,
|
||||
MONS_KEY_NUMPAD_8 = 80,
|
||||
MONS_KEY_NUMPAD_9 = 81,
|
||||
MONS_KEY_NUMPAD_0 = 90,
|
||||
MONS_KEY_NUMPAD_DIV = 106,
|
||||
MONS_KEY_NUMPAD_MUL = 63,
|
||||
MONS_KEY_NUMPAD_MINUS = 82,
|
||||
MONS_KEY_NUMPAD_PLUS = 86,
|
||||
MONS_KEY_NUMPAD_DELETE = 91,
|
||||
MONS_KEY_NUMPAD_ENTER = 104,
|
||||
} mons_keycode;
|
||||
|
||||
#endif
|
0
mons_exe/src/input.c
Normal file
0
mons_exe/src/input.c
Normal file
247
mons_exe/src/main.c
Normal file
247
mons_exe/src/main.c
Normal file
@ -0,0 +1,247 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include "glad/gl.h"
|
||||
#include "glad/glx.h"
|
||||
#include "vertex.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/quat.h"
|
||||
#include "embedded/shaders/basic.vert.glsl.h"
|
||||
#include "embedded/shaders/basic_lit.frag.glsl.h"
|
||||
#include "shader.h"
|
||||
#include "mesh.h"
|
||||
#include "qoi.h"
|
||||
#include "texture.h"
|
||||
#include "transform.h"
|
||||
#include "model.h"
|
||||
#include "projection.h"
|
||||
#include "json.h"
|
||||
#include "hashmap.h"
|
||||
#include "gltf.h"
|
||||
#include "input.h"
|
||||
#include "camera.h"
|
||||
#include <memory.h>
|
||||
#include "color.h"
|
||||
#include "light.h"
|
||||
|
||||
struct timespec current_time;
|
||||
double delta_time = 0;
|
||||
|
||||
int main(void) {
|
||||
// Make Window
|
||||
Display *display = XOpenDisplay(NULL);
|
||||
if (display == NULL) {
|
||||
printf("Cannot connect to X server\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int screen = DefaultScreen(display);
|
||||
|
||||
int glx_version = gladLoaderLoadGLX(display, screen);
|
||||
if (!glx_version) {
|
||||
printf("Unable to load GLX\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf("Loaded GLX %d.%d\n", GLAD_VERSION_MAJOR(glx_version),
|
||||
GLAD_VERSION_MINOR(glx_version));
|
||||
|
||||
Window root = RootWindow(display, screen);
|
||||
|
||||
GLint visual_attributes[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24,
|
||||
None};
|
||||
XVisualInfo *visual_info =
|
||||
glXChooseVisual(display, screen, visual_attributes);
|
||||
|
||||
Colormap colormap =
|
||||
XCreateColormap(display, root, visual_info->visual, AllocNone);
|
||||
|
||||
XSetWindowAttributes attributes;
|
||||
attributes.event_mask =
|
||||
ExposureMask | KeyPressMask | KeyReleaseMask | StructureNotifyMask;
|
||||
attributes.colormap = colormap;
|
||||
|
||||
Window window = XCreateWindow(
|
||||
display, root, 0, 0, 640, 480, 0, visual_info->depth, InputOutput,
|
||||
visual_info->visual, CWColormap | CWEventMask, &attributes);
|
||||
|
||||
// XSizeHints hints = {
|
||||
// .flags = PMinSize | PMaxSize,
|
||||
// .min_width = 640,
|
||||
// .min_height = 480,
|
||||
// .max_width = 640,
|
||||
// .max_height = 480,
|
||||
// };
|
||||
// XSetWMNormalHints(display, window, &hints);
|
||||
XMapWindow(display, window);
|
||||
XStoreName(display, window, "Hello World!");
|
||||
|
||||
if (!window) {
|
||||
printf("Unable to create window\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
GLXContext context = glXCreateContext(display, visual_info, NULL, GL_TRUE);
|
||||
glXMakeCurrent(display, window, context);
|
||||
|
||||
// Load GL
|
||||
int version = gladLoaderLoadGL();
|
||||
if (!version) {
|
||||
printf("Unable to load GL\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf("Loaded GL %d.%d\n", GLAD_VERSION_MAJOR(version),
|
||||
GLAD_VERSION_MINOR(version));
|
||||
|
||||
XWindowAttributes gwa;
|
||||
XGetWindowAttributes(display, window, &gwa);
|
||||
glViewport(0, 0, gwa.width, gwa.height);
|
||||
|
||||
// Setup Shaders
|
||||
mons_shader vertex_shader = mons_create_shader(
|
||||
MONS_SHADER_TYPE_VERTEX, (char *)__embed_shaders_basic_vert_glsl,
|
||||
__embed_shaders_basic_vert_glsl_len);
|
||||
mons_shader fragment_shader = mons_create_shader(
|
||||
MONS_SHADER_TYPE_FRAGMENT, (char *)__embed_shaders_basic_lit_frag_glsl,
|
||||
__embed_shaders_basic_lit_frag_glsl_len);
|
||||
mons_program shader_program =
|
||||
mons_create_program(vertex_shader, fragment_shader);
|
||||
|
||||
FILE *texture_file = fopen("test.qoi", "rb");
|
||||
mons_texture texture = mons_texture_load(texture_file);
|
||||
|
||||
mons_camera camera = {
|
||||
.projection =
|
||||
{
|
||||
.near = 0.01f,
|
||||
.far = 100.0f,
|
||||
.fov = M_PI / 4.0f,
|
||||
.aspect_ratio = gwa.width / (float)gwa.height,
|
||||
},
|
||||
.transform = {
|
||||
.translation = mons_vec3_mul_i(MONS_VEC3_Z, 3),
|
||||
.rotation = MONS_QUAT_IDENTITY,
|
||||
.scale = MONS_VEC3_ONE, }}; clock_gettime(CLOCK_MONOTONIC_RAW, ¤t_time);
|
||||
mons_model *models = NULL;
|
||||
int model_count = 0;
|
||||
mons_load_gltf("marble_bust/marble_bust_01_4k.gltf", shader_program,
|
||||
&models, &model_count);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
mons_directional_light light = {
|
||||
.transform = (mons_transform) {
|
||||
.translation = (mons_vec3) { 0.0f, 0.0f, 3.0f },
|
||||
.rotation = MONS_QUAT_IDENTITY,
|
||||
.scale = MONS_VEC3_ONE,
|
||||
},
|
||||
.color = MONS_COLOR_WHITE,
|
||||
};
|
||||
mons_shader_set_vec4_global("light_color", light.color);
|
||||
mons_shader_set_vec3_global("light_position", light.transform.translation);
|
||||
|
||||
float ambient_light = 0.1f;
|
||||
mons_shader_set_float_global("ambient_strength", ambient_light);
|
||||
|
||||
// Main Loop
|
||||
bool key_pressed[256] = {false};
|
||||
bool key_just_pressed[256] = {false};
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
memset(key_just_pressed, false, 256);
|
||||
while (XPending(display)) {
|
||||
XEvent xev;
|
||||
XNextEvent(display, &xev);
|
||||
switch (xev.type) {
|
||||
case KeyPress:
|
||||
key_pressed[xev.xkey.keycode] = true;
|
||||
key_just_pressed[xev.xkey.keycode] = true;
|
||||
break;
|
||||
case KeyRelease:
|
||||
key_pressed[xev.xkey.keycode] = false;
|
||||
break;
|
||||
case ConfigureNotify:
|
||||
if (xev.xconfigure.window == window) {
|
||||
camera.projection.aspect_ratio =
|
||||
xev.xconfigure.width / (float)xev.xconfigure.height;
|
||||
glViewport(0, 0, xev.xconfigure.width,
|
||||
xev.xconfigure.height);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
struct timespec new_time;
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &new_time);
|
||||
delta_time = (new_time.tv_sec - current_time.tv_sec) +
|
||||
(new_time.tv_nsec - current_time.tv_nsec) / 1.0e9;
|
||||
current_time = new_time;
|
||||
|
||||
mons_shader_set_mat4_global("view", mons_mat4_inverse(mons_transform_matrix(camera.transform)), GL_TRUE);
|
||||
mons_shader_set_mat4_global("projection", mons_projection_matrix(camera.projection), GL_FALSE);
|
||||
for (int i = 0; i < model_count; i++) {
|
||||
mons_model_draw(models[i]);
|
||||
mons_transform_rotate(&models[i].transform, mons_quat_from_axis_angle(MONS_VEC3_Y, (M_PI / 4.0f) * delta_time));
|
||||
}
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
|
||||
if (key_pressed[MONS_KEY_ESCAPE]) {
|
||||
quit = true;
|
||||
}
|
||||
|
||||
int x_axis_input = key_pressed[MONS_KEY_D] - key_pressed[MONS_KEY_A];
|
||||
mons_transform_translate(
|
||||
&camera.transform,
|
||||
mons_vec3_mul_f(mons_transform_right(camera.transform), delta_time * x_axis_input));
|
||||
int y_axis_input =
|
||||
key_pressed[MONS_KEY_SPACE] - key_pressed[MONS_KEY_LSHIFT];
|
||||
mons_transform_translate(
|
||||
&camera.transform,
|
||||
mons_vec3_mul_f(mons_transform_up(camera.transform), delta_time * y_axis_input));
|
||||
int z_axis_input = key_pressed[MONS_KEY_W] - key_pressed[MONS_KEY_S];
|
||||
mons_transform_translate(
|
||||
&camera.transform,
|
||||
mons_vec3_mul_f(mons_transform_forward(camera.transform), delta_time * -z_axis_input));
|
||||
int y_rot_input =
|
||||
key_pressed[MONS_KEY_RIGHT] - key_pressed[MONS_KEY_LEFT];
|
||||
mons_transform_rotate(
|
||||
&camera.transform,
|
||||
mons_quat_from_axis_angle(MONS_VEC3_Y,
|
||||
-M_PI / 2.0f * y_rot_input * delta_time));
|
||||
int x_rot_input = key_pressed[MONS_KEY_UP] - key_pressed[MONS_KEY_DOWN];
|
||||
mons_transform_rotate(
|
||||
&camera.transform,
|
||||
mons_quat_from_axis_angle(MONS_VEC3_X,
|
||||
M_PI / 2.0f * x_rot_input * delta_time));
|
||||
|
||||
int fov_input = key_pressed[MONS_KEY_MINUS] - key_pressed[MONS_KEY_EQUAL];
|
||||
camera.projection.fov += (fov_input * M_PI / 8.0 * delta_time);
|
||||
if(key_just_pressed[MONS_KEY_BACKSPACE]) {
|
||||
mons_transform_look_at(&camera.transform, MONS_VEC3_ZERO, MONS_VEC3_Y);
|
||||
}
|
||||
glXSwapBuffers(display, window);
|
||||
}
|
||||
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
|
||||
glXMakeCurrent(display, 0, 0);
|
||||
glXDestroyContext(display, context);
|
||||
|
||||
XDestroyWindow(display, window);
|
||||
XFreeColormap(display, colormap);
|
||||
XCloseDisplay(display);
|
||||
|
||||
gladLoaderUnloadGLX();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
30
mons_gltf/CMakeLists.txt
Normal file
30
mons_gltf/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_gltf LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_library(mons_gltf
|
||||
SHARED
|
||||
./src/gltf.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_gltf PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_gltf PRIVATE -coverage)
|
||||
target_link_options(mons_gltf PRIVATE -coverage)
|
||||
target_link_libraries(mons_gltf PUBLIC mons_json mons_3d)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_gltf)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
11
mons_gltf/include/gltf.h
Normal file
11
mons_gltf/include/gltf.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef MONS_GLTF_H
|
||||
#define MONS_GLTF_H
|
||||
|
||||
#include "shader.h"
|
||||
#include <stdio.h>
|
||||
|
||||
struct mons_model;
|
||||
|
||||
int mons_load_gltf(char *path, mons_program shader, struct mons_model **out, int *count);
|
||||
|
||||
#endif
|
549
mons_gltf/src/gltf.c
Normal file
549
mons_gltf/src/gltf.c
Normal file
@ -0,0 +1,549 @@
|
||||
#include "gltf.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <libgen.h>
|
||||
#include <string.h>
|
||||
#include "json.h"
|
||||
#include "transform.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/quat.h"
|
||||
#include "vertex.h"
|
||||
#include "mesh.h"
|
||||
#include "texture.h"
|
||||
#include "image.h"
|
||||
#include "qoi.h"
|
||||
#include "model.h"
|
||||
#include "shader.h"
|
||||
|
||||
// TODO: Morph Targets
|
||||
// TODO: Skins
|
||||
|
||||
typedef enum mons_gltf_buffer_view_target {
|
||||
MONS_GLTF_BUFFER_VIEW_ARRAY_BUFFER = 34962,
|
||||
MONS_GLTF_BUFFER_VIEW_ELEMENT_ARRAY_BUFFER = 34963,
|
||||
} mons_gltf_buffer_view_target;
|
||||
|
||||
typedef struct mons_gltf_buffer_view {
|
||||
int buffer_index;
|
||||
int byte_length;
|
||||
int byte_offset;
|
||||
int byte_stride;
|
||||
mons_gltf_buffer_view_target target;
|
||||
} mons_gltf_buffer_view;
|
||||
|
||||
typedef enum mons_gltf_accessor_component_type {
|
||||
MONS_GLTF_ACCESSOR_BYTE = 5120,
|
||||
MONS_GLTF_ACCESSOR_UNSIGNED_BYTE = 5121,
|
||||
MONS_GLTF_ACCESSOR_SHORT = 5122,
|
||||
MONS_GLTF_ACCESSOR_UNSIGNED_SHORT = 5123,
|
||||
MONS_GLTF_ACCESSOR_UNSIGNED_INT = 5125,
|
||||
MONS_GLTF_ACCESSOR_FLOAT = 5126,
|
||||
} mons_gltf_accessor_component_type;
|
||||
|
||||
typedef enum mons_gltf_accessor_type {
|
||||
MONS_GLTF_ACCESSOR_SCALAR = 1,
|
||||
MONS_GLTF_ACCESSOR_VEC2 = 2,
|
||||
MONS_GLTF_ACCESSOR_VEC3 = 3,
|
||||
MONS_GLTF_ACCESSOR_VEC4 = 4,
|
||||
MONS_GLTF_ACCESSOR_MAT2 = 4,
|
||||
MONS_GLTF_ACCESSOR_MAT3 = 9,
|
||||
MONS_GLTF_ACCESSOR_MAT4 = 16,
|
||||
} mons_gltf_accessor_type;
|
||||
|
||||
typedef struct mons_gltf_accessor {
|
||||
int buffer_view_index;
|
||||
int byte_offset;
|
||||
mons_gltf_accessor_component_type component_type;
|
||||
int count;
|
||||
float *min;
|
||||
float *max;
|
||||
mons_gltf_accessor_type type;
|
||||
} mons_gltf_accessor;
|
||||
|
||||
typedef enum mons_gltf_primitive_mode {
|
||||
MONS_GLTF_MODE_POINTS,
|
||||
MONS_GLTF_MODE_LINES,
|
||||
MONS_GLTF_MODE_LINE_LOOP,
|
||||
MONS_GLTF_MODE_LINE_STRIP,
|
||||
MONS_GLTF_MODE_TRIANGLES,
|
||||
MONS_GLTF_MODE_TRIANGLE_STRIP,
|
||||
MONS_GLTF_MODE_TRIANGLE_FAN,
|
||||
} mons_gltf_primitive_mode;
|
||||
|
||||
typedef struct mons_gltf_primitive {
|
||||
mons_hashmap attributes;
|
||||
int indices_accessor;
|
||||
int material;
|
||||
mons_gltf_primitive_mode mode;
|
||||
} mons_gltf_primitive;
|
||||
|
||||
typedef struct mons_gltf_mesh {
|
||||
mons_gltf_primitive *primitives;
|
||||
} mons_gltf_mesh;
|
||||
|
||||
typedef struct mons_gltf_texture {
|
||||
int sampler_index;
|
||||
int source_index;
|
||||
} mons_gltf_texture;
|
||||
|
||||
typedef enum mons_gltf_sampler_filter {
|
||||
MONS_SAMPLER_NEAREST = 9728,
|
||||
MONS_SAMPLER_LINEAR = 9729,
|
||||
MONS_SAMPLER_NEAREST_MIPMAP_NEAREST = 9984,
|
||||
MONS_SAMPLER_LINEAR_MIPMAP_NEAREST = 9985,
|
||||
MONS_SAMPLER_NEAREST_MIPMAP_LINEAR = 9986,
|
||||
MONS_SAMPLER_LINEAR_MIPMAP_LINEAR = 9987,
|
||||
} mons_gltf_sampler_filter;
|
||||
|
||||
typedef enum mons_gltf_sampler_wrapmode {
|
||||
MONS_SAMPLER_CLAMP_TO_EDGE = 33071,
|
||||
MONS_SAMPLER_MIRRORED_REPEAT = 33648,
|
||||
MONS_SAMPLER_REPEAT = 10497,
|
||||
} mons_gltf_sampler_wrapmode;
|
||||
|
||||
typedef struct mons_gltf_sampler {
|
||||
mons_gltf_sampler_filter mag_filter;
|
||||
mons_gltf_sampler_filter min_filter;
|
||||
mons_gltf_sampler_wrapmode wrap_s;
|
||||
mons_gltf_sampler_wrapmode wrap_t;
|
||||
} mons_gltf_sampler;
|
||||
|
||||
typedef struct mons_gltf_material {
|
||||
int base_color_texture_index;
|
||||
int normal_texture_index;
|
||||
int metallic_roughness_texture_index;
|
||||
} mons_gltf_material;
|
||||
|
||||
int mons_load_gltf(char *path, mons_program shader, mons_model **out, int *count) {
|
||||
|
||||
FILE *stream = fopen(path, "r");
|
||||
// Read JSON into memory
|
||||
fseek(stream, 0L, SEEK_END);
|
||||
unsigned long len = ftell(stream);
|
||||
char *json_buffer = malloc(len);
|
||||
rewind(stream);
|
||||
fread(json_buffer, 1, len, stream);
|
||||
fclose(stream);
|
||||
mons_json_value json;
|
||||
if (mons_json_parse(json_buffer, &json) == EXIT_FAILURE) {
|
||||
printf("Failed to parse JSON\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
mons_json_value asset_info;
|
||||
mons_json_get_value(json, "asset", &asset_info);
|
||||
char *asset_info_str = mons_json_to_string(asset_info);
|
||||
printf("Loading GLTF Asset: %s\n", asset_info_str);
|
||||
free(asset_info_str);
|
||||
|
||||
mons_json_array gltf_nodes_array;
|
||||
mons_json_get_array(json, "nodes", &gltf_nodes_array);
|
||||
printf("GLTF Node Count: %d\n", gltf_nodes_array.len);
|
||||
for (int i = 0; i < gltf_nodes_array.len; i++) {
|
||||
mons_json_value node_info = gltf_nodes_array.values[i];
|
||||
char *name;
|
||||
mons_json_get_string(node_info, "name", &name);
|
||||
printf("\t%d: %s\n", i, name);
|
||||
mons_transform transform = MONS_TRANSFORM_IDENTITY;
|
||||
mons_json_array node_transform_matrix_json;
|
||||
if (mons_json_get_array(node_info, "matrix",
|
||||
&node_transform_matrix_json) == EXIT_SUCCESS) {
|
||||
for (int j = 0; j < node_transform_matrix_json.len; j++) {
|
||||
*(((float *)&transform) + j) =
|
||||
node_transform_matrix_json.values[j].data.number;
|
||||
}
|
||||
} else {
|
||||
|
||||
mons_vec3 translation = MONS_VEC3_ZERO;
|
||||
mons_vec3 scale = MONS_VEC3_ONE;
|
||||
mons_quat rotation = MONS_QUAT_IDENTITY;
|
||||
mons_json_array node_translation_json;
|
||||
mons_json_array node_rotation_json;
|
||||
mons_json_array node_scale_json;
|
||||
int has_translation =
|
||||
(mons_json_get_array(node_info, "translation",
|
||||
&node_translation_json) == EXIT_SUCCESS);
|
||||
int has_rotation =
|
||||
(mons_json_get_array(node_info, "rotation",
|
||||
&node_rotation_json) == EXIT_SUCCESS);
|
||||
int has_scale =
|
||||
(mons_json_get_array(node_info, "scale",
|
||||
&node_translation_json) == EXIT_SUCCESS);
|
||||
|
||||
if (has_translation) {
|
||||
for (int j = 0; j < node_translation_json.len; j++) {
|
||||
*(((float *)&translation) + j) =
|
||||
node_translation_json.values[j].data.number;
|
||||
}
|
||||
printf("TRANSLATION: %f, %f, %f\n", translation.x,
|
||||
translation.y, translation.z);
|
||||
}
|
||||
if (has_rotation) {
|
||||
printf("ROTATION\n");
|
||||
for (int j = 0; j < node_rotation_json.len; j++) {
|
||||
*(((float *)&rotation) + j) =
|
||||
node_rotation_json.values[j].data.number;
|
||||
}
|
||||
}
|
||||
if (has_scale) {
|
||||
printf("SCALE\n");
|
||||
for (int j = 0; j < node_scale_json.len; j++) {
|
||||
*(((float *)&scale) + j) =
|
||||
node_scale_json.values[j].data.number;
|
||||
}
|
||||
}
|
||||
if (has_scale || has_rotation || has_translation) {
|
||||
transform = (mons_transform) {
|
||||
translation,
|
||||
scale,
|
||||
rotation,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mons_json_array gltf_scenes_array;
|
||||
mons_json_get_array(json, "scenes", &gltf_scenes_array);
|
||||
printf("GLTF Scene Count: %d\n", gltf_scenes_array.len);
|
||||
for (int i = 0; i < gltf_scenes_array.len; i++) {
|
||||
mons_json_value scene_info = gltf_scenes_array.values[i];
|
||||
char *name;
|
||||
mons_json_get_string(scene_info, "name", &name);
|
||||
printf("\t%d: %s\n", i, name);
|
||||
mons_json_array scene_nodes_array;
|
||||
mons_json_get_array(scene_info, "nodes", &scene_nodes_array);
|
||||
printf("\t%d Nodes:\n", scene_nodes_array.len);
|
||||
for (int j = 0; j < scene_nodes_array.len; j++) {
|
||||
int node_index = scene_nodes_array.values[j].data.number;
|
||||
printf("\t\t%d\n", node_index);
|
||||
}
|
||||
}
|
||||
|
||||
mons_json_array gltf_buffers_array;
|
||||
mons_json_get_array(json, "buffers", &gltf_buffers_array);
|
||||
printf("GLTF Buffer Count: %d\n", gltf_buffers_array.len);
|
||||
FILE **buffers = calloc(gltf_buffers_array.len, sizeof(FILE *));
|
||||
for (int i = 0; i < gltf_buffers_array.len; i++) {
|
||||
mons_json_value buffer_info = gltf_buffers_array.values[i];
|
||||
char *buffer_uri;
|
||||
mons_json_get_string(buffer_info, "uri", &buffer_uri);
|
||||
char *path_copy = strdup(path);
|
||||
char *base_path = dirname(path_copy);
|
||||
char *full_buffer_path =
|
||||
calloc(strlen(base_path) + strlen(buffer_uri) + 2, 1);
|
||||
snprintf(full_buffer_path, strlen(base_path) + strlen(buffer_uri) + 2,
|
||||
"%s/%s", base_path, buffer_uri);
|
||||
printf("\t%d: %s\n", i, full_buffer_path);
|
||||
buffers[i] = fopen(full_buffer_path, "rb");
|
||||
if (buffers[i] == NULL) {
|
||||
printf("Failed to open buffer for reading\n");
|
||||
}
|
||||
free(path_copy);
|
||||
}
|
||||
|
||||
mons_json_array gltf_buffer_views_array;
|
||||
mons_json_get_array(json, "bufferViews", &gltf_buffer_views_array);
|
||||
printf("GLTF Buffer View Count: %d\n", gltf_buffer_views_array.len);
|
||||
mons_gltf_buffer_view *buffer_views =
|
||||
calloc(gltf_buffer_views_array.len, sizeof(mons_gltf_buffer_view));
|
||||
for (int i = 0; i < gltf_buffer_views_array.len; i++) {
|
||||
mons_json_value buffer_view_info = gltf_buffer_views_array.values[i];
|
||||
mons_json_get_int(buffer_view_info, "buffer",
|
||||
&buffer_views[i].buffer_index);
|
||||
mons_json_get_int(buffer_view_info, "byteLength",
|
||||
&buffer_views[i].byte_length);
|
||||
mons_json_get_int(buffer_view_info, "byteOffset",
|
||||
&buffer_views[i].byte_offset);
|
||||
mons_json_get_int(buffer_view_info, "byteStride",
|
||||
&buffer_views[i].byte_stride);
|
||||
mons_json_get_int(buffer_view_info, "target",
|
||||
(int *)(&buffer_views[i].target));
|
||||
printf("%d: %d bytes @ %d+%d, type %d, stride %d\n", i,
|
||||
buffer_views[i].byte_length, buffer_views[i].buffer_index,
|
||||
buffer_views[i].byte_offset, buffer_views[i].target,
|
||||
buffer_views[i].byte_stride);
|
||||
}
|
||||
|
||||
mons_json_array gltf_accessors_array;
|
||||
mons_json_get_array(json, "accessors", &gltf_accessors_array);
|
||||
printf("GLTF Accessor Count: %d\n", gltf_accessors_array.len);
|
||||
mons_gltf_accessor *accessors =
|
||||
calloc(gltf_accessors_array.len, sizeof(mons_gltf_accessor));
|
||||
for (int i = 0; i < gltf_accessors_array.len; i++) {
|
||||
mons_json_value accessor_info = gltf_accessors_array.values[i];
|
||||
mons_json_get_int(accessor_info, "bufferView",
|
||||
&accessors[i].buffer_view_index);
|
||||
mons_json_get_int(accessor_info, "byteOffset",
|
||||
&accessors[i].byte_offset);
|
||||
mons_json_get_int(accessor_info, "componentType",
|
||||
(int *)(&accessors[i].component_type));
|
||||
mons_json_get_int(accessor_info, "count", &accessors[i].count);
|
||||
char *accessor_type;
|
||||
mons_json_get_string(accessor_info, "type", &accessor_type);
|
||||
if (strcmp(accessor_type, "SCALAR") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_SCALAR;
|
||||
} else if (strcmp(accessor_type, "VEC2") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_VEC2;
|
||||
} else if (strcmp(accessor_type, "VEC3") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_VEC3;
|
||||
} else if (strcmp(accessor_type, "VEC4") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_VEC4;
|
||||
} else if (strcmp(accessor_type, "MAT2") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_MAT2;
|
||||
} else if (strcmp(accessor_type, "MAT3") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_MAT3;
|
||||
} else if (strcmp(accessor_type, "MAT4") == 0) {
|
||||
accessors[i].type = MONS_GLTF_ACCESSOR_MAT4;
|
||||
}
|
||||
mons_json_array min_array;
|
||||
if (mons_json_get_array(accessor_info, "min", &min_array) ==
|
||||
EXIT_SUCCESS) {
|
||||
accessors[i].min = calloc(accessors[i].type, sizeof(float));
|
||||
for (int j = 0; j < accessors[i].type; j++) {
|
||||
accessors[i].min[j] = min_array.values[j].data.number;
|
||||
}
|
||||
}
|
||||
mons_json_array max_array;
|
||||
if (mons_json_get_array(accessor_info, "max", &max_array) ==
|
||||
EXIT_SUCCESS) {
|
||||
accessors[i].max = calloc(accessors[i].type, sizeof(float));
|
||||
for (int j = 0; j < accessors[i].type; j++) {
|
||||
accessors[i].max[j] = max_array.values[j].data.number;
|
||||
}
|
||||
}
|
||||
printf("%d: %d %d-component elements @ %d+%d\n", i, accessors[i].count,
|
||||
accessors[i].type, accessors[i].buffer_view_index,
|
||||
accessors[i].byte_offset);
|
||||
if (accessors[i].min != NULL) {
|
||||
printf("\tMin: [");
|
||||
for (int j = 0; j < accessors[i].type; j++) {
|
||||
printf("%f", accessors[i].min[j]);
|
||||
if (j + 1 < accessors[i].type) {
|
||||
printf(", ");
|
||||
} else {
|
||||
printf("]\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (accessors[i].max != NULL) {
|
||||
printf("\tMax: [");
|
||||
for (int j = 0; j < accessors[i].type; j++) {
|
||||
printf("%f", accessors[i].max[j]);
|
||||
if (j + 1 < accessors[i].type) {
|
||||
printf(", ");
|
||||
} else {
|
||||
printf("]\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mons_json_array gltf_images_array;
|
||||
mons_json_get_array(json, "images", &gltf_images_array);
|
||||
printf("GLTF Image Count: %d\n", gltf_images_array.len);
|
||||
mons_image *images = calloc(gltf_images_array.len, sizeof(mons_image));
|
||||
for (int i = 0; i < gltf_images_array.len; i++) {
|
||||
mons_json_value image_info = gltf_images_array.values[i];
|
||||
char *mimetype = NULL;
|
||||
mons_json_get_string(image_info, "mimeType", &mimetype);
|
||||
int buffer_view_index;
|
||||
FILE *image_buffer = NULL;
|
||||
if (mons_json_get_int(image_info, "bufferView", &buffer_view_index) ==
|
||||
EXIT_SUCCESS) {
|
||||
if (mimetype == NULL) {
|
||||
printf(
|
||||
"Cannot load image from buffer view, missing mimetype\n");
|
||||
continue;
|
||||
}
|
||||
mons_gltf_buffer_view image_buffer_view =
|
||||
buffer_views[buffer_view_index];
|
||||
image_buffer = buffers[image_buffer_view.buffer_index];
|
||||
}
|
||||
char *uri = NULL;
|
||||
if(mons_json_get_string(image_info, "uri", &uri) == EXIT_SUCCESS) {
|
||||
char *path_copy = strdup(path);
|
||||
char *base_path = dirname(path_copy);
|
||||
char *full_buffer_path =
|
||||
calloc(strlen(base_path) + strlen(uri) + 2, 1);
|
||||
snprintf(full_buffer_path, strlen(base_path) + strlen(uri) + 2,
|
||||
"%s/%s", base_path, uri);
|
||||
printf("%d: %s\n", i, full_buffer_path);
|
||||
image_buffer = fopen(full_buffer_path, "rb");
|
||||
}
|
||||
|
||||
if(image_buffer == NULL) {
|
||||
printf("Failed to open image buffer for reading\n");
|
||||
continue;
|
||||
}
|
||||
if (strcmp(mimetype, "image/qoi") == 0 ||
|
||||
(uri != NULL && strcmp(strrchr(uri, '.'), "qoi") == 0)) {
|
||||
images[i] = mons_load_qoi(image_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
mons_json_array gltf_samplers_array;
|
||||
mons_json_get_array(json, "samplers", &gltf_samplers_array);
|
||||
printf("GLTF Sampler Count: %d\n", gltf_samplers_array.len);
|
||||
mons_gltf_sampler *samplers = calloc(gltf_samplers_array.len, sizeof(mons_gltf_sampler));
|
||||
for(int i = 0; i < gltf_samplers_array.len; i++) {
|
||||
mons_json_value sampler_info = gltf_samplers_array.values[i];
|
||||
mons_json_get_int(sampler_info, "magFilter", (int*)&samplers[i].mag_filter);
|
||||
mons_json_get_int(sampler_info, "minFilter", (int*)&samplers[i].min_filter);
|
||||
mons_json_get_int(sampler_info, "wrapS", (int*)&samplers[i].wrap_s);
|
||||
mons_json_get_int(sampler_info, "wrapT", (int*)&samplers[i].wrap_t);
|
||||
printf("%d: min %d, mag %d, wrap %d, %d\n", i, samplers[i].min_filter, samplers[i].mag_filter, samplers[i].wrap_s, samplers[i].wrap_t);
|
||||
}
|
||||
|
||||
mons_json_array gltf_textures_array;
|
||||
mons_json_get_array(json, "textures", &gltf_textures_array);
|
||||
printf("GLTF Texture Count: %d\n", gltf_textures_array.len);
|
||||
mons_gltf_texture *textures =
|
||||
calloc(gltf_textures_array.len, sizeof(mons_gltf_texture));
|
||||
for (int i = 0; i < gltf_textures_array.len; i++) {
|
||||
mons_json_value texture_info = gltf_textures_array.values[i];
|
||||
textures[i].sampler_index = -1;
|
||||
textures[i].source_index = -1;
|
||||
mons_json_get_int(texture_info, "sampler", &textures[i].sampler_index);
|
||||
mons_json_get_int(texture_info, "source", &textures[i].source_index);
|
||||
printf("%d: sampler %d, source %d\n", i, textures[i].sampler_index,
|
||||
textures[i].source_index);
|
||||
}
|
||||
|
||||
mons_json_array gltf_materials_array;
|
||||
mons_json_get_array(json, "materials", &gltf_materials_array);
|
||||
mons_gltf_material *materials = calloc(gltf_materials_array.len, sizeof(mons_gltf_material));
|
||||
printf("GLTF Material Count: %d\n", gltf_materials_array.len);
|
||||
for (int i = 0; i < gltf_materials_array.len; i++) {
|
||||
mons_json_value material_info = gltf_materials_array.values[i];
|
||||
mons_json_get_int(material_info, "pbrMetallicRoughness.baseColorTexture.index", &materials[i].base_color_texture_index);
|
||||
mons_json_get_int(material_info, "normalTexture.index", &materials[i].normal_texture_index);
|
||||
mons_json_get_int(material_info, "pbrMetallicRoughness.metallicRoughnessTexture.index", &materials[i].metallic_roughness_texture_index);
|
||||
}
|
||||
|
||||
mons_json_array gltf_mesh_array;
|
||||
mons_json_get_array(json, "meshes", &gltf_mesh_array);
|
||||
printf("GLTF Mesh Count: %d\n", gltf_mesh_array.len);
|
||||
mons_model *models = calloc(gltf_mesh_array.len, sizeof(mons_model));
|
||||
for (int i = 0; i < gltf_mesh_array.len; i++) {
|
||||
mons_json_value mesh_info = gltf_mesh_array.values[i];
|
||||
mons_json_array primitives_array;
|
||||
mons_json_get_array(mesh_info, "primitives", &primitives_array);
|
||||
printf("%d: %d Primitives\n", i, primitives_array.len);
|
||||
for (int j = 0; j < primitives_array.len; j++) {
|
||||
mons_json_value primitive_info = primitives_array.values[j];
|
||||
mons_vertex *vertices;
|
||||
int vertex_count;
|
||||
int position_accessor_index;
|
||||
if (mons_json_get_int(primitive_info, "attributes.POSITION",
|
||||
&position_accessor_index) == EXIT_SUCCESS) {
|
||||
mons_gltf_accessor position_accessor =
|
||||
accessors[position_accessor_index];
|
||||
vertices = calloc(position_accessor.count, sizeof(mons_vertex));
|
||||
mons_gltf_buffer_view position_buffer_view =
|
||||
buffer_views[position_accessor.buffer_view_index];
|
||||
FILE *position_buffer =
|
||||
buffers[position_buffer_view.buffer_index];
|
||||
mons_vec3 *positions =
|
||||
calloc(position_accessor.count, sizeof(mons_vec3));
|
||||
fseek(position_buffer, position_buffer_view.byte_offset,
|
||||
SEEK_SET);
|
||||
fread(positions, sizeof(mons_vec3), position_accessor.count,
|
||||
position_buffer);
|
||||
for (int k = 0; k < position_accessor.count; k++) {
|
||||
vertices[k].position = positions[k];
|
||||
}
|
||||
vertex_count = position_accessor.count;
|
||||
}
|
||||
int texcoord_accessor_index;
|
||||
if (mons_json_get_int(primitive_info, "attributes.TEXCOORD_0",
|
||||
&texcoord_accessor_index) == EXIT_SUCCESS) {
|
||||
mons_gltf_accessor texcoord_accessor =
|
||||
accessors[texcoord_accessor_index];
|
||||
mons_gltf_buffer_view texcoord_buffer_view =
|
||||
buffer_views[texcoord_accessor.buffer_view_index];
|
||||
FILE *texcoord_buffer =
|
||||
buffers[texcoord_buffer_view.buffer_index];
|
||||
mons_vec2 *texcoords =
|
||||
calloc(texcoord_accessor.count, sizeof(mons_vec2));
|
||||
fseek(texcoord_buffer, texcoord_buffer_view.byte_offset,
|
||||
SEEK_SET);
|
||||
fread(texcoords, sizeof(mons_vec2), texcoord_accessor.count,
|
||||
texcoord_buffer);
|
||||
for (int k = 0; k < texcoord_accessor.count; k++) {
|
||||
vertices[k].texture_coords = texcoords[k];
|
||||
}
|
||||
}
|
||||
int index_accessor_index;
|
||||
int *indices;
|
||||
int index_count;
|
||||
if (mons_json_get_int(primitive_info, "indices",
|
||||
&index_accessor_index) == EXIT_SUCCESS) {
|
||||
mons_gltf_accessor index_accessor =
|
||||
accessors[index_accessor_index];
|
||||
mons_gltf_buffer_view index_buffer_view =
|
||||
buffer_views[index_accessor.buffer_view_index];
|
||||
FILE *index_buffer = buffers[index_buffer_view.buffer_index];
|
||||
indices = calloc(index_accessor.count, sizeof(int));
|
||||
unsigned short *index_shorts =
|
||||
calloc(index_accessor.count, sizeof(unsigned short));
|
||||
fseek(index_buffer, index_buffer_view.byte_offset, SEEK_SET);
|
||||
fread(index_shorts, sizeof(unsigned short),
|
||||
index_accessor.count, index_buffer);
|
||||
for (int k = 0; k < index_accessor.count; k++) {
|
||||
indices[k] = index_shorts[k];
|
||||
}
|
||||
index_count = index_accessor.count;
|
||||
}
|
||||
int normal_accessor_index;
|
||||
if (mons_json_get_int(primitive_info, "attributes.NORMAL",
|
||||
&normal_accessor_index) == EXIT_SUCCESS) {
|
||||
mons_gltf_accessor normal_accessor =
|
||||
accessors[normal_accessor_index];
|
||||
mons_gltf_buffer_view normal_buffer_view =
|
||||
buffer_views[normal_accessor.buffer_view_index];
|
||||
FILE *normal_buffer =
|
||||
buffers[normal_buffer_view.buffer_index];
|
||||
mons_vec3 *normals =
|
||||
calloc(normal_accessor.count, sizeof(mons_vec3));
|
||||
fseek(normal_buffer, normal_buffer_view.byte_offset,
|
||||
SEEK_SET);
|
||||
fread(normals, sizeof(mons_vec3), normal_accessor.count,
|
||||
normal_buffer);
|
||||
for (int k = 0; k < normal_accessor.count; k++) {
|
||||
vertices[k].normal = normals[k];
|
||||
}
|
||||
}
|
||||
|
||||
int material_index;
|
||||
mons_json_get_int(primitive_info, "material", &material_index);
|
||||
|
||||
mons_gltf_material material = materials[material_index];
|
||||
|
||||
mons_texture *textures = calloc(3, sizeof(mons_texture));
|
||||
textures[0] = mons_texture_from_image(images[material.base_color_texture_index]);
|
||||
textures[1] = mons_texture_from_image(images[material.normal_texture_index]);
|
||||
textures[2] = mons_texture_from_image(images[material.metallic_roughness_texture_index]);
|
||||
|
||||
models[i] = (mons_model) {
|
||||
.mesh = mons_create_mesh(vertices, vertex_count, indices, index_count),
|
||||
.textures = textures,
|
||||
.textures_len = 3,
|
||||
.shader = shader,
|
||||
.transform = MONS_TRANSFORM_IDENTITY,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
*out = models;
|
||||
*count = gltf_mesh_array.len;
|
||||
|
||||
for (int i = 0; i < gltf_accessors_array.len; i++) {
|
||||
free(accessors[i].min);
|
||||
free(accessors[i].max);
|
||||
}
|
||||
free(accessors);
|
||||
free(buffer_views);
|
||||
free(buffers);
|
||||
mons_json_free(json);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
30
mons_image/CMakeLists.txt
Normal file
30
mons_image/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_image LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_library(mons_image
|
||||
SHARED
|
||||
./src/image.c
|
||||
./src/qoi.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_image PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_image PRIVATE -coverage)
|
||||
target_link_options(mons_image PRIVATE -coverage)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_image)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
19
mons_image/include/image.h
Normal file
19
mons_image/include/image.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef MONS_IMAGE_H
|
||||
#define MONS_IMAGE_H
|
||||
|
||||
typedef struct mons_pixel {
|
||||
unsigned char r;
|
||||
unsigned char g;
|
||||
unsigned char b;
|
||||
unsigned char a;
|
||||
} mons_pixel;
|
||||
|
||||
typedef struct mons_image {
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
mons_pixel *data;
|
||||
} mons_image;
|
||||
|
||||
void mons_image_free(mons_image *image);
|
||||
|
||||
#endif
|
7
mons_image/include/qoi.h
Normal file
7
mons_image/include/qoi.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef MONS_QOI_H
|
||||
#define MONS_QOI_H
|
||||
#include <stdio.h>
|
||||
|
||||
struct mons_image mons_load_qoi(FILE *stream);
|
||||
|
||||
#endif
|
6
mons_image/src/image.c
Normal file
6
mons_image/src/image.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include "image.h"
|
||||
|
||||
void mons_image_free(mons_image *image) {
|
||||
free(image->data);
|
||||
}
|
140
mons_image/src/qoi.c
Normal file
140
mons_image/src/qoi.c
Normal file
@ -0,0 +1,140 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <byteswap.h>
|
||||
#include <inttypes.h>
|
||||
#include "qoi.h"
|
||||
#include "image.h"
|
||||
|
||||
#define MONS_QOI_RGB 3
|
||||
#define MONS_QOI_RGBA 4
|
||||
#define MONS_QOI_SRGB_LINEAR_ALPHA 0
|
||||
#define MONS_QOI_LINEAR 1
|
||||
#define MONS_QOI_HEADER_LEN 14
|
||||
|
||||
#define MONS_QOI_OP_RGB 0b11111110
|
||||
#define MONS_QOI_OP_RGBA 0b11111111
|
||||
#define MONS_QOI_OP_INDEX 0b00000000
|
||||
#define MONS_QOI_OP_DIFF 0b01000000
|
||||
#define MONS_QOI_OP_LUMA 0b10000000
|
||||
#define MONS_QOI_OP_RUN 0b11000000
|
||||
|
||||
typedef struct mons_qoi_header {
|
||||
char magic[4];
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint8_t channels;
|
||||
uint8_t colorspace;
|
||||
} mons_qoi_header;
|
||||
|
||||
unsigned char pixel_index_hash(mons_pixel pixel) {
|
||||
return (pixel.r * 3 + pixel.g * 5 + pixel.b * 7 + pixel.a * 11) % 64;
|
||||
}
|
||||
|
||||
mons_image mons_load_qoi(FILE *stream) {
|
||||
mons_qoi_header header;
|
||||
if (fread(&header, MONS_QOI_HEADER_LEN, 1, stream) != 1) {
|
||||
fprintf(stderr, "Error reading QOI header");
|
||||
return (mons_image){0};
|
||||
}
|
||||
header.width = __bswap_32(header.width);
|
||||
header.height = __bswap_32(header.height);
|
||||
mons_pixel previous = {0, 0, 0, 255};
|
||||
mons_pixel previously_seen[64] = {0};
|
||||
mons_image output = {
|
||||
.width = header.width,
|
||||
.height = header.height,
|
||||
.data = calloc(4, header.width * header.height),
|
||||
};
|
||||
for (size_t i = 0; i < header.width * header.height; i++) {
|
||||
unsigned char op;
|
||||
if (fread(&op, 1, 1, stream) != 1) {
|
||||
fprintf(stderr, "Failed reading operation byte!");
|
||||
return (mons_image){0};
|
||||
}
|
||||
switch (op) {
|
||||
case MONS_QOI_OP_RGB: {
|
||||
unsigned char rgb[3];
|
||||
if (fread(&rgb, 1, 3, stream) != 3) {
|
||||
fprintf(stderr, "Failed reading RGB data!");
|
||||
return (mons_image){0};
|
||||
}
|
||||
output.data[i] = (mons_pixel){
|
||||
.r = rgb[0],
|
||||
.g = rgb[1],
|
||||
.b = rgb[2],
|
||||
.a = previous.a,
|
||||
};
|
||||
break;
|
||||
}
|
||||
case MONS_QOI_OP_RGBA: {
|
||||
unsigned char rgba[4];
|
||||
if (fread(&rgba, 1, 4, stream) != 4) {
|
||||
fprintf(stderr, "Failed reading RGBA data!");
|
||||
return (mons_image){0};
|
||||
}
|
||||
output.data[i] = (mons_pixel){
|
||||
.r = rgba[0],
|
||||
.g = rgba[1],
|
||||
.b = rgba[2],
|
||||
.a = rgba[3],
|
||||
};
|
||||
break;
|
||||
}
|
||||
default:
|
||||
switch (op & 0b11000000) {
|
||||
case MONS_QOI_OP_INDEX: {
|
||||
unsigned char index = op & 0b00111111;
|
||||
output.data[i] = previously_seen[index];
|
||||
break;
|
||||
}
|
||||
case MONS_QOI_OP_DIFF: {
|
||||
char r_diff = ((op & 0b00110000) >> 4) - 2;
|
||||
char g_diff = ((op & 0b00001100) >> 2) - 2;
|
||||
char b_diff = (op & 0b00000011) - 2;
|
||||
output.data[i] = (mons_pixel){
|
||||
.r = previous.r + r_diff,
|
||||
.g = previous.g + g_diff,
|
||||
.b = previous.b + b_diff,
|
||||
.a = previous.a,
|
||||
};
|
||||
break;
|
||||
}
|
||||
case MONS_QOI_OP_LUMA: {
|
||||
int g_diff = (op & 0b00111111) - 32;
|
||||
unsigned char second_byte;
|
||||
if (fread(&second_byte, 1, 1, stream) != 1) {
|
||||
fprintf(stderr,
|
||||
"Failed to read second byte of luma operation!");
|
||||
return (mons_image){0};
|
||||
}
|
||||
int r_diff = g_diff - 8 + (((second_byte & 0b11110000) >> 4));
|
||||
int b_diff = g_diff - 8 + (second_byte & 0b00001111);
|
||||
output.data[i] = (mons_pixel){
|
||||
.r = previous.r + r_diff,
|
||||
.g = previous.g + g_diff,
|
||||
.b = previous.b + b_diff,
|
||||
.a = previous.a,
|
||||
};
|
||||
break;
|
||||
}
|
||||
case MONS_QOI_OP_RUN: {
|
||||
unsigned char run = (op & 0b00111111) + 1;
|
||||
for (int j = 0; j < run; j++) {
|
||||
output.data[i++] = previous;
|
||||
}
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
previous = output.data[i];
|
||||
unsigned int hash = pixel_index_hash(output.data[i]);
|
||||
previously_seen[hash] = output.data[i];
|
||||
}
|
||||
fclose(stream);
|
||||
|
||||
return output;
|
||||
}
|
30
mons_json/CMakeLists.txt
Normal file
30
mons_json/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_json LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_library(mons_json
|
||||
SHARED
|
||||
./src/json.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_json PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_json PRIVATE -coverage)
|
||||
target_link_options(mons_json PRIVATE -coverage)
|
||||
target_link_libraries(mons_json PUBLIC mons_collections)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_json)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
52
mons_json/include/json.h
Normal file
52
mons_json/include/json.h
Normal file
@ -0,0 +1,52 @@
|
||||
#ifndef MONS_JSON_H
|
||||
#define MONS_JSON_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "hashmap.h"
|
||||
|
||||
struct mons_json_value;
|
||||
union mons_json_entry;
|
||||
|
||||
typedef struct mons_json_array {
|
||||
struct mons_json_value *values;
|
||||
unsigned int len;
|
||||
} mons_json_array;
|
||||
|
||||
typedef union mons_json_value_data {
|
||||
char *string;
|
||||
float number;
|
||||
mons_hashmap object;
|
||||
mons_json_array array;
|
||||
bool boolean;
|
||||
void *null;
|
||||
} mons_json_value_data;
|
||||
|
||||
typedef enum mons_json_value_type {
|
||||
MONS_JSON_STRING,
|
||||
MONS_JSON_NUMBER,
|
||||
MONS_JSON_OBJECT,
|
||||
MONS_JSON_ARRAY,
|
||||
MONS_JSON_BOOL,
|
||||
MONS_JSON_NULL,
|
||||
} mons_json_value_type;
|
||||
|
||||
typedef struct mons_json_value {
|
||||
mons_json_value_type type;
|
||||
mons_json_value_data data;
|
||||
} mons_json_value;
|
||||
|
||||
int mons_json_parse(char *json, mons_json_value *out);
|
||||
|
||||
char *mons_json_to_string(mons_json_value json);
|
||||
|
||||
void mons_json_free(mons_json_value json);
|
||||
|
||||
int mons_json_get_value(mons_json_value json, char *name, mons_json_value *out);
|
||||
int mons_json_get_int(mons_json_value json, char *name, int *out);
|
||||
int mons_json_get_float(mons_json_value json, char *name, float *out);
|
||||
int mons_json_get_bool(mons_json_value json, char *name, bool *out);
|
||||
int mons_json_get_array(mons_json_value json, char *name, mons_json_array *out);
|
||||
int mons_json_get_object(mons_json_value json, char *name, mons_hashmap *out);
|
||||
int mons_json_get_string(mons_json_value json, char *name, char **out);
|
||||
|
||||
#endif
|
468
mons_json/src/json.c
Normal file
468
mons_json/src/json.c
Normal file
@ -0,0 +1,468 @@
|
||||
#include "json.h"
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int try_parse_string(char **current, mons_json_value *out);
|
||||
int try_parse_number(char **current, mons_json_value *out);
|
||||
int try_parse_object(char **current, mons_json_value *out);
|
||||
int try_parse_value(char **current, mons_json_value *out);
|
||||
int try_parse_array(char **current, mons_json_value *out);
|
||||
int try_parse_literal(char **current, mons_json_value *out);
|
||||
|
||||
int mons_json_parse(char *json, mons_json_value *out) {
|
||||
char *current = json;
|
||||
if (try_parse_value(¤t, out) == EXIT_SUCCESS) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int try_parse_value(char **current, mons_json_value *out) {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (**current == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (try_parse_literal(current, out) == EXIT_SUCCESS) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if (try_parse_array(current, out) == EXIT_SUCCESS) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if (try_parse_object(current, out) == EXIT_SUCCESS) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if (try_parse_number(current, out) == EXIT_SUCCESS) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if (try_parse_string(current, out) == EXIT_SUCCESS) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int try_parse_string(char **current, mons_json_value *out) {
|
||||
if (**current != '"') {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
(*current)++;
|
||||
char *end_of_string = strchr(*current, '"');
|
||||
int string_length = end_of_string - *current;
|
||||
char *buffer = calloc(string_length + 1, sizeof(char));
|
||||
// TODO: Unescape Characters
|
||||
memcpy(buffer, *current, string_length);
|
||||
out->type = MONS_JSON_STRING;
|
||||
out->data.string = buffer;
|
||||
*current += string_length + 1;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int try_parse_number(char **current, mons_json_value *out) {
|
||||
char *end_of_value = *current;
|
||||
while (!isspace(*end_of_value) && *end_of_value != ',' &&
|
||||
*end_of_value != 0 && *end_of_value != ']' && *end_of_value != '}') {
|
||||
end_of_value++;
|
||||
}
|
||||
int value_length = end_of_value - *current;
|
||||
if (value_length == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
bool has_decimal = false;
|
||||
bool has_exponent = false;
|
||||
for (int i = 0; i < value_length; i++) {
|
||||
if (i == 0 && **current == '-') {
|
||||
continue;
|
||||
}
|
||||
if (!isdigit(*(*current + i))) {
|
||||
switch (*(*current + i)) {
|
||||
case 'e':
|
||||
case 'E':
|
||||
if (has_exponent) {
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
has_exponent = true;
|
||||
}
|
||||
break;
|
||||
case '+':
|
||||
case '-':
|
||||
if (*(*current + i - 1) != 'e' && *(*current +i - 1) != 'E') {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
case '.':
|
||||
if (has_decimal) {
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
has_decimal = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Value is a valid number
|
||||
out->type = MONS_JSON_NUMBER;
|
||||
out->data.number = atof(*current);
|
||||
*current += value_length;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int try_parse_object(char **current, mons_json_value *out) {
|
||||
if (**current != '{') {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
(*current)++;
|
||||
|
||||
mons_hashmap map = mons_hashmap_new(sizeof(mons_json_value), 10);
|
||||
|
||||
while (true) {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (**current == 0) {
|
||||
printf("ALL WHITESPACE\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
mons_json_value key;
|
||||
if (try_parse_string(current, &key) == EXIT_SUCCESS) {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (*current == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (**current != ':') {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
(*current)++;
|
||||
mons_json_value value;
|
||||
if (try_parse_value(current, &value) == EXIT_FAILURE) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
mons_hashmap_insert(&map, key.data.string, &value);
|
||||
continue;
|
||||
} else {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (*current == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
switch (**current) {
|
||||
case ',':
|
||||
(*current)++;
|
||||
continue;
|
||||
case '}':
|
||||
(*current)++;
|
||||
out->type = MONS_JSON_OBJECT;
|
||||
out->data.object = map;
|
||||
return EXIT_SUCCESS;
|
||||
default:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int try_parse_array(char **current, mons_json_value *out) {
|
||||
if (**current != '[') {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
(*current)++;
|
||||
|
||||
mons_json_array array = {
|
||||
.values = NULL,
|
||||
.len = 0,
|
||||
};
|
||||
|
||||
while (true) {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (**current == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
mons_json_value value;
|
||||
if (try_parse_value(current, &value) == EXIT_SUCCESS) {
|
||||
array.len++;
|
||||
array.values =
|
||||
realloc(array.values, sizeof(mons_json_value) * array.len);
|
||||
array.values[array.len - 1] = value;
|
||||
continue;
|
||||
} else {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (*current == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
switch (**current) {
|
||||
case ',':
|
||||
(*current)++;
|
||||
continue;
|
||||
case ']':
|
||||
(*current)++;
|
||||
out->type = MONS_JSON_ARRAY;
|
||||
out->data.array = array;
|
||||
return EXIT_SUCCESS;
|
||||
default:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int try_parse_literal(char **current, mons_json_value *out) {
|
||||
// Trim space
|
||||
while (isspace(**current))
|
||||
(*current)++;
|
||||
if (**current == 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (strncmp(*current, "true", 4) == 0) {
|
||||
*current += 4;
|
||||
out->type = MONS_JSON_BOOL;
|
||||
out->data.boolean = true;
|
||||
return EXIT_SUCCESS;
|
||||
} else if (strncmp(*current, "false", 5) == 0) {
|
||||
*current += 5;
|
||||
out->type = MONS_JSON_BOOL;
|
||||
out->data.boolean = false;
|
||||
return EXIT_SUCCESS;
|
||||
} else if (strncmp(*current, "null", 4) == 0) {
|
||||
*current += 4;
|
||||
out->type = MONS_JSON_NULL;
|
||||
out->data.null = NULL;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
void json_value_to_string(mons_json_value value, char **buffer,
|
||||
unsigned int *current_len) {
|
||||
switch (value.type) {
|
||||
case MONS_JSON_NULL:
|
||||
*buffer = realloc(*buffer, *current_len + 4);
|
||||
strcpy(*buffer + *current_len, "null");
|
||||
*current_len += 4;
|
||||
break;
|
||||
case MONS_JSON_BOOL:
|
||||
if (value.data.boolean) {
|
||||
*buffer = realloc(*buffer, *current_len + 4);
|
||||
memcpy(*buffer + *current_len, "true", 4);
|
||||
*current_len += 4;
|
||||
break;
|
||||
} else {
|
||||
*buffer = realloc(*buffer, *current_len + 5);
|
||||
memcpy(*buffer + *current_len, "false", 5);
|
||||
*current_len += 5;
|
||||
break;
|
||||
}
|
||||
case MONS_JSON_ARRAY: {
|
||||
*buffer = realloc(*buffer, *current_len + 1);
|
||||
(*buffer)[*current_len] = '[';
|
||||
*current_len += 1;
|
||||
for (int i = 0; i < value.data.array.len; i++) {
|
||||
mons_json_value member = value.data.array.values[i];
|
||||
char *buff = NULL;
|
||||
unsigned int len = 0;
|
||||
json_value_to_string(member, &buff, &len);
|
||||
*buffer = realloc(*buffer, *current_len + len + 1);
|
||||
memcpy(*buffer + *current_len, buff, len);
|
||||
free(buff);
|
||||
*current_len += len;
|
||||
*buffer = realloc(*buffer, *current_len + 1);
|
||||
if (i + 1 < value.data.array.len) {
|
||||
(*buffer)[*current_len] = ',';
|
||||
} else {
|
||||
(*buffer)[*current_len] = ']';
|
||||
}
|
||||
*current_len += 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MONS_JSON_OBJECT:
|
||||
*buffer = realloc(*buffer, *current_len + 1);
|
||||
(*buffer)[*current_len] = '{';
|
||||
*current_len += 1;
|
||||
for (int i = 0; i < value.data.object.len; i++) {
|
||||
mons_hashmap_pair pair;
|
||||
mons_hashmap_at(value.data.object, i, &pair);
|
||||
char *key = pair.key;
|
||||
mons_json_value *entry_value = pair.value;
|
||||
// key
|
||||
*buffer = realloc(*buffer, *current_len + strlen(key) + 3);
|
||||
sprintf(*buffer + *current_len, "\"%s\":", key);
|
||||
*current_len += strlen(key) + 3;
|
||||
char *buff = NULL;
|
||||
unsigned int len = 0;
|
||||
json_value_to_string(*entry_value, &buff, &len);
|
||||
*buffer = realloc(*buffer, *current_len + len + 1);
|
||||
memcpy(*buffer + *current_len, buff, len);
|
||||
free(buff);
|
||||
*current_len += len;
|
||||
*buffer = realloc(*buffer, *current_len + 1);
|
||||
if (i + 1 < value.data.object.len) {
|
||||
(*buffer)[*current_len] = ',';
|
||||
} else {
|
||||
(*buffer)[*current_len] = '}';
|
||||
}
|
||||
*current_len += 1;
|
||||
}
|
||||
break;
|
||||
case MONS_JSON_NUMBER: {
|
||||
unsigned int len = snprintf(0, 0, "%e", value.data.number);
|
||||
*buffer = realloc(*buffer, *current_len + len);
|
||||
sprintf(*buffer + *current_len, "%e", value.data.number);
|
||||
*current_len += len;
|
||||
break;
|
||||
}
|
||||
case MONS_JSON_STRING: {
|
||||
unsigned int len = snprintf(0, 0, "\"%s\"", value.data.string);
|
||||
*buffer = realloc(*buffer, *current_len + len);
|
||||
sprintf(*buffer + *current_len, "\"%s\"", value.data.string);
|
||||
*current_len += len;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char *mons_json_to_string(mons_json_value json) {
|
||||
char *buffer = NULL;
|
||||
unsigned int len = 0;
|
||||
json_value_to_string(json, &buffer, &len);
|
||||
buffer = realloc(buffer, len + 1);
|
||||
buffer[len] = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void mons_json_free(mons_json_value json) {
|
||||
switch(json.type) {
|
||||
case MONS_JSON_OBJECT:
|
||||
mons_hashmap_free(&json.data.object);
|
||||
break;
|
||||
case MONS_JSON_ARRAY:
|
||||
for(int i = 0; i < json.data.array.len; i++) {
|
||||
mons_json_free(json.data.array.values[i]);
|
||||
}
|
||||
json.data.array.len = 0;
|
||||
free(json.data.array.values);
|
||||
json.data.array.values = NULL;
|
||||
break;
|
||||
case MONS_JSON_STRING:
|
||||
free(json.data.string);
|
||||
json.data.string = NULL;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int mons_json_get_value(mons_json_value json, char *name, mons_json_value *out) {
|
||||
int name_len = strlen(name);
|
||||
char *name_copy = calloc(name_len + 1, 1);
|
||||
strcpy(name_copy, name);
|
||||
char *field = strtok(name_copy, ".");
|
||||
mons_json_value member = json;
|
||||
while(field != NULL) {
|
||||
char *subscript = strchr(field, '[');
|
||||
int array_index = 0;
|
||||
if(subscript != NULL) {
|
||||
array_index = atoi(subscript + 1);
|
||||
*subscript = 0;
|
||||
}
|
||||
|
||||
if (member.type != MONS_JSON_OBJECT) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
mons_json_value value;
|
||||
if (mons_hashmap_get(member.data.object, field, &value) == EXIT_SUCCESS) {
|
||||
member = value;
|
||||
if (subscript != NULL) {
|
||||
member = member.data.array.values[array_index];
|
||||
}
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
field = strtok(NULL, ".");
|
||||
}
|
||||
*out = member;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int mons_json_get_int(mons_json_value json, char *name, int *out) {
|
||||
mons_json_value value = {0};
|
||||
if(mons_json_get_value(json, name, &value) == EXIT_SUCCESS && value.type == MONS_JSON_NUMBER) {
|
||||
*out = value.data.number;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int mons_json_get_float(mons_json_value json, char *name, float *out) {
|
||||
mons_json_value value = {0};
|
||||
if(mons_json_get_value(json, name, &value) == EXIT_SUCCESS && value.type == MONS_JSON_NUMBER) {
|
||||
*out = value.data.number;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int mons_json_get_bool(mons_json_value json, char *name, bool *out) {
|
||||
mons_json_value value = {0};
|
||||
if(mons_json_get_value(json, name, &value) == EXIT_SUCCESS && value.type == MONS_JSON_BOOL) {
|
||||
*out = value.data.boolean;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int mons_json_get_array(mons_json_value json, char *name, mons_json_array *out) {
|
||||
mons_json_value value = {0};
|
||||
if(mons_json_get_value(json, name, &value) == EXIT_SUCCESS && value.type == MONS_JSON_ARRAY) {
|
||||
*out = value.data.array;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int mons_json_get_object(mons_json_value json, char *name, mons_hashmap *out) {
|
||||
mons_json_value value = {0};
|
||||
if(mons_json_get_value(json, name, &value) == EXIT_SUCCESS && value.type == MONS_JSON_OBJECT) {
|
||||
*out = value.data.object;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int mons_json_get_string(mons_json_value json, char *name, char **out) {
|
||||
mons_json_value value = {0};
|
||||
if(mons_json_get_value(json, name, &value) == EXIT_SUCCESS && value.type == MONS_JSON_STRING) {
|
||||
*out = value.data.string;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
43
mons_math/CMakeLists.txt
Normal file
43
mons_math/CMakeLists.txt
Normal file
@ -0,0 +1,43 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(mons_math LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
add_library(mons_math
|
||||
SHARED
|
||||
./src/mat2.c
|
||||
./src/mat3.c
|
||||
./src/mat4.c
|
||||
./src/vec2.c
|
||||
./src/vec3.c
|
||||
./src/vec4.c
|
||||
./src/util.c
|
||||
./src/quat.c
|
||||
)
|
||||
|
||||
target_include_directories(mons_math PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
target_compile_options(mons_math PRIVATE -coverage)
|
||||
target_link_options(mons_math PRIVATE -coverage)
|
||||
target_link_libraries(mons_math PUBLIC m)
|
||||
|
||||
include(CTest)
|
||||
|
||||
function(TESTCASE NAME)
|
||||
add_executable(test_${NAME} ./tests/${NAME}.c)
|
||||
target_link_libraries(test_${NAME} PUBLIC mons_math)
|
||||
add_test(
|
||||
NAME ${NAME}
|
||||
COMMAND $<TARGET_FILE:test_${NAME}>
|
||||
)
|
||||
endfunction()
|
||||
|
||||
testcase(vec2_ops)
|
||||
testcase(vec3_ops)
|
||||
testcase(vec4_ops)
|
||||
# testcase(mat2_ops)
|
||||
# testcase(mat3_ops)
|
||||
# testcase(mat4_ops)
|
39
mons_math/include/mons_math/mat2.h
Normal file
39
mons_math/include/mons_math/mat2.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef MONS_MAT2_H
|
||||
#define MONS_MAT2_H
|
||||
|
||||
#include "mons_math/vec2.h"
|
||||
|
||||
typedef struct mons_mat2 {
|
||||
mons_vec2 m1;
|
||||
mons_vec2 m2;
|
||||
} mons_mat2;
|
||||
|
||||
mons_mat2 mons_mat2_add(mons_mat2 a, mons_mat2 b);
|
||||
void mons_mat2_add_inplace(mons_mat2 *a, mons_mat2 b);
|
||||
|
||||
mons_mat2 mons_mat2_mul_f(mons_mat2 a, float b);
|
||||
mons_mat2 mons_mat2_mul_i(mons_mat2 a, int b);
|
||||
mons_mat2 mons_mat2_mul(mons_mat2 a, mons_mat2 b);
|
||||
|
||||
void mons_mat2_mul_f_inplace(mons_mat2 *a, float b);
|
||||
void mons_mat2_mul_i_inplace(mons_mat2 *a, int b);
|
||||
|
||||
mons_mat2 mons_mat2_inverse(mons_mat2 a);
|
||||
void mons_mat2_inverse_inplace(mons_mat2 *a);
|
||||
|
||||
mons_mat2 mons_mat2_transpose(mons_mat2 a);
|
||||
void mons_mat2_transpose_inplace(mons_mat2 *a);
|
||||
|
||||
mons_vec2 mons_mat2_n1(mons_mat2 a);
|
||||
mons_vec2 mons_mat2_n2(mons_mat2 a);
|
||||
|
||||
float mons_mat2_determinant(mons_mat2 a);
|
||||
mons_mat2 mons_mat2_adjoint(mons_mat2 a);
|
||||
mons_mat2 mons_mat2_inverse(mons_mat2 a);
|
||||
|
||||
extern const mons_mat2 MONS_MAT2_ZERO;
|
||||
extern const mons_mat2 MONS_MAT2_IDENTITY;
|
||||
|
||||
int mons_mat2_equal(mons_mat2 a, mons_mat2 b);
|
||||
|
||||
#endif
|
41
mons_math/include/mons_math/mat3.h
Normal file
41
mons_math/include/mons_math/mat3.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef MONS_MAT3_H
|
||||
#define MONS_MAT3_H
|
||||
|
||||
#include "mons_math/vec3.h"
|
||||
|
||||
typedef struct mons_mat3 {
|
||||
mons_vec3 m1;
|
||||
mons_vec3 m2;
|
||||
mons_vec3 m3;
|
||||
} mons_mat3;
|
||||
|
||||
mons_mat3 mons_mat3_add(mons_mat3 a, mons_mat3 b);
|
||||
void mons_mat3_add_inplace(mons_mat3 *a, mons_mat3 b);
|
||||
|
||||
mons_mat3 mons_mat3_mul_f(mons_mat3 a, float b);
|
||||
mons_mat3 mons_mat3_mul_i(mons_mat3 a, int b);
|
||||
mons_mat3 mons_mat3_mul(mons_mat3 a, mons_mat3 b);
|
||||
|
||||
void mons_mat3_mul_f_inplace(mons_mat3 *a, float b);
|
||||
void mons_mat3_mul_i_inplace(mons_mat3 *a, int b);
|
||||
|
||||
mons_mat3 mons_mat3_inverse(mons_mat3 a);
|
||||
|
||||
mons_mat3 mons_mat3_transpose(mons_mat3 a);
|
||||
void mons_mat3_transpose_inplace(mons_mat3 *a);
|
||||
|
||||
mons_vec3 mons_mat3_n1(mons_mat3 a);
|
||||
mons_vec3 mons_mat3_n2(mons_mat3 a);
|
||||
mons_vec3 mons_mat3_n3(mons_mat3 a);
|
||||
|
||||
float mons_mat3_determinant(mons_mat3 a);
|
||||
mons_mat3 mons_mat3_minor(mons_mat3 a);
|
||||
mons_mat3 mons_mat3_cofactor(mons_mat3 a);
|
||||
mons_mat3 mons_mat3_adjoint(mons_mat3 a);
|
||||
|
||||
extern const mons_mat3 MONS_MAT3_ZERO;
|
||||
extern const mons_mat3 MONS_MAT3_IDENTITY;
|
||||
|
||||
int mons_mat3_equal(mons_mat3 a, mons_mat3 b);
|
||||
|
||||
#endif
|
57
mons_math/include/mons_math/mat4.h
Normal file
57
mons_math/include/mons_math/mat4.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef MONS_MATH_MATRIX_H
|
||||
#define MONS_MATH_MATRIX_H
|
||||
|
||||
#include "mons_math/vec4.h"
|
||||
|
||||
typedef struct mons_mat4 {
|
||||
mons_vec4 m1;
|
||||
mons_vec4 m2;
|
||||
mons_vec4 m3;
|
||||
mons_vec4 m4;
|
||||
} mons_mat4;
|
||||
|
||||
mons_mat4 mons_mat4_add(mons_mat4 a, mons_mat4 b);
|
||||
void mons_mat4_add_inplace(mons_mat4 *a, mons_mat4 b);
|
||||
|
||||
mons_mat4 mons_mat4_mul_f(mons_mat4 a, float b);
|
||||
mons_mat4 mons_mat4_mul_i(mons_mat4 a, int b);
|
||||
mons_mat4 mons_mat4_mul(mons_mat4 a, mons_mat4 b);
|
||||
void mons_mat4_mul_inplace(mons_mat4 *a, mons_mat4 b);
|
||||
|
||||
void mons_mat4_mul_f_inplace(mons_mat4 *a, float b);
|
||||
void mons_mat4_mul_i_inplace(mons_mat4 *a, int b);
|
||||
|
||||
mons_mat4 mons_mat4_inverse(mons_mat4 a);
|
||||
|
||||
mons_mat4 mons_mat4_transpose(mons_mat4 a);
|
||||
void mons_mat4_transpose_inplace(mons_mat4 *a);
|
||||
|
||||
mons_vec4 mons_mat4_n1(mons_mat4 a);
|
||||
mons_vec4 mons_mat4_n2(mons_mat4 a);
|
||||
mons_vec4 mons_mat4_n3(mons_mat4 a);
|
||||
mons_vec4 mons_mat4_n4(mons_mat4 a);
|
||||
|
||||
float mons_mat4_determinant(mons_mat4 a);
|
||||
|
||||
extern const mons_mat4 MONS_MAT4_ZERO;
|
||||
extern const mons_mat4 MONS_MAT4_IDENTITY;
|
||||
|
||||
int mons_mat4_equal(mons_mat4 a, mons_mat4 b);
|
||||
|
||||
void mons_mat4_print(mons_mat4 mat);
|
||||
|
||||
#define MONS_MAT4_ZERO {\
|
||||
{ 0, 0, 0, 0 },\
|
||||
{ 0, 0, 0, 0 },\
|
||||
{ 0, 0, 0, 0 },\
|
||||
{ 0, 0, 0, 0 },\
|
||||
}
|
||||
|
||||
#define MONS_MAT4_IDENTITY {\
|
||||
{ 1, 0, 0, 0 },\
|
||||
{ 0, 1, 0, 0 },\
|
||||
{ 0, 0, 1, 0 },\
|
||||
{ 0, 0, 0, 1 },\
|
||||
}
|
||||
|
||||
#endif
|
8
mons_math/include/mons_math/matrix.h
Normal file
8
mons_math/include/mons_math/matrix.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef MONS_MATRIX_H
|
||||
#define MONS_MATRIX_H
|
||||
|
||||
#include "mat2.h"
|
||||
#include "mat3.h"
|
||||
#include "mat4.h"
|
||||
|
||||
#endif
|
44
mons_math/include/mons_math/quat.h
Normal file
44
mons_math/include/mons_math/quat.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef MONS_QUAT_H
|
||||
#define MONS_QUAT_H
|
||||
|
||||
struct mons_vec3;
|
||||
|
||||
typedef struct mons_quat {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
} mons_quat;
|
||||
|
||||
mons_quat mons_quat_from_axis_angle(struct mons_vec3 axis, float angle);
|
||||
|
||||
mons_quat mons_quat_mul(mons_quat a, mons_quat b);
|
||||
|
||||
void mons_quat_mul_inplace(mons_quat *a, mons_quat b);
|
||||
|
||||
mons_quat mons_quat_mul_f(mons_quat a, float b);
|
||||
|
||||
void mons_quat_mul_f_inplace(mons_quat *a, float b);
|
||||
|
||||
mons_quat mons_quat_mul_i(mons_quat a, int b);
|
||||
|
||||
void mons_quat_mul_i_inplace(mons_quat *a, int b);
|
||||
|
||||
struct mons_mat4 mons_quat_to_mat4(mons_quat a);
|
||||
|
||||
mons_quat mons_quat_normalize(mons_quat a);
|
||||
|
||||
void mons_quat_normalize_inplace(mons_quat *a);
|
||||
|
||||
struct mons_vec3 mons_quat_transform_vec3(mons_quat quat, struct mons_vec3 vec);
|
||||
|
||||
mons_quat mons_quat_conjugate(mons_quat quat);
|
||||
|
||||
mons_quat mons_quat_looking_at(struct mons_vec3 eye, struct mons_vec3 target, struct mons_vec3 up);
|
||||
|
||||
void mons_quat_print(mons_quat quat);
|
||||
|
||||
#define MONS_QUAT_IDENTITY (mons_quat) { 0, 0, 0, 1 }
|
||||
|
||||
#endif
|
||||
|
8
mons_math/include/mons_math/util.h
Normal file
8
mons_math/include/mons_math/util.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef MONS_MATH_UTIL_H
|
||||
#define MONS_MATH_UTIL_H
|
||||
|
||||
#define MONS_FLOAT_EQUAL_EPSILON 0.0001f;
|
||||
|
||||
int mons_float_approx_equal(float a, float b);
|
||||
|
||||
#endif
|
46
mons_math/include/mons_math/vec2.h
Normal file
46
mons_math/include/mons_math/vec2.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef MONS_VEC2_H
|
||||
#define MONS_VEC2_H
|
||||
|
||||
typedef struct mons_vec2 {
|
||||
float x;
|
||||
float y;
|
||||
} mons_vec2;
|
||||
|
||||
mons_vec2 mons_vec2_add(mons_vec2 a, mons_vec2 b);
|
||||
void mons_vec2_add_inplace(mons_vec2 *a, mons_vec2 b);
|
||||
|
||||
mons_vec2 mons_vec2_sub(mons_vec2 a, mons_vec2 b);
|
||||
void mons_vec2_sub_inplace(mons_vec2 *a, mons_vec2 b);
|
||||
|
||||
mons_vec2 mons_vec2_mul_f(mons_vec2 a, float b);
|
||||
mons_vec2 mons_vec2_mul_i(mons_vec2 a, int b);
|
||||
float mons_vec2_dot(mons_vec2 a, mons_vec2 b);
|
||||
|
||||
void mons_vec2_mul_f_inplace(mons_vec2 *a, float b);
|
||||
void mons_vec2_mul_i_inplace(mons_vec2 *a, int b);
|
||||
|
||||
mons_vec2 mons_vec2_div_f(mons_vec2 a, float b);
|
||||
mons_vec2 mons_vec2_div_i(mons_vec2 a, int b);
|
||||
void mons_vec2_div_f_inplace(mons_vec2 *a, float b);
|
||||
void mons_vec2_div_i_inplace(mons_vec2 *a, int b);
|
||||
|
||||
float mons_vec2_len(mons_vec2 a);
|
||||
float mons_vec2_len_squared(mons_vec2 a);
|
||||
|
||||
struct mons_vec3 mons_vec2_extend(mons_vec2 a);
|
||||
|
||||
int mons_vec2_equal(mons_vec2 a, mons_vec2 b);
|
||||
mons_vec2 mons_vec2_negate(mons_vec2 a);
|
||||
void mons_vec2_negate_inplace(mons_vec2 *a);
|
||||
|
||||
mons_vec2 mons_vec2_normalize(mons_vec2 a);
|
||||
void mons_vec2_normalize_inplace(mons_vec2 *a);
|
||||
|
||||
#define MONS_VEC2_ZERO (mons_vec2) { 0, 0 }
|
||||
#define MONS_VEC2_ONE (mons_vec2) { 1, 1 }
|
||||
#define MONS_VEC2_X (mons_vec2) { 1, 0 }
|
||||
#define MONS_VEC2_Y (mons_vec2) { 0, 1 }
|
||||
#define MONS_VEC2_NEG_X (mons_vec2) {-1,0}
|
||||
#define MONS_VEC2_NEG_Y (mons_vec2) {0,-1}
|
||||
|
||||
#endif
|
56
mons_math/include/mons_math/vec3.h
Normal file
56
mons_math/include/mons_math/vec3.h
Normal file
@ -0,0 +1,56 @@
|
||||
#ifndef MONS_VEC3_H
|
||||
#define MONS_VEC3_H
|
||||
|
||||
typedef struct mons_vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} mons_vec3;
|
||||
|
||||
mons_vec3 mons_vec3_add(mons_vec3 a, mons_vec3 b);
|
||||
void mons_vec3_add_inplace(mons_vec3 *a, mons_vec3 b);
|
||||
|
||||
mons_vec3 mons_vec3_sub(mons_vec3 a, mons_vec3 b);
|
||||
void mons_vec3_sub_inplace(mons_vec3 *a, mons_vec3 b);
|
||||
|
||||
mons_vec3 mons_vec3_mul_memberwise(mons_vec3 a, mons_vec3 b);
|
||||
void mons_vec3_mul_memberwise_inplace(mons_vec3 *a, mons_vec3 b);
|
||||
mons_vec3 mons_vec3_mul_f(mons_vec3 a, float b);
|
||||
mons_vec3 mons_vec3_mul_i(mons_vec3 a, int b);
|
||||
float mons_vec3_dot(mons_vec3 a, mons_vec3 b);
|
||||
|
||||
void mons_vec3_mul_f_inplace(mons_vec3 *a, float b);
|
||||
void mons_vec3_mul_i_inplace(mons_vec3 *a, int b);
|
||||
|
||||
mons_vec3 mons_vec3_div_f(mons_vec3 a, float b);
|
||||
mons_vec3 mons_vec3_div_i(mons_vec3 a, int b);
|
||||
void mons_vec3_div_f_inplace(mons_vec3 *a, float b);
|
||||
void mons_vec3_div_i_inplace(mons_vec3 *a, int b);
|
||||
|
||||
mons_vec3 mons_vec3_cross(mons_vec3 a, mons_vec3 b);
|
||||
|
||||
float mons_vec3_len(mons_vec3 a);
|
||||
float mons_vec3_len_squared(mons_vec3 a);
|
||||
|
||||
struct mons_vec2 mons_vec3_truncate(mons_vec3 a);
|
||||
struct mons_vec4 mons_vec3_extend(mons_vec3 a);
|
||||
|
||||
int mons_vec3_equal(mons_vec3 a, mons_vec3 b);
|
||||
mons_vec3 mons_vec3_negate(mons_vec3 a);
|
||||
void mons_vec3_negate_inplace(mons_vec3 *a);
|
||||
|
||||
mons_vec3 mons_vec3_normalize(mons_vec3 a);
|
||||
void mons_vec3_normalize_inplace(mons_vec3 *a);
|
||||
|
||||
void mons_vec3_print(mons_vec3 vec);
|
||||
|
||||
#define MONS_VEC3_ZERO (mons_vec3) { 0, 0, 0 }
|
||||
#define MONS_VEC3_ONE (mons_vec3) { 1, 1, 1 }
|
||||
#define MONS_VEC3_X (mons_vec3) { 1, 0, 0 }
|
||||
#define MONS_VEC3_Y (mons_vec3) { 0, 1, 0 }
|
||||
#define MONS_VEC3_Z (mons_vec3) { 0, 0, 1 }
|
||||
#define MONS_VEC3_NEG_X (mons_vec3) {-1,0,0}
|
||||
#define MONS_VEC3_NEG_Y (mons_vec3) {0,-1,0}
|
||||
#define MONS_VEC3_NEG_Z (mons_vec3) {0,0,-1}
|
||||
|
||||
#endif
|
53
mons_math/include/mons_math/vec4.h
Normal file
53
mons_math/include/mons_math/vec4.h
Normal file
@ -0,0 +1,53 @@
|
||||
#ifndef MONS_MATH_VEC4_H
|
||||
#define MONS_MATH_VEC4_H
|
||||
|
||||
typedef struct mons_vec4 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
} mons_vec4;
|
||||
|
||||
mons_vec4 mons_vec4_add(mons_vec4 a, mons_vec4 b);
|
||||
void mons_vec4_add_inplace(mons_vec4 *a, mons_vec4 b);
|
||||
|
||||
mons_vec4 mons_vec4_sub(mons_vec4 a, mons_vec4 b);
|
||||
void mons_vec4_sub_inplace(mons_vec4 *a, mons_vec4 b);
|
||||
|
||||
mons_vec4 mons_vec4_mul_f(mons_vec4 a, float b);
|
||||
mons_vec4 mons_vec4_mul_i(mons_vec4 a, int b);
|
||||
float mons_vec4_dot(mons_vec4 a, mons_vec4 b);
|
||||
|
||||
void mons_vec4_mul_f_inplace(mons_vec4 *a, float b);
|
||||
void mons_vec4_mul_i_inplace(mons_vec4 *a, int b);
|
||||
|
||||
mons_vec4 mons_vec4_div_f(mons_vec4 a, float b);
|
||||
mons_vec4 mons_vec4_div_i(mons_vec4 a, int b);
|
||||
void mons_vec4_div_f_inplace(mons_vec4 *a, float b);
|
||||
void mons_vec4_div_i_inplace(mons_vec4 *a, int b);
|
||||
|
||||
float mons_vec4_len(mons_vec4 a);
|
||||
float mons_vec4_len_squared(mons_vec4 a);
|
||||
|
||||
struct mons_vec3 mons_vec4_truncate(mons_vec4 a);
|
||||
|
||||
int mons_vec4_equal(mons_vec4 a, mons_vec4 b);
|
||||
mons_vec4 mons_vec4_negate(mons_vec4 a);
|
||||
void mons_vec4_negate_inplace(mons_vec4 *a);
|
||||
mons_vec4 mons_vec4_normalize(mons_vec4 a);
|
||||
void mons_vec4_normalize_inplace(mons_vec4 *a);
|
||||
|
||||
void mons_vec4_print(mons_vec4 vec);
|
||||
|
||||
#define MONS_VEC4_ZERO (mons_vec4) { 0, 0, 0, 0 }
|
||||
#define MONS_VEC4_ONE (mons_vec4) { 1, 1, 1, 1 }
|
||||
#define MONS_VEC4_X (mons_vec4) { 1, 0, 0, 0 }
|
||||
#define MONS_VEC4_Y (mons_vec4) { 0, 1, 0, 0 }
|
||||
#define MONS_VEC4_Z (mons_vec4) { 0, 0, 1, 0 }
|
||||
#define MONS_VEC4_W (mons_vec4) { 0, 0, 0, 1 }
|
||||
#define MONS_VEC4_NEG_X (mons_vec4) {-1,0,0,0}
|
||||
#define MONS_VEC4_NEG_Y (mons_vec4) {0,-1,0,0}
|
||||
#define MONS_VEC4_NEG_Z (mons_vec4) {0,0,-1,0}
|
||||
#define MONS_VEC4_NEG_W (mons_vec4) {0,0,0,-1}
|
||||
|
||||
#endif
|
8
mons_math/include/mons_math/vector.h
Normal file
8
mons_math/include/mons_math/vector.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef MONS_VECTOR_H
|
||||
#define MONS_VECTOR_H
|
||||
|
||||
#include "vec2.h"
|
||||
#include "vec3.h"
|
||||
#include "vec4.h"
|
||||
|
||||
#endif
|
6
mons_math/run_tests
Executable file
6
mons_math/run_tests
Executable file
@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
cmake . -B build &&
|
||||
make -C build &&
|
||||
ctest --test-dir build --output-on-failure -T Test -T Coverage
|
||||
|
123
mons_math/src/mat2.c
Normal file
123
mons_math/src/mat2.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include "mons_math/mat2.h"
|
||||
|
||||
const mons_mat2 MONS_MAT2_ZERO = {
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
};
|
||||
|
||||
const mons_mat2 MONS_MAT2_IDENTITY = {
|
||||
{1, 0},
|
||||
{0, 1},
|
||||
};
|
||||
|
||||
mons_mat2 mons_mat2_add(mons_mat2 a, mons_mat2 b) {
|
||||
mons_mat2 result = {
|
||||
mons_vec2_add(a.m1, b.m1),
|
||||
mons_vec2_add(a.m2, b.m2),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat2_add_inplace(mons_mat2 *a, mons_mat2 b) {
|
||||
mons_vec2_add_inplace(&a->m1, b.m1);
|
||||
mons_vec2_add_inplace(&a->m2, b.m2);
|
||||
}
|
||||
|
||||
mons_mat2 mons_mat2_mul_f(mons_mat2 a, float b) {
|
||||
mons_mat2 result = {
|
||||
mons_vec2_mul_f(a.m1, b),
|
||||
mons_vec2_mul_f(a.m2, b),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat2 mons_mat2_mul_i(mons_mat2 a, int b) {
|
||||
mons_mat2 result = {
|
||||
mons_vec2_mul_i(a.m1, b),
|
||||
mons_vec2_mul_i(a.m2, b),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat2 mons_mat2_mul(mons_mat2 a, mons_mat2 b) {
|
||||
mons_vec2 b_n1 = mons_mat2_n1(b);
|
||||
mons_vec2 b_n2 = mons_mat2_n2(b);
|
||||
|
||||
mons_mat2 result = {
|
||||
{
|
||||
mons_vec2_dot(a.m1, b_n1),
|
||||
mons_vec2_dot(a.m1, b_n2),
|
||||
},
|
||||
{
|
||||
mons_vec2_dot(a.m2, b_n1),
|
||||
mons_vec2_dot(a.m2, b_n2),
|
||||
},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat2_mul_f_inplace(mons_mat2 *a, float b) {
|
||||
mons_vec2_mul_f_inplace(&a->m1, b);
|
||||
mons_vec2_mul_f_inplace(&a->m2, b);
|
||||
}
|
||||
|
||||
void mons_mat2_mul_i_inplace(mons_mat2 *a, int b) {
|
||||
mons_vec2_mul_i_inplace(&a->m1, b);
|
||||
mons_vec2_mul_i_inplace(&a->m2, b);
|
||||
}
|
||||
|
||||
mons_mat2 mons_mat2_transpose(mons_mat2 a) {
|
||||
mons_mat2 result = {
|
||||
mons_mat2_n1(a),
|
||||
mons_mat2_n2(a),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat2_transpose_inplace(mons_mat2 *a) { *a = mons_mat2_transpose(*a); }
|
||||
|
||||
mons_vec2 mons_mat2_n1(mons_mat2 a) {
|
||||
mons_vec2 result = {
|
||||
a.m1.x,
|
||||
a.m2.x,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec2 mons_mat2_n2(mons_mat2 a) {
|
||||
mons_vec2 result = {
|
||||
a.m1.y,
|
||||
a.m2.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
float mons_mat2_determinant(mons_mat2 a) {
|
||||
// ad - bc
|
||||
return (a.m1.x * a.m2.y) - (a.m1.y * a.m2.x);
|
||||
}
|
||||
|
||||
mons_mat2 mons_mat2_adjoint(mons_mat2 a) {
|
||||
/*
|
||||
* d -b
|
||||
* -c a
|
||||
*/
|
||||
mons_mat2 result = {{
|
||||
a.m2.y,
|
||||
-a.m1.y,
|
||||
},
|
||||
{
|
||||
-a.m2.x,
|
||||
a.m1.x,
|
||||
}};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat2 mons_mat2_inverse(mons_mat2 a) {
|
||||
return mons_mat2_mul_f(mons_mat2_adjoint(a),
|
||||
1.0 / mons_mat2_determinant(a));
|
||||
}
|
||||
|
||||
int mons_mat2_equal(mons_mat2 a, mons_mat2 b) {
|
||||
return mons_vec2_equal(a.m1, b.m1) && mons_vec2_equal(a.m2, b.m2);
|
||||
}
|
237
mons_math/src/mat3.c
Normal file
237
mons_math/src/mat3.c
Normal file
@ -0,0 +1,237 @@
|
||||
#include "mons_math/mat3.h"
|
||||
#include "mons_math/mat2.h"
|
||||
#include "mons_math/vec3.h"
|
||||
|
||||
const mons_mat3 MONS_MAT3_ZERO = {
|
||||
{0, 0, 0},
|
||||
{0, 0, 0},
|
||||
{0, 0, 0},
|
||||
};
|
||||
|
||||
const mons_mat3 MONS_MAT3_IDENTITY = {
|
||||
{1, 0, 0},
|
||||
{0, 1, 0},
|
||||
{0, 0, 1},
|
||||
};
|
||||
|
||||
mons_mat3 mons_mat3_add(mons_mat3 a, mons_mat3 b) {
|
||||
mons_mat3 result = {
|
||||
mons_vec3_add(a.m1, b.m1),
|
||||
mons_vec3_add(a.m2, b.m2),
|
||||
mons_vec3_add(a.m3, b.m3),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat3_add_inplace(mons_mat3 *a, mons_mat3 b) {
|
||||
mons_vec3_add_inplace(&a->m1, b.m1);
|
||||
mons_vec3_add_inplace(&a->m2, b.m2);
|
||||
mons_vec3_add_inplace(&a->m3, b.m3);
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_mul_f(mons_mat3 a, float b) {
|
||||
mons_mat3 result = {
|
||||
mons_vec3_mul_f(a.m1, b),
|
||||
mons_vec3_mul_f(a.m2, b),
|
||||
mons_vec3_mul_f(a.m3, b),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_mul_i(mons_mat3 a, int b) {
|
||||
mons_mat3 result = {
|
||||
mons_vec3_mul_i(a.m1, b),
|
||||
mons_vec3_mul_i(a.m2, b),
|
||||
mons_vec3_mul_i(a.m3, b),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_mul(mons_mat3 a, mons_mat3 b) {
|
||||
mons_vec3 b_n1 = mons_mat3_n1(b);
|
||||
mons_vec3 b_n2 = mons_mat3_n2(b);
|
||||
mons_vec3 b_n3 = mons_mat3_n3(b);
|
||||
|
||||
mons_mat3 result = {
|
||||
{
|
||||
mons_vec3_dot(a.m1, b_n1),
|
||||
mons_vec3_dot(a.m1, b_n2),
|
||||
mons_vec3_dot(a.m1, b_n3),
|
||||
},
|
||||
{
|
||||
mons_vec3_dot(a.m2, b_n1),
|
||||
mons_vec3_dot(a.m2, b_n2),
|
||||
mons_vec3_dot(a.m2, b_n3),
|
||||
},
|
||||
{
|
||||
mons_vec3_dot(a.m3, b_n1),
|
||||
mons_vec3_dot(a.m3, b_n2),
|
||||
mons_vec3_dot(a.m3, b_n3),
|
||||
},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat3_mul_f_inplace(mons_mat3 *a, float b) {
|
||||
mons_vec3_mul_f_inplace(&a->m1, b);
|
||||
mons_vec3_mul_f_inplace(&a->m2, b);
|
||||
mons_vec3_mul_f_inplace(&a->m3, b);
|
||||
}
|
||||
|
||||
void mons_mat3_mul_i_inplace(mons_mat3 *a, int b) {
|
||||
mons_vec3_mul_i_inplace(&a->m1, b);
|
||||
mons_vec3_mul_i_inplace(&a->m2, b);
|
||||
mons_vec3_mul_i_inplace(&a->m3, b);
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_transpose(mons_mat3 a) {
|
||||
mons_mat3 result = {
|
||||
mons_mat3_n1(a),
|
||||
mons_mat3_n2(a),
|
||||
mons_mat3_n3(a),
|
||||
};
|
||||
}
|
||||
|
||||
void mons_mat3_transpose_inplace(mons_mat3 *a) { *a = mons_mat3_transpose(*a); }
|
||||
|
||||
mons_vec3 mons_mat3_n1(mons_mat3 a) {
|
||||
mons_vec3 result = {
|
||||
a.m1.x,
|
||||
a.m2.x,
|
||||
a.m3.x,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec3 mons_mat3_n2(mons_mat3 a) {
|
||||
mons_vec3 result = {
|
||||
a.m1.y,
|
||||
a.m2.y,
|
||||
a.m3.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec3 mons_mat3_n3(mons_mat3 a) {
|
||||
mons_vec3 result = {
|
||||
a.m1.z,
|
||||
a.m2.z,
|
||||
a.m3.z,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
float mons_mat3_determinant(mons_mat3 a) {
|
||||
/*
|
||||
* a * det(
|
||||
* e f
|
||||
* h i
|
||||
* )
|
||||
* - b * det(
|
||||
* d f
|
||||
* g i
|
||||
* )
|
||||
* + c * det(
|
||||
* d e
|
||||
* g h
|
||||
* )
|
||||
*/
|
||||
mons_mat2 efhi = {
|
||||
{a.m2.y, a.m2.z},
|
||||
{a.m3.y, a.m3.z},
|
||||
};
|
||||
mons_mat2 dfgi = {
|
||||
{a.m2.x, a.m2.z},
|
||||
{a.m3.x, a.m3.z},
|
||||
};
|
||||
mons_mat2 degh = {
|
||||
{a.m2.x, a.m2.y},
|
||||
{a.m3.x, a.m3.y},
|
||||
};
|
||||
|
||||
return (a.m1.x * mons_mat2_determinant(efhi)) -
|
||||
(a.m1.y * mons_mat2_determinant(dfgi)) +
|
||||
(a.m1.z * mons_mat2_determinant(degh));
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_minor(mons_mat3 a) {
|
||||
// Get determinants of minors
|
||||
// First row elements
|
||||
mons_mat2 minor_1_1 = {
|
||||
{a.m2.y, a.m2.z},
|
||||
{a.m3.y, a.m3.z},
|
||||
};
|
||||
float det_1_1 = mons_mat2_determinant(minor_1_1);
|
||||
mons_mat2 minor_1_2 = {
|
||||
{a.m2.x, a.m2.z},
|
||||
{a.m3.x, a.m3.z},
|
||||
};
|
||||
float det_1_2 = mons_mat2_determinant(minor_1_2);
|
||||
mons_mat2 minor_1_3 = {
|
||||
{a.m2.x, a.m2.y},
|
||||
{a.m3.x, a.m3.y},
|
||||
};
|
||||
float det_1_3 = mons_mat2_determinant(minor_1_3);
|
||||
// Second row elements
|
||||
mons_mat2 minor_2_1 = {
|
||||
{a.m1.y, a.m1.z},
|
||||
{a.m3.y, a.m3.z},
|
||||
};
|
||||
float det_2_1 = mons_mat2_determinant(minor_2_1);
|
||||
mons_mat2 minor_2_2 = {
|
||||
{a.m1.x, a.m1.z},
|
||||
{a.m3.x, a.m3.z},
|
||||
};
|
||||
float det_2_2 = mons_mat2_determinant(minor_2_2);
|
||||
mons_mat2 minor_2_3 = {
|
||||
{a.m1.x, a.m1.y},
|
||||
{a.m3.x, a.m3.y},
|
||||
};
|
||||
float det_2_3 = mons_mat2_determinant(minor_2_3);
|
||||
// Third row elements
|
||||
mons_mat2 minor_3_1 = {
|
||||
{a.m1.y, a.m1.z},
|
||||
{a.m2.y, a.m2.z},
|
||||
};
|
||||
float det_3_1 = mons_mat2_determinant(minor_3_1);
|
||||
mons_mat2 minor_3_2 = {
|
||||
{a.m1.x, a.m1.z},
|
||||
{a.m2.x, a.m2.z},
|
||||
};
|
||||
float det_3_2 = mons_mat2_determinant(minor_3_2);
|
||||
mons_mat2 minor_3_3 = {
|
||||
{a.m1.x, a.m1.y},
|
||||
{a.m2.x, a.m2.y},
|
||||
};
|
||||
float det_3_3 = mons_mat2_determinant(minor_3_3);
|
||||
|
||||
mons_mat3 result = {
|
||||
{det_1_1, det_1_2, det_1_3},
|
||||
{det_2_1, det_2_2, det_2_3},
|
||||
{det_3_1, det_3_2, det_3_3},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_cofactor(mons_mat3 a) {
|
||||
mons_mat3 result = mons_mat3_minor(a);
|
||||
result.m1.y *= -1;
|
||||
result.m2.x *= -1;
|
||||
result.m2.z *= -1;
|
||||
result.m3.y *= -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_adjoint(mons_mat3 a) {
|
||||
return mons_mat3_transpose(mons_mat3_cofactor(a));
|
||||
}
|
||||
|
||||
mons_mat3 mons_mat3_inverse(mons_mat3 a) {
|
||||
return mons_mat3_mul_f(mons_mat3_adjoint(a),
|
||||
1.0 / mons_mat3_determinant(a));
|
||||
}
|
||||
|
||||
int mons_mat3_equal(mons_mat3 a, mons_mat3 b) {
|
||||
return mons_vec3_equal(a.m1, b.m1) && mons_vec3_equal(a.m2, b.m2) &&
|
||||
mons_vec3_equal(a.m3, b.m3);
|
||||
}
|
321
mons_math/src/mat4.c
Normal file
321
mons_math/src/mat4.c
Normal file
@ -0,0 +1,321 @@
|
||||
#include "mons_math/mat4.h"
|
||||
#include "mons_math/mat3.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include <stdio.h>
|
||||
|
||||
mons_mat4 mons_mat4_add(mons_mat4 a, mons_mat4 b) {
|
||||
mons_mat4 result = {
|
||||
mons_vec4_add(a.m1, b.m1),
|
||||
mons_vec4_add(a.m2, b.m2),
|
||||
mons_vec4_add(a.m3, b.m3),
|
||||
mons_vec4_add(a.m4, b.m4),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat4_add_inplace(mons_mat4 *a, mons_mat4 b) {
|
||||
mons_vec4_add_inplace(&a->m1, b.m1);
|
||||
mons_vec4_add_inplace(&a->m2, b.m2);
|
||||
mons_vec4_add_inplace(&a->m3, b.m3);
|
||||
mons_vec4_add_inplace(&a->m4, b.m4);
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_mul_f(mons_mat4 a, float b) {
|
||||
mons_mat4 result = {
|
||||
mons_vec4_mul_f(a.m1, b),
|
||||
mons_vec4_mul_f(a.m2, b),
|
||||
mons_vec4_mul_f(a.m3, b),
|
||||
mons_vec4_mul_f(a.m4, b),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_mul_i(mons_mat4 a, int b) {
|
||||
mons_mat4 result = {
|
||||
mons_vec4_mul_i(a.m1, b),
|
||||
mons_vec4_mul_i(a.m2, b),
|
||||
mons_vec4_mul_i(a.m3, b),
|
||||
mons_vec4_mul_i(a.m4, b),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat4_mul_f_inplace(mons_mat4 *a, float b) {
|
||||
mons_vec4_mul_f_inplace(&a->m1, b);
|
||||
mons_vec4_mul_f_inplace(&a->m2, b);
|
||||
mons_vec4_mul_f_inplace(&a->m3, b);
|
||||
mons_vec4_mul_f_inplace(&a->m4, b);
|
||||
}
|
||||
|
||||
void mons_mat4_mul_i_inplace(mons_mat4 *a, int b) {
|
||||
mons_vec4_mul_i_inplace(&a->m1, b);
|
||||
mons_vec4_mul_i_inplace(&a->m2, b);
|
||||
mons_vec4_mul_i_inplace(&a->m3, b);
|
||||
mons_vec4_mul_i_inplace(&a->m4, b);
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_mul(mons_mat4 a, mons_mat4 b) {
|
||||
mons_vec4 b_n1 = mons_mat4_n1(b);
|
||||
mons_vec4 b_n2 = mons_mat4_n2(b);
|
||||
mons_vec4 b_n3 = mons_mat4_n3(b);
|
||||
mons_vec4 b_n4 = mons_mat4_n4(b);
|
||||
|
||||
mons_mat4 result = {
|
||||
{
|
||||
mons_vec4_dot(a.m1, b_n1),
|
||||
mons_vec4_dot(a.m1, b_n2),
|
||||
mons_vec4_dot(a.m1, b_n3),
|
||||
mons_vec4_dot(a.m1, b_n4),
|
||||
},
|
||||
{
|
||||
mons_vec4_dot(a.m2, b_n1),
|
||||
mons_vec4_dot(a.m2, b_n2),
|
||||
mons_vec4_dot(a.m2, b_n3),
|
||||
mons_vec4_dot(a.m2, b_n4),
|
||||
},
|
||||
{
|
||||
mons_vec4_dot(a.m3, b_n1),
|
||||
mons_vec4_dot(a.m3, b_n2),
|
||||
mons_vec4_dot(a.m3, b_n3),
|
||||
mons_vec4_dot(a.m3, b_n4),
|
||||
},
|
||||
{
|
||||
mons_vec4_dot(a.m4, b_n1),
|
||||
mons_vec4_dot(a.m4, b_n2),
|
||||
mons_vec4_dot(a.m4, b_n3),
|
||||
mons_vec4_dot(a.m4, b_n4),
|
||||
},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_transpose(mons_mat4 a) {
|
||||
mons_mat4 result = {
|
||||
mons_mat4_n1(a),
|
||||
mons_mat4_n2(a),
|
||||
mons_mat4_n3(a),
|
||||
mons_mat4_n4(a),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_mat4_transpose_inplace(mons_mat4 *a) { *a = mons_mat4_transpose(*a); }
|
||||
|
||||
mons_vec4 mons_mat4_n1(mons_mat4 a) {
|
||||
mons_vec4 result = {
|
||||
a.m1.x,
|
||||
a.m2.x,
|
||||
a.m3.x,
|
||||
a.m4.x,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec4 mons_mat4_n2(mons_mat4 a) {
|
||||
mons_vec4 result = {
|
||||
a.m1.y,
|
||||
a.m2.y,
|
||||
a.m3.y,
|
||||
a.m4.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec4 mons_mat4_n3(mons_mat4 a) {
|
||||
mons_vec4 result = {
|
||||
a.m1.z,
|
||||
a.m2.z,
|
||||
a.m3.z,
|
||||
a.m4.z,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec4 mons_mat4_n4(mons_mat4 a) {
|
||||
mons_vec4 result = {
|
||||
a.m1.w,
|
||||
a.m2.w,
|
||||
a.m3.w,
|
||||
a.m4.w,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
float mons_mat4_determinant(mons_mat4 a) {
|
||||
mons_mat3 minor_1_1 = {
|
||||
{a.m2.y, a.m2.z, a.m2.w},
|
||||
{a.m3.y, a.m3.z, a.m3.w},
|
||||
{a.m4.y, a.m4.z, a.m4.w},
|
||||
};
|
||||
mons_mat3 minor_1_2 = {
|
||||
{a.m2.x, a.m2.z, a.m2.w},
|
||||
{a.m3.x, a.m3.z, a.m3.w},
|
||||
{a.m4.x, a.m4.z, a.m4.w},
|
||||
};
|
||||
mons_mat3 minor_1_3 = {
|
||||
{a.m2.x, a.m2.y, a.m2.w},
|
||||
{a.m3.x, a.m3.y, a.m3.w},
|
||||
{a.m4.x, a.m4.y, a.m4.w},
|
||||
};
|
||||
mons_mat3 minor_1_4 = {
|
||||
{a.m2.x, a.m2.y, a.m2.z},
|
||||
{a.m3.x, a.m3.y, a.m3.z},
|
||||
{a.m4.x, a.m4.y, a.m4.z},
|
||||
};
|
||||
|
||||
return (a.m1.x * mons_mat3_determinant(minor_1_1)) -
|
||||
(a.m1.y * mons_mat3_determinant(minor_1_2)) +
|
||||
(a.m1.z * mons_mat3_determinant(minor_1_3)) -
|
||||
(a.m1.w * mons_mat3_determinant(minor_1_4));
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_minor(mons_mat4 a) {
|
||||
// First Row
|
||||
mons_mat3 minor_1_1 = {
|
||||
{a.m2.y, a.m2.z, a.m2.w},
|
||||
{a.m3.y, a.m3.z, a.m3.w},
|
||||
{a.m4.y, a.m4.z, a.m4.w},
|
||||
};
|
||||
float det_1_1 = mons_mat3_determinant(minor_1_1);
|
||||
mons_mat3 minor_1_2 = {
|
||||
{a.m2.x, a.m2.z, a.m2.w},
|
||||
{a.m3.x, a.m3.z, a.m3.w},
|
||||
{a.m4.x, a.m4.z, a.m4.w},
|
||||
};
|
||||
float det_1_2 = mons_mat3_determinant(minor_1_2);
|
||||
mons_mat3 minor_1_3 = {
|
||||
{a.m2.x, a.m2.y, a.m2.w},
|
||||
{a.m3.x, a.m3.y, a.m3.w},
|
||||
{a.m4.x, a.m4.y, a.m4.w},
|
||||
};
|
||||
float det_1_3 = mons_mat3_determinant(minor_1_3);
|
||||
mons_mat3 minor_1_4 = {
|
||||
{a.m2.x, a.m2.y, a.m2.z},
|
||||
{a.m3.x, a.m3.y, a.m3.z},
|
||||
{a.m4.x, a.m4.y, a.m4.z},
|
||||
};
|
||||
float det_1_4 = mons_mat3_determinant(minor_1_4);
|
||||
// Second Row
|
||||
mons_mat3 minor_2_1 = {
|
||||
{a.m1.y, a.m1.z, a.m1.w},
|
||||
{a.m3.y, a.m3.z, a.m3.w},
|
||||
{a.m4.y, a.m4.z, a.m4.w},
|
||||
};
|
||||
float det_2_1 = mons_mat3_determinant(minor_2_1);
|
||||
mons_mat3 minor_2_2 = {
|
||||
{a.m1.x, a.m1.z, a.m1.w},
|
||||
{a.m3.x, a.m3.z, a.m3.w},
|
||||
{a.m4.x, a.m4.z, a.m4.w},
|
||||
};
|
||||
float det_2_2 = mons_mat3_determinant(minor_2_2);
|
||||
mons_mat3 minor_2_3 = {
|
||||
{a.m1.x, a.m1.y, a.m1.w},
|
||||
{a.m3.x, a.m3.y, a.m3.w},
|
||||
{a.m4.x, a.m4.y, a.m4.w},
|
||||
};
|
||||
float det_2_3 = mons_mat3_determinant(minor_2_3);
|
||||
mons_mat3 minor_2_4 = {
|
||||
{a.m1.x, a.m1.y, a.m1.z},
|
||||
{a.m3.x, a.m3.y, a.m3.z},
|
||||
{a.m4.x, a.m4.y, a.m4.z},
|
||||
};
|
||||
float det_2_4 = mons_mat3_determinant(minor_2_4);
|
||||
// Third Row
|
||||
mons_mat3 minor_3_1 = {
|
||||
{a.m1.y, a.m1.z, a.m1.w},
|
||||
{a.m2.y, a.m2.z, a.m2.w},
|
||||
{a.m4.y, a.m4.z, a.m4.w},
|
||||
};
|
||||
float det_3_1 = mons_mat3_determinant(minor_3_1);
|
||||
mons_mat3 minor_3_2 = {
|
||||
{a.m1.x, a.m1.z, a.m1.w},
|
||||
{a.m2.x, a.m2.z, a.m2.w},
|
||||
{a.m4.x, a.m4.z, a.m4.w},
|
||||
};
|
||||
float det_3_2 = mons_mat3_determinant(minor_3_2);
|
||||
mons_mat3 minor_3_3 = {
|
||||
{a.m1.x, a.m1.y, a.m1.w},
|
||||
{a.m2.x, a.m2.y, a.m2.w},
|
||||
{a.m4.x, a.m4.y, a.m4.w},
|
||||
};
|
||||
float det_3_3 = mons_mat3_determinant(minor_3_3);
|
||||
mons_mat3 minor_3_4 = {
|
||||
{a.m1.x, a.m1.y, a.m1.z},
|
||||
{a.m2.x, a.m2.y, a.m2.z},
|
||||
{a.m4.x, a.m4.y, a.m4.z},
|
||||
};
|
||||
float det_3_4 = mons_mat3_determinant(minor_3_4);
|
||||
// Fourth Row
|
||||
mons_mat3 minor_4_1 = {
|
||||
{a.m1.y, a.m1.z, a.m1.w},
|
||||
{a.m2.y, a.m2.z, a.m2.w},
|
||||
{a.m3.y, a.m3.z, a.m3.w},
|
||||
};
|
||||
float det_4_1 = mons_mat3_determinant(minor_4_1);
|
||||
mons_mat3 minor_4_2 = {
|
||||
{a.m1.x, a.m1.z, a.m1.w},
|
||||
{a.m2.x, a.m2.z, a.m2.w},
|
||||
{a.m3.x, a.m3.z, a.m3.w},
|
||||
};
|
||||
float det_4_2 = mons_mat3_determinant(minor_4_2);
|
||||
mons_mat3 minor_4_3 = {
|
||||
{a.m1.x, a.m1.y, a.m1.w},
|
||||
{a.m2.x, a.m2.y, a.m2.w},
|
||||
{a.m3.x, a.m3.y, a.m3.w},
|
||||
};
|
||||
float det_4_3 = mons_mat3_determinant(minor_4_3);
|
||||
mons_mat3 minor_4_4 = {
|
||||
{a.m1.x, a.m1.y, a.m1.z},
|
||||
{a.m2.x, a.m2.y, a.m2.z},
|
||||
{a.m3.x, a.m3.y, a.m3.z},
|
||||
};
|
||||
float det_4_4 = mons_mat3_determinant(minor_4_4);
|
||||
|
||||
mons_mat4 result = {
|
||||
{det_1_1, det_1_2, det_1_3, det_1_4},
|
||||
{det_2_1, det_2_2, det_2_3, det_2_4},
|
||||
{det_3_1, det_3_2, det_3_3, det_3_4},
|
||||
{det_4_1, det_4_2, det_4_3, det_4_4},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_cofactor(mons_mat4 a) {
|
||||
mons_mat4 result = mons_mat4_minor(a);
|
||||
result.m1.y *= -1;
|
||||
result.m1.w *= -1;
|
||||
result.m2.x *= -1;
|
||||
result.m2.z *= -1;
|
||||
result.m3.y *= -1;
|
||||
result.m3.w *= -1;
|
||||
result.m4.x *= -1;
|
||||
result.m4.z *= -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_adjoint(mons_mat4 a) {
|
||||
return mons_mat4_transpose(mons_mat4_cofactor(a));
|
||||
}
|
||||
|
||||
mons_mat4 mons_mat4_inverse(mons_mat4 a) {
|
||||
return mons_mat4_mul_f(mons_mat4_adjoint(a),
|
||||
1.0 / mons_mat4_determinant(a));
|
||||
}
|
||||
|
||||
int mons_mat4_equal(mons_mat4 a, mons_mat4 b) {
|
||||
return mons_vec4_equal(a.m1, b.m1)
|
||||
&& mons_vec4_equal(a.m2, b.m2)
|
||||
&& mons_vec4_equal(a.m3, b.m3)
|
||||
&& mons_vec4_equal(a.m4, b.m4);
|
||||
}
|
||||
|
||||
void mons_mat4_mul_inplace(mons_mat4 *a, mons_mat4 b) {
|
||||
*a = mons_mat4_mul(*a, b);
|
||||
}
|
||||
|
||||
void mons_mat4_print(mons_mat4 mat) {
|
||||
printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.x, mat.m2.x, mat.m3.x, mat.m4.x);
|
||||
printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.y, mat.m2.y, mat.m3.y, mat.m4.y);
|
||||
printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.z, mat.m2.z, mat.m3.z, mat.m4.z);
|
||||
printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.w, mat.m2.w, mat.m3.w, mat.m4.w);
|
||||
}
|
141
mons_math/src/quat.c
Normal file
141
mons_math/src/quat.c
Normal file
@ -0,0 +1,141 @@
|
||||
#include "mons_math/quat.h"
|
||||
#include "mons_math/mat4.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
mons_quat mons_quat_from_axis_angle(mons_vec3 axis, float angle) {
|
||||
float half_angle = 0.5f * angle;
|
||||
float s = sinf(half_angle);
|
||||
return (mons_quat){
|
||||
axis.x * s,
|
||||
axis.y * s,
|
||||
axis.z * s,
|
||||
cosf(half_angle),
|
||||
};
|
||||
}
|
||||
|
||||
mons_quat mons_quat_mul(mons_quat a, mons_quat b) {
|
||||
return (mons_quat){
|
||||
(a.w * b.x) + (a.x * b.w) + (a.y * b.z) - (a.z * b.y),
|
||||
(a.w * b.y) + (a.y * b.w) + (a.z * b.x) - (a.x * b.z),
|
||||
(a.w * b.z) + (a.z * b.w) + (a.x * b.y) - (a.y * b.x),
|
||||
(a.w * b.w) - (a.x * b.x) - (a.y * b.y) - (a.z * b.z),
|
||||
};
|
||||
}
|
||||
|
||||
void mons_quat_mul_inplace(mons_quat *a, mons_quat b) {
|
||||
*a = mons_quat_mul(*a, b);
|
||||
}
|
||||
|
||||
mons_quat mons_quat_mul_f(mons_quat a, float b) {
|
||||
return (mons_quat){
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
a.z * b,
|
||||
a.w * b,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_quat_mul_f_inplace(mons_quat *a, float b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
a->z *= b;
|
||||
a->w *= b;
|
||||
}
|
||||
|
||||
mons_quat mons_quat_mul_i(mons_quat a, int b) {
|
||||
return (mons_quat){
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
a.z * b,
|
||||
a.w * b,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_quat_mul_i_inplace(mons_quat *a, int b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
a->z *= b;
|
||||
a->w *= b;
|
||||
}
|
||||
|
||||
mons_mat4 mons_quat_to_mat4(mons_quat a) {
|
||||
return (mons_mat4){
|
||||
{
|
||||
1.0 - (2.0 * a.y * a.y) - (2.0 * a.z * a.z),
|
||||
(2.0 * a.x * a.y) - (2.0 * a.w * a.z),
|
||||
(2.0 * a.x * a.z) + (2.0 * a.w * a.y),
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
(2.0 * a.x * a.y) + (2.0 * a.w * a.z),
|
||||
1.0 - (2.0 * a.x * a.x) - (2.0 * a.z * a.z),
|
||||
(2.0 * a.y * a.z) - (2.0 * a.w * a.x),
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
(2.0 * a.x * a.z) - (2.0 * a.w * a.y),
|
||||
(2.0 * a.y * a.z) + (2.0 * a.w * a.x),
|
||||
1.0 - (2.0 * a.x * a.x) - (2.0 * a.y * a.y),
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
mons_quat mons_quat_normalize(mons_quat a) {
|
||||
mons_vec4 normalized = mons_vec4_normalize(*((mons_vec4 *)&a));
|
||||
return *((mons_quat *)&normalized);
|
||||
}
|
||||
|
||||
void mons_quat_normalize_inplace(mons_quat *a) {
|
||||
mons_vec4_normalize_inplace((mons_vec4 *)a);
|
||||
}
|
||||
|
||||
mons_vec3 mons_quat_transform_vec3(mons_quat quat, mons_vec3 vec) {
|
||||
mons_vec3 quat_vec = (mons_vec3){quat.x, quat.y, quat.z};
|
||||
float s = quat.w;
|
||||
|
||||
return mons_vec3_add(
|
||||
mons_vec3_add(
|
||||
mons_vec3_mul_f(quat_vec, mons_vec3_dot(quat_vec, vec) * 2.0f),
|
||||
mons_vec3_mul_f(vec, s * s - mons_vec3_dot(quat_vec, quat_vec))),
|
||||
mons_vec3_mul_f(mons_vec3_cross(quat_vec, vec), 2.0f * s));
|
||||
}
|
||||
|
||||
mons_quat mons_quat_conjugate(mons_quat quat) {
|
||||
return (mons_quat){
|
||||
.x = -quat.x,
|
||||
.y = -quat.y,
|
||||
.z = -quat.z,
|
||||
.w = quat.w,
|
||||
};
|
||||
}
|
||||
|
||||
mons_quat mons_quat_looking_at(mons_vec3 eye, mons_vec3 target, mons_vec3 up) {
|
||||
mons_vec3 f = mons_vec3_normalize(mons_vec3_sub(target, eye));
|
||||
mons_vec3 side = mons_vec3_normalize(mons_vec3_cross(up, f));
|
||||
mons_vec3 b = mons_vec3_normalize(mons_vec3_add(f, MONS_VEC3_NEG_Z));
|
||||
mons_quat p = mons_quat_from_axis_angle(mons_vec3_cross(b, f), mons_vec3_dot(b,f));
|
||||
mons_vec3 r = (mons_vec3) {
|
||||
p.w*p.w + p.x*p.x - p.y*p.y - p.z*p.z,
|
||||
(2.0f * p.x * p.y) - (2.0f * p.w * p.z),
|
||||
(2.0f * p.x * p.z) + (2.0f * p.w * p.y),
|
||||
};
|
||||
b = mons_vec3_normalize(mons_vec3_add(side, r));
|
||||
mons_quat q = mons_quat_from_axis_angle(mons_vec3_cross(b,side), mons_vec3_dot(b, side));
|
||||
|
||||
return mons_quat_mul(p, q);
|
||||
}
|
||||
|
||||
void mons_quat_print(mons_quat quat) {
|
||||
mons_vec4_print(*(mons_vec4*)&quat);
|
||||
}
|
7
mons_math/src/util.c
Normal file
7
mons_math/src/util.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include "mons_math/util.h"
|
||||
#include <math.h>
|
||||
|
||||
int mons_float_approx_equal(float a, float b) {
|
||||
return fabs(a - b) < MONS_FLOAT_EQUAL_EPSILON;
|
||||
}
|
||||
|
120
mons_math/src/vec2.c
Normal file
120
mons_math/src/vec2.c
Normal file
@ -0,0 +1,120 @@
|
||||
#include "mons_math/vec2.h"
|
||||
#include "mons_math/util.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include <math.h>
|
||||
|
||||
mons_vec2 mons_vec2_add(mons_vec2 a, mons_vec2 b) {
|
||||
mons_vec2 result = {
|
||||
a.x + b.x,
|
||||
a.y + b.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec2_add_inplace(mons_vec2 *a, mons_vec2 b) {
|
||||
a->x += b.x;
|
||||
a->y += b.y;
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_sub(mons_vec2 a, mons_vec2 b) {
|
||||
mons_vec2 result = {
|
||||
a.x - b.x,
|
||||
a.y - b.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec2_sub_inplace(mons_vec2 *a, mons_vec2 b) {
|
||||
a->x -= b.x;
|
||||
a->y -= b.y;
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_mul_f(mons_vec2 a, float b) {
|
||||
mons_vec2 result = {
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_mul_i(mons_vec2 a, int b) {
|
||||
mons_vec2 result = {
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
float mons_vec2_dot(mons_vec2 a, mons_vec2 b) {
|
||||
return (a.x * b.x) + (a.y * b.y);
|
||||
}
|
||||
|
||||
void mons_vec2_mul_f_inplace(mons_vec2 *a, float b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
}
|
||||
|
||||
void mons_vec2_mul_i_inplace(mons_vec2 *a, int b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
}
|
||||
|
||||
float mons_vec2_len(mons_vec2 a) { return sqrtf(mons_vec2_len_squared(a)); }
|
||||
|
||||
float mons_vec2_len_squared(mons_vec2 a) { return (a.x * a.x) + (a.y * a.y); }
|
||||
|
||||
mons_vec3 mons_vec2_extend(mons_vec2 a) {
|
||||
mons_vec3 result = {
|
||||
a.x,
|
||||
a.y,
|
||||
0.0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
int mons_vec2_equal(mons_vec2 a, mons_vec2 b) {
|
||||
return mons_float_approx_equal(a.x, b.x) &&
|
||||
mons_float_approx_equal(a.y, b.y);
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_div_f(mons_vec2 a, float b) {
|
||||
return (mons_vec2) {
|
||||
a.x / b,
|
||||
a.y / b,
|
||||
};
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_div_i(mons_vec2 a, int b) {
|
||||
return (mons_vec2) {
|
||||
a.x / b,
|
||||
a.y / b,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_vec2_div_f_inplace(mons_vec2 *a, float b) {
|
||||
a->x /= b;
|
||||
a->y /= b;
|
||||
}
|
||||
|
||||
void mons_vec2_div_i_inplace(mons_vec2 *a, int b) {
|
||||
a->x /= b;
|
||||
a->y /= b;
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_negate(mons_vec2 a) {
|
||||
return mons_vec2_mul_i(a, -1);
|
||||
}
|
||||
|
||||
void mons_vec2_negate_inplace(mons_vec2 *a) {
|
||||
mons_vec2_mul_i_inplace(a, -1);
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec2_normalize(mons_vec2 a) {
|
||||
float len = mons_vec2_len(a);
|
||||
return mons_vec2_div_f(a, len);
|
||||
}
|
||||
|
||||
void mons_vec2_normalize_inplace(mons_vec2 *a) {
|
||||
float len = mons_vec2_len(*a);
|
||||
mons_vec2_div_f_inplace(a, len);
|
||||
}
|
175
mons_math/src/vec3.c
Normal file
175
mons_math/src/vec3.c
Normal file
@ -0,0 +1,175 @@
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/vec2.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/util.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
mons_vec3 mons_vec3_add(mons_vec3 a, mons_vec3 b) {
|
||||
mons_vec3 result = {
|
||||
a.x + b.x,
|
||||
a.y + b.y,
|
||||
a.z + b.z,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec3_add_inplace(mons_vec3 *a, mons_vec3 b) {
|
||||
a->x += b.x;
|
||||
a->y += b.y;
|
||||
a->z += b.z;
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_sub(mons_vec3 a, mons_vec3 b) {
|
||||
mons_vec3 result = {
|
||||
a.x - b.x,
|
||||
a.y - b.y,
|
||||
a.z - b.z,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec3_sub_inplace(mons_vec3 *a, mons_vec3 b) {
|
||||
a->x -= b.x;
|
||||
a->y -= b.y;
|
||||
a->z -= b.z;
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_mul_f(mons_vec3 a, float b) {
|
||||
mons_vec3 result = {
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
a.z * b,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_mul_i(mons_vec3 a, int b) {
|
||||
mons_vec3 result = {
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
a.z * b,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
float mons_vec3_dot(mons_vec3 a, mons_vec3 b) {
|
||||
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
|
||||
}
|
||||
|
||||
void mons_vec3_mul_f_inplace(mons_vec3 *a, float b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
a->z *= b;
|
||||
}
|
||||
|
||||
void mons_vec3_mul_i_inplace(mons_vec3 *a, int b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
a->z *= b;
|
||||
}
|
||||
|
||||
float mons_vec3_len(mons_vec3 a) { return sqrtf(mons_vec3_len_squared(a)); }
|
||||
|
||||
float mons_vec3_len_squared(mons_vec3 a) {
|
||||
return (a.x * a.x) + (a.y * a.y) + (a.z * a.z);
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec3_extend(mons_vec3 a) {
|
||||
mons_vec4 result = {
|
||||
a.x,
|
||||
a.y,
|
||||
a.z,
|
||||
0.0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec2 mons_vec3_truncate(mons_vec3 a) {
|
||||
mons_vec2 result = {
|
||||
a.x,
|
||||
a.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
int mons_vec3_equal(mons_vec3 a, mons_vec3 b) {
|
||||
return mons_float_approx_equal(a.x, b.x)
|
||||
&& mons_float_approx_equal(a.y, b.y)
|
||||
&& mons_float_approx_equal(a.z, b.z);
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_div_f(mons_vec3 a, float b) {
|
||||
return (mons_vec3) {
|
||||
a.x / b,
|
||||
a.y / b,
|
||||
a.z / b,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_vec3_div_f_inplace(mons_vec3 *a, float b) {
|
||||
a->x /= b;
|
||||
a->y /= b;
|
||||
a->z /= b;
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_div_i(mons_vec3 a, int b) {
|
||||
return (mons_vec3) {
|
||||
a.x / b,
|
||||
a.y / b,
|
||||
a.z / b,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_vec3_div_i_inplace(mons_vec3 *a, int b) {
|
||||
a->x /= b;
|
||||
a->y /= b;
|
||||
a->z /= b;
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_negate(mons_vec3 a) {
|
||||
return mons_vec3_mul_i(a, -1);
|
||||
}
|
||||
|
||||
void mons_vec3_negate_inplace(mons_vec3 *a) {
|
||||
return mons_vec3_mul_i_inplace(a, -1);
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_normalize(mons_vec3 a) {
|
||||
float length = mons_vec3_len(a);
|
||||
return mons_vec3_div_f(a, length);
|
||||
}
|
||||
|
||||
void mons_vec3_normalize_inplace(mons_vec3 *a) {
|
||||
float length = mons_vec3_len(*a);
|
||||
mons_vec3_div_f_inplace(a, length);
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_cross(mons_vec3 a, mons_vec3 b) {
|
||||
return (mons_vec3) {
|
||||
(a.y * b.z) - (a.z * b.y),
|
||||
(a.z * b.x) - (a.x * b.z),
|
||||
(a.x * b.y) - (a.y * b.x),
|
||||
};
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec3_mul_memberwise(mons_vec3 a, mons_vec3 b) {
|
||||
return (mons_vec3) {
|
||||
a.x * b.x,
|
||||
a.y * b.y,
|
||||
a.z * b.z,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_vec3_mul_memberwise_inplace(mons_vec3 *a, mons_vec3 b) {
|
||||
a->x *= b.x;
|
||||
a->y *= b.y;
|
||||
a->z *= b.z;
|
||||
}
|
||||
|
||||
void mons_vec3_print(mons_vec3 vec) {
|
||||
printf("[%f, %f, %f]", vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
149
mons_math/src/vec4.c
Normal file
149
mons_math/src/vec4.c
Normal file
@ -0,0 +1,149 @@
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/util.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
mons_vec4 mons_vec4_add(mons_vec4 a, mons_vec4 b) {
|
||||
mons_vec4 result = {
|
||||
a.x + b.x,
|
||||
a.y + b.y,
|
||||
a.z + b.z,
|
||||
a.w + b.w,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec4_add_inplace(mons_vec4 *a, mons_vec4 b) {
|
||||
a->x += b.x;
|
||||
a->y += b.y;
|
||||
a->z += b.z;
|
||||
a->w += b.w;
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_sub(mons_vec4 a, mons_vec4 b) {
|
||||
mons_vec4 result = {
|
||||
a.x - b.x,
|
||||
a.y - b.y,
|
||||
a.z - b.z,
|
||||
a.w - b.w,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec4_sub_inplace(mons_vec4 *a, mons_vec4 b) {
|
||||
a->x -= b.x;
|
||||
a->y -= b.y;
|
||||
a->z -= b.z;
|
||||
a->w -= b.w;
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_mul_f(mons_vec4 a, float b) {
|
||||
mons_vec4 result = {
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
a.z * b,
|
||||
a.w * b,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_mul_i(mons_vec4 a, int b) {
|
||||
mons_vec4 result = {
|
||||
a.x * b,
|
||||
a.y * b,
|
||||
a.z * b,
|
||||
a.w * b,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
void mons_vec4_mul_f_inplace(mons_vec4 *a, float b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
a->z *= b;
|
||||
a->w *= b;
|
||||
}
|
||||
|
||||
void mons_vec4_mul_i_inplace(mons_vec4 *a, int b) {
|
||||
a->x *= b;
|
||||
a->y *= b;
|
||||
a->z *= b;
|
||||
a->w *= b;
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_div_f(mons_vec4 a, float b) {
|
||||
return (mons_vec4) {
|
||||
a.x / b,
|
||||
a.y / b,
|
||||
a.z / b,
|
||||
a.w / b,
|
||||
};
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_div_i(mons_vec4 a, int b) {
|
||||
return (mons_vec4) {
|
||||
a.x / b,
|
||||
a.y / b,
|
||||
a.z / b,
|
||||
a.w / b,
|
||||
};
|
||||
}
|
||||
|
||||
void mons_vec4_div_f_inplace(mons_vec4 *a, float b) {
|
||||
a->x /= b;
|
||||
a->y /= b;
|
||||
a->z /= b;
|
||||
a->w /= b;
|
||||
}
|
||||
|
||||
void mons_vec4_div_i_inplace(mons_vec4 *a, int b) {
|
||||
a->x /= b;
|
||||
a->y /= b;
|
||||
a->z /= b;
|
||||
a->w /= b;
|
||||
}
|
||||
|
||||
float mons_vec4_dot(mons_vec4 a, mons_vec4 b) {
|
||||
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
|
||||
}
|
||||
|
||||
float mons_vec4_len(mons_vec4 a) { return sqrt(mons_vec4_len_squared(a)); }
|
||||
|
||||
float mons_vec4_len_squared(mons_vec4 a) {
|
||||
return (a.x * a.x) + (a.y * a.y) + (a.z * a.z) + (a.w * a.w);
|
||||
}
|
||||
|
||||
mons_vec3 mons_vec4_truncate(mons_vec4 a) {
|
||||
mons_vec3 result = {a.x, a.y, a.z};
|
||||
return result;
|
||||
}
|
||||
|
||||
int mons_vec4_equal(mons_vec4 a, mons_vec4 b) {
|
||||
return mons_float_approx_equal(a.x, b.x)
|
||||
&& mons_float_approx_equal(a.y, b.y)
|
||||
&& mons_float_approx_equal(a.z, b.z)
|
||||
&& mons_float_approx_equal(a.w, b.w);
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_negate(mons_vec4 a) {
|
||||
return mons_vec4_mul_i(a, -1);
|
||||
}
|
||||
|
||||
void mons_vec4_negate_inplace(mons_vec4 *a) {
|
||||
mons_vec4_mul_i_inplace(a, -1);
|
||||
}
|
||||
|
||||
mons_vec4 mons_vec4_normalize(mons_vec4 a) {
|
||||
float len = mons_vec4_len(a);
|
||||
return mons_vec4_div_f(a, len);
|
||||
}
|
||||
|
||||
void mons_vec4_normalize_inplace(mons_vec4 *a) {
|
||||
float len = mons_vec4_len(*a);
|
||||
mons_vec4_div_f_inplace(a, len);
|
||||
}
|
||||
|
||||
void mons_vec4_print(mons_vec4 vec) {
|
||||
printf("[%f, %f, %f, %f]", vec.x, vec.y, vec.z, vec.w);
|
||||
}
|
0
mons_math/tests/mat2_ops.c
Normal file
0
mons_math/tests/mat2_ops.c
Normal file
0
mons_math/tests/mat3_ops.c
Normal file
0
mons_math/tests/mat3_ops.c
Normal file
0
mons_math/tests/mat4_ops.c
Normal file
0
mons_math/tests/mat4_ops.c
Normal file
14
mons_math/tests/test.h
Normal file
14
mons_math/tests/test.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef TEST_H
|
||||
#define TEST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ASSERT(cond) {\
|
||||
if (!(cond)) {\
|
||||
fprintf(stderr, "Assertion Failed @ %s:%d (%s): %s", __func__, __LINE__, __FILE__, #cond);\
|
||||
exit(EXIT_FAILURE);\
|
||||
}\
|
||||
}
|
||||
|
||||
#endif
|
81
mons_math/tests/vec2_ops.c
Normal file
81
mons_math/tests/vec2_ops.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include "mons_math/vec2.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/util.h"
|
||||
#include "test.h"
|
||||
|
||||
int main(void) {
|
||||
mons_vec2 a = {2.0, 3.0};
|
||||
mons_vec2 b = {6.0, 5.0};
|
||||
|
||||
// Add
|
||||
mons_vec2 sum = mons_vec2_add(a, b);
|
||||
ASSERT(mons_vec2_equal(sum, (mons_vec2){8.0, 8.0}));
|
||||
|
||||
// Add in Place
|
||||
mons_vec2_add_inplace(&a, b);
|
||||
ASSERT(mons_vec2_equal(a, sum));
|
||||
|
||||
// Subtract
|
||||
mons_vec2 diff = mons_vec2_sub(sum, b);
|
||||
ASSERT(mons_vec2_equal(diff, (mons_vec2){2.0, 3.0}));
|
||||
|
||||
// Subtract in Place
|
||||
mons_vec2_sub_inplace(&a, b);
|
||||
ASSERT(mons_vec2_equal(diff, a));
|
||||
|
||||
// Multiply (float)
|
||||
mons_vec2 product_f = mons_vec2_mul_f(a, -3.0);
|
||||
ASSERT(mons_vec2_equal(product_f, (mons_vec2){-6.0, -9.0}))
|
||||
|
||||
// Multiply in Place (float)
|
||||
mons_vec2_mul_f_inplace(&a, 3.0);
|
||||
ASSERT(mons_vec2_equal(mons_vec2_negate(product_f), a))
|
||||
|
||||
// Divide (float)
|
||||
mons_vec2 quotient_f = mons_vec2_div_f(product_f, 3.0);
|
||||
mons_vec2_negate_inplace("ient_f);
|
||||
ASSERT(mons_vec2_equal(quotient_f, (mons_vec2){2.0, 3.0}));
|
||||
|
||||
// Divide in Place (float)
|
||||
mons_vec2_div_f_inplace(&a, 3.0);
|
||||
ASSERT(mons_vec2_equal(quotient_f, a));
|
||||
|
||||
// Multiply (int)
|
||||
mons_vec2 product_i = mons_vec2_mul_i(a, -3);
|
||||
ASSERT(mons_vec2_equal(product_i, (mons_vec2){-6.0, -9.0}))
|
||||
|
||||
// Multiply in Place (int)
|
||||
mons_vec2_mul_i_inplace(&a, 3);
|
||||
ASSERT(mons_vec2_equal(mons_vec2_negate(product_i), a))
|
||||
|
||||
// Divide (int)
|
||||
mons_vec2 quotient_i = mons_vec2_div_i(product_i, 3);
|
||||
mons_vec2_negate_inplace("ient_i);
|
||||
ASSERT(mons_vec2_equal(quotient_i, (mons_vec2){2.0, 3.0}));
|
||||
|
||||
// Divide in Place (int)
|
||||
mons_vec2_div_i_inplace(&a, 3);
|
||||
ASSERT(mons_vec2_equal(quotient_i, a));
|
||||
|
||||
// Get Length
|
||||
float a_len = mons_vec2_len(a);
|
||||
ASSERT(mons_float_approx_equal(a_len, 3.60555));
|
||||
|
||||
// Dot Product
|
||||
float dot = mons_vec2_dot(a, b);
|
||||
ASSERT(mons_float_approx_equal(dot, 27.0));
|
||||
|
||||
// Extend
|
||||
mons_vec3 extended = mons_vec2_extend(a);
|
||||
ASSERT(mons_vec3_equal(extended, (mons_vec3){a.x, a.y, 0.0}));
|
||||
|
||||
// Normalize
|
||||
mons_vec2 normalized = mons_vec2_normalize(a);
|
||||
ASSERT(mons_float_approx_equal(mons_vec2_len(normalized), 1.0));
|
||||
|
||||
// Normalize in Place
|
||||
mons_vec2_normalize_inplace(&a);
|
||||
ASSERT(mons_float_approx_equal(mons_vec2_len(a), 1.0));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
90
mons_math/tests/vec3_ops.c
Normal file
90
mons_math/tests/vec3_ops.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include "mons_math/vec2.h"
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/util.h"
|
||||
#include "test.h"
|
||||
|
||||
int main(void) {
|
||||
mons_vec3 a = {2.0, 3.0, 4.0};
|
||||
mons_vec3 b = {6.0, 5.0, 4.0};
|
||||
|
||||
// Add
|
||||
mons_vec3 sum = mons_vec3_add(a, b);
|
||||
ASSERT(mons_vec3_equal(sum, (mons_vec3){8.0, 8.0, 8.0}));
|
||||
|
||||
// Add in Place
|
||||
mons_vec3_add_inplace(&a, b);
|
||||
ASSERT(mons_vec3_equal(a, sum));
|
||||
|
||||
// Subtract
|
||||
mons_vec3 diff = mons_vec3_sub(sum, b);
|
||||
ASSERT(mons_vec3_equal(diff, (mons_vec3){2.0, 3.0, 4.0}));
|
||||
|
||||
// Subtract in Place
|
||||
mons_vec3_sub_inplace(&a, b);
|
||||
ASSERT(mons_vec3_equal(diff, a));
|
||||
|
||||
// Multiply (float)
|
||||
mons_vec3 product_f = mons_vec3_mul_f(a, -3.0);
|
||||
ASSERT(mons_vec3_equal(product_f, (mons_vec3){-6.0, -9.0, -12.0}))
|
||||
|
||||
// Multiply in Place (float)
|
||||
mons_vec3_mul_f_inplace(&a, 3.0);
|
||||
ASSERT(mons_vec3_equal(mons_vec3_negate(product_f), a))
|
||||
|
||||
// Divide (float)
|
||||
mons_vec3 quotient_f = mons_vec3_div_f(product_f, 3.0);
|
||||
mons_vec3_negate_inplace("ient_f);
|
||||
ASSERT(mons_vec3_equal(quotient_f, (mons_vec3){2.0, 3.0, 4.0}));
|
||||
|
||||
// Divide in Place (float)
|
||||
mons_vec3_div_f_inplace(&a, 3.0);
|
||||
ASSERT(mons_vec3_equal(quotient_f, a));
|
||||
|
||||
// Multiply (int)
|
||||
mons_vec3 product_i = mons_vec3_mul_i(a, -3);
|
||||
ASSERT(mons_vec3_equal(product_i, (mons_vec3){-6.0, -9.0, -12.0}))
|
||||
|
||||
// Multiply in Place (int)
|
||||
mons_vec3_mul_i_inplace(&a, 3);
|
||||
ASSERT(mons_vec3_equal(mons_vec3_negate(product_i), a))
|
||||
|
||||
// Divide (int)
|
||||
mons_vec3 quotient_i = mons_vec3_div_i(product_i, 3);
|
||||
mons_vec3_negate_inplace("ient_i);
|
||||
ASSERT(mons_vec3_equal(quotient_i, (mons_vec3){2.0, 3.0, 4.0}));
|
||||
|
||||
// Divide in Place (int)
|
||||
mons_vec3_div_i_inplace(&a, 3);
|
||||
ASSERT(mons_vec3_equal(quotient_i, a));
|
||||
|
||||
// Get Length
|
||||
float a_len = mons_vec3_len(a);
|
||||
ASSERT(mons_float_approx_equal(a_len, 5.38516));
|
||||
|
||||
// Dot Product
|
||||
float dot = mons_vec3_dot(a, b);
|
||||
ASSERT(mons_float_approx_equal(dot, 43.0));
|
||||
|
||||
// Cross Product
|
||||
mons_vec3 cross = mons_vec3_cross(a, b);
|
||||
ASSERT(mons_vec3_equal(cross, (mons_vec3){-8.0, 16.0, -8.0}));
|
||||
|
||||
// Extend
|
||||
mons_vec4 extended = mons_vec3_extend(a);
|
||||
ASSERT(mons_vec4_equal(extended, (mons_vec4){a.x, a.y, a.z, 0.0}));
|
||||
|
||||
// Truncate
|
||||
mons_vec2 truncated = mons_vec3_truncate(a);
|
||||
ASSERT(mons_vec2_equal(truncated, (mons_vec2){a.x, a.y}));
|
||||
|
||||
// Normalize
|
||||
mons_vec3 normalized = mons_vec3_normalize(a);
|
||||
ASSERT(mons_float_approx_equal(mons_vec3_len(normalized), 1.0));
|
||||
|
||||
// Normalize in Place
|
||||
mons_vec3_normalize_inplace(&a);
|
||||
ASSERT(mons_float_approx_equal(mons_vec3_len(a), 1.0));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
81
mons_math/tests/vec4_ops.c
Normal file
81
mons_math/tests/vec4_ops.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include "mons_math/vec3.h"
|
||||
#include "mons_math/vec4.h"
|
||||
#include "mons_math/util.h"
|
||||
#include "test.h"
|
||||
|
||||
int main(void) {
|
||||
mons_vec4 a = {2.0, 3.0, 4.0, 5.0};
|
||||
mons_vec4 b = {6.0, 5.0, 4.0, 3.0};
|
||||
|
||||
// Add
|
||||
mons_vec4 sum = mons_vec4_add(a, b);
|
||||
ASSERT(mons_vec4_equal(sum, (mons_vec4){8.0, 8.0, 8.0, 8.0}));
|
||||
|
||||
// Add in Place
|
||||
mons_vec4_add_inplace(&a, b);
|
||||
ASSERT(mons_vec4_equal(a, sum));
|
||||
|
||||
// Subtract
|
||||
mons_vec4 diff = mons_vec4_sub(sum, b);
|
||||
ASSERT(mons_vec4_equal(diff, (mons_vec4){2.0, 3.0, 4.0, 5.0}));
|
||||
|
||||
// Subtract in Place
|
||||
mons_vec4_sub_inplace(&a, b);
|
||||
ASSERT(mons_vec4_equal(diff, a));
|
||||
|
||||
// Multiply (float)
|
||||
mons_vec4 product_f = mons_vec4_mul_f(a, -3.0);
|
||||
ASSERT(mons_vec4_equal(product_f, (mons_vec4){-6.0, -9.0, -12.0, -15.0}))
|
||||
|
||||
// Multiply in Place (float)
|
||||
mons_vec4_mul_f_inplace(&a, 3.0);
|
||||
ASSERT(mons_vec4_equal(mons_vec4_negate(product_f), a))
|
||||
|
||||
// Divide (float)
|
||||
mons_vec4 quotient_f = mons_vec4_div_f(product_f, 3.0);
|
||||
mons_vec4_negate_inplace("ient_f);
|
||||
ASSERT(mons_vec4_equal(quotient_f, (mons_vec4){2.0, 3.0, 4.0, 5.0}));
|
||||
|
||||
// Divide in Place (float)
|
||||
mons_vec4_div_f_inplace(&a, 3.0);
|
||||
ASSERT(mons_vec4_equal(quotient_f, a));
|
||||
|
||||
// Multiply (int)
|
||||
mons_vec4 product_i = mons_vec4_mul_i(a, -3);
|
||||
ASSERT(mons_vec4_equal(product_i, (mons_vec4){-6.0, -9.0, -12.0, -15.0}))
|
||||
|
||||
// Multiply in Place (int)
|
||||
mons_vec4_mul_i_inplace(&a, 3);
|
||||
ASSERT(mons_vec4_equal(mons_vec4_negate(product_i), a))
|
||||
|
||||
// Divide (int)
|
||||
mons_vec4 quotient_i = mons_vec4_div_i(product_i, 3);
|
||||
mons_vec4_negate_inplace("ient_i);
|
||||
ASSERT(mons_vec4_equal(quotient_i, (mons_vec4){2.0, 3.0, 4.0, 5.0}));
|
||||
|
||||
// Divide in Place (int)
|
||||
mons_vec4_div_i_inplace(&a, 3);
|
||||
ASSERT(mons_vec4_equal(quotient_i, a));
|
||||
|
||||
// Get Length
|
||||
float a_len = mons_vec4_len(a);
|
||||
ASSERT(mons_float_approx_equal(a_len, 7.34846));
|
||||
|
||||
// Dot Product
|
||||
float dot = mons_vec4_dot(a, b);
|
||||
ASSERT(mons_float_approx_equal(dot, 58.0));
|
||||
|
||||
// Truncate
|
||||
mons_vec3 truncated = mons_vec4_truncate(a);
|
||||
ASSERT(mons_vec3_equal(truncated, (mons_vec3){a.x, a.y, a.z}));
|
||||
|
||||
// Normalize
|
||||
mons_vec4 normalized = mons_vec4_normalize(a);
|
||||
ASSERT(mons_float_approx_equal(mons_vec4_len(normalized), 1.0));
|
||||
|
||||
// Normalize in Place
|
||||
mons_vec4_normalize_inplace(&a);
|
||||
ASSERT(mons_float_approx_equal(mons_vec4_len(a), 1.0));
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user