Add LH & RH_NO perspective functions

This commit adds functions `glm_perspective_lh_no` and
`glm_perspective_rh_no` to the code. Unit tests are added and this
commit follows the new pattern of adding the a new file per
coordinate-system and clip-space tuple.

. Makefile.am updated
. removed test/glm_cmp project stub
. unit tests include naive implementations to as well as magic number
  ref-data generated by the corresponding GLM functions.

No tests run yet on Windows or Mac.
This commit is contained in:
michaelg
2021-04-30 21:53:17 +01:00
committed by Tai Chi Minh Ralph Eastwood
parent b3a18b8a15
commit c013bd462c
21 changed files with 266 additions and 89 deletions

View File

@@ -59,6 +59,8 @@ add_library(${PROJECT_NAME}
src/cam.c
src/cam_lh_zo.c
src/cam_rh_zo.c
src/cam_lh_no.c
src/cam_rh_no.c
src/vec2.c
src/vec3.c
src/vec4.c

View File

@@ -43,6 +43,8 @@ cglm_HEADERS = include/cglm/version.h \
include/cglm/cam.h \
include/cglm/cam_lh_zo.h \
include/cglm/cam_rh_zo.h \
include/cglm/cam_lh_no.h \
include/cglm/cam_rh_no.h \
include/cglm/io.h \
include/cglm/mat4.h \
include/cglm/mat3.h \
@@ -148,6 +150,8 @@ libcglm_la_SOURCES=\
src/cam.c \
src/cam_lh_zo.c \
src/cam_rh_zo.c \
src/cam_lh_no.c \
src/cam_rh_no.c \
src/vec2.c \
src/vec3.c \
src/vec4.c \
@@ -172,6 +176,8 @@ test_tests_SOURCES=\
test/src/test_cam.c \
test/src/test_cam_lh_zo.c \
test/src/test_cam_rh_zo.c \
test/src/test_cam_lh_no.c \
test/src/test_cam_rh_no.c \
test/src/test_clamp.c \
test/src/test_euler.c \
test/src/test_bezier.c \

55
include/cglm/cam_lh_no.h Normal file
View File

@@ -0,0 +1,55 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
/*
Functions:
CGLM_INLINE void glm_perspective_lh_no(float fovy,
float aspect,
float nearVal,
float farVal,
mat4 dest)
*/
#ifndef cglm_cam_lh_no_h
#define cglm_cam_lh_no_h
#include "common.h"
#include "plane.h"
/*!
* @brief set up perspective projection matrix with a left-hand coordinate
* system and a clip-space of [-1, 1]
*
* @param[in] fovy field of view angle
* @param[in] aspect aspect ratio ( width / height )
* @param[in] nearVal near clipping plane
* @param[in] farVal far clipping planes
* @param[out] dest result matrix
*/
CGLM_INLINE
void
glm_perspective_lh_no(float fovy,
float aspect,
float nearVal,
float farVal,
mat4 dest) {
/* Impl follows glm::perspectiveLH_NO in glm/ext/matrix_clip_space.inl */
float fl, fn;
glm_mat4_zero(dest);
fl = 1.0f / tanf(fovy * 0.5f);
fn = 1.0f / (farVal - nearVal);
dest[0][0] = fl / aspect;
dest[1][1] = fl;
dest[2][2] = (farVal + nearVal) * fn;
dest[2][3] = 1.0f;
dest[3][2] = -2.0f * farVal * nearVal * fn;
}
#endif /*cglm_cam_lh_no_h*/

View File

@@ -20,7 +20,6 @@
#include "common.h"
#include "plane.h"
/*!
* @brief set up perspective projection matrix with a left-hand coordinate
* system (suitable, apparently, for DirectX and Metal) and a clip-space with
@@ -51,7 +50,7 @@ glm_perspective_lh_zo(float fovy,
dest[1][1] = fl;
dest[2][2] = farVal * fn;
dest[2][3] = 1.0f;
dest[3][2] = -(farVal * nearVal * fn);
dest[3][2] = -farVal * nearVal * fn;
}
#endif /*cglm_cam_lh_zo_h*/

55
include/cglm/cam_rh_no.h Normal file
View File

@@ -0,0 +1,55 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
/*
Functions:
CGLM_INLINE void glm_perspective_rh_no(float fovy,
float aspect,
float nearVal,
float farVal,
mat4 dest)
*/
#ifndef cglm_cam_rh_no_h
#define cglm_cam_rh_no_h
#include "common.h"
#include "plane.h"
/*!
* @brief set up perspective projection matrix with a right-hand coordinate
* system and a clip space of [-1, 1].
*
* @param[in] fovy field of view angle
* @param[in] aspect aspect ratio ( width / height )
* @param[in] nearVal near clipping plane
* @param[in] farVal far clipping planes
* @param[out] dest result matrix
*/
CGLM_INLINE
void
glm_perspective_rh_no(float fovy,
float aspect,
float nearVal,
float farVal,
mat4 dest) {
/* Impl follows glm::perspectiveRH_NO in glm/ext/matrix_clip_space.inl */
float fl, fn;
glm_mat4_zero(dest);
fl = 1.0f / tanf(fovy * 0.5f);
fn = 1.0f / (farVal - nearVal);
dest[0][0] = fl / aspect;
dest[1][1] = fl;
dest[2][2] = -(farVal + nearVal) * fn;
dest[2][3] = -1.0f;
dest[3][2] = -2.0f * farVal * nearVal * fn;
}
#endif /*cglm_cam_rh_no_h*/

View File

@@ -25,8 +25,6 @@
* system (suitable for Vulkan) and a clip-space with depth values from zero
* to one.
*
* https://github.com/godlikepanos/anki-3d-engine/blob/317cb379ff3a7b09f9034f49c7bdab0f96a1c0b3/AnKi/Math/Mat.h#L1254
*
* @param[in] fovy field of view angle
* @param[in] aspect aspect ratio ( width / height )
* @param[in] nearVal near clipping plane
@@ -55,4 +53,4 @@ glm_perspective_rh_zo(float fovy,
dest[3][2] = -farVal * nearVal * fn;
}
#endif /*cglm_cam_lh_zo_h*/
#endif /*cglm_cam_rh_zo_h*/

View File

@@ -19,6 +19,8 @@
#include "cam.h"
#include "cam_lh_zo.h"
#include "cam_rh_zo.h"
#include "cam_lh_no.h"
#include "cam_rh_no.h"
#include "frustum.h"
#include "quat.h"
#include "euler.h"

23
src/cam_lh_no.c Normal file
View File

@@ -0,0 +1,23 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#include "../include/cglm/cglm.h"
CGLM_EXPORT
void
glmc_perspective_lh_no(float fovy,
float aspect,
float nearVal,
float farVal,
mat4 dest) {
glm_perspective_lh_no(fovy,
aspect,
nearVal,
farVal,
dest);
}

View File

@@ -6,7 +6,6 @@
*/
#include "../include/cglm/cglm.h"
#include "../include/cglm/cam_lh_zo.h"
CGLM_EXPORT
void

23
src/cam_rh_no.c Normal file
View File

@@ -0,0 +1,23 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#include "../include/cglm/cglm.h"
CGLM_EXPORT
void
glmc_perspective_rh_no(float fovy,
float aspect,
float nearVal,
float farVal,
mat4 dest) {
glm_perspective_rh_no(fovy,
aspect,
nearVal,
farVal,
dest);
}

View File

@@ -6,7 +6,6 @@
*/
#include "../include/cglm/cglm.h"
#include "../include/cglm/cam_rh_zo.h"
CGLM_EXPORT
void

View File

@@ -8,6 +8,8 @@ set(TESTFILES
src/test_cam.c
src/test_cam_lh_zo.c
src/test_cam_rh_zo.c
src/test_cam_lh_no.c
src/test_cam_rh_no.c
src/test_struct.c
src/test_clamp.c
src/test_common.c

View File

@@ -1 +0,0 @@
glm

View File

@@ -1,12 +0,0 @@
cmake_minimum_required(VERSION 3.17)
project(glm_cmp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMP_MAIN glmcmp)
add_subdirectory(glm)
add_executable(${CMP_MAIN} src/main.cpp)
target_link_libraries(${CMP_MAIN} PRIVATE glm)

View File

@@ -1,8 +0,0 @@
### Simple GLM app for quick generation of reference values
#### Usage
1. Clone, link or copy the GLM library's root directory to `test/glm_cmp/glm`.
1. Ensuring your current directory is `test/glm_cmp`:
`mkdir build && cd build && cmake .. && make && ./glmcmp`
Please do not delete prior reference-data-producing functions as it may be necessary to return to these to examine future bugs or assumptions. By all means remove or comment-out the call site from the `main` function, though, to focus on what you're working on.

View File

@@ -1,39 +0,0 @@
#include <stdio.h>
#include "glm/glm.hpp"
#include "glm/mat4x4.hpp"
#include <glm/ext/matrix_clip_space.hpp>
static void outputForPerspectiveLH_ZO() {
const float fovy = glm::radians(45.0f);
const float aspect = 640/480.0f;
const float near = 0.1f;
const float far = 100.0f;
glm::mat4 cmp = glm::perspectiveLH_ZO(fovy, aspect, near, far);
puts("/*reference test data for glm_perspective_lh_zo*/");
puts("mat4 cmp = {0};");
printf("cmp[0][0] = %0.7ff;\n", cmp[0][0]);
printf("cmp[1][1] = %0.7ff;\n", cmp[1][1]);
printf("cmp[2][2] = %0.7ff;\n", cmp[2][2]);
printf("cmp[2][3] = %0.7ff;\n", cmp[2][3]);
printf("cmp[3][2] = %0.7ff;\n", cmp[3][2]);
}
static void outputForPerspectiveRH_ZO() {
const float fovy = glm::radians(45.0f);
const float aspect = 640/480.0f;
const float near = 0.1f;
const float far = 100.0f;
glm::mat4 cmp = glm::perspectiveRH_ZO(fovy, aspect, near, far);
puts("/*reference test data for glm_perspective_rh_zo*/");
puts("mat4 cmp = {0};");
printf("cmp[0][0] = %0.7ff;\n", cmp[0][0]);
printf("cmp[1][1] = %0.7ff;\n", cmp[1][1]);
printf("cmp[2][2] = %0.7ff;\n", cmp[2][2]);
printf("cmp[2][3] = %0.7ff;\n", cmp[2][3]);
printf("cmp[3][2] = %0.7ff;\n", cmp[3][2]);
}
int main(int argc, char** argv) {
outputForPerspectiveRH_ZO();
return 0;
}

36
test/src/test_cam_lh_no.c Normal file
View File

@@ -0,0 +1,36 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#include "test_common.h"
TEST_IMPL(perspective_lh_no) {
mat4 dst;
const float fovy = glm_rad(45.0f);
const float aspect = 640/480.0f;
const float zNearVal = 0.1f;
const float zFarVal = 100.0f;
glm_perspective_lh_no(fovy, aspect, zNearVal, zFarVal, dst);
/* Sanity mk. I: longhand version */
ASSERT(test_eq(dst[0][0], 1.0f / (tanf(fovy / 2) * aspect)))
ASSERT(test_eq(dst[1][1], 1.0f / tanf(fovy / 2)))
ASSERT(test_eq(dst[2][2], (zFarVal + zNearVal) / (zFarVal - zNearVal)))
ASSERT(test_eq(dst[2][3], 1.0f))
ASSERT(test_eq(dst[3][2], -2 * zFarVal * zNearVal / (zFarVal - zNearVal)))
/* Sanity mk. II */
/*reference test data for glm_perspective_lh_no*/
mat4 cmp = {0};
cmp[0][0] = 1.8106601f;
cmp[1][1] = 2.4142134f;
cmp[2][2] = 1.0020020f;
cmp[2][3] = 1.0000000f;
cmp[3][2] = -0.2002002f;
return test_assert_mat4_eq(dst, cmp);
}

View File

@@ -16,10 +16,9 @@ TEST_IMPL(perspective_lh_zo) {
glm_perspective_lh_zo(fovy, aspect, zNearVal, zFarVal, dst);
/* Sanity mk. I */
/* Longhand version of what the above function _should_ be doing */
ASSERT(test_eq(dst[0][0], 1 / (tanf(fovy / 2) * aspect)))
ASSERT(test_eq(dst[1][1], 1 / tanf(fovy / 2)))
/* Sanity mk. I: longhand version */
ASSERT(test_eq(dst[0][0], 1.0f / (tanf(fovy / 2) * aspect)))
ASSERT(test_eq(dst[1][1], 1.0f / tanf(fovy / 2)))
ASSERT(test_eq(dst[2][2], zFarVal / (zFarVal - zNearVal)))
ASSERT(test_eq(dst[2][3], 1.0f))
ASSERT(test_eq(dst[3][2], -1 * zFarVal * zNearVal / (zFarVal - zNearVal)))
@@ -33,5 +32,5 @@ TEST_IMPL(perspective_lh_zo) {
cmp[2][3] = 1.0000000f;
cmp[3][2] = -0.1001001f;
return (test_assert_mat4_eq(dst, cmp));
return test_assert_mat4_eq(dst, cmp);
}

36
test/src/test_cam_rh_no.c Normal file
View File

@@ -0,0 +1,36 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#include "test_common.h"
TEST_IMPL(perspective_rh_no) {
mat4 dst;
const float fovy = glm_rad(45.0f);
const float aspect = 640/480.0f;
const float zNearVal = 0.1f;
const float zFarVal = 100.0f;
glm_perspective_rh_no(fovy, aspect, zNearVal, zFarVal, dst);
/* Sanity mk. I: longhand version */
ASSERT(test_eq(dst[0][0], 1.0f / (tanf(fovy / 2) * aspect)))
ASSERT(test_eq(dst[1][1], 1.0f / tanf(fovy / 2)))
ASSERT(test_eq(dst[2][2], -1.0f * (zFarVal + zNearVal) / (zFarVal - zNearVal)))
ASSERT(test_eq(dst[2][3], -1.0f))
ASSERT(test_eq(dst[3][2], -2 * zFarVal * zNearVal / (zFarVal - zNearVal)))
/* Sanity mk. II */
/*reference test data for glm_perspective_rh_no*/
mat4 cmp = {0};
cmp[0][0] = 1.8106601f;
cmp[1][1] = 2.4142134f;
cmp[2][2] = -1.0020020f;
cmp[2][3] = -1.0000000f;
cmp[3][2] = -0.2002002f;
return test_assert_mat4_eq(dst, cmp);
}

View File

@@ -17,9 +17,8 @@ TEST_IMPL(perspective_rh_zo) {
glm_perspective_rh_zo(fovy, aspect, zNearVal, zFarVal, dst);
/* Sanity mk. I: longhand version */
float focal_len = 1 / tanf(fovy / 2);
ASSERT(test_eq(dst[0][0], focal_len / aspect))
ASSERT(test_eq(dst[1][1], focal_len))
ASSERT(test_eq(dst[0][0], 1 / (tanf(fovy / 2) * aspect)))
ASSERT(test_eq(dst[1][1], 1 / tanf(fovy / 2)))
ASSERT(test_eq(dst[2][2], zFarVal / (zNearVal - zFarVal)))
ASSERT(test_eq(dst[2][3], -1.0f))
ASSERT(test_eq(dst[3][2], -1 * zFarVal * zNearVal / (zFarVal - zNearVal)))

View File

@@ -225,6 +225,8 @@ TEST_DECLARE(glmc_mat2_rmc)
/* camera (incl [LR]H cross [NZ]O) */
TEST_DECLARE(perspective_lh_zo)
TEST_DECLARE(perspective_rh_zo)
TEST_DECLARE(perspective_lh_no)
TEST_DECLARE(perspective_rh_no)
TEST_DECLARE(camera_lookat)
TEST_DECLARE(camera_decomp)
@@ -951,6 +953,8 @@ TEST_LIST {
/* camera (incl [LR]H cross [NZ]O) */
TEST_ENTRY(perspective_lh_zo)
TEST_ENTRY(perspective_rh_zo)
TEST_ENTRY(perspective_lh_no)
TEST_ENTRY(perspective_rh_no)
TEST_ENTRY(camera_lookat)
TEST_ENTRY(camera_decomp)