src/ascension/camera.h

Wed, 14 May 2025 20:14:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 14 May 2025 20:14:18 +0200
changeset 121
ede9a9e92ff9
parent 105
2b7f92ff2c15
permissions
-rw-r--r--

add viewport_clear flag to camera settings

/*
 * 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_CAMERA_H
#define ASCENSION_CAMERA_H

#include "datatypes.h"

typedef struct AscCamera AscCamera;

typedef asc_recti(*asc_camera_viewport_update_func)(asc_vec2u window_size);
typedef void(*asc_camera_projection_update_func)(AscCamera*, asc_vec2u window_size);

struct AscCamera {
    asc_mat4f projection;
    asc_mat4f view;
    asc_recti viewport;
    /**
     * Function that gets invoked whenever the window sized changed.
     * Calculates the new drawing viewport.
     * If @c NULL, the entire window will be used.
     */
    asc_camera_viewport_update_func viewport_update_func;
    asc_camera_projection_update_func projection_update_func;
    asc_col4f clear_color;
    bool viewport_clear;
};

enum AscCameraType {
    ASC_CAMERA_CUSTOM,
    ASC_CAMERA_ORTHO,
    ASC_CAMERA_PERSPECTIVE,
};

struct asc_camera_init_args {
    enum AscCameraType type;
    union {
        struct {
            asc_recti rect;
        } ortho;
        /*struct {
            TODO: implement
        } perspective;*/
    };
    asc_camera_viewport_update_func viewport_update_func;
    asc_camera_projection_update_func projection_update_func;
    /**
     * Indicates whether the viewport for this camera shall be cleared before rendering.
     * The active drawing color will be used as the clear color (can be changed in the camera struct, afterward).
     */
    bool viewport_clear;
};

__attribute__((__nonnull__))
void asc_camera_init_(AscCamera *camera, struct asc_camera_init_args args);

#define asc_camera_init(camera,...) asc_camera_init_(camera, (struct asc_camera_init_args){__VA_ARGS__})

__attribute__((__nonnull__))
void asc_camera_ortho(AscCamera *camera, asc_recti rect);

/**
 * Shorter version of updating an orthographic camera which assumes the top right corner at (0,0).
 *
 * @attention The camera MUST have been initialized with asc_camera_ortho() at position (0,0).
 *
 * @note This function can be used as an asc_camera_projection_update_func to keep the orthographic projection
 * aligned with the window size (useful for UIs or backdrops).
 *
 * @param camera the camera
 * @param size the new size
 */
__attribute__((__nonnull__))
void asc_camera_ortho_update_size(AscCamera *camera, asc_vec2u size);

#endif //ASCENSION_CAMERA_H

mercurial