use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class TestChaseCamera method simpleInitApp.
public void simpleInitApp() {
// Load a teapot model
teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
Material mat_tea = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
teaGeom.setMaterial(mat_tea);
rootNode.attachChild(teaGeom);
// Load a floor model
Material mat_ground = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat_ground.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
Geometry ground = new Geometry("ground", new Quad(50, 50));
ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
ground.setLocalTranslation(-25, -1, 25);
ground.setMaterial(mat_ground);
rootNode.attachChild(ground);
// Disable the default first-person cam!
flyCam.setEnabled(false);
// Enable a chase cam
chaseCam = new ChaseCamera(cam, teaGeom, inputManager);
//Uncomment this to invert the camera's vertical rotation Axis
//chaseCam.setInvertVerticalAxis(true);
//Uncomment this to invert the camera's horizontal rotation Axis
//chaseCam.setInvertHorizontalAxis(true);
//Comment this to disable smooth camera motion
chaseCam.setSmoothMotion(true);
//Uncomment this to disable trailing of the camera
//WARNING, trailing only works with smooth motion enabled. It is true by default.
//chaseCam.setTrailingEnabled(false);
//Uncomment this to look 3 world units above the target
//chaseCam.setLookAtOffset(Vector3f.UNIT_Y.mult(3));
//Uncomment this to enable rotation when the middle mouse button is pressed (like Blender)
//WARNING : setting this trigger disable the rotation on right and left mouse button click
//chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
//Uncomment this to set mutiple triggers to enable rotation of the cam
//Here spade bar and middle mouse button
//chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE),new KeyTrigger(KeyInput.KEY_SPACE));
//registering inputs for target's movement
registerInput();
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class Spatial method rotate.
/**
* Rotates the spatial by the xAngle, yAngle and zAngle angles (in radians),
* (aka pitch, yaw, roll) in the local coordinate space.
*
* @return The spatial on which this method is called, e.g <code>this</code>.
*/
public Spatial rotate(float xAngle, float yAngle, float zAngle) {
TempVars vars = TempVars.get();
Quaternion q = vars.quat1;
q.fromAngles(xAngle, yAngle, zAngle);
rotate(q);
vars.release();
return this;
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class Spatial method lookAt.
/**
* <code>lookAt</code> is a convenience method for auto-setting the local
* rotation based on a position in world space and an up vector. It computes the rotation
* to transform the z-axis to point onto 'position' and the y-axis to 'up'.
* Unlike {@link Quaternion#lookAt(com.jme3.math.Vector3f, com.jme3.math.Vector3f) }
* this method takes a world position to look at and not a relative direction.
*
* Note : 28/01/2013 this method has been fixed as it was not taking into account the parent rotation.
* This was resulting in improper rotation when the spatial had rotated parent nodes.
* This method is intended to work in world space, so no matter what parent graph the
* spatial has, it will look at the given position in world space.
*
* @param position
* where to look at in terms of world coordinates
* @param upVector
* a vector indicating the (local) up direction. (typically {0,
* 1, 0} in jME.)
*/
public void lookAt(Vector3f position, Vector3f upVector) {
Vector3f worldTranslation = getWorldTranslation();
TempVars vars = TempVars.get();
Vector3f compVecA = vars.vect4;
compVecA.set(position).subtractLocal(worldTranslation);
getLocalRotation().lookAt(compVecA, upVector);
if (getParent() != null) {
Quaternion rot = vars.quat1;
rot = rot.set(parent.getWorldRotation()).inverseLocal().multLocal(getLocalRotation());
rot.normalizeLocal();
setLocalRotation(rot);
}
vars.release();
setTransformRefresh();
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class Spatial method rotateUpTo.
/**
* <code>rotateUpTo</code> is a utility function that alters the
* local rotation to point the Y axis in the direction given by newUp.
*
* @param newUp
* the up vector to use - assumed to be a unit vector.
*/
public void rotateUpTo(Vector3f newUp) {
TempVars vars = TempVars.get();
Vector3f compVecA = vars.vect1;
Quaternion q = vars.quat1;
// First figure out the current up vector.
Vector3f upY = compVecA.set(Vector3f.UNIT_Y);
Quaternion rot = localTransform.getRotation();
rot.multLocal(upY);
// get angle between vectors
float angle = upY.angleBetween(newUp);
// figure out rotation axis by taking cross product
Vector3f rotAxis = upY.crossLocal(newUp).normalizeLocal();
// Build a rotation quat and apply current local rotation.
q.fromAngleNormalAxis(angle, rotAxis);
q.mult(rot, rot);
vars.release();
setTransformRefresh();
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class MultiPassLightingLogic method render.
@Override
public void render(RenderManager renderManager, Shader shader, Geometry geometry, LightList lights, int lastTexUnit) {
Renderer r = renderManager.getRenderer();
Uniform lightDir = shader.getUniform("g_LightDirection");
Uniform lightColor = shader.getUniform("g_LightColor");
Uniform lightPos = shader.getUniform("g_LightPosition");
Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
boolean isFirstLight = true;
boolean isSecondLight = false;
getAmbientColor(lights, false, ambientLightColor);
for (int i = 0; i < lights.size(); i++) {
Light l = lights.get(i);
if (l instanceof AmbientLight) {
continue;
}
if (isFirstLight) {
// set ambient color for first light only
ambientColor.setValue(VarType.Vector4, ambientLightColor);
isFirstLight = false;
isSecondLight = true;
} else if (isSecondLight) {
ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
// apply additive blending for 2nd and future lights
r.applyRenderState(ADDITIVE_LIGHT);
isSecondLight = false;
}
TempVars vars = TempVars.get();
Quaternion tmpLightDirection = vars.quat1;
Quaternion tmpLightPosition = vars.quat2;
ColorRGBA tmpLightColor = vars.color;
Vector4f tmpVec = vars.vect4f1;
ColorRGBA color = l.getColor();
tmpLightColor.set(color);
tmpLightColor.a = l.getType().getId();
lightColor.setValue(VarType.Vector4, tmpLightColor);
switch(l.getType()) {
case Directional:
DirectionalLight dl = (DirectionalLight) l;
Vector3f dir = dl.getDirection();
//FIXME : there is an inconstency here due to backward
//compatibility of the lighting shader.
//The directional light direction is passed in the
//LightPosition uniform. The lighting shader needs to be
//reworked though in order to fix this.
tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
lightPos.setValue(VarType.Vector4, tmpLightPosition);
tmpLightDirection.set(0, 0, 0, 0);
lightDir.setValue(VarType.Vector4, tmpLightDirection);
break;
case Point:
PointLight pl = (PointLight) l;
Vector3f pos = pl.getPosition();
float invRadius = pl.getInvRadius();
tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
lightPos.setValue(VarType.Vector4, tmpLightPosition);
tmpLightDirection.set(0, 0, 0, 0);
lightDir.setValue(VarType.Vector4, tmpLightDirection);
break;
case Spot:
SpotLight sl = (SpotLight) l;
Vector3f pos2 = sl.getPosition();
Vector3f dir2 = sl.getDirection();
float invRange = sl.getInvSpotRange();
float spotAngleCos = sl.getPackedAngleCos();
tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
lightPos.setValue(VarType.Vector4, tmpLightPosition);
//We transform the spot direction in view space here to save 5 varying later in the lighting shader
//one vec4 less and a vec4 that becomes a vec3
//the downside is that spotAngleCos decoding happens now in the frag shader.
tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0);
renderManager.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos);
lightDir.setValue(VarType.Vector4, tmpLightDirection);
break;
case Probe:
break;
default:
throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
}
vars.release();
r.setShader(shader);
renderMeshFromGeometry(r, geometry);
}
if (isFirstLight) {
// Either there are no lights at all, or only ambient lights.
// Render a dummy "normal light" so we can see the ambient color.
ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, false, ambientLightColor));
lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha);
lightPos.setValue(VarType.Vector4, NULL_DIR_LIGHT);
r.setShader(shader);
renderMeshFromGeometry(r, geometry);
}
}
Aggregations