/*********************************************************************************************/
/*
*/
/* Java 3D Engine Basics Tutorials - Tut Render Order
*/
/* Auth: bkenwright@xbdev.net
*/
/*
*/
/*
*/
/*********************************************************************************************/
import
java.awt.image.*;
import
java.awt.*;
import
java.applet.*;
/*********************************************************************************************/
/*
*/
/* class Triangle
*/
/* We need a way of representing our triangles...so that we can
have a number of them */
/* and not have to deal with large numbers of arrays of values and
things. By putting */
/* them in a class each triangle is all nice and
tidy. */
/*
*/
/*********************************************************************************************/
class
Triangle
{
....// Commented out
}// End of class triangle
/*********************************************************************************************/
/*
*/
/* Program Entry
Point
*/
/*
*/
/*********************************************************************************************/
public class
render_order
extends
Applet
{
Image myImage;
Graphics offScreen;
int
mouse_x_left=0;
int
mouse_y_left=0;
int
mouse_x_right=0;
int
mouse_y_right=0;
int
m_NumTris = 2;
Triangle[] m_tri;
public
void
init()
{
Dimension
appletSize =
this.getSize();
myImage =
createImage(appletSize.width,appletSize.height);
offScreen
= myImage.getGraphics();
m_tri
=
new
Triangle[m_NumTris];
m_tri[0]
=
new
Triangle(-1.0f, -1.0f, -0.5f,
0.0f,
1.0f, -0.5f,
1.0f, -1.0f, -0.5f,
Color.red);
m_tri[1] =
new
Triangle(-1.0f, -1.0f, 0.5f,
0.0f,
1.0f, 0.5f,
1.0f, -1.0f, 0.5f,
Color.blue);
}//
End of init(..)
public
void
RenderTriangle(Triangle t)
{
ScreenPerspective( myImage,
t.tx0, t.ty0, t.tz0,
t.tx1, t.ty1, t.tz1,
t.tx2, t.ty2, t.tz2,
t.tc);
}//
End of
RenderTriangles(..)
void
SortRenderOrder(Triangle t[],
int
iNumTris)
{
// First lets generate an average z for each triangle
float
average_z;
for(
int
i=0; i<iNumTris; i++ )
{
average_z = t[i].tz0 + t[i].tz1 + t[i].tz2;
average_z /= 3.0f;
t[i].avg_z = average_z;
}//
End for loop
// Now lets do a bruit force sort, so each triangle is in
order
// of our average z
for(
int
outer=0; outer<iNumTris; outer++ )
{
for(
int
inner=0; inner<iNumTris; inner++ )
{
if(
t[outer].avg_z > t[inner].avg_z )
{
Triangle temp = t[outer];
t[outer] = t[inner];
t[inner] = temp;
}//
End if(..)
}//
End inner for loop
}//
End outer for loop
}//
SortRenderOrder(..)
void
RenderAllTriangles(Triangle t[],
int
iNumTris)
{
for(int
i=0; i<iNumTris; i++)
{
RenderTriangle( t[i] );
}//
End for loop
}//
End of RenderAllTriangles(..)
void
ResetAllCoords(Triangle t[],
int
iNumTris)
{
for(int
i=0; i<iNumTris; i++)
{
t[i].ResetCoords();
}//
End for loop
}//
End of ResetAllCoords(..)
float
m_AngleX = 0.0f;
float
m_AngleY = 0.0f;
float
m_AngleZ = 0.0f;
public
void
paint(Graphics g)
{
// Clear screen
offScreen.setColor(Color.white);
offScreen.fillRect(0,0,this.getSize().width,this.getSize().height);
Dimension
appletSize =
this.getSize();
int
width =
appletSize.width;
int
height =
appletSize.height;
ResetAllCoords( m_tri, m_NumTris );
//
Lets rotate one of the tiangles
m_tri[0].RotateY(m_AngleY);
m_tri[1].RotateY(m_AngleY);
m_AngleY += 0.2f;
m_tri[0].Translate(0.0f, 0.0f, 3.0f);
m_tri[1].Translate(0.0f, 0.0f, 3.0f);
SortRenderOrder( m_tri, m_NumTris );
RenderAllTriangles(
m_tri, m_NumTris );
g.drawImage(myImage,0,0,this);
}//
End of paint(..)
public
void
ScreenPerspective( Image image,
float
x0,
float
y0,
float
z0,
float
x1,
float
y1,
float
z1,
float
x2,
float
y2,
float
z2,
Color
c )
{
....//
Commented out
}//
End ScreenPerspective(...)
public
void
drawTriangle( Image image,
float
x0,
float
y0,
float
x1,
float
y1,
float
x2,
float
y2,
Color
c )
{
....// Commented out
}//End
of drawTriangle(..)
public
boolean
mouseDown(Event evt,
int
x,
int
y)
{
....// Commented out
}//
End of mouseDown(..)
}// End of our Applet |