-
Notifications
You must be signed in to change notification settings - Fork 25
/
mesh.h
157 lines (125 loc) · 3.63 KB
/
mesh.h
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
/* Mesh
*
* Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __MESH_H__
#define __MESH_H__
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mathlib.h"
#define MESH_STRIP_MAGIC ('m' | 's' << 8 | '0' << 16 | '2' << 24)
#define MESH_RAW_MAGIC ('m' | 'r' << 8 | '0' << 16 | '2' << 24)
class Mesh {
public:
Mesh();
Mesh(const char *name);
Mesh(const Mesh *mesh);
virtual ~Mesh();
virtual int render(int ppl = 0,int s = -1);
void findSilhouette(const vec4 &light,int s = -1);
int getNumIntersections(const vec3 &line0,const vec3 &line1,int s = -1);
virtual int renderShadowVolume(int s = -1);
int intersection(const vec3 &line0,const vec3 &line1,vec3 &point,vec3 &normal,int s = -1);
void transform(const mat4 &m,int s = -1);
struct Vertex {
vec3 xyz;
vec3 normal;
vec3 tangent;
vec3 binormal;
vec2 texcoord;
};
struct Edge {
vec3 v[2];
char reverse;
char flag;
};
struct Triangle {
vec3 v[3]; // vertexes
int e[3]; // edges
char reverse[3]; // edge reverse flag
vec4 plane; // plane
vec4 c[3]; // fast point in triangle
};
int getNumSurfaces();
const char *getSurfaceName(int s);
int getSurface(const char *name);
int getNumVertex(int s);
Vertex *getVertex(int s);
int getNumStrips(int s);
int *getIndices(int s);
int getNumEdges(int s);
Edge *getEdges(int s);
int getNumTriangles(int s);
Triangle *getTriangles(int s);
const vec3 &getMin(int s = -1);
const vec3 &getMax(int s = -1);
const vec3 &getCenter(int s = -1);
float getRadius(int s = -1);
// io
void addSurface(Mesh *mesh,int surface);
void addSurface(const char *name,Vertex *vertex,int num_vertex);
int load(const char *name);
int save(const char *name);
// strip mesh format without header
void load(FILE *file);
void save(FILE *file);
int load_mesh(const char *name);
int load_3ds(const char *name);
void calculate_tangent();
void calculate_bounds();
void create_shadow_volumes();
void create_triangle_strips();
protected:
vec3 min;
vec3 max;
vec3 center;
float radius;
struct Silhouette {
vec4 light;
vec4 *vertex;
int num_vertex;
char *flags; // front/back triangle
};
enum {
NUM_SURFACES = 512,
NUM_SILHOUETTES = 4,
};
struct Surface {
char name[128]; // surface name
int num_vertex; // number of vertexes
Vertex *vertex;
int num_edges; // number of edges
Edge *edges;
int num_triangles; // number of triangles
Triangle *triangles;
int num_indices; // number of indices
int num_strips; // number of triangle strips
int *indices;
Silhouette silhouettes[NUM_SILHOUETTES]; // silhouette history
int last_silhouette;
Silhouette *silhouette; // current silhouette
vec3 min; // bound box
vec3 max;
vec3 center; // bound sphere
float radius;
};
int num_surfaces;
Surface *surfaces[NUM_SURFACES];
};
#endif /* __MESH_H__ */