Test Java Cube Rendering

This is a barebones renderer for one object, with a software solid triangle rasteriser

Articles

Java applet, run by CheerpJ. Main class: House

Screen Buffer

The screen buffer is provided by tinyptc. It is an int[] linear RGB pixel array.

Matrices & Vectors

These come from IDX Graphics 3D engine.

Triangle Rendering

I render the triangles like this:

//---- tri_col

// Arguments are stored in 'this' // this.tri_x1 // this.tri_y1 // this.tri_x2 // this.tri_y2 // this.tri_x3 // this.tri_y3

public void tri_col(int col) {

float t;
int x, y, y1, y2, x1, x2, o, ti;
float c1, c2;
float dc;

//  Don't draw if backfacing

if((tri_x2-tri_x1)*(tri_y3-tri_y1) > (tri_x3-tri_x1)*(tri_y2-tri_y1)) return;

// Sort points

if(tri_y1 > tri_y2) { t=tri_y2; tri_y2=tri_y1; tri_y1=t;  t=tri_x2; tri_x2=tri_x1; tri_x1=t; }
if(tri_y2 > tri_y3) { t=tri_y3; tri_y3=tri_y2; tri_y2=t;  t=tri_x3; tri_x3=tri_x2; tri_x2=t; }
if(tri_y1 > tri_y2) { t=tri_y2; tri_y2=tri_y1; tri_y1=t;  t=tri_x2; tri_x2=tri_x1; tri_x1=t; }

// Clip top point to screen and pixel

y1 = (int)tri_y1; if(y1>=h) return; if(y1<0) y1 = 0;
y2 = (int)tri_y3; if(y2<0) return; if(y2>h) y2 = h;

c1=0; c2=0;

for(y=y1-1; y<y2+1; y++)
{
  if(y >= tri_y1 && y < tri_y3)
  {
    if(y >= tri_y1 && y < tri_y2)
    {
      x1 = (int)(tri_x1 + (tri_x3-tri_x1) * (y-tri_y1) / (tri_y3-tri_y1));
      x2 = (int)(tri_x1 + (tri_x2-tri_x1) * (y-tri_y1) / (tri_y2-tri_y1));
    }
    else
    {
      x1 = (int)(tri_x1 + (tri_x3-tri_x1) * (y-tri_y1) / (tri_y3-tri_y1));
      x2 = (int)(tri_x2 + (tri_x3-tri_x2) * (y-tri_y2) / (tri_y3-tri_y2));
    }
    if(x1 > x2)
    {
      ti = x2; x2 = x1; x1 = ti;
      t = c2; c2 = c1; c1 = t;
    }
    if(x1 < 0) x1 = 0;
    if(x2 > w) x2 = w;

    if(x2 > x1) {
      dc = (c2-c1)/(x2-x1);

      o = y*w + x1;

      for(x = x1; x < x2; x ++) buf[o++] = col;
    }

  }
}

}

Source 12 files