]> Dogcows Code - chaz/yoink/blob - src/Moof/Aabb.cc
minor refactoring and state progress
[chaz/yoink] / src / Moof / Aabb.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include "Aabb.hh"
30 #include "Frustum.hh"
31 #include "OpenGL.hh"
32 #include "Texture.hh"
33
34
35 namespace Mf {
36
37
38 void Aabb::getOctant(Aabb& octant, int num) const
39 {
40 Vector3 mid = getCenter();
41
42 switch (num)
43 {
44 case 0:
45 octant.init(Vector3(min[0], min[1], mid[2]),
46 Vector3(mid[0], mid[1], max[2]));
47 break;
48
49 case 1:
50 octant.init(Vector3(mid[0], min[1], mid[2]),
51 Vector3(max[0], mid[1], max[2]));
52 break;
53
54 case 2:
55 octant.init(mid, max);
56 break;
57
58 case 3:
59 octant.init(Vector3(min[0], mid[1], mid[2]),
60 Vector3(mid[0], max[1], max[2]));
61 break;
62
63 case 4:
64 octant.init(min, mid);
65 break;
66
67 case 5:
68 octant.init(Vector3(mid[0], min[1], min[2]),
69 Vector3(max[0], mid[1], mid[2]));
70 break;
71
72 case 6:
73 octant.init(Vector3(mid[0], mid[1], min[2]),
74 Vector3(max[0], max[1], mid[2]));
75 break;
76
77 case 7:
78 octant.init(Vector3(min[0], mid[1], min[2]),
79 Vector3(mid[0], max[1], mid[2]));
80 break;
81 }
82 }
83
84
85 void Aabb::getCorners(Vector3 corners[8]) const
86 {
87 corners[0][0] = min[0]; corners[0][1] = min[1]; corners[0][2] = max[2];
88 corners[1][0] = max[0]; corners[1][1] = min[1]; corners[1][2] = max[2];
89 corners[2][0] = max[0]; corners[2][1] = max[1]; corners[2][2] = max[2];
90 corners[3][0] = min[0]; corners[3][1] = max[1]; corners[3][2] = max[2];
91 corners[4][0] = min[0]; corners[4][1] = min[1]; corners[4][2] = min[2];
92 corners[5][0] = max[0]; corners[5][1] = min[1]; corners[5][2] = min[2];
93 corners[6][0] = max[0]; corners[6][1] = max[1]; corners[6][2] = min[2];
94 corners[7][0] = min[0]; corners[7][1] = max[1]; corners[7][2] = min[2];
95 }
96
97
98 void Aabb::encloseVertices(const Vector3 vertices[], unsigned count)
99 {
100 min = vertices[0];
101 max = vertices[0];
102
103 for (unsigned i = 1; i < count; ++i)
104 {
105 if (vertices[i][0] < min[0]) min[0] = vertices[i][0];
106 if (vertices[i][0] > max[0]) max[0] = vertices[i][0];
107 if (vertices[i][1] < min[1]) min[1] = vertices[i][1];
108 if (vertices[i][1] > max[1]) max[1] = vertices[i][1];
109 if (vertices[i][2] < min[2]) min[2] = vertices[i][2];
110 if (vertices[i][2] > max[2]) max[2] = vertices[i][2];
111 }
112 }
113
114
115 void Aabb::draw(Scalar alpha) const
116 {
117 Scalar vertices[] = {min[0], min[1], min[2],
118 min[0], max[1], min[2],
119 max[0], max[1], min[2],
120 max[0], min[1], min[2],
121 min[0], max[1], max[2],
122 min[0], min[1], max[2],
123 max[0], min[1], max[2],
124 max[0], max[1], max[2]};
125
126 GLubyte indices[] = {0, 1, 2, 3,
127 1, 2, 7, 4,
128 3, 0, 5, 6,
129 2, 3, 6, 7,
130 5, 0, 1, 4,
131 4, 5, 6, 7};
132
133 glEnableClientState(GL_VERTEX_ARRAY);
134 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
135 glVertexPointer(3, GL_SCALAR, 0, vertices);
136
137 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
138 Texture::resetBind();
139
140 glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE, indices);
141
142 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
143 //glDisableClientState(GL_VERTEX_ARRAY);
144
145 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
146 }
147
148 bool Aabb::isVisible(const Frustum& frustum) const
149 {
150 return frustum.contains(*this);
151 }
152
153
154 } // namespace Mf
155
156 /** vim: set ts=4 sw=4 tw=80: *************************************************/
157
This page took 0.037299 seconds and 4 git commands to generate.