Fixed errors
This commit is contained in:
@@ -10,6 +10,7 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(camera)
|
||||
add_subdirectory(framebuffer)
|
||||
add_subdirectory(texture)
|
||||
add_subdirectory(mesh)
|
||||
10
src/duskgl/display/camera/CMakeLists.txt
Normal file
10
src/duskgl/display/camera/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
cameragl.c
|
||||
)
|
||||
132
src/duskgl/display/camera/cameragl.c
Normal file
132
src/duskgl/display/camera/cameragl.c
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "display/camera/camera.h"
|
||||
#include "display/framebuffer/framebuffer.h"
|
||||
#include "display/screen/screen.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void cameraPushMatrixGL(camera_t *camera) {
|
||||
assertNotNull(camera, "Not a camera component");
|
||||
|
||||
mat4 projection;
|
||||
mat4 view;
|
||||
|
||||
switch(camera->projType) {
|
||||
case CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC: {
|
||||
assertTrue(
|
||||
camera->orthographic.right != camera->orthographic.left &&
|
||||
camera->orthographic.top != camera->orthographic.bottom,
|
||||
"Invalid orthographic projection parameters"
|
||||
);
|
||||
glm_ortho(
|
||||
camera->orthographic.left,
|
||||
camera->orthographic.right,
|
||||
camera->orthographic.bottom,
|
||||
camera->orthographic.top,
|
||||
camera->nearClip,
|
||||
camera->farClip,
|
||||
projection
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
case CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED:
|
||||
case CAMERA_PROJECTION_TYPE_PERSPECTIVE: {
|
||||
const float_t aspect = (
|
||||
(float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) /
|
||||
(float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
|
||||
);
|
||||
glm_perspective(
|
||||
camera->perspective.fov,
|
||||
aspect,
|
||||
camera->nearClip,
|
||||
camera->farClip,
|
||||
projection
|
||||
);
|
||||
|
||||
if(camera->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED) {
|
||||
// Flip Y axis
|
||||
projection[1][1] *= -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch(camera->viewType) {
|
||||
case CAMERA_VIEW_TYPE_MATRIX:
|
||||
glm_mat4_copy(camera->view, view);
|
||||
break;
|
||||
|
||||
case CAMERA_VIEW_TYPE_LOOKAT:
|
||||
glm_lookat(
|
||||
camera->lookat.position,
|
||||
camera->lookat.target,
|
||||
camera->lookat.up,
|
||||
view
|
||||
);
|
||||
break;
|
||||
|
||||
case CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT:
|
||||
assertTrue(
|
||||
camera->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE ||
|
||||
camera->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED,
|
||||
"Pixel perfect camera view requires perspective projection"
|
||||
);
|
||||
|
||||
// const float_t viewportHeight = (
|
||||
// (float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND)
|
||||
// );
|
||||
const float_t viewportHeight = (float_t)SCREEN.height;
|
||||
const float_t z = (viewportHeight / 2.0f) / (
|
||||
camera->lookatPixelPerfect.pixelsPerUnit *
|
||||
tanf(camera->perspective.fov / 2.0f)
|
||||
);
|
||||
|
||||
vec3 position;
|
||||
glm_vec3_copy(camera->lookatPixelPerfect.target, position);
|
||||
glm_vec3_add(position, camera->lookatPixelPerfect.offset, position);
|
||||
position[2] += z;
|
||||
glm_lookat(
|
||||
position,
|
||||
camera->lookatPixelPerfect.target,
|
||||
camera->lookatPixelPerfect.up,
|
||||
view
|
||||
);
|
||||
break;
|
||||
|
||||
case CAMERA_VIEW_TYPE_2D:
|
||||
glm_mat4_identity(view);
|
||||
glm_translate(view, (vec3){
|
||||
-camera->_2d.position[0],
|
||||
-camera->_2d.position[1],
|
||||
0.0f
|
||||
});
|
||||
glm_scale(view, (vec3){
|
||||
camera->_2d.zoom,
|
||||
camera->_2d.zoom,
|
||||
1.0f
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
assertUnreachable("Invalid camera view type");
|
||||
}
|
||||
|
||||
glPushMatrix();
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glLoadMatrixf((const GLfloat*)projection);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
glLoadMatrixf((const GLfloat*)view);
|
||||
}
|
||||
|
||||
void cameraPopMatrixGL(void) {
|
||||
glPopMatrix();
|
||||
}
|
||||
22
src/duskgl/display/camera/cameragl.h
Normal file
22
src/duskgl/display/camera/cameragl.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef struct camera_s camera_t;
|
||||
|
||||
/**
|
||||
* Initializes a camera with default perspective parameters.
|
||||
*
|
||||
* @param camera The camera to initialize.
|
||||
*/
|
||||
void cameraPushMatrixGL(camera_t *camera);
|
||||
|
||||
/**
|
||||
* Removes the previously pushed camera matrix off the matrix stack.
|
||||
*/
|
||||
void cameraPopMatrixGL(void);
|
||||
12
src/duskgl/display/camera/cameraplatform.h
Normal file
12
src/duskgl/display/camera/cameraplatform.h
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "cameragl.h"
|
||||
|
||||
#define cameraPushMatrixPlatform cameraPushMatrixGL
|
||||
#define cameraPopMatrixPlatform cameraPopMatrixGL
|
||||
@@ -75,7 +75,7 @@ errorret_t frameBufferGLBind(framebuffer_t *framebuffer) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void frameBufferClear(const uint8_t flags, const color_t color) {
|
||||
void frameBufferGLClear(const uint8_t flags, const color_t color) {
|
||||
GLbitfield glFlags = 0;
|
||||
|
||||
if(flags & FRAMEBUFFER_CLEAR_COLOR) {
|
||||
|
||||
@@ -45,6 +45,14 @@ uint32_t frameBufferGLGetHeight(const framebuffergl_t *framebuffer);
|
||||
*/
|
||||
errorret_t frameBufferGLBind(framebuffergl_t *framebuffer);
|
||||
|
||||
/**
|
||||
* Clears the framebuffer with the specified flags and color.
|
||||
*
|
||||
* @param flags The clear flags.
|
||||
* @param color The clear color.
|
||||
*/
|
||||
void frameBufferGLClear(const uint8_t flags, const color_t color);
|
||||
|
||||
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
|
||||
/**
|
||||
* Initializes an OpenGL style framebuffer.
|
||||
|
||||
@@ -13,6 +13,7 @@ typedef framebuffergl_t framebufferplatform_t;
|
||||
#define frameBufferPlatformGetWidth frameBufferGLGetWidth
|
||||
#define frameBufferPlatformGetHeight frameBufferGLGetHeight
|
||||
#define frameBufferPlatformBind frameBufferGLBind
|
||||
#define frameBufferPlatformClear frameBufferGLClear
|
||||
|
||||
#if DUSK_DISPLAY_SIZE_DYNAMIC
|
||||
#define frameBufferPlatformInit frameBufferGLInit
|
||||
|
||||
Reference in New Issue
Block a user