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

@@ -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,22 +16,21 @@ 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)))
ASSERT(test_eq(dst[2][2], zFarVal / (zFarVal - zNearVal)))
ASSERT(test_eq(dst[2][3], 1.0f))
/* 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)))
/* Sanity mk. II */
/* "Reference values" generated by GLM's glm::perspectiveLH_ZO */
mat4 cmp = {0};
cmp[0][0] = 1.8106601f;
cmp[1][1] = 2.4142134f;
cmp[2][2] = 1.0010010f;
cmp[2][3] = 1.0000000f;
cmp[0][0] = 1.8106601f;
cmp[1][1] = 2.4142134f;
cmp[2][2] = 1.0010010f;
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)))
@@ -27,8 +26,8 @@ TEST_IMPL(perspective_rh_zo) {
/* Sanity mk. II */
/*reference test data for glm_perspective_rh_zo*/
mat4 cmp = {0};
cmp[0][0] = 1.8106601f;
cmp[1][1] = 2.4142134f;
cmp[0][0] = 1.8106601f;
cmp[1][1] = 2.4142134f;
cmp[2][2] = -1.0010010f;
cmp[2][3] = -1.0000000f;
cmp[3][2] = -0.1001001f;

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)