# LWJGL Forum

## Programming => General Java Game Development => Topic started by: xixi on November 09, 2010, 15:45:56

Title: only draw visible triangles (or models)
Post by: xixi on November 09, 2010, 15:45:56
Hello,

i was asking myself if lwjgl has something to calculate if my triangles that i draw are on screen or not

do anyone know this and if not, how would i calculate that, because my math is not so good.

Title: Re: only draw visible triangles (or models)
Post by: broumbroum on November 09, 2010, 19:59:01
This is something tough to code. It s part of shadow mapping. isn't it? I read about that in the red book, and NeHe tutorial could help.
Title: Re: only draw visible triangles (or models)
Post by: jediTofu on November 09, 2010, 20:03:01
For this, all you would need to do is see if all 3 corners of the triangle are off the screen.  But it gets complicated trying to figure out what the location of the corners are after rotation, scaling, etc.  Luckily, there is a method for this:  gluProject.  But this can be slow with a lot of triangles, so we're back to math to figure out the points of the 3 vertexes after rotation and scaling.

Personally, I'd just check a bounding box (or circle), as you don't need perfect collision because you're just testing if off screen.

//all >= 0
int x1 = centerTriangleX;
int y1 = centerTriangleY;
int x2 = farthestTriangleXLength;
int y2 = farthestTriangleYLength;

if((x1 + x2) < 0 || (x1 - x2) > width ||
(y1 + y2) < 0 || (y1 - y2) > height) {
//off screen
}
Title: Re: only draw visible triangles (or models)
Post by: broumbroum on November 09, 2010, 20:57:51
Ur calculations are generally not correct for ordinary projectional displays. Triangles from within the range depth will Show even at higher x,y coords.
Title: Re: only draw visible triangles (or models)
Post by: jediTofu on November 09, 2010, 22:42:44
All he said was triangles, so that's all I based my calculation on.  But yes, it would need to be adjusted for depth, but I'm not quite sure if he is even using depth (or rotation for that matter) based on his post...

Of course, the gluProject way would adjust for rotation, scaling, depth, etc.