/*********************************************************************************************/
/*
*/
/* Java 3D Engine Basics Tutorials - Tut Solid Triangle using
Perspective */
/* Auth: bkenwright@xbdev.net
*/
/* URL: www.xbdev.net
*/
/*
*/
/* Perspective - x, y and of course
Z */
/*
*/
/*********************************************************************************************/
import
java.awt.image.*;
import
java.awt.*;
import
java.applet.*;
public class
perspec
extends
Applet
{
public
void
paint(Graphics g)
{
float
z = 1.0f;
ScreenPerspective( g,
-1.0f, -1.0f, z,
// bottom left point
1.0f, -1.0f, z,
// bottom right point
0.0f, 1.0f, z,
// top middle point
Color.blue);
}//
End of paint(..)
public
void
ScreenPerspective( Graphics g,
float
x0,
float
y0,
float
z0,
float
x1,
float
y1,
float
z1,
float
x2,
float
y2,
float
z2,
Color c )
{
// Get the screen size of our
applet
Dimension appletSize =
this.getSize();
int
width =
appletSize.width;
int
height =
appletSize.height;
// Check our triangle isn't
behind us
if( (z0<1) && (z1<1) &&
(z2<1) )
return;
// Something worth noting - we
have a pole at z=0...as when we
// create a perspective ..which
is x_per = d*x/z for example, if
// z is 0 or very close to zero,
we'll get an infinit number...so
// if we have a z value less
than 1 we'll just round z to 1 and it
// will get clipped later down
the line.
// We've not done any checking
for values which cross our z=0 pole
// plane...and are in front and
behind the camera - we would need
// to do a clipping stage before
this one to prevent this.
// Add some clipping code so Z
is clipped to the near view plane
// ++ //
//
From x,y,z to x,y for the screen.
//
-1-Perspective Conversion
// Focul point of 1 (Normalised
view plane 90 degrees)
float
d = 1;
float
Perspective_x0 = -d*x0 / z0;
float
Perspective_x1 = -d*x1 / z1;
float
Perspective_x2 = -d*x2 / z2;
//
its between -1 to 1...so we need to scale it to 0 to 1...and scale to
//
the size of the screen.
Perspective_x0
+= 1;
Perspective_x1
+= 1;
Perspective_x2
+= 1;
//
So now its between 0 and 2 and not -1 to 1;
Perspective_x0
*= 0.5;
Perspective_x1
*= 0.5;
Perspective_x2
*= 0.5;
//
-2- Now we scale it to the size of the screen
float
Screen_x0 = (width-1)*Perspective_x0;
float
Screen_x1 = (width-1)*Perspective_x1;
float
Screen_x2 = (width-1)*Perspective_x2;
//
Add some clipping to make sure its on the screen here
//
++ //
//
Now we do the same for the y value to convert it
// to screen coordinates … its
exactly the same as for the x above,
// but I've combined all the
stages into a single line for each
// one
//
Note y1 is the top of square, and y2 is the bottom of our square
float
Screen_y0 = ((-d*y0 / z0)+1)*0.5f *(height-1);
float
Screen_y1 = ((-d*y1 / z1)+1)*0.5f *(height-1);
float
Screen_y2 = ((-d*y2 / z2)+1)*0.5f *(height-1);
System.out.println(
"x0: "
+ Screen_x0 +
" y0: "
+ Screen_y0 );
System.out.println(
"x1: "
+ Screen_x1 +
" y1: "
+ Screen_y1 );
System.out.println(
"x2: "
+ Screen_x2 +
" y2: "
+ Screen_y2 );
System.out.println("");
//
Render our data
SolidTriangleAPI( g,
Screen_x0, Screen_y0,
Screen_x1, Screen_y1,
Screen_x2, Screen_y2,
c );
}//
End ScreenPerspective(...)
void
SolidTriangleAPI( Graphics g,
float
x0,
float
y0,
float
x1,
float
y1,
float
x2,
float
y2,
Color c )
{
Polygon triangle =
new
Polygon();
triangle.addPoint((int)x0, (int)y0);
triangle.addPoint((int)x1, (int)y1);
triangle.addPoint((int)x2, (int)y2);
triangle.addPoint((int)x0, (int)y0);
g.setColor( c );
// Wireframe Mode
// g.drawPolygon(triangle);
// Solid Mode
g.fillPolygon(triangle);
}//
End of drawTriangleFlatAPI(..)
}// End of our Applet |