summaryrefslogtreecommitdiff
path: root/konrender.h
blob: 1e81e3f389828592d7c85a4f417084ed05735ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* SPDX-License-Identifier: MIT */
/*
 * konrender.h
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */


#ifndef KONRENDER_H
#define KONRENDER_H

#include "konmath.h"
#include "konsofren.h"

typedef enum kon_projection {
	KON_PROJECTION_PERSPECTIVE = 0,
	KON_PROJECTION_ORTHOGRAPHIC
} kon_projection_t;

typedef struct kon_camera {
	kon_vec3_t position;
	kon_vec3_t target;
	kon_vec3_t up;
	kon_projection_t projection;
	float fov;       /* vertical field of view in radians, perspective only */
	float orthoSize; /* half the visible height in world units, orthographic only */
	float nearZ, farZ;
} kon_camera_t;

/* edges holds pairs of indices into vertices */
typedef struct kon_wireMesh {
	const kon_vec3_t *vertices;
	const uint16_t *edges;
	int edgeCount;
} kon_wireMesh_t;

/*** camera declarations ***/

kon_camera_t kon_cameraDefault(void);
kon_mat4_t kon_cameraView(const kon_camera_t *camera);
kon_mat4_t kon_cameraProjection(const kon_camera_t *camera, int width, int height);
kon_mat4_t kon_cameraViewProjection(const kon_camera_t *camera, int width, int height);

/*** draw declarations ***/

/* returns 0 if the point is behind the camera */
int kon_worldToScreen(const kon_framebuffer_t *fb, kon_mat4_t viewProjection, kon_vec3_t point, kon_vec2_t *out);

void kon_drawLine3D(kon_framebuffer_t *fb, kon_mat4_t viewProjection, kon_vec3_t a, kon_vec3_t b, uint32_t color);
void kon_drawWireMesh(kon_framebuffer_t *fb, kon_mat4_t viewProjection, kon_mat4_t model, const kon_wireMesh_t *mesh, uint32_t color);

/*** implementation ***/

#ifdef KONRENDER_IMPLEMENTATION

/*** private helper ***/

static kon_vec2_t kon_clipToScreen_(const kon_framebuffer_t *fb, kon_vec4_t clip) {
	/* clamped so the later float to int conversion can't overflow */
	float ndcX = kon_clamp(clip.x / clip.w, -1.0e5f, 1.0e5f);
	float ndcY = kon_clamp(clip.y / clip.w, -1.0e5f, 1.0e5f);

	/* screen +Y points down, NDC +Y points up */
	return KON_VEC2((ndcX + 1.0f) * 0.5f * (float)fb->width, (1.0f - ndcY) * 0.5f * (float)fb->height);
}

/*** camera implementation ***/

kon_camera_t kon_cameraDefault(void) {
	kon_camera_t camera;
	camera.position = KON_VEC3(0, 0, 5);
	camera.target = KON_VEC3_ZERO;
	camera.up = KON_VEC3_UP;
	camera.projection = KON_PROJECTION_PERSPECTIVE;
	camera.fov = KON_DEG2RAD(60);
	camera.orthoSize = 5.0f;
	camera.nearZ = 0.1f;
	camera.farZ = 100.0f;
	return camera;
}

kon_mat4_t kon_cameraView(const kon_camera_t *camera) {
	return kon_mat4LookAt(camera->position, camera->target, camera->up);
}

kon_mat4_t kon_cameraProjection(const kon_camera_t *camera, int width, int height) {
	float aspect = (height > 0) ? (float)width / (float)height : 1.0f;

	if (camera->projection == KON_PROJECTION_ORTHOGRAPHIC) {
		float halfH = camera->orthoSize;
		float halfW = halfH * aspect;
		return kon_mat4Ortho(-halfW, halfW, -halfH, halfH, camera->nearZ, camera->farZ);
	}

	return kon_mat4Perspective(camera->fov, aspect, camera->nearZ, camera->farZ);
}

kon_mat4_t kon_cameraViewProjection(const kon_camera_t *camera, int width, int height) {
	return kon_mat4Mul(kon_cameraProjection(camera, width, height), kon_cameraView(camera));
}

/*** draw implementation ***/

int kon_worldToScreen(const kon_framebuffer_t *fb, kon_mat4_t viewProjection, kon_vec3_t point, kon_vec2_t *out) {
	if (!fb || !out) return 0;

	kon_vec4_t clip = kon_mat4MulVec4(viewProjection, KON_VEC4(point.x, point.y, point.z, 1));

	/* in front of the near plane when z >= -w */
	if (clip.z < -clip.w || clip.w <= 0.0f) return 0;

	*out = kon_clipToScreen_(fb, clip);
	return 1;
}

void kon_drawLine3D(kon_framebuffer_t *fb, kon_mat4_t viewProjection, kon_vec3_t a, kon_vec3_t b, uint32_t color) {
	if (!fb) return;

	kon_vec4_t ca = kon_mat4MulVec4(viewProjection, KON_VEC4(a.x, a.y, a.z, 1));
	kon_vec4_t cb = kon_mat4MulVec4(viewProjection, KON_VEC4(b.x, b.y, b.z, 1));

	/* clip against the near plane (z + w >= 0) before dividing, so points behind the camera never get projected */
	float da = ca.z + ca.w;
	float db = cb.z + cb.w;

	if (da < 0.0f && db < 0.0f) return;

	if (da < 0.0f || db < 0.0f) {
		float t = da / (da - db);
		kon_vec4_t hit = KON_VEC4(
			ca.x + (cb.x - ca.x) * t,
			ca.y + (cb.y - ca.y) * t,
			ca.z + (cb.z - ca.z) * t,
			ca.w + (cb.w - ca.w) * t
		);

		if (da < 0.0f) ca = hit;
		else cb = hit;
	}

	if (ca.w <= 0.0f || cb.w <= 0.0f) return;

	kon_vec2_t sa = kon_clipToScreen_(fb, ca);
	kon_vec2_t sb = kon_clipToScreen_(fb, cb);

	kon_drawLine(fb, (int)sa.x, (int)sa.y, (int)sb.x, (int)sb.y, color);
}

void kon_drawWireMesh(kon_framebuffer_t *fb, kon_mat4_t viewProjection, kon_mat4_t model, const kon_wireMesh_t *mesh, uint32_t color) {
	if (!fb || !mesh) return;

	kon_mat4_t mvp = kon_mat4Mul(viewProjection, model);

	for (int i = 0; i < mesh->edgeCount; i++) {
		kon_vec3_t a = mesh->vertices[mesh->edges[i * 2 + 0]];
		kon_vec3_t b = mesh->vertices[mesh->edges[i * 2 + 1]];
		kon_drawLine3D(fb, mvp, a, b, color);
	}
}

#endif /* end of KONRENDER_IMPLEMENTATION */

#endif