use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class ImageBasedHeightMap method load.
public boolean load(boolean flipX, boolean flipY) {
int imageWidth = colorImage.getWidth();
int imageHeight = colorImage.getHeight();
if (imageWidth != imageHeight)
throw new RuntimeException("imageWidth: " + imageWidth + " != imageHeight: " + imageHeight);
size = imageWidth;
ImageRaster raster = getImageRaster();
heightData = new float[(imageWidth * imageHeight)];
ColorRGBA colorStore = new ColorRGBA();
int index = 0;
if (flipY) {
for (int h = 0; h < imageHeight; ++h) {
if (flipX) {
for (int w = imageWidth - 1; w >= 0; --w) {
//int baseIndex = (h * imageWidth)+ w;
//heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
}
} else {
for (int w = 0; w < imageWidth; ++w) {
//int baseIndex = (h * imageWidth)+ w;
//heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
}
}
}
} else {
for (int h = imageHeight - 1; h >= 0; --h) {
if (flipX) {
for (int w = imageWidth - 1; w >= 0; --w) {
//int baseIndex = (h * imageWidth)+ w;
//heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
}
} else {
for (int w = 0; w < imageWidth; ++w) {
//int baseIndex = (h * imageWidth)+ w;
//heightData[index++] = getHeightAtPostion(raster, baseIndex, colorStore)*heightScale;
heightData[index++] = calculateHeight(raster.getPixel(w, h, colorStore)) * heightScale * backwardsCompScale;
}
}
}
}
return true;
}
use of com.jme3.math.ColorRGBA 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);
}
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class StaticPassLightingLogic method updateLightListUniforms.
private void updateLightListUniforms(Matrix4f viewMatrix, Shader shader, LightList lights) {
Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, true, ambientLightColor));
Uniform lightData = shader.getUniform("g_LightData");
int totalSize = tempDirLights.size() * 2 + tempPointLights.size() * 2 + tempSpotLights.size() * 3;
lightData.setVector4Length(totalSize);
int index = 0;
for (DirectionalLight light : tempDirLights) {
ColorRGBA color = light.getColor();
tempDirection.set(light.getDirection());
transformDirection(viewMatrix, tempDirection);
lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, 1f, index++);
}
for (PointLight light : tempPointLights) {
ColorRGBA color = light.getColor();
tempPosition.set(light.getPosition());
float invRadius = light.getInvRadius();
transformPosition(viewMatrix, tempPosition);
lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRadius, index++);
}
for (SpotLight light : tempSpotLights) {
ColorRGBA color = light.getColor();
Vector3f pos = light.getPosition();
Vector3f dir = light.getDirection();
tempPosition.set(light.getPosition());
tempDirection.set(light.getDirection());
transformPosition(viewMatrix, tempPosition);
transformDirection(viewMatrix, tempDirection);
float invRange = light.getInvSpotRange();
float spotAngleCos = light.getPackedAngleCos();
lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRange, index++);
lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, spotAngleCos, index++);
}
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class RollingTheMonkey method simpleInitApp.
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
cam.setLocation(new Vector3f(0.0f, 12.0f, 21.0f));
viewPort.setBackgroundColor(new ColorRGBA(0.2118f, 0.0824f, 0.6549f, 1.0f));
// init physics
BulletAppState bulletState = new BulletAppState();
stateManager.attach(bulletState);
space = bulletState.getPhysicsSpace();
space.addCollisionListener(this);
// create light
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.7f, -0.3f, -0.5f)).normalizeLocal());
System.out.println("Here We Go: " + sun.getDirection());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
// create materials
Material materialRed = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialRed.setBoolean("UseMaterialColors", true);
materialRed.setBoolean("HardwareShadows", true);
materialRed.setColor("Diffuse", new ColorRGBA(0.9451f, 0.0078f, 0.0314f, 1.0f));
materialRed.setColor("Specular", ColorRGBA.White);
materialRed.setFloat("Shininess", 64.0f);
Material materialGreen = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialGreen.setBoolean("UseMaterialColors", true);
materialGreen.setBoolean("HardwareShadows", true);
materialGreen.setColor("Diffuse", new ColorRGBA(0.0431f, 0.7725f, 0.0078f, 1.0f));
materialGreen.setColor("Specular", ColorRGBA.White);
materialGreen.setFloat("Shininess", 64.0f);
Material logoMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
logoMaterial.setBoolean("UseMaterialColors", true);
logoMaterial.setBoolean("HardwareShadows", true);
logoMaterial.setTexture("DiffuseMap", assetManager.loadTexture("com/jme3/app/Monkey.png"));
logoMaterial.setColor("Diffuse", ColorRGBA.White);
logoMaterial.setColor("Specular", ColorRGBA.White);
logoMaterial.setFloat("Shininess", 32.0f);
Material materialYellow = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
materialYellow.setBoolean("UseMaterialColors", true);
materialYellow.setBoolean("HardwareShadows", true);
materialYellow.setColor("Diffuse", new ColorRGBA(0.9529f, 0.7843f, 0.0078f, 1.0f));
materialYellow.setColor("Specular", ColorRGBA.White);
materialYellow.setFloat("Shininess", 64.0f);
// create level spatial
// TODO: create your own level mesh
Node level = new Node("level");
level.setShadowMode(ShadowMode.CastAndReceive);
Geometry floor = new Geometry("floor", new Box(22.0f, 0.5f, 22.0f));
floor.setShadowMode(ShadowMode.Receive);
floor.setLocalTranslation(0.0f, -0.5f, 0.0f);
floor.setMaterial(materialGreen);
Geometry wallNorth = new Geometry("wallNorth", new Box(22.0f, 2.0f, 0.5f));
wallNorth.setLocalTranslation(0.0f, 2.0f, 21.5f);
wallNorth.setMaterial(materialRed);
Geometry wallSouth = new Geometry("wallSouth", new Box(22.0f, 2.0f, 0.5f));
wallSouth.setLocalTranslation(0.0f, 2.0f, -21.5f);
wallSouth.setMaterial(materialRed);
Geometry wallEast = new Geometry("wallEast", new Box(0.5f, 2.0f, 21.0f));
wallEast.setLocalTranslation(-21.5f, 2.0f, 0.0f);
wallEast.setMaterial(materialRed);
Geometry wallWest = new Geometry("wallWest", new Box(0.5f, 2.0f, 21.0f));
wallWest.setLocalTranslation(21.5f, 2.0f, 0.0f);
wallWest.setMaterial(materialRed);
level.attachChild(floor);
level.attachChild(wallNorth);
level.attachChild(wallSouth);
level.attachChild(wallEast);
level.attachChild(wallWest);
// The easy way: level.addControl(new RigidBodyControl(0));
// create level Shape
CompoundCollisionShape levelShape = new CompoundCollisionShape();
BoxCollisionShape floorShape = new BoxCollisionShape(new Vector3f(22.0f, 0.5f, 22.0f));
BoxCollisionShape wallNorthShape = new BoxCollisionShape(new Vector3f(22.0f, 2.0f, 0.5f));
BoxCollisionShape wallSouthShape = new BoxCollisionShape(new Vector3f(22.0f, 2.0f, 0.5f));
BoxCollisionShape wallEastShape = new BoxCollisionShape(new Vector3f(0.5f, 2.0f, 21.0f));
BoxCollisionShape wallWestShape = new BoxCollisionShape(new Vector3f(0.5f, 2.0f, 21.0f));
levelShape.addChildShape(floorShape, new Vector3f(0.0f, -0.5f, 0.0f));
levelShape.addChildShape(wallNorthShape, new Vector3f(0.0f, 2.0f, -21.5f));
levelShape.addChildShape(wallSouthShape, new Vector3f(0.0f, 2.0f, 21.5f));
levelShape.addChildShape(wallEastShape, new Vector3f(-21.5f, 2.0f, 0.0f));
levelShape.addChildShape(wallEastShape, new Vector3f(21.5f, 2.0f, 0.0f));
level.addControl(new RigidBodyControl(levelShape, 0));
rootNode.attachChild(level);
space.addAll(level);
// create Pickups
// TODO: create your own pickUp mesh
// create single mesh for all pickups
// HINT: think particles.
pickUps = new Node("pickups");
Quaternion rotation = new Quaternion();
Vector3f translation = new Vector3f(0.0f, PICKUP_SIZE * 1.5f, -PICKUP_RADIUS);
int index = 0;
float ammount = FastMath.TWO_PI / PICKUP_COUNT;
for (float angle = 0; angle < FastMath.TWO_PI; angle += ammount) {
Geometry pickUp = new Geometry("pickUp" + (index++), new Box(PICKUP_SIZE, PICKUP_SIZE, PICKUP_SIZE));
pickUp.setShadowMode(ShadowMode.CastAndReceive);
pickUp.setMaterial(materialYellow);
pickUp.setLocalRotation(rotation.fromAngles(FastMath.rand.nextFloat() * FastMath.TWO_PI, FastMath.rand.nextFloat() * FastMath.TWO_PI, FastMath.rand.nextFloat() * FastMath.TWO_PI));
rotation.fromAngles(0.0f, angle, 0.0f);
rotation.mult(translation, pickUp.getLocalTranslation());
pickUps.attachChild(pickUp);
pickUp.addControl(new GhostControl(new SphereCollisionShape(PICKUP_SIZE)));
space.addAll(pickUp);
//space.addCollisionListener(pickUpControl);
}
rootNode.attachChild(pickUps);
// Create player
// TODO: create your own player mesh
Geometry playerGeometry = new Geometry("player", new Sphere(16, 32, PLAYER_RADIUS));
playerGeometry.setShadowMode(ShadowMode.CastAndReceive);
playerGeometry.setLocalTranslation(PLAYER_START.clone());
playerGeometry.setMaterial(logoMaterial);
// Store control for applying forces
// TODO: create your own player control
player = new RigidBodyControl(new SphereCollisionShape(PLAYER_RADIUS), PLAYER_MASS);
player.setRestitution(PLAYER_REST);
playerGeometry.addControl(player);
rootNode.attachChild(playerGeometry);
space.addAll(playerGeometry);
inputManager.addMapping(INPUT_MAPPING_FORWARD, new KeyTrigger(KeyInput.KEY_UP), new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(INPUT_MAPPING_BACKWARD, new KeyTrigger(KeyInput.KEY_DOWN), new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping(INPUT_MAPPING_LEFT, new KeyTrigger(KeyInput.KEY_LEFT), new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(INPUT_MAPPING_RIGHT, new KeyTrigger(KeyInput.KEY_RIGHT), new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(INPUT_MAPPING_RESET, new KeyTrigger(KeyInput.KEY_R));
inputManager.addListener(this, INPUT_MAPPING_FORWARD, INPUT_MAPPING_BACKWARD, INPUT_MAPPING_LEFT, INPUT_MAPPING_RIGHT, INPUT_MAPPING_RESET);
// init UI
infoText = new BitmapText(guiFont, false);
infoText.setText(INFO_MESSAGE);
guiNode.attachChild(infoText);
scoreText = new BitmapText(guiFont, false);
scoreText.setText("Score: 0");
guiNode.attachChild(scoreText);
messageText = new BitmapText(guiFont, false);
messageText.setText(MESSAGE);
messageText.setLocalScale(0.0f);
guiNode.attachChild(messageText);
infoText.setLocalTranslation(0.0f, cam.getHeight(), 0.0f);
scoreText.setLocalTranslation((cam.getWidth() - scoreText.getLineWidth()) / 2.0f, scoreText.getLineHeight(), 0.0f);
messageText.setLocalTranslation((cam.getWidth() - messageText.getLineWidth()) / 2.0f, (cam.getHeight() - messageText.getLineHeight()) / 2, 0.0f);
// init shadows
FilterPostProcessor processor = new FilterPostProcessor(assetManager);
DirectionalLightShadowFilter filter = new DirectionalLightShadowFilter(assetManager, 2048, 1);
filter.setLight(sun);
processor.addFilter(filter);
viewPort.addProcessor(processor);
}
use of com.jme3.math.ColorRGBA in project jmonkeyengine by jMonkeyEngine.
the class WorldOfInception method setupFog.
private void setupFog() {
// use fog to give more sense of depth
FilterPostProcessor fpp;
FogFilter fog;
fpp = new FilterPostProcessor(assetManager);
fog = new FogFilter();
fog.setFogColor(new ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f));
fog.setFogDistance(poiRadius);
fog.setFogDensity(2.0f);
fpp.addFilter(fog);
viewPort.addProcessor(fpp);
}
Aggregations