1 #ifndef __GNUC__
2 #pragma once
3 #endif
4 #ifndef __XR_GEOM_BUF_H__
5 #define __XR_GEOM_BUF_H__
6 
7 #include <vector>
8 #include "xr_vector2.h"
9 #include "xr_vector3.h"
10 #include "xr_color.h"
11 #include "xr_influence.h"
12 #include "xr_ogf_format.h"
13 
14 namespace xray_re {
15 
16 // in-game format definitions for level geometry in v5...v8 - from D3D7 actually.
17 enum {
18 	D3D_FVF_POSITION_MASK	= 0x00e,
19 	D3D_FVF_XYZ		= 0x002,	// x, y, z
20 
21 	D3D_FVF_NORMAL		= 0x010,	// not used
22 	D3D_FVF_DIFFUSE		= 0x040,	// quantized normal
23 	D3D_FVF_SPECULAR	= 0x080,	// not used
24 	D3D_FVF_NC_MASK		= 0x0f0,	// not used
25 
26 	D3D_FVF_TEX1		= 0x100,	// base texture
27 	D3D_FVF_TEX2		= 0x200,	// base texture + light map
28 	D3D_FVF_TEXCOUNT_MASK	= 0xf00,
29 	D3D_FVF_TEXCOUNT_SHIFT	= 8,
30 };
31 
32 // in-game format definitions for v13+ - from D3D9 actually.
33 enum {
34 	D3D_VE_USAGE_POSITION		= 0,
35 	D3D_VE_USAGE_BLENDWEIGHT	= 1,
36 	D3D_VE_USAGE_BLENDINDICES	= 2,
37 	D3D_VE_USAGE_NORMAL		= 3,
38 	D3D_VE_USAGE_PSIZE		= 4,
39 	D3D_VE_USAGE_TEXCOORD		= 5,
40 	D3D_VE_USAGE_TANGENT		= 6,
41 	D3D_VE_USAGE_BINORMAL		= 7,
42 	D3D_VE_USAGE_TESSFACTOR		= 8,
43 	D3D_VE_USAGE_POSITIONT		= 9,
44 	D3D_VE_USAGE_COLOR		= 10,
45 	D3D_VE_USAGE_FOG		= 11,
46 	D3D_VE_USAGE_DEPTH		= 12,
47 	D3D_VE_USAGE_SAMPLE		= 13,
48 };
49 
50 enum {
51 	D3D_VE_METHOD_DEFAULT	= 0,
52 	D3D_VE_METHOD_PARTIALU	= 1,
53 	D3D_VE_METHOD_PARTIALV	= 2,
54 	D3D_VE_METHOD_CROSSUV	= 3,
55 	D3D_VE_METHOD_UV	= 4,
56 };
57 
58 enum {
59 	D3D_VE_TYPE_FLOAT1	= 0,
60 	D3D_VE_TYPE_FLOAT2	= 1,
61 	D3D_VE_TYPE_FLOAT3	= 2,
62 	D3D_VE_TYPE_FLOAT4	= 3,
63 	D3D_VE_TYPE_D3DCOLOR	= 4,
64 
65 	D3D_VE_TYPE_UBYTE4	= 5,
66 	D3D_VE_TYPE_SHORT2	= 6,
67 	D3D_VE_TYPE_SHORT4	= 7,
68 
69 	D3D_VE_TYPE_UBYTE4N	= 8,
70 	D3D_VE_TYPE_SHORT2N	= 9,
71 	D3D_VE_TYPE_SHORT4N	= 10,
72 	D3D_VE_TYPE_USHORT2N	= 11,
73 	D3D_VE_TYPE_USHORT4N	= 12,
74 	D3D_VE_TYPE_UDEC3	= 13,
75 	D3D_VE_TYPE_DEC3N	= 14,
76 	D3D_VE_TYPE_FLOAT16_2	= 15,
77 	D3D_VE_TYPE_FLOAT16_4	= 16,
78 	D3D_VE_TYPE_UNUSED	= 17,
79 };
80 
81 typedef uint32_t d3d_fvf;
82 
83 struct d3d_vertex_element {
84 	uint16_t	stream;
85 	uint16_t	offset;
86 	uint8_t		type;
87 	uint8_t		method;
88 	uint8_t		usage;
89 	uint8_t		usage_index;
90 };
91 
92 class xr_flexbuf {
93 public:
94 	virtual		~xr_flexbuf();
95 	size_t		size() const;
96 	bool		owner() const;
97 
98 protected:
99 			xr_flexbuf();
100 	void		set_size(size_t size);
101 	void		set_owner(bool owner);
102 	void		clear();
103 	template<typename T> T* copy(const T* source, T* target, size_t size) const;
104 	template<typename T> T*	duplicate(const T* source) const;
105 
106 private:
107 	bool		m_owner;
108 	size_t		m_size;
109 };
110 
111 class xr_reader;
112 class xr_writer;
113 
114 class xr_vbuf: public xr_flexbuf {
115 public:
116 			xr_vbuf();
117 			xr_vbuf(const xr_vbuf& that);
118 			xr_vbuf(size_t n, const fvector3* points, const fvector3* normals, const fvector2* texcoords);
119 	virtual		~xr_vbuf();
120 	void		clear();
121 	void		proxy(const xr_vbuf& that, size_t base, size_t n);
122 	void		load_d3d7(xr_reader& r, size_t n, uint32_t fvf);
123 	void		load_d3d9(xr_reader& r, size_t n, const d3d_vertex_element ve[], size_t n_ve);
124 	void		load_dm(xr_reader& r, size_t n);
125 	void		save_dm(xr_writer& w) const;
126 	void		load_ogf3(xr_reader& r, size_t n, ogf_vertex_format vf);
127 	void		load_ogf4(xr_reader& r, size_t n, ogf_vertex_format vf);
128 	xr_vbuf&	operator=(const xr_vbuf& right);
129 //	bool		operator==(const xr_vbuf& right) const;
130 //	bool		operator!=(const xr_vbuf& right) const;
131 
132 	uint32_t	signature() const;
133 
134 	bool		has_points() const;
135 	bool		has_normals() const;
136 	bool		has_texcoords() const;
137 	bool		has_influences() const;
138 	bool		has_colors() const;
139 	bool		has_lightmaps() const;
140 
141 	const fvector3&		p(size_t at) const;
142 	const fvector3&		n(size_t at) const;
143 	const fvector2&		tc(size_t at) const;
144 	const fvector2&		lm(size_t at) const;
145 	const finfluence&	w(size_t at) const;
146 	const fcolor&		c(size_t at) const;
147 
148 	const fvector3*		p() const;
149 	const fvector3*		n() const;
150 	const fvector2*		tc() const;
151 	const fvector2*		lm() const;
152 	const finfluence*	w() const;
153 	const fcolor*		c() const;
154 
155 	enum {
156 		S_POINTS	= 0x01,
157 		S_NORMALS	= 0x02,
158 		S_TEXCOORDS	= 0x04,
159 		S_LIGHTMAPS	= 0x08,
160 		S_INFLUENCES	= 0x10,
161 		S_COLORS	= 0x20,
162 	};
163 
164 protected:
165 	void		make_signature();
166 
167 protected:
168 	uint32_t	m_signature;
169 
170 	fvector3*	m_points;
171 	fvector3*	m_normals;
172 	fvector2*	m_texcoords;
173 	fvector2*	m_lightmaps;
174 	finfluence*	m_influences;
175 	fcolor*		m_colors;
176 };
177 
TYPEDEF_STD_VECTOR(xr_vbuf)178 TYPEDEF_STD_VECTOR(xr_vbuf)
179 
180 class xr_ibuf: public xr_flexbuf {
181 public:
182 			xr_ibuf();
183 			xr_ibuf(const xr_ibuf& that);
184 	virtual		~xr_ibuf();
185 
186 	void		load(xr_reader& r, size_t n);
187 	void		save(xr_writer& w) const;
188 	void		proxy(const xr_ibuf& that, size_t base, size_t n);
189 	void		clear();
190 	xr_ibuf&	operator=(const xr_ibuf& right);
191 //	bool		operator==(const xr_ibuf& right) const;
192 //	bool		operator!=(const xr_ibuf& right) const;
193 	const uint16_t&	operator[](size_t at) const;
194 	uint16_t&	operator[](size_t at);
195 
196 private:
197 	uint16_t*	m_indices;
198 };
199 
TYPEDEF_STD_VECTOR(xr_ibuf)200 TYPEDEF_STD_VECTOR(xr_ibuf)
201 
202 class xr_swibuf: public xr_flexbuf {
203 public:
204 				xr_swibuf();
205 				xr_swibuf(const xr_swibuf& that);
206 	virtual			~xr_swibuf();
207 	void			proxy(const xr_swibuf& that);
208 	void			load(xr_reader& r);
209 	void			save(xr_writer& w) const;
210 	void			clear();
211 	xr_swibuf&		operator=(const xr_swibuf& right);
212 	const ogf4_slide_window&operator[](size_t at) const;
213 
214 private:
215 	uint32_t		m_reserved[4];
216 	ogf4_slide_window*	m_slide_windows;
217 };
218 
TYPEDEF_STD_VECTOR(xr_swibuf)219 TYPEDEF_STD_VECTOR(xr_swibuf)
220 
221 inline xr_flexbuf::xr_flexbuf(): m_owner(true), m_size(0) {}
~xr_flexbuf()222 inline xr_flexbuf::~xr_flexbuf() {}
clear()223 inline void xr_flexbuf::clear() { m_owner = true; m_size = 0; }
size()224 inline size_t xr_flexbuf::size() const { return m_size; }
set_size(size_t size)225 inline void xr_flexbuf::set_size(size_t size) { m_size = size; }
owner()226 inline bool xr_flexbuf::owner() const { return m_owner; }
set_owner(bool owner)227 inline void xr_flexbuf::set_owner(bool owner) { m_owner = owner; }
228 
copy(const T * source,T * target,size_t size)229 template<typename T> inline T* xr_flexbuf::copy(const T* source, T* target, size_t size) const
230 {
231 #if defined(_MSC_VER) && _MSC_VER >= 1400 && _MSC_VER < 1600
232 	stdext::unchecked_uninitialized_copy(source, source + size, target);
233 #else
234 	std::uninitialized_copy(source, source + size, target);
235 #endif
236 	return target;
237 }
238 
duplicate(const T * source)239 template<typename T> inline T* xr_flexbuf::duplicate(const T* source) const
240 {
241 	return source ? copy(source, new T[m_size], m_size) : 0;
242 }
243 
signature()244 inline uint32_t xr_vbuf::signature() const { return m_signature; }
has_points()245 inline bool xr_vbuf::has_points() const { return !!(m_signature & S_POINTS); }
has_normals()246 inline bool xr_vbuf::has_normals() const { return !!(m_signature & S_NORMALS); }
has_texcoords()247 inline bool xr_vbuf::has_texcoords() const { return !!(m_signature & S_TEXCOORDS); }
has_lightmaps()248 inline bool xr_vbuf::has_lightmaps() const { return !!(m_signature & S_LIGHTMAPS); }
has_influences()249 inline bool xr_vbuf::has_influences() const { return !!(m_signature & S_INFLUENCES); }
has_colors()250 inline bool xr_vbuf::has_colors() const { return !!(m_signature & S_COLORS); }
251 //inline bool xr_vbuf::operator!=(const xr_vbuf& right) const { return !(*this == right); }
p(size_t at)252 inline const fvector3& xr_vbuf::p(size_t at) const { return m_points[at]; }
n(size_t at)253 inline const fvector3& xr_vbuf::n(size_t at) const { return m_normals[at]; }
tc(size_t at)254 inline const fvector2& xr_vbuf::tc(size_t at) const { return m_texcoords[at]; }
lm(size_t at)255 inline const fvector2& xr_vbuf::lm(size_t at) const { return m_lightmaps[at]; }
w(size_t at)256 inline const finfluence& xr_vbuf::w(size_t at) const { return m_influences[at]; }
c(size_t at)257 inline const fcolor& xr_vbuf::c(size_t at) const { return m_colors[at]; }
p()258 inline const fvector3* xr_vbuf::p() const { return m_points; }
n()259 inline const fvector3* xr_vbuf::n() const { return m_normals; }
tc()260 inline const fvector2* xr_vbuf::tc() const { return m_texcoords; }
lm()261 inline const fvector2* xr_vbuf::lm() const { return m_lightmaps; }
w()262 inline const finfluence* xr_vbuf::w() const { return m_influences; }
c()263 inline const fcolor* xr_vbuf::c() const { return m_colors; }
264 
265 //inline bool xr_ibuf::operator!=(const xr_ibuf& right) const { return !(*this == right); }
266 inline const uint16_t& xr_ibuf::operator[](size_t at) const { return m_indices[at]; }
267 inline uint16_t& xr_ibuf::operator[](size_t at) { return m_indices[at]; }
268 
269 inline const ogf4_slide_window& xr_swibuf::operator[](size_t at) const { return m_slide_windows[at]; }
270 
271 } // end of namespace xray_re
272 
273 #endif
274