use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.
the class Torus method setGeometryData.
private void setGeometryData() {
// allocate vertices
int vertCount = (circleSamples + 1) * (radialSamples + 1);
FloatBuffer fpb = BufferUtils.createVector3Buffer(vertCount);
setBuffer(Type.Position, 3, fpb);
// allocate normals if requested
FloatBuffer fnb = BufferUtils.createVector3Buffer(vertCount);
setBuffer(Type.Normal, 3, fnb);
// allocate texture coordinates
FloatBuffer ftb = BufferUtils.createVector2Buffer(vertCount);
setBuffer(Type.TexCoord, 2, ftb);
// generate geometry
float inverseCircleSamples = 1.0f / circleSamples;
float inverseRadialSamples = 1.0f / radialSamples;
int i = 0;
// generate the cylinder itself
Vector3f radialAxis = new Vector3f(), torusMiddle = new Vector3f(), tempNormal = new Vector3f();
for (int circleCount = 0; circleCount < circleSamples; circleCount++) {
// compute center point on torus circle at specified angle
float circleFraction = circleCount * inverseCircleSamples;
float theta = FastMath.TWO_PI * circleFraction;
float cosTheta = FastMath.cos(theta);
float sinTheta = FastMath.sin(theta);
radialAxis.set(cosTheta, sinTheta, 0);
radialAxis.mult(outerRadius, torusMiddle);
// compute slice vertices with duplication at end point
int iSave = i;
for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
float radialFraction = radialCount * inverseRadialSamples;
// in [0,1)
float phi = FastMath.TWO_PI * radialFraction;
float cosPhi = FastMath.cos(phi);
float sinPhi = FastMath.sin(phi);
tempNormal.set(radialAxis).multLocal(cosPhi);
tempNormal.z += sinPhi;
fnb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
tempNormal.multLocal(innerRadius).addLocal(torusMiddle);
fpb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
ftb.put(radialFraction).put(circleFraction);
i++;
}
BufferUtils.copyInternalVector3(fpb, iSave, i);
BufferUtils.copyInternalVector3(fnb, iSave, i);
ftb.put(1.0f).put(circleFraction);
i++;
}
// duplicate the cylinder ends to form a torus
for (int iR = 0; iR <= radialSamples; iR++, i++) {
BufferUtils.copyInternalVector3(fpb, iR, i);
BufferUtils.copyInternalVector3(fnb, iR, i);
BufferUtils.copyInternalVector2(ftb, iR, i);
ftb.put(i * 2 + 1, 1.0f);
}
}
use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.
the class Sphere method setGeometryData.
/**
* builds the vertices based on the radius, radial and zSamples.
*/
private void setGeometryData() {
// allocate vertices
vertCount = (zSamples - 2) * (radialSamples + 1) + 2;
FloatBuffer posBuf = BufferUtils.createVector3Buffer(vertCount);
// allocate normals if requested
FloatBuffer normBuf = BufferUtils.createVector3Buffer(vertCount);
// allocate texture coordinates
FloatBuffer texBuf = BufferUtils.createVector2Buffer(vertCount);
setBuffer(Type.Position, 3, posBuf);
setBuffer(Type.Normal, 3, normBuf);
setBuffer(Type.TexCoord, 2, texBuf);
// generate geometry
float fInvRS = 1.0f / radialSamples;
float fZFactor = 2.0f / (zSamples - 1);
// Generate points on the unit circle to be used in computing the mesh
// points on a sphere slice.
float[] afSin = new float[(radialSamples + 1)];
float[] afCos = new float[(radialSamples + 1)];
for (int iR = 0; iR < radialSamples; iR++) {
float fAngle = FastMath.TWO_PI * fInvRS * iR;
afCos[iR] = FastMath.cos(fAngle);
afSin[iR] = FastMath.sin(fAngle);
}
afSin[radialSamples] = afSin[0];
afCos[radialSamples] = afCos[0];
TempVars vars = TempVars.get();
Vector3f tempVa = vars.vect1;
Vector3f tempVb = vars.vect2;
Vector3f tempVc = vars.vect3;
// generate the sphere itself
int i = 0;
for (int iZ = 1; iZ < (zSamples - 1); iZ++) {
// in (-pi/2, pi/2)
float fAFraction = FastMath.HALF_PI * (-1.0f + fZFactor * iZ);
float fZFraction;
if (useEvenSlices) {
// in (-1, 1)
fZFraction = -1.0f + fZFactor * iZ;
} else {
// in (-1,1)
fZFraction = FastMath.sin(fAFraction);
}
float fZ = radius * fZFraction;
// compute center of slice
Vector3f kSliceCenter = tempVb.set(Vector3f.ZERO);
kSliceCenter.z += fZ;
// compute radius of slice
float fSliceRadius = FastMath.sqrt(FastMath.abs(radius * radius - fZ * fZ));
// compute slice vertices with duplication at end point
Vector3f kNormal;
int iSave = i;
for (int iR = 0; iR < radialSamples; iR++) {
// in [0,1)
float fRadialFraction = iR * fInvRS;
Vector3f kRadial = tempVc.set(afCos[iR], afSin[iR], 0);
kRadial.mult(fSliceRadius, tempVa);
posBuf.put(kSliceCenter.x + tempVa.x).put(kSliceCenter.y + tempVa.y).put(kSliceCenter.z + tempVa.z);
BufferUtils.populateFromBuffer(tempVa, posBuf, i);
kNormal = tempVa;
kNormal.normalizeLocal();
if (// allow interior texture vs. exterior
!interior) {
normBuf.put(kNormal.x).put(kNormal.y).put(kNormal.z);
} else {
normBuf.put(-kNormal.x).put(-kNormal.y).put(-kNormal.z);
}
if (textureMode == TextureMode.Original) {
texBuf.put(fRadialFraction).put(0.5f * (fZFraction + 1.0f));
} else if (textureMode == TextureMode.Projected) {
texBuf.put(fRadialFraction).put(FastMath.INV_PI * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
} else if (textureMode == TextureMode.Polar) {
float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
float u = r * afCos[iR] + 0.5f;
float v = r * afSin[iR] + 0.5f;
texBuf.put(u).put(v);
}
i++;
}
BufferUtils.copyInternalVector3(posBuf, iSave, i);
BufferUtils.copyInternalVector3(normBuf, iSave, i);
if (textureMode == TextureMode.Original) {
texBuf.put(1.0f).put(0.5f * (fZFraction + 1.0f));
} else if (textureMode == TextureMode.Projected) {
texBuf.put(1.0f).put(FastMath.INV_PI * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
} else if (textureMode == TextureMode.Polar) {
float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
texBuf.put(r + 0.5f).put(0.5f);
}
i++;
}
vars.release();
// south pole
posBuf.position(i * 3);
posBuf.put(0f).put(0f).put(-radius);
normBuf.position(i * 3);
if (!interior) {
// allow for inner
normBuf.put(0).put(0).put(-1);
} else // texture orientation
// later.
{
normBuf.put(0).put(0).put(1);
}
texBuf.position(i * 2);
if (textureMode == TextureMode.Polar) {
texBuf.put(0.5f).put(0.5f);
} else {
texBuf.put(0.5f).put(0.0f);
}
i++;
// north pole
posBuf.put(0).put(0).put(radius);
if (!interior) {
normBuf.put(0).put(0).put(1);
} else {
normBuf.put(0).put(0).put(-1);
}
if (textureMode == TextureMode.Polar) {
texBuf.put(0.5f).put(0.5f);
} else {
texBuf.put(0.5f).put(1.0f);
}
updateBound();
}
use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.
the class LODGeomap method getGridTrianglesAtPoint.
/**
* Get the two triangles that make up the grid section at the specified point.
*
* For every grid space there are two triangles oriented like this:
* *----*
* |a / |
* | / b|
* *----*
* The corners of the mesh have differently oriented triangles. The two
* corners that we have to special-case are the top left and bottom right
* corners. They are oriented inversely:
* *----*
* | \ b|
* |a \ |
* *----*
*
* @param x local x coordinate
* @param z local z coordinate
* @return
*/
protected Triangle[] getGridTrianglesAtPoint(float x, float z) {
int gridX = (int) x;
int gridY = (int) z;
int index = findClosestHeightIndex(gridX, gridY);
if (index < 0) {
return null;
}
Triangle t = new Triangle(new Vector3f(), new Vector3f(), new Vector3f());
Triangle t2 = new Triangle(new Vector3f(), new Vector3f(), new Vector3f());
// top left
float h1 = hdata[index];
// top right
float h2 = hdata[index + 1];
// bottom left
float h3 = hdata[index + width];
// bottom right
float h4 = hdata[index + width + 1];
if ((gridX == 0 && gridY == 0) || (gridX == width - 2 && gridY == width - 2)) {
// top left or bottom right grid point
t.get(0).x = (gridX);
t.get(0).y = (h1);
t.get(0).z = (gridY);
t.get(1).x = (gridX);
t.get(1).y = (h3);
t.get(1).z = (gridY + 1);
t.get(2).x = (gridX + 1);
t.get(2).y = (h4);
t.get(2).z = (gridY + 1);
t2.get(0).x = (gridX);
t2.get(0).y = (h1);
t2.get(0).z = (gridY);
t2.get(1).x = (gridX + 1);
t2.get(1).y = (h4);
t2.get(1).z = (gridY + 1);
t2.get(2).x = (gridX + 1);
t2.get(2).y = (h2);
t2.get(2).z = (gridY);
} else {
// all other grid points
t.get(0).x = (gridX);
t.get(0).y = (h1);
t.get(0).z = (gridY);
t.get(1).x = (gridX);
t.get(1).y = (h3);
t.get(1).z = (gridY + 1);
t.get(2).x = (gridX + 1);
t.get(2).y = (h2);
t.get(2).z = (gridY);
t2.get(0).x = (gridX + 1);
t2.get(0).y = (h2);
t2.get(0).z = (gridY);
t2.get(1).x = (gridX);
t2.get(1).y = (h3);
t2.get(1).z = (gridY + 1);
t2.get(2).x = (gridX + 1);
t2.get(2).y = (h4);
t2.get(2).z = (gridY + 1);
}
return new Triangle[] { t, t2 };
}
use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.
the class LODGeomap method getTriangleAtPoint.
/**
* Get the triangle that the point is on.
*
* @param x coordinate in local space to the geomap
* @param z coordinate in local space to the geomap
* @return triangle in local space to the geomap
*/
protected Triangle getTriangleAtPoint(float x, float z) {
Triangle[] triangles = getGridTrianglesAtPoint(x, z);
if (triangles == null) {
//System.out.println("x,z: " + x + "," + z);
return null;
}
Vector2f point = new Vector2f(x, z);
Vector2f t1 = new Vector2f(triangles[0].get1().x, triangles[0].get1().z);
Vector2f t2 = new Vector2f(triangles[0].get2().x, triangles[0].get2().z);
Vector2f t3 = new Vector2f(triangles[0].get3().x, triangles[0].get3().z);
if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
return triangles[0];
}
t1.set(triangles[1].get1().x, triangles[1].get1().z);
t1.set(triangles[1].get2().x, triangles[1].get2().z);
t1.set(triangles[1].get3().x, triangles[1].get3().z);
if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
return triangles[1];
}
return null;
}
use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.
the class LODGeomap method getTriangleAtPoint.
/**
* Get a representation of the underlying triangle at the given point,
* translated to world coordinates.
*
* @param x local x coordinate
* @param z local z coordinate
* @return a triangle in world space not local space
*/
protected Triangle getTriangleAtPoint(float x, float z, Vector3f scale, Vector3f translation) {
Triangle tri = getTriangleAtPoint(x, z);
if (tri != null) {
tri.get1().multLocal(scale).addLocal(translation);
tri.get2().multLocal(scale).addLocal(translation);
tri.get3().multLocal(scale).addLocal(translation);
}
return tri;
}
Aggregations