Ich hab begonnen eine einfache 3D Engine für ein eigenes Spiel zu schreiben. Mein Problem ist nun das ich mit meinem Algorithmus zum Füllen der Polygone auf dem Screen nur noch 2 fps erreiche. Wenn ich "fillPoly(...)" verwende werden daraus 250 fps. Nachfolgend der Code:
Der Teil der Alles auszubremsen scheint ist drawHLine(...) mit plotPixel(...). Wenn ich das einfach durch g.drawLine(...) ersetze erreiche ich immerhin noch 60 fps.
Meine Frage nun: Warum läuft das Prog so langsam? Gibts da bessere Möglichkeiten einzelne Pixel auf dem Bildschirm zu setzten?
Code:
public void drawCube(Graphics g, cube c)
{
if (c != null)
{
for (i=0; i<6; i++)
{
for (j=0; j<2; j++)
{
if (c.r[i].t[j].visible)
{
int arx[] = {
c.r[i].t[j].p[0].sx, // ScreenX
c.r[i].t[j].p[1].sx,
c.r[i].t[j].p[2].sx };
int ary[] = {
c.r[i].t[j].p[0].sy, //ScreenY
c.r[i].t[j].p[1].sy,
c.r[i].t[j].p[2].sy };
fillTriangle(g, arx, ary, c.r[i].color);
}
}
}
}
}
public void fillTriangle(Graphics g, int x[], int y[], Color color)
{
double incshort, inclong, xinc1, xinc2;
int i, j;
g.setColor(color);
for(i=0; i < 3; i++)
{
for(j=0; j < 3; j++)
{
if(y[i]<y[j])
{
int tmp=y[i];
y[i]=y[j];
y[j]=tmp;
tmp=x[i];
x[i]=x[j];
x[j]=tmp;
}
}
}
// if(y[2]>y[1]){
// swap(y, 2, 1);
// }else if(y[1]>y[0]){
// swap(y, 0, 2);
// }else if(y[0]>y[2]){
// swap(y, 0, 2);
// }
if(x[1]<x[0])
{
double dx=x[0]-x[1];
double dy=y[1]-y[0];
incshort = (dx/dy)*(-1);
}
else
{
double dx=x[1]-x[0];
double dy=y[1]-y[0];
incshort = dx/dy;
}
if(x[2]<x[0])
{
double dxlong=x[0]-x[2];
double dylong=y[2]-y[0];
inclong = (dxlong/dylong)*(-1);
}
else
{
double dxlong=x[2]-x[0];
double dylong=y[2]-y[0];
inclong = dxlong/dylong;
}
xinc1=x[0];
xinc2=x[0];
for(i=y[0]; i<=y[1]; i++)
{
g.drawLine((int)xinc1, i, (int)xinc2, i);
xinc1=xinc1+incshort;
xinc2=xinc2+inclong;
}
}
public void drawHLine(Graphics g, int xs, int ys, int xe, int ye, Color color)
{
int i, j;
// if(xs<xe)
// {
// for(i=xs; i<=xe; i++)
// {
// plotPixel(g, i, ys, color);
// }
// }
if(xs>xe)
{
for(i=xs; i>=xe; i--)
{
plotPixel(g, i, ys, color);
}
}
}
public void plotPixel(Graphics g, int x, int y, Color c)
{
g.drawLine(x, y, x, y);
}
Der Teil der Alles auszubremsen scheint ist drawHLine(...) mit plotPixel(...). Wenn ich das einfach durch g.drawLine(...) ersetze erreiche ich immerhin noch 60 fps.
Meine Frage nun: Warum läuft das Prog so langsam? Gibts da bessere Möglichkeiten einzelne Pixel auf dem Bildschirm zu setzten?