src/ascension/transform.h

Thu, 28 Mar 2024 23:30:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 28 Mar 2024 23:30:21 +0100
changeset 45
18de2af03531
parent 32
86468a71dd73
permissions
-rw-r--r--

simplify how transforms work

universe@32 1 /*
universe@32 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@32 3 * Copyright 2023 Mike Becker. All rights reserved.
universe@32 4 *
universe@32 5 * Redistribution and use in source and binary forms, with or without
universe@32 6 * modification, are permitted provided that the following conditions are met:
universe@32 7 *
universe@32 8 * 1. Redistributions of source code must retain the above copyright
universe@32 9 * notice, this list of conditions and the following disclaimer.
universe@32 10 *
universe@32 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@32 12 * notice, this list of conditions and the following disclaimer in the
universe@32 13 * documentation and/or other materials provided with the distribution.
universe@32 14 *
universe@32 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@32 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@32 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@32 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@32 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@32 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@32 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@32 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@32 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@32 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@32 25 * POSSIBILITY OF SUCH DAMAGE.
universe@32 26 */
universe@32 27
universe@32 28 #ifndef ASCENSION_TRANSFORM_H
universe@32 29 #define ASCENSION_TRANSFORM_H
universe@32 30
universe@32 31 #include "datatypes.h"
universe@32 32
universe@32 33 typedef asc_mat4f asc_transform;
universe@32 34
universe@32 35 #define ASC_TRANSFORM_SIZE (sizeof(float)*16)
universe@32 36
universe@32 37 #ifdef __GNUC__
universe@32 38 #define ASC_TRANFORM_FUNC_ATTRIBUTES \
universe@32 39 __attribute__((__nonnull__, __always_inline__))
universe@32 40 #else
universe@32 41 #define ASC_TRANFORM_FUNC_ATTRIBUTES
universe@32 42 #endif
universe@32 43 #define ASC_TRANFORM_FUNC ASC_TRANFORM_FUNC_ATTRIBUTES static inline
universe@32 44
universe@32 45
universe@32 46 ASC_TRANFORM_FUNC void asc_transform_identity(asc_transform transform) {
universe@32 47 memset(transform, 0, ASC_TRANSFORM_SIZE);
universe@32 48 transform[asc_mat4_index(0, 0)] = 1;
universe@32 49 transform[asc_mat4_index(1, 1)] = 1;
universe@32 50 transform[asc_mat4_index(2, 2)] = 1;
universe@32 51 transform[asc_mat4_index(3, 3)] = 1;
universe@32 52 }
universe@32 53
universe@32 54 ASC_TRANFORM_FUNC void asc_transform_copy(asc_transform dest, asc_transform src) {
universe@32 55 memcpy(dest, src, ASC_TRANSFORM_SIZE);
universe@32 56 }
universe@32 57
universe@32 58 ASC_TRANFORM_FUNC void asc_transform_translate(
universe@32 59 asc_transform transform,
universe@45 60 asc_vec3f vec
universe@32 61 ) {
universe@45 62 transform[asc_mat4_index(3, 0)] += vec.x;
universe@45 63 transform[asc_mat4_index(3, 1)] += vec.y;
universe@45 64 transform[asc_mat4_index(3, 2)] += vec.z;
universe@32 65 }
universe@32 66
universe@32 67 ASC_TRANFORM_FUNC void asc_transform_scale(
universe@32 68 asc_transform transform,
universe@45 69 asc_vec3f vec
universe@32 70 ) {
universe@32 71 for (unsigned i = 0 ; i < 3 ; i++) {
universe@45 72 transform[asc_mat4_index(0, i)] *= vec.width;
universe@45 73 transform[asc_mat4_index(1, i)] *= vec.height;
universe@45 74 transform[asc_mat4_index(2, i)] *= vec.depth;
universe@32 75 }
universe@32 76 }
universe@32 77
universe@45 78 ASC_TRANFORM_FUNC void asc_transform_rotate(
universe@32 79 asc_transform transform,
universe@45 80 asc_vec3f vec
universe@32 81 ) {
universe@45 82 // TODO: implement
universe@45 83 }
universe@45 84
universe@45 85 ASC_TRANFORM_FUNC void asc_transform_from_parts(
universe@45 86 asc_transform transform,
universe@45 87 asc_vec3f position,
universe@45 88 asc_vec3f scale,
universe@45 89 asc_vec3f rotation
universe@45 90 ) {
universe@45 91 asc_transform_identity(transform);
universe@45 92 asc_transform_scale(transform, scale);
universe@45 93 asc_transform_translate(transform, position);
universe@45 94 asc_transform_rotate(transform, rotation);
universe@32 95 }
universe@32 96
universe@32 97 #endif //ASCENSION_TRANSFORM_H

mercurial