home games dev
Lewpen.com»Research & Development»3D Graphics»Java 3D Engine»3D Panoramas»Test Java Cube Rendering

Test Java Cube Rendering

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

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;
        }

      }
    }
  }

Files

Applet
Source

Comments

Related Articles

Placing the viewpoint inside the cube and making the faces of the cube textured

Instead of rotating the cube the texture can be warped and the cube and view point can remain static

Sponsored Links

Toys & Games:

Doggie Doo
Nerf Vortex
Monster High
Lagoona Hydration Station
Milky Bunny
Moshling Tree House
Lego Ninja Go Fire Temple
Fireman Sam Pontypandy Rescue
Rock Elmo
Star Wars Ultimate Force Lightsaber

Games

The Dodge Game
Flatspace

2-Player Games:

Quake 2D
Meteora

Puzzle Games:

Mini Tetris
Sudoku Solver

Development

3D Graphics:

3D Graphics Articles
WebGL Examples
Flash 3D Engine
Java 3D Engine

Development:

Programming Articles
Animation Demos
Game Development Examples

Links

iBuddy Social Network
Local Legends Football
PHP Charts & Graphs
CubeLogix Studios