@ -49,6 +49,7 @@ import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Type ;
import com.jme3.scene.VertexBuffer.Type ;
import com.jme3.util.BufferUtils ;
import com.jme3.util.BufferUtils ;
import com.jme3.util.TangentBinormalGenerator ;
import com.jme3.util.TangentBinormalGenerator ;
import com.jme3.util.TempVars ;
import java.io.IOException ;
import java.io.IOException ;
/ * *
/ * *
@ -69,8 +70,8 @@ public class LODGeomap extends GeoMap {
public LODGeomap ( ) {
public LODGeomap ( ) {
}
}
public LODGeomap ( int size , FloatBuffer heightMap ) {
public LODGeomap ( int size , float [ ] heightMap ) {
super ( heightMap , null , size , size , 1 ) ;
super ( heightMap , size , size , 1 ) ;
}
}
public Mesh createMesh ( Vector3f scale , Vector2f tcScale , Vector2f tcOffset , float offsetAmount , int totalSize , boolean center ) {
public Mesh createMesh ( Vector3f scale , Vector2f tcScale , Vector2f tcOffset , float offsetAmount , int totalSize , boolean center ) {
@ -98,10 +99,6 @@ public class LODGeomap extends GeoMap {
return m ;
return m ;
}
}
protected void removeNormalBuffer ( ) {
ndata = null ;
}
public FloatBuffer writeTexCoordArray ( FloatBuffer store , Vector2f offset , Vector2f scale , float offsetAmount , int totalSize ) {
public FloatBuffer writeTexCoordArray ( FloatBuffer store , Vector2f offset , Vector2f scale , float offsetAmount , int totalSize ) {
if ( store ! = null ) {
if ( store ! = null ) {
if ( store . remaining ( ) < getWidth ( ) * getHeight ( ) * 2 ) {
if ( store . remaining ( ) < getWidth ( ) * getHeight ( ) * 2 ) {
@ -762,94 +759,150 @@ public class LODGeomap extends GeoMap {
}
}
store . rewind ( ) ;
store . rewind ( ) ;
Vector3f rootPoint = new Vector3f ( ) ;
TempVars vars = TempVars . get ( ) ;
Vector3f rightPoint = new Vector3f ( ) ;
Vector3f leftPoint = new Vector3f ( ) ;
Vector3f rootPoint = vars . vect1 ;
Vector3f topPoint = new Vector3f ( ) ;
Vector3f rightPoint = vars . vect2 ;
Vector3f bottomPoint = new Vector3f ( ) ;
Vector3f leftPoint = vars . vect3 ;
Vector3f topPoint = vars . vect4 ;
Vector3f bottomPoint = vars . vect5 ;
Vector3f tmp1 = vars . vect6 ;
Vector3f tmp2 = vars . vect7 ;
// calculate normals for each polygon
// calculate normals for each polygon
for ( int r = 0 ; r < getHeight ( ) ; r + + ) {
for ( int r = 0 ; r < getHeight ( ) ; r + + ) {
for ( int c = 0 ; c < getWidth ( ) ; c + + ) {
for ( int c = 0 ; c < getWidth ( ) ; c + + ) {
rootPoint . set ( c , getValue ( c , r ) , r ) ;
rootPoint . set ( c , getValue ( c , r ) , r ) ;
Vector3f normal = new Vector3f ( ) ;
Vector3f normal = vars . vect8 ;
if ( r = = 0 ) { // first row
if ( r = = 0 ) { // first row
if ( c = = 0 ) { // first column
if ( c = = 0 ) { // first column
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
normal . set ( getNormal ( bottomPoint , rootPoint , rightPoint , scale ) ) ;
getNormal ( bottomPoint , rootPoint , rightPoint , scale , normal ) ;
normal . set ( Vector3f . UNIT_Y ) ;
} else if ( c = = getWidth ( ) - 1 ) { // last column
} else if ( c = = getWidth ( ) - 1 ) { // last column
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
normal . set ( getNormal ( leftPoint , rootPoint , bottomPoint , scale ) ) ;
getNormal ( leftPoint , rootPoint , bottomPoint , scale , normal ) ;
normal . set ( Vector3f . UNIT_Y ) ;
} else { // all middle columns
} else { // all middle columns
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
Vector3f n1 = getNormal ( leftPoint , rootPoint , bottomPoint , scale ) ;
Vector3f n2 = getNormal ( bottomPoint , rootPoint , rightPoint , scale ) ;
normal . set ( getNormal ( leftPoint , rootPoint , bottomPoint , scale , tmp1 ) ) ;
normal . set ( n1 . add ( n2 ) . normalizeLocal ( ) ) ;
normal . add ( getNormal ( bottomPoint , rootPoint , rightPoint , scale , tmp1 ) ) ;
normal . normalizeLocal ( ) ;
normal . set ( Vector3f . UNIT_Y ) ;
}
}
} else if ( r = = getHeight ( ) - 1 ) { // last row
} else if ( r = = getHeight ( ) - 1 ) { // last row
if ( c = = 0 ) { // first column
if ( c = = 0 ) { // first column
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
normal . set ( getNormal ( rightPoint , rootPoint , topPoint , scale ) ) ;
getNormal ( rightPoint , rootPoint , topPoint , scale , normal ) ;
normal . set ( Vector3f . UNIT_Y ) ;
} else if ( c = = getWidth ( ) - 1 ) { // last column
} else if ( c = = getWidth ( ) - 1 ) { // last column
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
normal . set ( getNormal ( topPoint , rootPoint , leftPoint , scale ) ) ;
getNormal ( topPoint , rootPoint , leftPoint , scale , normal ) ;
normal . set ( Vector3f . UNIT_Y ) ;
} else { // all middle columns
} else { // all middle columns
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
Vector3f n1 = getNormal ( topPoint , rootPoint , leftPoint , scale ) ;
Vector3f n2 = getNormal ( rightPoint , rootPoint , topPoint , scale ) ;
normal . set ( getNormal ( topPoint , rootPoint , leftPoint , scale , tmp1 ) ) ;
normal . set ( n1 . add ( n2 ) . normalizeLocal ( ) ) ;
normal . add ( getNormal ( rightPoint , rootPoint , topPoint , scale , tmp1 ) ) ;
normal . normalizeLocal ( ) ;
normal . set ( Vector3f . UNIT_Y ) ;
}
}
} else { // all middle rows
} else { // all middle rows
if ( c = = 0 ) { // first column
if ( c = = 0 ) { // first column
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
Vector3f n1 = getNormal ( rightPoint , rootPoint , topPoint , scale ) ;
Vector3f n2 = getNormal ( bottomPoint , rootPoint , rightPoint , scale ) ;
normal . set ( getNormal ( rightPoint , rootPoint , topPoint , scale , tmp1 ) ) ;
normal . set ( n1 . add ( n2 ) . normalizeLocal ( ) ) ;
normal . add ( getNormal ( bottomPoint , rootPoint , rightPoint , scale , tmp1 ) ) ;
normal . normalizeLocal ( ) ;
normal . set ( Vector3f . UNIT_Y ) ;
} else if ( c = = getWidth ( ) - 1 ) { // last column
} else if ( c = = getWidth ( ) - 1 ) { // last column
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
Vector3f n1 = getNormal ( topPoint , rootPoint , leftPoint , scale ) ;
Vector3f n2 = getNormal ( leftPoint , rootPoint , bottomPoint , scale ) ;
normal . set ( getNormal ( topPoint , rootPoint , leftPoint , scale , tmp1 ) ) ;
normal . set ( n1 . add ( n2 ) . normalizeLocal ( ) ) ;
normal . add ( getNormal ( leftPoint , rootPoint , bottomPoint , scale , tmp1 ) ) ;
normal . normalizeLocal ( ) ;
normal . set ( Vector3f . UNIT_Y ) ;
} else { // all middle columns
} else { // all middle columns
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
topPoint . set ( c , getValue ( c , r - 1 ) , r - 1 ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
leftPoint . set ( c - 1 , getValue ( c - 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
rightPoint . set ( c + 1 , getValue ( c + 1 , r ) , r ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
bottomPoint . set ( c , getValue ( c , r + 1 ) , r + 1 ) ;
Vector3f n1 = getNormal ( topPoint , rootPoint , leftPoint , scale ) ;
Vector3f n2 = getNormal ( leftPoint , rootPoint , bottomPoint , scale ) ;
normal . set ( getNormal ( topPoint , rootPoint , leftPoint , scale , tmp1 ) ) ;
Vector3f n3 = getNormal ( bottomPoint , rootPoint , rightPoint , scale ) ;
normal . add ( getNormal ( leftPoint , rootPoint , bottomPoint , scale , tmp1 ) ) ;
Vector3f n4 = getNormal ( rightPoint , rootPoint , topPoint , scale ) ;
normal . add ( getNormal ( bottomPoint , rootPoint , rightPoint , scale , tmp1 ) ) ;
normal . set ( n1 . add ( n2 ) . add ( n3 ) . add ( n4 ) . normalizeLocal ( ) ) ;
normal . add ( getNormal ( rightPoint , rootPoint , topPoint , scale , tmp1 ) ) ;
normal . normalizeLocal ( ) ;
normal . set ( Vector3f . UNIT_Y ) ;
}
}
}
}
normal . set ( Vector3f . UNIT_Y ) ;
BufferUtils . setInBuffer ( normal , store , ( r * getWidth ( ) + c ) ) ; // save the normal
BufferUtils . setInBuffer ( normal , store , ( r * getWidth ( ) + c ) ) ; // save the normal
}
}
}
}
vars . release ( ) ;
return store ;
return store ;
}
}
private Vector3f getNormal ( Vector3f firstPoint , Vector3f rootPoint , Vector3f secondPoint , Vector3f scale ) {
private Vector3f getNormal ( Vector3f firstPoint , Vector3f rootPoint , Vector3f secondPoint , Vector3f scale , Vector3f store ) {
Vector3f normal = new Vector3f ( ) ;
// store.set(firstPoint).subtractLocal(rootPoint);
//scale = Vector3f.UNIT_XYZ;
// tmp.set(secondPoint).subtractLocal(rootPoint);
normal . set ( firstPoint . mult ( scale ) ) . subtractLocal ( rootPoint . mult ( scale ) ) . crossLocal ( secondPoint . mult ( scale ) . subtract ( rootPoint . mult ( scale ) ) ) . normalizeLocal ( ) ;
// store.crossLocal(tmp).mult(scale).normalizeLocal();
return normal ;
// store.set(
// firstPoint.mult(scale)).subtractLocal(rootPoint.mult(scale))
// .crossLocal(
// secondPoint.mult(scale).subtract(rootPoint.mult(scale))).normalizeLocal();
float x1 = firstPoint . x - rootPoint . x ;
float y1 = firstPoint . y - rootPoint . y ;
float z1 = firstPoint . z - rootPoint . z ;
float x2 = secondPoint . x - rootPoint . x ;
float y2 = secondPoint . y - rootPoint . y ;
float z2 = secondPoint . z - rootPoint . z ;
float x3 = ( y1 * z2 ) - ( z1 * y2 ) ;
float y3 = ( z1 * x2 ) - ( x1 * z2 ) ;
float z3 = ( x1 * y2 ) - ( y1 * x2 ) ;
x3 * = scale . x ;
y3 * = scale . y ;
z3 * = scale . z ;
float inv = 1 . 0f / FastMath . sqrt ( x3 * x3 + y3 * y3 + z3 * z3 ) ;
store . x = x3 * inv ;
store . y = y3 * inv ;
store . z = z3 * inv ;
return store ;
}
}
/ * *
/ * *
@ -953,10 +1006,10 @@ public class LODGeomap extends GeoMap {
Triangle t = new Triangle ( new Vector3f ( ) , new Vector3f ( ) , new Vector3f ( ) ) ;
Triangle t = new Triangle ( new Vector3f ( ) , new Vector3f ( ) , new Vector3f ( ) ) ;
Triangle t2 = new Triangle ( new Vector3f ( ) , new Vector3f ( ) , new Vector3f ( ) ) ;
Triangle t2 = new Triangle ( new Vector3f ( ) , new Vector3f ( ) , new Vector3f ( ) ) ;
float h1 = hdata . get ( index ) ; // top left
float h1 = hdata [ index ] ; // top left
float h2 = hdata . get ( index + 1 ) ; // top right
float h2 = hdata [ index + 1 ] ; // top right
float h3 = hdata . get ( index + width ) ; // bottom left
float h3 = hdata [ index + width ] ; // bottom left
float h4 = hdata . get ( index + width + 1 ) ; // bottom right
float h4 = hdata [ index + width + 1 ] ; // bottom right
if ( ( gridX = = 0 & & gridY = = 0 ) | | ( gridX = = width - 1 & & gridY = = width - 1 ) ) {
if ( ( gridX = = 0 & & gridY = = 0 ) | | ( gridX = = width - 1 & & gridY = = width - 1 ) ) {