DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
convex.h
Go to the documentation of this file.
1/*
2Copyright (c) 2022 Ashley Rose Hale (LadyHavoc)
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21*/
22
23// This module is a variant of the QuickHull algorithm intended to create hulls
24// (brushes, aka n-sided polytopes or hulls) from a series of points provided by
25// the caller
26
27#pragma once
28
29#ifndef CONVEX_H
30
36
37typedef struct convex_corner_s
38{
39 float x;
40 float y;
41 float z;
42 float w; // 1.0f
43}
45
46typedef struct convex_face_s
47{
48 // plane equation: a * x + b * y + c * z + d * w = 0.0f
49 float x;
50 float y;
51 float z;
52 float w;
53}
55
56typedef struct convex_builder_state_s
57{
58 // axially aligned bounding box
59 float extents[2][3];
60
63
66
67 // we consider points to be equivalent if they are within this distance
68 // suggested value is maxextent / 1048576.0f, which is a way of saying
69 // 'equivalent within 20 bits of precision'
70 float epsilon;
71}
73
74// set up a builer state to receive points
76
77// this is a variant of QuickHull that relies on the caller to provide points
78// in a reasonable order - the result will be the same regardless of point order
79// but it's more efficient if the furthest points are provided first
80//
81// this could be a little more efficient if we kept track of edges during the
82// build, but I think it may be more numerically stable this way
83void convex_builder_add_point(convex_builder_state_t* b, float x, float y, float z);
84
85// returns computed faces in array of vec4
86// positivew=0 is for plane equations of the form a*x+b*y+c*z+w, which is the
87// internal format
88// positivew=1 is for plane equations of the form a*x+b*y+c*z-w, which tend to
89// be less friendly in terms of vector ops
90int convex_builder_get_planes4f(convex_builder_state_t* b, float* outplanes4f, int maxplanes, int positivew);
91
92// returns the points as an array of vec3
93// internal format is vec4, so this is just repacking the data
94int convex_builder_get_points3f(convex_builder_state_t* b, float* outpoints3f, int maxpoints);
95
96#endif
void convex_builder_add_point(convex_builder_state_t *b, float x, float y, float z)
Definition convex.c:39
convex_enums
Definition convex.h:32
@ CONVEX_MAX_CORNERS
Definition convex.h:33
@ CONVEX_MAX_FACES
Definition convex.h:34
int convex_builder_get_planes4f(convex_builder_state_t *b, float *outplanes4f, int maxplanes, int positivew)
Definition convex.c:197
void convex_builder_initialize(convex_builder_state_t *b, float epsilon)
Definition convex.c:26
int convex_builder_get_points3f(convex_builder_state_t *b, float *outpoints3f, int maxpoints)
Definition convex.c:226
GLubyte GLubyte GLubyte z
Definition glquake.h:782
GLint GLenum GLint GLint y
Definition glquake.h:651
GLint GLenum GLint x
Definition glquake.h:651
dp_FragColor b
float z
Definition convex.h:51
float y
Definition convex.h:50
float x
Definition convex.h:49
float w
Definition convex.h:52