src/ascension/transform.h

Tue, 05 Aug 2025 20:00:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Aug 2025 20:00:24 +0200
changeset 253
6ab35fcb8676
parent 204
be5cf64b5c29
permissions
-rw-r--r--

upgrade to SDL 3

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2023 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ASCENSION_TRANSFORM_H
#define ASCENSION_TRANSFORM_H

#include "datatypes.h"

typedef asc_mat4f asc_transform;

#define ASC_TRANSFORM_SIZE (sizeof(float)*16)

#ifdef __GNUC__
#define ASC_TRANFORM_FUNC __attribute__((__always_inline__)) static inline
#else
#define ASC_TRANFORM_FUNC static inline
#endif

ASC_TRANFORM_FUNC void asc_transform_copy(asc_transform dest, const asc_transform src) {
    memcpy(dest, src, ASC_TRANSFORM_SIZE);
}

ASC_TRANFORM_FUNC void asc_transform_apply(asc_transform dest, const asc_transform left, const asc_transform right) {
    asc_mat4f_mul(dest, left, right);
}

ASC_TRANFORM_FUNC void asc_transform_identity(asc_transform transform) {
    asc_mat4f_unit(transform);
}

/**
 * Makes the transformation matrix a translation matrix.
 *
 * @param transform the matrix to initialize
 * @param vec the translation vector
 */
ASC_TRANFORM_FUNC void asc_transform_translate3f(
        asc_transform transform,
        asc_vec3f vec
) {
    asc_mat4f_unit(transform);
    transform[asc_mat4_index(3, 0)] = vec.x;
    transform[asc_mat4_index(3, 1)] = vec.y;
    transform[asc_mat4_index(3, 2)] = vec.z;
}

/**
 * Makes the transformation matrix a scale matrix.
 *
 * @param transform the matrix to initialize
 * @param vec the scale vector
 */
ASC_TRANFORM_FUNC void asc_transform_scale3f(
        asc_transform transform,
        asc_vec3f vec
) {
    memset(transform, 0, ASC_TRANSFORM_SIZE);
    transform[asc_mat4_index(0, 0)] = vec.width;
    transform[asc_mat4_index(1, 1)] = vec.height;
    transform[asc_mat4_index(2, 2)] = vec.depth;
    transform[asc_mat4_index(3, 3)] = 1.f;
}

/**
 * Makes the transformation matrix a translation matrix.
 *
 * @param transform the matrix to initialize
 * @param vec the translation vector
 */
ASC_TRANFORM_FUNC void asc_transform_translate2f(
        asc_transform transform,
        asc_vec2f vec
) {
    asc_transform_translate3f(transform, ASC_VEC3F(vec.x, vec.y, 0.f));
}

/**
 * Makes the transformation matrix a scale matrix.
 *
 * @param transform the matrix to initialize
 * @param vec the scale vector
 */
ASC_TRANFORM_FUNC void asc_transform_scale2f(
        asc_transform transform,
        asc_vec2f vec
) {
    asc_transform_scale3f(transform, ASC_VEC3F(vec.width, vec.height, 1.f));
}

/**
 * Makes the transformation matrix a rotation matrix around the y-axis.
 *
 * @param transform the matrix to initialize
 * @param angle the angle in radians
 */
ASC_TRANFORM_FUNC void asc_transform_yaw(
        asc_transform transform,
        float angle
) {
    memset(transform, 0, ASC_TRANSFORM_SIZE);
    const float s = asc_sin(angle);
    const float c = asc_cos(angle);
    transform[asc_mat4_index(0, 0)] = c;
    transform[asc_mat4_index(0, 2)] = -s;
    transform[asc_mat4_index(1, 1)] = 1;
    transform[asc_mat4_index(2, 0)] = s;
    transform[asc_mat4_index(2, 2)] = c;
    transform[asc_mat4_index(3, 3)] = 1;
}

/**
 * Makes the transformation matrix a rotation matrix around the x-axis.
 *
 * @param transform the matrix to initialize
 * @param angle the angle in radians
 */
ASC_TRANFORM_FUNC void asc_transform_pitch(
    asc_transform transform,
    float angle
) {
    memset(transform, 0, ASC_TRANSFORM_SIZE);
    const float s = asc_sin(angle);
    const float c = asc_cos(angle);
    transform[asc_mat4_index(0, 0)] = 1;
    transform[asc_mat4_index(1, 1)] = c;
    transform[asc_mat4_index(1, 2)] = s;
    transform[asc_mat4_index(2, 1)] = -s;
    transform[asc_mat4_index(2, 2)] = c;
    transform[asc_mat4_index(3, 3)] = 1;
}

/**
 * Makes the transformation matrix a rotation matrix around the z-axis.
 *
 * @param transform the matrix to initialize
 * @param angle the angle in radians
 */
ASC_TRANFORM_FUNC void asc_transform_roll(
    asc_transform transform,
    float angle
) {
    memset(transform, 0, ASC_TRANSFORM_SIZE);
    const float s = asc_sin(angle);
    const float c = asc_cos(angle);
    transform[asc_mat4_index(0, 0)] = c;
    transform[asc_mat4_index(0, 1)] = s;
    transform[asc_mat4_index(1, 0)] = -s;
    transform[asc_mat4_index(1, 1)] = c;
    transform[asc_mat4_index(2, 2)] = 1;
    transform[asc_mat4_index(3, 3)] = 1;
}


#endif //ASCENSION_TRANSFORM_H

mercurial