PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

a question re Voxel demo performance

As I'm working out a terrain routine for my next title I have been looking at existing examples including the Davilex landscape technology demo on ARM which I believe the Phantom was responsible for.

My question, if you can answer without giving away trade secrets, is this; how did you achieve the performance. ie: what factors most contribute to the speed & smoothness seen in the demo. Also is the terrain based on a height map?

My own terrain is generated ( as I need so many of them I can't utilise height maps ) & though the performance is close to acceptable on iPaq it does not come close to the voxel terrain demo. The water is very well done btw. I know that my code will be fine on xscale but I would like to push it further now. PocketGL was not really designed for large landscapes but as I have access to most of the source I'm free to dabble as required. note that I am using a 240 x 240 viewport. I think the Voxel demo is similar
the landscape demo in PTC 1.0.18 is amazingly fast even translated to PPC !!! really impressive, i suggest to have a look at it !
Conan · 30 Apr 2002, 13:50 UTC — What's PTC ?
Sorry to be thick here, what's PTC 1.0.18 ? Is it a conversion of a PC 3D project ? I'll have a search on the web for PTC
Conan:
You mention PocketGL... are you using a polygon based landscape system? I suspect that using a raster-style engine is going to be faster.

Search for a paper called:

A Terrain Rendering Method Using
Vertical Ray Coherence
by
Cheol-Hi Lee and Yeong Gil Shin

and see if that helps any.

Cheers,

Refractor
that's "Open PTC" which is a part needed with easyCE to run your PPC sources on a win32 plateform
i'll try to post here the code of the demo i translated straight for PPC (i just always have a few problems with bilinear color interpolation but should be solved soon)
huhu this is a rough lanscape i translated from Open PTC demo. that was a helicopter overview, i added the viewer height to give a "walktrough" effect. the fadeout has to be solved, and i'm not sure to get the best bilinear color calculation :/ anyway, it works and is fast (EasyCE 1.6 tested)

Code:

#include "easyce.h"
#include <math.h>
#include <stdlib.h>

PIXEL* surface;

#define SCREENWIDTH 240
#define SCREENHEIGHT 240

unsigned char HMap[256*256];
unsigned char CMap[256*256];
unsigned short pal[256];

int lasty[SCREENWIDTH], // Last pixel drawn on a given column
lastc[SCREENWIDTH]; // Color of last pixel on a column

int CosT [2048],
SinT [2048]; // Cosine and Sine tables
int FOV=256; // half of the xy field of view (This is based on the 0-2048 convention)

//
// Function prototypes
//
void ComputeMap(void);
int Clamp(int x);
void FLine(int x0,int y0,int x1,int y1,int hy,int s, int fadeout);
void View(int x0,int y0,int angle, int height);
void InitTables (void);
unsigned short rvb(unsigned char R, unsigned char V, unsigned char B);
int computeheight(int x0, int y0);

//
// Main function
//
void main()
{
surface = getbuffer();

unsigned short skycol;
skycol = rvb( 32, 0, 220 );

//
// Compute the height map
//
ComputeMap();
InitTables();

int k, x0, y0;
k=x0=y0=0;

int height=-200;
double angle=0;
double deltaAngle=0;
double deltaSpeed=80;
double CurrentSpeed=deltaSpeed*10;

// time scaling constant
const double scale = 20;

// create timer
resettimer();

// main loop
while (!clicked())
{

// needs a scrolling cloudy sky here
memset( surface, skycol, 240*220*2 );

// draw current landscape view
View(x0,y0,(int)angle,computeheight(x0, y0)-20);
// copy surface to console
update();

if ( buttonup() ) CurrentSpeed += deltaSpeed * scale;

if ( buttondown() ) CurrentSpeed -= deltaSpeed * scale;

if ( buttonleft() ) deltaAngle -= 0.1;

if ( buttonright() ) deltaAngle += 0.1;

if ( buttonA() )
{
CurrentSpeed = 0;
deltaAngle = 0;
}

//
// Update position/angle
//
angle += deltaAngle * scale;

int index = int(angle) & 0x7FF;
x0+=(int)(CurrentSpeed * CosT[index])>>8;
y0+=(int)(CurrentSpeed * SinT[index])>>8;
}

}

//
// Heightfield and colormap computation
//
void ComputeMap(void)
{
int p,i,j,k,k2,p2;


for (i=0; i<256; i++)
pal[i] = rvb( 0, i, 0);

//
// Start from a plasma clouds fractal
//
HMap[0]=128;
for ( p=256; p>1; p=p2 )
{
p2=p>>1;
k=p*8+20;
k2=k>>1;
for ( i=0; i<256; i+=p )
{
for ( j=0; j<256; j+=p )
{
int a,b,c,d;

a=HMap[(i<<8)+j];
b=HMap[(((i+p)&255)<<8)+j];
c=HMap[(i<<8)+((j+p)&255)];
d=HMap[(((i+p)&255)<<8)+((j+p)&255)];

HMap[(i<<8)+((j+p2)&255)]=
Clamp(((a+c)>>1)+(rand()%k-k2));
HMap[(((i+p2)&255)<<8)+((j+p2)&255)]=
Clamp(((a+b+c+d)>>2)+(rand()%k-k2));
HMap[(((i+p2)&255)<<8)+j]=
Clamp(((a+b)>>1)+(rand()%k-k2));
}
}
}

//
// Smoothing
//
for ( k=0; k<3; k++ )
{
for ( i=0; i<256*256; i+=256 )
{
for ( j=0; j<256; j++ )
{
HMap[i+j]=(HMap[((i+256)&0xFF00)+j]+HMap[i+((j+1)&0xFF)]+
HMap[((i-256)&0xFF00)+j]+HMap[i+((j-1)&0xFF)])>>2;
}
}
}

//
// Color computation (derivative of the height field)
//
for ( i=0; i<256*256; i+=256 )
{
for ( j=0; j<256; j++ )
{
k=128+(HMap[((i+256)&0xFF00)+((j+1)&255)]-HMap[i+j])*4;
if ( k<0 )
k=0;
if (k>255)
k=255;

CMap[i+j]=k;
}
}

}

//
// Reduces a value to 0..255 (used in height field computation)
//
int Clamp(int x)
{
return (x<0 ? 0 : (x>255 ? 255 : x));
}

//
// Draw the view from the point x0,y0 (16.16) looking at angle a
//
void View(int x0,int y0,int angle, int height)
{
//
// Initialize last-y and last-color arrays
//
for ( int d=0; d<SCREENWIDTH; d++ )
{
lasty[d]=SCREENHEIGHT;
lastc[d]=-1;
}

//
// Compute the xy coordinates; a and b will be the position inside the
// single map cell (0..255).
//
int u0=(x0>>16)&0xFF;
int a=(x0>>8)&0xFF;
int v0=((y0>>8)&0xFF00);
int b=(y0>>8)&0xFF;
int u1=(u0+1)&0xFF;
int v1=(v0+256)&0xFF00;

//
// Fetch the height at the four corners of the square the point is in
//
int h0=HMap[u0+v0];
int h1=HMap[u1+v0];
int h2=HMap[u0+v1];
int h3=HMap[u1+v1];

//
// Compute the height using bilinear interpolation
//
h0=(h0<<8)+a*(h1-h0);
h2=(h2<<8)+a*(h3-h2);
int h=((h0<<8)+b*(h2-h0))>>16;

//
// Draw the landscape from near to far without overdraw
//
for ( d=0; d<128; d+=1+(d>>6) )
FLine(x0+(d<<8)*CosT[(angle-FOV)&0x7FF],
y0+(d<<8)*SinT[(angle-FOV)&0x7FF],
x0+(d<<8)*CosT[(angle+FOV)&0x7FF],
y0+(d<<8)*SinT[(angle+FOV)&0x7FF],
height,(100<<8)/(d+1), d);
}

//
// Draw a "section" of the landscape; x0,y0 and x1,y1 and the xy coordinates
// on the height field, hy is the viewpoint height, s is the scaling factor
// for the distance. x0,y0,x1,y1 are 16.16 fixed point numbers and the
// scaling factor is a 16.8 fixed point value.
//
void FLine(int x0,int y0,int x1,int y1,int hy,int s, int fadeout)
{
// Compute xy speed
int sx=(x1-x0)/SCREENWIDTH;
int sy=(y1-y0)/SCREENWIDTH;

for ( int i=0; i<SCREENWIDTH; i++ )
{
//
// Compute the xy coordinates; a and b will be the position inside the
// single map cell (0..255).
//
int a=(x0>>8)&0xFF;
int b=(y0>>8)&0xFF;

int u0=(x0>>16)&0xFF;
int u1=(u0+1)&0xFF;
int v0=((y0>>8)&0xFF00);
int v1=(v0+256)&0xFF00;

//
// Fetch the height at the four corners of the square the point is in
//
int h0=HMap[u0+v0];
int h1=HMap[u1+v0];
int h2=HMap[u0+v1];
int h3=HMap[u1+v1];

//
// Compute the height using bilinear interpolation
//
h0=(h0<<8)+a*(h1-h0);
h2=(h2<<8)+a*(h3-h2);
int h=((h0<<8)+b*(h2-h0))>>16;

//
// Fetch the color at the centre of the square the point is in
//
h0=CMap[u0+v0];
h1=CMap[u1+v0];
h2=CMap[u0+v1];
h3=CMap[u1+v1];

//
// Compute the color using bilinear interpolation (in 16.16)
//
h0=(h0<<8)+a*(h1-h0);
h2=(h2<<8)+a*(h3-h2);
int c=((h0<<8)+b*(h2-h0));

//
// Compute screen height using the scaling factor
//
int y=(((h-hy)*s)>>11)+(SCREENHEIGHT>>1);

//
// Draw the column
//
if ( y<(a=lasty[i]) )
{
int coord_x = i;
int coord_y = a;
long sc,cc;

if ( lastc[i]==-1 )
lastc[i]=c;

sc=(c-lastc[i])/(a-y);
cc=lastc[i];

if ( a>(SCREENHEIGHT-1))
{
coord_y-=(a-(SCREENHEIGHT-1));
a=(SCREENHEIGHT-1);
}

if ( y<0 )
y=0;

while ( y<a )
{
unsigned char currentColor = ( cc>>16 );
plot( coord_x, coord_y, pal[currentColor]);
cc+=sc;
coord_y-=1;
a--;
}

lasty[i]=y;
}
lastc[i]=c;

//
// Advance to next xy position
//
x0+=sx; y0+=sy;
}
}

//
// Calculate the lookup tables
//
void InitTables (void)
{
for (int a = 0; a < 2048; a++)
{
//
// Precalculate cosine
//
float result = (float)(cos ((float)a * PI / 1024.0) * 256.0);

CosT [a] = (int)result;

//
// and sine
//
result = (float)(sin ((float)a * PI / 1024.0) * 256.0);
SinT [a] = (int)result;
}
}

// ********* returns a RRRRRGGGGGGBBBBB from a R, G, B, struct ***********
unsigned short rvb(unsigned char R, unsigned char V, unsigned char B)
{ return ((R>>3)<<11) + ((V>>2)<<5) + (B>>3); }


// ******** added to get a real walkthrough ****************
int computeheight(int x0, int y0)
{
int a=(x0>>8)&0xFF;
int b=(y0>>8)&0xFF;

int u0=(x0>>16)&0xFF;
int u1=(u0+1)&0xFF;
int v0=((y0>>8)&0xFF00);
int v1=(v0+256)&0xFF00;

//
// Fetch the height at the four corners of the square the point is in
//
int h0=HMap[u0+v0];
int h1=HMap[u1+v0];
int h2=HMap[u0+v1];
int h3=HMap[u1+v1];

//
// Compute the height using bilinear interpolation
//
h0=(h0<<8)+a*(h1-h0);
h2=(h2<<8)+a*(h3-h2);
int h=((h0<<8)+b*(h2-h0))>>16;

return( h );

}
[/code]
Conan · 30 Apr 2002, 19:19 UTC — thanks for the source & feedback
Thanks for posting your source Lewil, I will see how fast it runs compared to my source.

In answer to refractor, yes it's polygon based. The terrain height map is generated & I make a little mesh for each point. I'm sure I will find lots of improvements in the next few months. I will track down the article if I don't have it. I think I downloaded most anything to do with terrain but don't remember the title.

I need to use an existing 3D engine as my maths is very poor so I can't just change the system but I'll see have a look see.
I'm glad you found the PTC stuff, since I can't say much about the Voxel engine: I sold it. :)
Conan · 01 May 2002, 09:22 UTC — ok, understood
OK. No problemo. The main importance of the Voxel demo to me is that it points out that I can do better. My code needs to improve. So far since I got the first code working I have doubled the speed but it's still not as smooth as the Voxel demo. More work required.
Check out this doc I wrote a while back:
http://www.flipcode.com/portal/issue10.shtml
At the end a way of super fast bilerp is outlined.
Lewil · 02 May 2002, 16:17 UTC — Lewil
Phantom, about that page (thanks for the Bilp lesson... no words)

i found something strange here:
Code:

long addColors (long color1, long color2)
{ int red=(color1 & REDMASK) + (color2 & REDMASK);
int green=(color1 & GREENMASK) + (color2 & REDMASK);
int blue=(color1 & BLUEMASK) + (color2 & BLUEMASK);
if (red > REDMASK ) red = REDMASK;
if (green > GREENMASK) green = GREENMASK;
if (blue > BLUEMASK ) blue = BLUEMASK;
return (red+green+blue);
}


your green definition of color 2 looks "a little" red :') because of the mask set to REDMASK instead of GREENMASK :)

Code:

int green=(color1 & GREENMASK) + (color2 & GREENMASK);


should be good ?

a question: do u agree that we post here sample sources of Bilp using yout method ? ( so we can discuss about how newbies we are ? )