1. ## intersection

Hi

say I have two cube which I can move around the scene - how could I go about showing their resultant area when they intersect - either in a different colour or transparent - would I have to set up vectors, normals etc.. also say I have a small sphere which I want to light up only when it enters the area of the cube? how could you show that it is in side the cube?

cheers

2. ## Re: intersection

For displaying the intersecting volume. You just use the planes of each side of one cube to clip the sides of the other cube, keeping the part behind the clipping plane, and adding new faces where the clipping planes intersect the other cube. Then you just draw the intersecting volume, then each cube, with depth testing appropriately set so that the intersecting parts of the cubes don't overwrite the just drawn intersected volume.

3. ## Re: intersection

could you sample code? what about if i rotate one cube at 45 degrees then intersect them - also would this work with say cylinders?

thanx

4. ## Re: intersection

I don't have any code available, but the idea is rather straight forward. There is a method that can be implemented for two simple convex polyhedra. In this method you ignore the fact that you have cubes or cylinders or whatever as what only matters are the planes of each face, of each polyhedron. What you do is first intersect each plane with every other plane. This will produce a set of lines. Then you intersect each line with every other line. This will give you a set of points. Then clip the set of points by all of the planes, keeping only the points that are on or behind all of the planes. This final set of points are the vertices of the intersecting volume. Then intersect this final set of points by each plane, to pick only the points that are on that plane. If there is more than one point in the plane, then these points define an edge if it is two points and so can be ignored as it will be part of a face as well, and a face if it is more than two points. To create the face, the vertices must be connected in either ccw or cw order (whichever you are using). After this process is completed, you will have the set of faces for the intersecting volume (if any such volume exists).

[This message has been edited by DFrey (edited 01-11-2001).]

5. ## Re: intersection

Could you illustrate with the below:

I have three cubes of the same dimension drawn counter clockwise say 2 units apart if one is movedand made to intersect the second how could i show the resultant area

glPushMatrix();
//Top of cube
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.1776, 0.0);
glVertex3d( 0.363, 0.0575, -0.1225);//FRIGHT
glVertex3d( -0.363, 0.0575, -0.1225);//FLEFT
glVertex3d( -0.363, 0.0575, 0.1225);//NLEFT
glVertex3d( 0.363, 0.0575, 0.1225);//NRIGHT
glEnd();
glPopMatrix();

//Front face of cube
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, 0.0);
glVertex3d( 0.363, -0.0575, 0.1225);//top right corner
glVertex3d(-0.363, -0.0575, 0.1225); //top left corner
glVertex3d(-0.363, -0.0575,0.1225); //bottom left corner
glVertex3d( 0.363, -0.0575,0.1225); //bottom right corner
glEnd();
glPopMatrix();

//horizontal right face
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.02818, 0.0, 0.0);
glVertex3d( 0.363, 0.0575, -0.1225);//FTOP
glVertex3d( 0.363, 0.0575, 0.1225);//NTOP
glVertex3d( 0.363, -0.0575, 0.1225);//NBOTTOM
glVertex3d( 0.363, -0.0575, -0.1225);//FBOTTOM
glEnd();
glPopMatrix();

//Back face of cube
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, -0.083375);
glVertex3d( -0.363, 0.0575, -0.1225);//top left
glVertex3d( 0.363, 0.0575, -0.1225);//top right
glVertex3d( 0.363, -0.0575, -0.1225);//bottom right
glVertex3d( -0.363, -0.0575, -0.1225);//bottom left
glEnd();
glPopMatrix();

//horizontal left face
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( -0.028175, 0.0, 0.0);
glVertex3d( -0.363, 0.0575, 0.1225);//NTOP
glVertex3d( -0.363, 0.0575, -0.1225);//FTOP
glVertex3d( -0.363, -0.0575, -0.1225);//FBOTTOM
glVertex3d( -0.363, -0.0575, 0.1225);//NBOTTOM
glEnd();
glPopMatrix();

//Bottom face of cube
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
glBegin(GL_POLYGON);
glNormal3d( 0.0, -0.1776, 0.0);
glVertex3d( 0.363, -0.0575, 0.1225);//NRIGHT
glVertex3d( -0.363, -0.0575, 0.1225);//NLEFT
glVertex3d( -0.363, -0.0575, -0.1225);//FLEFT
glVertex3d( 0.363, -0.0575, -0.1225);//FRIGHT
glEnd();
glPopMatrix();
glPopMatrix();

6. ## Re: intersection

AS I think this belongs to CSG.
How does one make CSG stuff with UNconvex bodies. I found myself doing CSG with convex bodies, but the results tend to be UNconvex and I can't work any further with them. Is the trick to cut them down in convex parts again?

7. ## Re: intersection

fox, I'm getting the feeling you don't want to do all the math involved. In that case you can also highlight the intersected volume by using the stencil buffer. Though this method is only simple if 2 objects are intersecting and both are simple convex polyhedra. It goes something like this:

1. Disable depth writes
2. Enable stencil testing
3. Clear stencil buffer to all 0
4. Set stencil function to GL_ALWAYS, 0, 0
5. Set stencil operation to GL_INCR, GL_KEEP, GL_KEEP
6. Disable color buffer writes
7. Enable backface culling
8. Draw each cube (no texture or lighting needed)
9. Enable color buffer writes
10. Set stencil function GL_LESS, 2, 255
11. Set stencil operation to GL_KEEP, GL_KEEP, GL_KEEP
12*. Set renderstate for each cube and draw them
13. Set stencil function to GL_EQUAL, 2, 255
14. Set renderstate for intersection
15. Draw one cube
16. Disable stencil test

* Enabling depth writes is ok now too, but is of course dependent upon the result you want.

[This message has been edited by DFrey (edited 01-11-2001).]

8. ## Re: intersection

Thanx I'll try that out - yes I would want to avoid the maths otherwise it could get very messy but the other thing is i'm not quite sure how to go about it - would it involve vectors and matrices - also would it be similar to implementing collision detection?

cheers

[This message has been edited by fox (edited 01-11-2001).]

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•