src/ascension/transform.h

Thu, 29 May 2025 11:20:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 29 May 2025 11:20:49 +0200
changeset 127
46aaf0bfb81e
parent 125
0a8747b02df8
child 130
c3fce2a543ee
permissions
-rw-r--r--

remove unused context variable

/*
 * 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_identity(asc_transform transform) {
    memset(transform, 0, ASC_TRANSFORM_SIZE);
    transform[asc_mat4_index(0, 0)] = 1;
    transform[asc_mat4_index(1, 1)] = 1;
    transform[asc_mat4_index(2, 2)] = 1;
    transform[asc_mat4_index(3, 3)] = 1;
}

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

ASC_TRANFORM_FUNC void asc_transform_translate(
        asc_transform transform,
        asc_vec3f vec
) {
    transform[asc_mat4_index(3, 0)] += vec.x;
    transform[asc_mat4_index(3, 1)] += vec.y;
    transform[asc_mat4_index(3, 2)] += vec.z;
}

ASC_TRANFORM_FUNC void asc_transform_scale(
        asc_transform transform,
        asc_vec3f vec
) {
    for (unsigned i = 0 ; i < 3 ; i++) {
        transform[asc_mat4_index(0, i)] *= vec.width;
        transform[asc_mat4_index(1, i)] *= vec.height;
        transform[asc_mat4_index(2, i)] *= vec.depth;
    }
}

ASC_TRANFORM_FUNC void asc_transform_rotate(
        __attribute__((__unused__)) asc_transform transform,
        __attribute__((__unused__)) asc_vec3f vec
) {
    // TODO: implement
}

ASC_TRANFORM_FUNC void asc_transform_from_parts(
        asc_transform transform,
        asc_vec3f position,
        asc_vec3f scale,
        asc_vec3f rotation
) {
    asc_transform_identity(transform);
    asc_transform_scale(transform, scale);
    asc_transform_translate(transform, position);
    asc_transform_rotate(transform, rotation);
}

#endif //ASCENSION_TRANSFORM_H

mercurial