#include "polygon.h"
#include "lightmap.h"
#include "portal.h"
#include "tll.h"
#include "decal.h"
Go to the source code of this file.
Compounds | |
struct | BSP_node |
Typedefs | |
typedef BSP_node | BSP_node |
Enumerations | |
enum | { Front, Back, TwoFrontOneBack, OneFrontTwoBack, OneFrontOneBack, PortalWasSplit, OnPartition } |
Functions | |
int | FindCurrentLeaf (VECTOR Position, BSP_node *node) |
int | SplitPolygon (POLYGON, POLYGON, POLYGON *) |
int | SelectPartitionfromList (POLYGON *nodepolylist, int numpolys, int *bestfront, int *bestback) |
void | BuildBSP (BSP_node *node) |
int | RenderBSP (BSP_node *node) |
void | DeleteBSP (BSP_node *node) |
void | DrawIntersectionSphere (VECTOR coordinates) |
|
|
|
Definition at line 11 of file bsp.h.
00011 {Front, Back, TwoFrontOneBack, OneFrontTwoBack, OneFrontOneBack, PortalWasSplit, OnPartition}; |
|
Definition at line 118 of file bsp.cpp. References Back, BSP_node::backnode, Front, BSP_node::frontnode, BSP_node::leaf, BSP_node::nodeid, BSP_node::nodepolylist, BSP_node::numdecals, numleaves, numpartitions, BSP_node::numpolys, BSP_node::numportals, OneFrontOneBack, OneFrontTwoBack, BSP_node::partition, SelectPartitionfromList(), SplitPolygon(), TwoFrontOneBack, and BSP_node::visible. Referenced by InitGL().
00119 { 00120 int result, front, back, polytoclassify, partplane; 00121 POLYGON output[3]; 00122 00123 partplane = SelectPartitionfromList(node->nodepolylist, node->numpolys, &front, &back); 00124 00125 if (partplane == -1) 00126 { 00127 node->nodeid = ++numleaves; 00128 node->leaf = true; 00129 return; 00130 } 00131 00132 node->nodeid = ++numpartitions; 00133 node->partition = node->nodepolylist[partplane]; 00134 00135 //Allocate memory for a front and back node 00136 node->frontnode = new BSP_node; 00137 node->frontnode->visible = 0; 00138 node->frontnode->leaf = 0; 00139 node->frontnode->numpolys = front; 00140 node->frontnode->nodepolylist = new POLYGON[front]; 00141 node->frontnode->numportals = 0; 00142 node->frontnode->numdecals = 0; 00143 00144 node->backnode = new BSP_node; 00145 node->backnode->visible = 0; 00146 node->backnode->leaf = 0; 00147 node->backnode->numpolys = back; 00148 node->backnode->nodepolylist = new POLYGON[back]; 00149 node->backnode->numportals = 0; 00150 node->backnode->numdecals = 0; 00151 00152 //Classify each polygon in the current node with respect to the partitioning plane. 00153 front = back = 0; 00154 for (polytoclassify = 0; polytoclassify < node->numpolys; polytoclassify++) 00155 { 00156 output[0] = node->nodepolylist[polytoclassify]; 00157 output[1] = node->nodepolylist[polytoclassify]; 00158 output[2] = node->nodepolylist[polytoclassify]; 00159 00160 result = SplitPolygon(node->nodepolylist[polytoclassify], node->partition, output); 00161 switch (result) 00162 { 00163 case Front: 00164 node->frontnode->nodepolylist[front] = node->nodepolylist[polytoclassify]; 00165 front++; 00166 break; 00167 00168 case Back: 00169 node->backnode->nodepolylist[back] = node->nodepolylist[polytoclassify]; 00170 back++; 00171 break; 00172 00173 case TwoFrontOneBack: 00174 node->frontnode->nodepolylist[front] = output[0]; 00175 node->frontnode->nodepolylist[front + 1] = output[1]; 00176 front += 2; 00177 node->backnode->nodepolylist[back] = output[2]; 00178 back++; 00179 break; 00180 00181 case OneFrontTwoBack: 00182 node->frontnode->nodepolylist[front] = output[0]; 00183 front++; 00184 node->backnode->nodepolylist[back] = output[1]; 00185 node->backnode->nodepolylist[back + 1] = output[2]; 00186 back += 2; 00187 break; 00188 00189 case OneFrontOneBack: 00190 node->frontnode->nodepolylist[front] = output[0]; 00191 front++; 00192 node->backnode->nodepolylist[back] = output[1]; 00193 back++; 00194 break; 00195 } 00196 } 00197 00198 node->numpolys = 0; 00199 delete[] node->nodepolylist; 00200 node->nodepolylist = 0; 00201 // delete[] node->nodelightmaplist; 00202 00203 BuildBSP(node->frontnode); 00204 BuildBSP(node->backnode); 00205 } |
|
Definition at line 351 of file bsp.cpp. References BSP_node::backnode, LinkedList< PORTAL >::Delete(), BSP_node::frontnode, LinkedList< PORTAL >::Get(), BSP_node::leaf, BSP_node::nodelightmaplist, BSP_node::nodepolylist, BSP_node::numportals, BSP_node::portallist, and PORTAL::Vertex. Referenced by WndProc().
00352 { 00353 if (node->leaf == true) 00354 { 00355 delete[] node->nodepolylist; 00356 delete[] node->nodelightmaplist; 00357 for (int i = node->numportals; i > 0 ; i--) 00358 { 00359 PORTAL* temp = node->portallist.Get(i); 00360 delete[] temp->Vertex; 00361 node->portallist.Delete(i); 00362 delete temp; 00363 } 00364 node->numportals = 0; 00365 return; 00366 } 00367 00368 DeleteBSP(node->frontnode); 00369 delete node->frontnode; 00370 DeleteBSP(node->backnode); 00371 delete node->backnode; 00372 } |
|
Definition at line 374 of file bsp.cpp. References VECTOR::x, VECTOR::y, and VECTOR::z.
00375 { 00376 float mat_ambient[] = { 0.2, 1.0, 0.1, 1.0 }; 00377 float mat_diffuse[] = { 0.2, 1.0, 0.1, 1.0 }; 00378 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); 00379 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); 00380 glDisable(GL_TEXTURE_2D); 00381 glPushMatrix(); 00382 glTranslatef(coordinates.x, coordinates.y, coordinates.z); 00383 GLUquadricObj * sphere = gluNewQuadric(); 00384 gluQuadricOrientation(sphere, GLU_OUTSIDE); 00385 gluSphere(sphere,0.3,20,20); 00386 glPopMatrix(); 00387 glEnable(GL_TEXTURE_2D); 00388 } |
|
Definition at line 31 of file bsp.cpp. References BSP_node::backnode, ClassifyPoint(), CrossVector(), BSP_node::frontnode, BSP_node::leaf, BSP_node::nodeid, numcurrentportals, BSP_node::numportals, BSP_node::partition, POLYGON::Vertex, VERTEX::x, VECTOR::x, VERTEX::y, VECTOR::y, VERTEX::z, and VECTOR::z. Referenced by CheckForParticleCollision(), DrawGLScene(), ParticleManager::Update(), and UpdateBullets().
00032 { 00033 if (node->leaf == true) 00034 { 00035 numcurrentportals = node->numportals; 00036 return node->nodeid; 00037 } 00038 00039 VECTOR edge1, edge2, planeNormal, temp; 00040 // get the partitioning planes normal 00041 edge1.x = node->partition.Vertex[1].x - node->partition.Vertex[0].x; 00042 edge1.y = node->partition.Vertex[1].y - node->partition.Vertex[0].y; 00043 edge1.z = node->partition.Vertex[1].z - node->partition.Vertex[0].z; 00044 edge2.x = node->partition.Vertex[2].x - node->partition.Vertex[0].x; 00045 edge2.y = node->partition.Vertex[2].y - node->partition.Vertex[0].y; 00046 edge2.z = node->partition.Vertex[2].z - node->partition.Vertex[0].z; 00047 planeNormal = CrossVector(edge1, edge2); 00048 temp.x = node->partition.Vertex[0].x; 00049 temp.y = node->partition.Vertex[0].y; 00050 temp.z = node->partition.Vertex[0].z; 00051 00052 int side = ClassifyPoint(Position, temp, planeNormal); 00053 00054 if (side == 1 || side == 0) 00055 { 00056 return FindCurrentLeaf(Position, node->frontnode); 00057 } 00058 else 00059 { 00060 return FindCurrentLeaf(Position, node->backnode); 00061 } 00062 } |
|
Definition at line 207 of file bsp.cpp. References BULLET::active, BSP_node::backnode, ClassifyPoint(), DECAL::counter, CrossVector(), currentCamera, currentleaf, BSP_node::decallist, LinkedList< DECAL >::Delete(), BULLET::Draw(), BSP_node::frontnode, LinkedList< DECAL >::Get(), LinkedList< PORTAL >::Get(), BULLET::leaf, BSP_node::leaf, BSP_node::nodeid, BSP_node::nodelightmaplist, BSP_node::nodepolylist, numBullets, BSP_node::numdecals, BSP_node::numpolys, BSP_node::numportals, PORTAL::numVertices, VERTEX::nx, BSP_node::partition, BSP_node::portallist, RenderBulletDecal(), RenderBurnDecal(), RenderParticles(), POLYGON::Rotate, POLYGON::Scale, POLYGON::Shift, TEXTURE::TexID, Lightmap::Texture, POLYGON::Texture, DECAL::type, VERTEX::u, VERTEX::v, PORTAL::Vertex, POLYGON::Vertex, Lightmap::vertex_u, Lightmap::vertex_v, BSP_node::visible, VERTEX::x, VECTOR::x, VERTEX::y, VECTOR::y, VERTEX::z, and VECTOR::z. Referenced by DrawWorld().
00208 { 00209 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 00210 00211 int Side; 00212 VECTOR Position, edge1, edge2, planeNormal, temp; 00213 00214 //The current position of the player/viewpoint 00215 Position.x = camera[currentCamera].Position.x; 00216 Position.y = camera[currentCamera].Position.y; 00217 Position.z = camera[currentCamera].Position.z; 00218 00219 if (!node->leaf) 00220 { 00221 // get the partitioning planes normal 00222 edge1.x = node->partition.Vertex[1].x - node->partition.Vertex[0].x; 00223 edge1.y = node->partition.Vertex[1].y - node->partition.Vertex[0].y; 00224 edge1.z = node->partition.Vertex[1].z - node->partition.Vertex[0].z; 00225 edge2.x = node->partition.Vertex[2].x - node->partition.Vertex[0].x; 00226 edge2.y = node->partition.Vertex[2].y - node->partition.Vertex[0].y; 00227 edge2.z = node->partition.Vertex[2].z - node->partition.Vertex[0].z; 00228 planeNormal = CrossVector(edge1, edge2); 00229 temp.x = node->partition.Vertex[0].x; 00230 temp.y = node->partition.Vertex[0].y; 00231 temp.z = node->partition.Vertex[0].z; 00232 Side = ClassifyPoint(Position, temp, planeNormal); 00233 00234 if (Side == -1) 00235 { 00236 RenderBSP(node->frontnode); 00237 RenderBSP(node->backnode); 00238 } 00239 else 00240 { 00241 RenderBSP(node->backnode); 00242 RenderBSP(node->frontnode); 00243 } 00244 } 00245 00246 if (node->leaf && node->visible) 00247 { 00248 node->visible = false; 00249 //Draw polygons that are in the leaf 00250 for (int loop = 0; loop < node->numpolys; loop++) 00251 { 00252 glMatrixMode(GL_TEXTURE); 00253 glPushMatrix(); 00254 00255 glScalef(node->nodepolylist[loop].Scale[0], node->nodepolylist[loop].Scale[1], 1.0f); 00256 glTranslatef(node->nodepolylist[loop].Shift[0], node->nodepolylist[loop].Shift[1], 0.0f); 00257 glRotatef(node->nodepolylist[loop].Rotate, 0.0f, 0.0f, 1.0f); 00258 glBindTexture(GL_TEXTURE_2D, node->nodepolylist[loop].Texture); 00259 glBegin(GL_TRIANGLES); 00260 glNormal3fv(&node->nodepolylist[loop].Vertex[0].nx); 00261 glTexCoord2f(node->nodepolylist[loop].Vertex[0].u, node->nodepolylist[loop].Vertex[0].v); 00262 glVertex3fv(&node->nodepolylist[loop].Vertex[0].x); 00263 glTexCoord2f(node->nodepolylist[loop].Vertex[1].u, node->nodepolylist[loop].Vertex[1].v); 00264 glVertex3fv(&node->nodepolylist[loop].Vertex[1].x); 00265 glTexCoord2f(node->nodepolylist[loop].Vertex[2].u, node->nodepolylist[loop].Vertex[2].v); 00266 glVertex3fv(&node->nodepolylist[loop].Vertex[2].x); 00267 glEnd(); 00268 glPopMatrix(); 00269 glMatrixMode(GL_MODELVIEW); 00270 00271 glEnable(GL_BLEND); 00272 glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR); 00273 00274 glBindTexture(GL_TEXTURE_2D, node->nodelightmaplist[loop].Texture.TexID); 00275 glBegin(GL_TRIANGLES); 00276 glNormal3fv(&node->nodepolylist[loop].Vertex[0].nx); 00277 glTexCoord2f(node->nodelightmaplist[loop].vertex_u[0], node->nodelightmaplist[loop].vertex_v[0]); 00278 glVertex3fv(&node->nodepolylist[loop].Vertex[0].x); 00279 glTexCoord2f(node->nodelightmaplist[loop].vertex_u[1], node->nodelightmaplist[loop].vertex_v[1]); 00280 glVertex3fv(&node->nodepolylist[loop].Vertex[1].x); 00281 glTexCoord2f(node->nodelightmaplist[loop].vertex_u[2], node->nodelightmaplist[loop].vertex_v[2]); 00282 glVertex3fv(&node->nodepolylist[loop].Vertex[2].x); 00283 glEnd(); 00284 glDisable(GL_BLEND); 00285 } 00286 00287 if (showportals) 00288 { 00289 // Draw the leaf portals 00290 glDisable(GL_TEXTURE_2D); 00291 glDisable(GL_LIGHTING); 00292 glEnable(GL_BLEND); 00293 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00294 glEnable(GL_ALPHA_TEST); 00295 glAlphaFunc(GL_GREATER, 0); 00296 glColor4f(1.0, 1.0, 0.0, 0.2); 00297 00298 if (currentleaf == node->nodeid) 00299 { 00300 for (int loop = 1; loop <= node->numportals; loop++) 00301 { 00302 PORTAL* tempportal = node->portallist.Get(loop); 00303 glBegin(GL_POLYGON); 00304 glNormal3fv(&tempportal->Vertex[0].nx); 00305 for (int innerloop = 0; innerloop < tempportal->numVertices; innerloop++) 00306 glVertex3f(tempportal->Vertex[innerloop].x, tempportal->Vertex[innerloop].y, tempportal->Vertex[innerloop].z); 00307 glEnd(); 00308 } 00309 } 00310 glDisable(GL_BLEND); 00311 glDisable(GL_ALPHA_TEST); 00312 glEnable(GL_TEXTURE_2D); 00313 glEnable(GL_LIGHTING); 00314 } 00315 00316 // Draw decals 00317 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 00318 DECAL* tempDecal; 00319 for (int loop = 1; loop <= node->numdecals; loop++) 00320 { 00321 tempDecal = node->decallist.Get(loop); 00322 if (tempDecal->type == 0) 00323 RenderBulletDecal(*tempDecal); 00324 else 00325 RenderBurnDecal(*tempDecal); 00326 --tempDecal->counter; 00327 if (tempDecal->counter == 0) 00328 { 00329 node->decallist.Delete(loop); 00330 --node->numdecals; 00331 } 00332 } 00333 00334 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 00335 00336 // Draw bullets 00337 for (int loop = 0; loop < numBullets; loop++) 00338 { 00339 if (bullet[loop].leaf == node->nodeid && bullet[loop].active) 00340 bullet[loop].Draw(); 00341 } 00342 } 00343 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 00344 00345 // Draw particles 00346 RenderParticles(node->nodeid); 00347 00348 return 1; 00349 } |
|
Definition at line 64 of file bsp.cpp. References Back, Front, OneFrontOneBack, OneFrontTwoBack, SplitPolygon(), and TwoFrontOneBack. Referenced by BuildBSP().
00065 { 00066 int count = 0, result, absdifference = 1000000000, bestplane = 0, front, back, potentialplane, polytoclassify; 00067 VECTOR temp; 00068 00069 // Loop through all the polygons and find the best splitting plane 00070 for(potentialplane = 0; potentialplane < numpolys; potentialplane++) 00071 { 00072 front = back = 0; 00073 for (polytoclassify = 0; polytoclassify < numpolys; polytoclassify++) 00074 { 00075 result = SplitPolygon(nodepolylist[polytoclassify], nodepolylist[potentialplane], NULL); 00076 switch (result) 00077 { 00078 case Front: 00079 front++; 00080 break; 00081 00082 case Back: 00083 back++; 00084 break; 00085 00086 case TwoFrontOneBack: 00087 front += 2; 00088 back++; 00089 break; 00090 00091 case OneFrontTwoBack: 00092 front++; 00093 back += 2; 00094 break; 00095 00096 case OneFrontOneBack: 00097 front++; 00098 back++; 00099 break; 00100 } 00101 } 00102 if (abs(front - back) < absdifference) 00103 { 00104 absdifference = abs(front - back); 00105 bestplane = potentialplane; 00106 *bestfront = front; 00107 *bestback = back; 00108 } 00109 if (front == 0 || back == 0) 00110 count++; 00111 } 00112 if (count == numpolys) 00113 return -1; 00114 else 00115 return bestplane; 00116 } |
|
Definition at line 384 of file polygon.cpp.
00385 { 00386 VECTOR planeNormal, polysNormal, pointOnPlane, edge1, edge2, temp; 00387 VERTEX ptA, ptB, outpts[4], inpts[4], intersection; 00388 int count = 0, out_c = 0, in_c = 0, sideA, sideB, outputFlag; 00389 VERTEX texvert1, texvert2; // texture calculation variables 00390 VECTOR t1, t2; 00391 float scale; 00392 00393 // get a point on the plane 00394 pointOnPlane.x = planeTriangle.Vertex[0].x; 00395 pointOnPlane.y = planeTriangle.Vertex[0].y; 00396 pointOnPlane.z = planeTriangle.Vertex[0].z; 00397 00398 // get the splitting planes normal 00399 edge1.x = planeTriangle.Vertex[1].x - planeTriangle.Vertex[0].x; 00400 edge1.y = planeTriangle.Vertex[1].y - planeTriangle.Vertex[0].y; 00401 edge1.z = planeTriangle.Vertex[1].z - planeTriangle.Vertex[0].z; 00402 edge2.x = planeTriangle.Vertex[2].x - planeTriangle.Vertex[0].x; 00403 edge2.y = planeTriangle.Vertex[2].y - planeTriangle.Vertex[0].y; 00404 edge2.z = planeTriangle.Vertex[2].z - planeTriangle.Vertex[0].z; 00405 planeNormal = CrossVector(edge1, edge2); 00406 00407 // get the normal of the triangle to split 00408 edge1.x = triangleToSplit.Vertex[1].x - triangleToSplit.Vertex[0].x; 00409 edge1.y = triangleToSplit.Vertex[1].y - triangleToSplit.Vertex[0].y; 00410 edge1.z = triangleToSplit.Vertex[1].z - triangleToSplit.Vertex[0].z; 00411 edge2.x = triangleToSplit.Vertex[2].x - triangleToSplit.Vertex[0].x; 00412 edge2.y = triangleToSplit.Vertex[2].y - triangleToSplit.Vertex[0].y; 00413 edge2.z = triangleToSplit.Vertex[2].z - triangleToSplit.Vertex[0].z; 00414 polysNormal = CrossVector(edge1, edge2); 00415 00416 // check if the triangle lies on the plane 00417 for (int loop = 0; loop < 3; loop++) 00418 { 00419 temp.x = triangleToSplit.Vertex[loop].x; 00420 temp.y = triangleToSplit.Vertex[loop].y; 00421 temp.z = triangleToSplit.Vertex[loop].z; 00422 if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0) 00423 count++; 00424 else 00425 break; 00426 } 00427 if (count == 3) 00428 { 00429 if (ClassifyPoint(polysNormal, pointOnPlane, planeNormal) == 1) 00430 return Front; 00431 if (ClassifyPoint(polysNormal, pointOnPlane, planeNormal) == -1) 00432 return Back; 00433 } 00434 00435 // find if all of the points are infront of or behind the plane 00436 int frontcount = 0, backcount = 0; 00437 for (int loop = 0; loop < 3; loop++) 00438 { 00439 temp.x = triangleToSplit.Vertex[loop].x; 00440 temp.y = triangleToSplit.Vertex[loop].y; 00441 temp.z = triangleToSplit.Vertex[loop].z; 00442 if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 0) 00443 { 00444 frontcount++; 00445 backcount++; 00446 } 00447 else if (ClassifyPoint(temp, pointOnPlane, planeNormal) == 1) 00448 frontcount++; 00449 else if (ClassifyPoint(temp, pointOnPlane, planeNormal) == -1) 00450 backcount++; 00451 } 00452 if (frontcount == 3) 00453 return Front; 00454 if (backcount == 3) 00455 return Back; 00456 00457 // try to split the triangle 00458 ptA = triangleToSplit.Vertex[2]; 00459 temp.x = ptA.x; 00460 temp.y = ptA.y; 00461 temp.z = ptA.z; 00462 sideA = ClassifyPoint(temp, pointOnPlane, planeNormal); 00463 for (int i = -1; ++i < 3;) 00464 { 00465 ptB = triangleToSplit.Vertex[i]; 00466 temp.x = ptB.x; 00467 temp.y = ptB.y; 00468 temp.z = ptB.z; 00469 sideB = ClassifyPoint(temp, pointOnPlane, planeNormal); 00470 if (sideB > 0) 00471 { 00472 if (sideA < 0) 00473 { 00474 // find intersection 00475 edge1.x = ptA.x; 00476 edge1.y = ptA.y; 00477 edge1.z = ptA.z; 00478 edge2.x = ptB.x; 00479 edge2.y = ptB.y; 00480 edge2.z = ptB.z; 00481 00482 temp = GetEdgeIntersection(edge1, edge2, planeTriangle); 00483 intersection.x = temp.x; 00484 intersection.y = temp.y; 00485 intersection.z = temp.z; 00486 00487 // find the new texture coordinates 00488 texvert1.x = ptB.x - ptA.x; 00489 texvert1.y = ptB.y - ptA.y; 00490 texvert1.z = ptB.z - ptA.z; 00491 texvert2.x = intersection.x - ptA.x; 00492 texvert2.y = intersection.y - ptA.y; 00493 texvert2.z = intersection.z - ptA.z; 00494 texvert1.u = ptA.u; 00495 texvert2.u = ptB.u; 00496 texvert1.v = ptA.v; 00497 texvert2.v = ptB.v; 00498 t1.x = texvert1.x; 00499 t1.y = texvert1.y; 00500 t1.z = texvert1.z; 00501 t2.x = texvert2.x; 00502 t2.y = texvert2.y; 00503 t2.z = texvert2.z; 00504 scale = sqrt(t2.x*t2.x+t2.y*t2.y+t2.z*t2.z)/sqrt(t1.x*t1.x+t1.y*t1.y+t1.z*t1.z); 00505 intersection.u = texvert1.u + (texvert2.u - texvert1.u) * scale; 00506 intersection.v = texvert1.v + (texvert2.v - texvert1.v) * scale; 00507 00508 outpts[out_c++] = inpts[in_c++] = intersection; 00509 } 00510 inpts[in_c++] = ptB; 00511 } 00512 else if (sideB < 0) 00513 { 00514 if (sideA > 0) 00515 { 00516 // find intersection 00517 edge1.x = ptA.x; 00518 edge1.y = ptA.y; 00519 edge1.z = ptA.z; 00520 edge2.x = ptB.x; 00521 edge2.y = ptB.y; 00522 edge2.z = ptB.z; 00523 00524 temp = GetEdgeIntersection(edge1, edge2, planeTriangle); 00525 intersection.x = temp.x; 00526 intersection.y = temp.y; 00527 intersection.z = temp.z; 00528 00529 // find the new texture coordinates 00530 texvert1.x = ptB.x - ptA.x; 00531 texvert1.y = ptB.y - ptA.y; 00532 texvert1.z = ptB.z - ptA.z; 00533 texvert2.x = intersection.x - ptA.x; 00534 texvert2.y = intersection.y - ptA.y; 00535 texvert2.z = intersection.z - ptA.z; 00536 texvert1.u = ptA.u; 00537 texvert2.u = ptB.u; 00538 texvert1.v = ptA.v; 00539 texvert2.v = ptB.v; 00540 t1.x = texvert1.x; 00541 t1.y = texvert1.y; 00542 t1.z = texvert1.z; 00543 t2.x = texvert2.x; 00544 t2.y = texvert2.y; 00545 t2.z = texvert2.z; 00546 scale = sqrt(t2.x*t2.x+t2.y*t2.y+t2.z*t2.z)/sqrt(t1.x*t1.x+t1.y*t1.y+t1.z*t1.z); 00547 intersection.u = texvert1.u + (texvert2.u - texvert1.u) * scale; 00548 intersection.v = texvert1.v + (texvert2.v - texvert1.v) * scale; 00549 00550 outpts[out_c++] = inpts[in_c++] = intersection; 00551 } 00552 outpts[out_c++] = ptB; 00553 } 00554 else 00555 outpts[out_c++] = inpts[in_c++] = ptB; 00556 ptA = ptB; 00557 sideA = sideB; 00558 } 00559 00560 if (in_c == 4) // two triangles are infront, one behind 00561 { 00562 outputFlag = TwoFrontOneBack; 00563 if (triangles) 00564 { 00565 triangles[0].Vertex[0] = inpts[0]; 00566 triangles[0].Vertex[1] = inpts[1]; 00567 triangles[0].Vertex[2] = inpts[2]; 00568 triangles[0].numVertices = 3; 00569 triangles[0].SetNormal(); 00570 triangles[1].Vertex[0] = inpts[0]; 00571 triangles[1].Vertex[1] = inpts[2]; 00572 triangles[1].Vertex[2] = inpts[3]; 00573 triangles[1].numVertices = 3; 00574 triangles[1].SetNormal(); 00575 triangles[2].Vertex[0] = outpts[0]; 00576 triangles[2].Vertex[1] = outpts[1]; 00577 triangles[2].Vertex[2] = outpts[2]; 00578 triangles[2].numVertices = 3; 00579 triangles[2].SetNormal(); 00580 } 00581 } 00582 else if (out_c == 4) // one triangle is infront, two behind 00583 { 00584 outputFlag = OneFrontTwoBack; 00585 if (triangles) 00586 { 00587 triangles[0].Vertex[0] = inpts[0]; 00588 triangles[0].Vertex[1] = inpts[1]; 00589 triangles[0].Vertex[2] = inpts[2]; 00590 triangles[0].numVertices = 3; 00591 triangles[0].SetNormal(); 00592 triangles[1].Vertex[0] = outpts[0]; 00593 triangles[1].Vertex[1] = outpts[1]; 00594 triangles[1].Vertex[2] = outpts[2]; 00595 triangles[1].numVertices = 3; 00596 triangles[1].SetNormal(); 00597 triangles[2].Vertex[0] = outpts[0]; 00598 triangles[2].Vertex[1] = outpts[2]; 00599 triangles[2].Vertex[2] = outpts[3]; 00600 triangles[2].numVertices = 3; 00601 triangles[2].SetNormal(); 00602 } 00603 } 00604 else if (in_c == 3 && out_c == 3) // plane bisects the triangle 00605 { 00606 outputFlag = OneFrontOneBack; 00607 if (triangles) 00608 { 00609 triangles[0].Vertex[0] = inpts[0]; 00610 triangles[0].Vertex[1] = inpts[1]; 00611 triangles[0].Vertex[2] = inpts[2]; 00612 triangles[0].numVertices = 3; 00613 triangles[0].SetNormal(); 00614 triangles[1].Vertex[0] = outpts[0]; 00615 triangles[1].Vertex[1] = outpts[1]; 00616 triangles[1].Vertex[2] = outpts[2]; 00617 triangles[1].numVertices = 3; 00618 triangles[1].SetNormal(); 00619 } 00620 } 00621 else // then triangle must be totally infront of or behind the plane 00622 { 00623 int side; 00624 00625 for (int loop = 0; loop < 3; loop++) 00626 { 00627 temp.x = triangleToSplit.Vertex[loop].x; 00628 temp.y = triangleToSplit.Vertex[loop].y; 00629 temp.z = triangleToSplit.Vertex[loop].z; 00630 side = ClassifyPoint(temp, pointOnPlane, planeNormal); 00631 if (side == 1) 00632 { 00633 outputFlag = Front; 00634 break; 00635 } 00636 else if (side == -1) 00637 { 00638 outputFlag = Back; 00639 break; 00640 } 00641 } 00642 } 00643 return outputFlag; 00644 } |