use of com.jme3.scene.shape.Sphere 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.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class TestDirectionalLightShadow method loadScene.
public void loadScene() {
obj = new Spatial[2];
// Setup first view
mat = new Material[2];
mat[0] = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
mat[1] = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
mat[1].setBoolean("UseMaterialColors", true);
mat[1].setColor("Ambient", ColorRGBA.White);
mat[1].setColor("Diffuse", ColorRGBA.White.clone());
obj[0] = new Geometry("sphere", new Sphere(30, 30, 2));
obj[0].setShadowMode(ShadowMode.CastAndReceive);
obj[1] = new Geometry("cube", new Box(1.0f, 1.0f, 1.0f));
obj[1].setShadowMode(ShadowMode.CastAndReceive);
TangentBinormalGenerator.generate(obj[1]);
TangentBinormalGenerator.generate(obj[0]);
Spatial t = obj[0].clone(false);
t.setLocalScale(10f);
t.setMaterial(mat[1]);
rootNode.attachChild(t);
t.setLocalTranslation(0, 25, 0);
for (int i = 0; i < 60; i++) {
t = obj[FastMath.nextRandomInt(0, obj.length - 1)].clone(false);
t.setLocalScale(FastMath.nextRandomFloat() * 10f);
t.setMaterial(mat[FastMath.nextRandomInt(0, mat.length - 1)]);
rootNode.attachChild(t);
t.setLocalTranslation(FastMath.nextRandomFloat() * 200f, FastMath.nextRandomFloat() * 30f + 20, 30f * (i + 2f));
}
Box b = new Box(1000, 2, 1000);
b.scaleTextureCoordinates(new Vector2f(10, 10));
ground = new Geometry("soil", b);
ground.setLocalTranslation(0, 10, 550);
matGroundU = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matGroundU.setColor("Color", ColorRGBA.Green);
matGroundL = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
grass.setWrap(WrapMode.Repeat);
matGroundL.setTexture("DiffuseMap", grass);
ground.setMaterial(matGroundL);
ground.setShadowMode(ShadowMode.CastAndReceive);
rootNode.attachChild(ground);
l = new DirectionalLight();
//l.setDirection(new Vector3f(0.5973172f, -0.16583486f, 0.7846725f).normalizeLocal());
l.setDirection(new Vector3f(-1, -1, -1));
rootNode.addLight(l);
al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.02f));
rootNode.addLight(al);
Spatial sky = SkyFactory.createSky(assetManager, "Scenes/Beach/FullskiesSunset0068.dds", false);
sky.setLocalScale(350);
rootNode.attachChild(sky);
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class TestLightNode method simpleInitApp.
@Override
public void simpleInitApp() {
Torus torus = new Torus(10, 6, 1, 3);
// Torus torus = new Torus(50, 30, 1, 3);
Geometry g = new Geometry("Torus Geom", torus);
g.rotate(-FastMath.HALF_PI, 0, 0);
g.center();
// g.move(0, 1, 0);
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat.setFloat("Shininess", 32f);
mat.setBoolean("UseMaterialColors", true);
mat.setColor("Ambient", ColorRGBA.Black);
mat.setColor("Diffuse", ColorRGBA.White);
mat.setColor("Specular", ColorRGBA.White);
// mat.setBoolean("VertexLighting", true);
// mat.setBoolean("LowQuality", true);
g.setMaterial(mat);
rootNode.attachChild(g);
Geometry lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
movingNode = new Node("lightParentNode");
movingNode.attachChild(lightMdl);
rootNode.attachChild(movingNode);
PointLight pl = new PointLight();
pl.setColor(ColorRGBA.Green);
pl.setRadius(4f);
rootNode.addLight(pl);
LightNode lightNode = new LightNode("pointLight", pl);
movingNode.attachChild(lightNode);
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.Red);
dl.setDirection(new Vector3f(0, 1, 0));
rootNode.addLight(dl);
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class TestShadowsPerf method simpleInitApp.
@Override
public void simpleInitApp() {
Logger.getLogger("com.jme3").setLevel(Level.SEVERE);
flyCam.setMoveSpeed(50);
flyCam.setEnabled(false);
viewPort.setBackgroundColor(ColorRGBA.DarkGray);
cam.setLocation(new Vector3f(-53.952988f, 27.15874f, -32.875023f));
cam.setRotation(new Quaternion(0.1564309f, 0.6910534f, -0.15713608f, 0.6879555f));
// cam.setLocation(new Vector3f(53.64627f, 130.56f, -11.247704f));
// cam.setRotation(new Quaternion(-6.5737107E-4f, 0.76819664f, -0.64021313f, -7.886125E-4f));
////
cam.setFrustumFar(500);
mat = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
Box b = new Box(800, 1, 700);
b.scaleTextureCoordinates(new Vector2f(50, 50));
Geometry ground = new Geometry("ground", b);
ground.setMaterial(mat);
rootNode.attachChild(ground);
ground.setShadowMode(ShadowMode.Receive);
Sphere sphMesh = new Sphere(32, 32, 1);
sphMesh.setTextureMode(Sphere.TextureMode.Projected);
sphMesh.updateGeometry(32, 32, 1, false, false);
TangentBinormalGenerator.generate(sphMesh);
sphere = new Geometry("Rock Ball", sphMesh);
sphere.setLocalTranslation(0, 5, 0);
sphere.setMaterial(mat);
sphere.setShadowMode(ShadowMode.CastAndReceive);
rootNode.attachChild(sphere);
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
dl.setColor(ColorRGBA.White);
rootNode.addLight(dl);
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.7f));
rootNode.addLight(al);
//rootNode.setShadowMode(ShadowMode.CastAndReceive);
createballs();
final DirectionalLightShadowRenderer pssmRenderer = new DirectionalLightShadowRenderer(assetManager, 1024, 4);
viewPort.addProcessor(pssmRenderer);
//
// final PssmShadowFilter pssmRenderer = new PssmShadowFilter(assetManager, 1024, 4);
// FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
// fpp.addFilter(pssmRenderer);
// viewPort.addProcessor(fpp);
pssmRenderer.setLight(dl);
pssmRenderer.setLambda(0.55f);
pssmRenderer.setShadowIntensity(0.55f);
pssmRenderer.setShadowCompareMode(com.jme3.shadow.CompareMode.Software);
pssmRenderer.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
//pssmRenderer.displayDebug();
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("display") && isPressed) {
//pssmRenderer.debugFrustrums();
System.out.println("tetetetet");
}
if (name.equals("add") && isPressed) {
createballs();
}
}
}, "display", "add");
inputManager.addMapping("display", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("add", new KeyTrigger(KeyInput.KEY_RETURN));
}
use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.
the class TestSimpleLighting method simpleInitApp.
@Override
public void simpleInitApp() {
Geometry teapot = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
TangentBinormalGenerator.generate(teapot.getMesh(), true);
teapot.setLocalScale(2f);
Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
// mat.selectTechnique("GBuf");
mat.setFloat("Shininess", 25);
mat.setBoolean("UseMaterialColors", true);
cam.setLocation(new Vector3f(0.015041917f, 0.4572918f, 5.2874837f));
cam.setRotation(new Quaternion(-1.8875003E-4f, 0.99882424f, 0.04832061f, 0.0039016632f));
// mat.setTexture("ColorRamp", assetManager.loadTexture("Textures/ColorRamp/cloudy.png"));
//
// mat.setBoolean("VTangent", true);
// mat.setBoolean("Minnaert", true);
// mat.setBoolean("WardIso", true);
// mat.setBoolean("VertexLighting", true);
// mat.setBoolean("LowQuality", true);
// mat.setBoolean("HighQuality", true);
mat.setColor("Ambient", ColorRGBA.Black);
mat.setColor("Diffuse", ColorRGBA.Gray);
mat.setColor("Specular", ColorRGBA.Gray);
teapot.setMaterial(mat);
rootNode.attachChild(teapot);
lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
lightMdl.getMesh().setStatic();
rootNode.attachChild(lightMdl);
pl = new PointLight();
pl.setColor(ColorRGBA.White);
pl.setRadius(4f);
rootNode.addLight(pl);
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
dl.setColor(ColorRGBA.Green);
rootNode.addLight(dl);
MaterialDebugAppState debug = new MaterialDebugAppState();
debug.registerBinding("Common/ShaderLib/BlinnPhongLighting.glsllib", teapot);
stateManager.attach(debug);
setPauseOnLostFocus(false);
flyCam.setDragToRotate(true);
}
Aggregations