1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * ConvexPolygon.cpp: convex polygon class |
---|
5 | * |
---|
6 | * ====================================================================== |
---|
7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
9 | * |
---|
10 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | * ====================================================================== |
---|
15 | */ |
---|
16 | #include <assert.h> |
---|
17 | |
---|
18 | #include <GL/gl.h> |
---|
19 | |
---|
20 | #include "ConvexPolygon.h" |
---|
21 | #include "Trace.h" |
---|
22 | |
---|
23 | ConvexPolygon::ConvexPolygon(VertexVector newvertices) |
---|
24 | { |
---|
25 | vertices.insert(vertices.begin(), newvertices.begin(), newvertices.end()); |
---|
26 | } |
---|
27 | |
---|
28 | // Finds the intersection of the line through |
---|
29 | // p1 and p2 with the given plane, putting the |
---|
30 | // result in intersect. |
---|
31 | // |
---|
32 | // If the line lies in the plane, an arbitrary |
---|
33 | // point on the line is returned. |
---|
34 | // |
---|
35 | // http://astronomy.swin.edu.au/pbourke/geometry/planeline/ |
---|
36 | bool |
---|
37 | findIntersection(const Vector4& pt1, const Vector4& pt2, const Vector4& plane, Vector4& ret) |
---|
38 | { |
---|
39 | float a = plane.x; |
---|
40 | float b = plane.y; |
---|
41 | float c = plane.z; |
---|
42 | float d = plane.w; |
---|
43 | |
---|
44 | Vector4 p1 = pt1; |
---|
45 | p1.perspective_divide(); |
---|
46 | float x1 = p1.x; |
---|
47 | float y1 = p1.y; |
---|
48 | float z1 = p1.z; |
---|
49 | |
---|
50 | Vector4 p2 = pt2; |
---|
51 | p2.perspective_divide(); |
---|
52 | float x2 = p2.x; |
---|
53 | float y2 = p2.y; |
---|
54 | float z2 = p2.z; |
---|
55 | |
---|
56 | float uDenom = a * (x1 - x2) + b * (y1 - y2) + c * (z1 - z2); |
---|
57 | float uNumer = a * x1 + b * y1 + c * z1 + d; |
---|
58 | |
---|
59 | if (uDenom == 0){ |
---|
60 | //plane parallel to line |
---|
61 | ERROR("Unexpected code path: ConvexPolygon.cpp\n"); |
---|
62 | if (uNumer == 0){ |
---|
63 | ret = p1; |
---|
64 | return true; |
---|
65 | } |
---|
66 | return false; |
---|
67 | } |
---|
68 | |
---|
69 | float u = uNumer / uDenom; |
---|
70 | ret.x = x1 + u * (x2 - x1); |
---|
71 | ret.y = y1 + u * (y2 - y1); |
---|
72 | ret.z = z1 + u * (z2 - z1); |
---|
73 | ret.w = 1; |
---|
74 | return true; |
---|
75 | } |
---|
76 | |
---|
77 | void |
---|
78 | ConvexPolygon::transform(const Mat4x4& mat) |
---|
79 | { |
---|
80 | VertexVector tmp = vertices; |
---|
81 | vertices.clear(); |
---|
82 | |
---|
83 | for (unsigned int i = 0; i < tmp.size(); i++) { |
---|
84 | Vector4 vec = tmp[i]; |
---|
85 | vertices.push_back(mat.transform(vec)); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | void |
---|
90 | ConvexPolygon::translate(const Vector4& shift) |
---|
91 | { |
---|
92 | VertexVector tmp = vertices; |
---|
93 | vertices.clear(); |
---|
94 | |
---|
95 | for (unsigned int i = 0; i < tmp.size(); i++) { |
---|
96 | Vector4 vec = tmp[i]; |
---|
97 | vertices.push_back(vec+shift); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void |
---|
102 | ConvexPolygon::clip(Plane& clipPlane, bool copy_to_texcoord) |
---|
103 | { |
---|
104 | if (vertices.size() == 0) { |
---|
105 | //ERROR("ConvexPolygon: polygon has no vertices\n"); |
---|
106 | return; |
---|
107 | } |
---|
108 | |
---|
109 | VertexVector clippedVerts; |
---|
110 | clippedVerts.reserve(2 * vertices.size()); |
---|
111 | |
---|
112 | // The algorithm is as follows: for each vertex |
---|
113 | // in the current poly, check to see which |
---|
114 | // half space it is in: clipped or retained. |
---|
115 | // If the previous vertex was in the other |
---|
116 | // half space, find the intersect of that edge |
---|
117 | // with the clip plane and add that intersect |
---|
118 | // point to the new list of vertices. If the |
---|
119 | // current vertex is in the retained half-space, |
---|
120 | // add it to the new list as well. |
---|
121 | |
---|
122 | Vector4 intersect; |
---|
123 | Vector4 plane = clipPlane.get_coeffs(); |
---|
124 | |
---|
125 | bool prevRetained = is_retained(vertices[0], plane); |
---|
126 | if (prevRetained) |
---|
127 | clippedVerts.push_back(vertices[0]); |
---|
128 | |
---|
129 | for (unsigned int i = 1; i < vertices.size(); i++) { |
---|
130 | bool retained = is_retained(vertices[i], plane); |
---|
131 | if (retained != prevRetained) { |
---|
132 | bool found = findIntersection(vertices[i - 1], vertices[i], |
---|
133 | plane, intersect); |
---|
134 | assert(found); |
---|
135 | clippedVerts.push_back(intersect); |
---|
136 | } |
---|
137 | if (retained) { |
---|
138 | clippedVerts.push_back(vertices[i]); |
---|
139 | } |
---|
140 | prevRetained = retained; |
---|
141 | } |
---|
142 | |
---|
143 | bool retained = is_retained(vertices[0], plane); |
---|
144 | if (retained != prevRetained) { |
---|
145 | bool found = findIntersection(vertices[vertices.size() - 1], |
---|
146 | vertices[0], plane, intersect); |
---|
147 | assert(found); |
---|
148 | clippedVerts.push_back(intersect); |
---|
149 | } |
---|
150 | |
---|
151 | vertices.clear(); |
---|
152 | vertices.insert(vertices.begin(), |
---|
153 | clippedVerts.begin(), |
---|
154 | clippedVerts.end()); |
---|
155 | |
---|
156 | if (copy_to_texcoord) |
---|
157 | copy_vertices_to_texcoords(); |
---|
158 | } |
---|
159 | |
---|
160 | void |
---|
161 | ConvexPolygon::copy_vertices_to_texcoords() |
---|
162 | { |
---|
163 | if (texcoords.size() > 0) |
---|
164 | texcoords.clear(); |
---|
165 | |
---|
166 | for (unsigned int i = 0; i < vertices.size(); i++) { |
---|
167 | texcoords.push_back(vertices[i]); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | void |
---|
172 | ConvexPolygon::Emit(bool use_texture) |
---|
173 | { |
---|
174 | if (vertices.size() >= 3) { |
---|
175 | for (unsigned int i = 0; i < vertices.size(); i++) { |
---|
176 | if(use_texture) { |
---|
177 | glTexCoord4fv((float *)&(texcoords[i])); |
---|
178 | //glTexCoord4fv((float *)&(vertices[i])); |
---|
179 | } |
---|
180 | glVertex4fv((float *)&(vertices[i])); |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | void |
---|
186 | ConvexPolygon::Emit(bool use_texture, const Vector3& shift, const Vector3& scale) |
---|
187 | { |
---|
188 | if (vertices.size() >= 3) { |
---|
189 | for (unsigned int i = 0; i < vertices.size(); i++) { |
---|
190 | if (use_texture) { |
---|
191 | glTexCoord4fv((float *)&(vertices[i])); |
---|
192 | } |
---|
193 | Vector4 tmp = (vertices[i]); |
---|
194 | Vector4 shift_4d = Vector4(shift.x, shift.y, shift.z, 0); |
---|
195 | tmp = tmp + shift_4d; |
---|
196 | tmp.x = tmp.x * scale.x; |
---|
197 | tmp.y = tmp.y * scale.y; |
---|
198 | tmp.z = tmp.z * scale.z; |
---|
199 | glVertex4fv((float *)(&tmp)); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|