1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __SURFACE_PATCH_H__
30 #define __SURFACE_PATCH_H__
31
32 #include "idlib/geometry/Surface.h"
33 #include "framework/Common.h"
34
35 /*
36 ===============================================================================
37
38 Bezier patch surface.
39
40 ===============================================================================
41 */
42
43 class idSurface_Patch : public idSurface {
44
45 public:
46 idSurface_Patch( void );
47 idSurface_Patch( int maxPatchWidth, int maxPatchHeight );
48 idSurface_Patch( const idSurface_Patch &patch );
49 ~idSurface_Patch( void );
50
51 void SetSize( int patchWidth, int patchHeight );
52 int GetWidth( void ) const;
53 int GetHeight( void ) const;
54
55 // subdivide the patch mesh based on error
56 void Subdivide( float maxHorizontalError, float maxVerticalError, float maxLength, bool genNormals = false );
57 // subdivide the patch up to an explicit number of horizontal and vertical subdivisions
58 void SubdivideExplicit( int horzSubdivisions, int vertSubdivisions, bool genNormals, bool removeLinear = false );
59
60 protected:
61 int width; // width of patch
62 int height; // height of patch
63 int maxWidth; // maximum width allocated for
64 int maxHeight; // maximum height allocated for
65 bool expanded; // true if vertices are spaced out
66
67 private:
68 // put the approximation points on the curve
69 void PutOnCurve( void );
70 // remove columns and rows with all points on one line
71 void RemoveLinearColumnsRows( void );
72 // resize verts buffer
73 void ResizeExpanded( int height, int width );
74 // space points out over maxWidth * maxHeight buffer
75 void Expand( void );
76 // move all points to the start of the verts buffer
77 void Collapse( void );
78 // project a point onto a vector to calculate maximum curve error
79 void ProjectPointOntoVector( const idVec3 &point, const idVec3 &vStart, const idVec3 &vEnd, idVec3 &vProj );
80 // generate normals
81 void GenerateNormals( void );
82 // generate triangle indexes
83 void GenerateIndexes( void );
84 // lerp point from two patch point
85 void LerpVert( const idDrawVert &a, const idDrawVert &b, idDrawVert &out ) const;
86 // sample a single 3x3 patch
87 void SampleSinglePatchPoint( const idDrawVert ctrl[3][3], float u, float v, idDrawVert *out ) const;
88 void SampleSinglePatch( const idDrawVert ctrl[3][3], int baseCol, int baseRow, int width, int horzSub, int vertSub, idDrawVert *outVerts ) const;
89 };
90
91 /*
92 =================
93 idSurface_Patch::idSurface_Patch
94 =================
95 */
idSurface_Patch(void)96 ID_INLINE idSurface_Patch::idSurface_Patch( void ) {
97 height = width = maxHeight = maxWidth = 0;
98 expanded = false;
99 }
100
101 /*
102 =================
103 idSurface_Patch::idSurface_Patch
104 =================
105 */
idSurface_Patch(int maxPatchWidth,int maxPatchHeight)106 ID_INLINE idSurface_Patch::idSurface_Patch( int maxPatchWidth, int maxPatchHeight ) {
107 width = height = 0;
108 maxWidth = maxPatchWidth;
109 maxHeight = maxPatchHeight;
110 verts.SetNum( maxWidth * maxHeight );
111 expanded = false;
112 }
113
114 /*
115 =================
116 idSurface_Patch::idSurface_Patch
117 =================
118 */
idSurface_Patch(const idSurface_Patch & patch)119 ID_INLINE idSurface_Patch::idSurface_Patch( const idSurface_Patch &patch ) {
120 (*this) = patch;
121 }
122
123 /*
124 =================
125 idSurface_Patch::~idSurface_Patch
126 =================
127 */
~idSurface_Patch()128 ID_INLINE idSurface_Patch::~idSurface_Patch() {
129 }
130
131 /*
132 =================
133 idSurface_Patch::GetWidth
134 =================
135 */
GetWidth(void)136 ID_INLINE int idSurface_Patch::GetWidth( void ) const {
137 return width;
138 }
139
140 /*
141 =================
142 idSurface_Patch::GetHeight
143 =================
144 */
GetHeight(void)145 ID_INLINE int idSurface_Patch::GetHeight( void ) const {
146 return height;
147 }
148
149 #endif /* !__SURFACE_PATCH_H__ */
150