Node createDebugGeometry() { Line l = new Line(from.getPlanet().getPosition(), to.getPlanet().getPosition()); line = new Geometry("Line #" + from.getPlanet().getID() + " to #" + to.getPlanet().getID(), l); Material material = new Material( SolarWarsApplication.getInstance().getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); Player p = from.getPlanet().getOwner(); ColorRGBA c; if (p == null) { c = ColorRGBA.White.clone(); c.a = 0.5f; material.setColor("Color", c); } else { c = p.getColor(); c.a = 0.5f; material.setColor("Color", c); } material.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); line.setMaterial(material); createLabel(); Node lineNode = new Node(line.getName() + "_Node"); lineNode.attachChild(line); lineNode.attachChild(label); // Vector3f pos = to.getPlanet().getPosition(). // subtract(from.getPlanet().getPosition()); // lineNode.setLocalTranslation(pos.mult(0.5f)); return lineNode; }
private Material getMaterial(ColorRGBA color) { Material mat = new Material(main.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); mat.setColor("Diffuse", color); ColorRGBA clone = color.clone(); clone.multLocal(0.8f); clone.a = 1f; mat.setColor("Ambient", clone); mat.setBoolean("UseMaterialColors", true); return mat; }
private ColorRGBA getAmbientColor(LightList lightList) { ambientLightColor.set(0, 0, 0, 1); for (int j = 0; j < lightList.size(); j++) { Light l = lightList.get(j); if (l instanceof AmbientLight) { ambientLightColor.addLocal(l.getColor()); } } ambientLightColor.a = 1.0f; return ambientLightColor; }
@Override public void simpleInitApp() { // add a random cube Box b = new Box(1, 1, 1); Geometry geom = new Geometry("Box", b); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.randomColor()); geom.setMaterial(mat); geom.move( (FastMath.nextRandomFloat() * 10) - 5, (FastMath.nextRandomFloat() * 10) - 5, (FastMath.nextRandomFloat() * -10)); rootNode.attachChild(geom); // add saved cubes String userHome = System.getProperty("user.home"); BinaryImporter importer = BinaryImporter.getInstance(); importer.setAssetManager(assetManager); try { File file = new File(userHome + "/mycoolgame/savedgame.j3o"); Node sceneNode = (Node) importer.load(file); sceneNode.setName("My restored node"); rootNode.attachChild(sceneNode); Logger.getLogger(SaveAndLoad.class.getName()).log(Level.INFO, "Success: Loaded saved node."); } catch (IOException ex) { Logger.getLogger(SaveAndLoad.class.getName()) .log(Level.INFO, "Warning: Could not load saved node.", ex); } }
protected void resetMaterial() { ambient.set(ColorRGBA.DarkGray); diffuse.set(ColorRGBA.LightGray); specular.set(ColorRGBA.Black); shininess = 16; disallowAmbient = false; disallowSpecular = false; shadeless = false; transparent = false; matName = null; diffuseMap = null; specularMap = null; normalMap = null; alphaMap = null; alpha = 1; }
private void displayPlayersPositions() { Map<Vector3f, Integer> playerPositions = gm.getMapManager().getBoard().getPlayerPositions(); Iterator it = playerPositions.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); Vector3f pos = (Vector3f) pairs.getKey(); Integer number = (Integer) pairs.getValue(); Box b = new Box(Vector3f.ZERO, 2.0f, 1.0f, 2.0f); Geometry geom = new Geometry("Box", b); Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.randomColor()); geom.setMaterial(mat); geom.setLocalTranslation(pos.x, 0, pos.z); String geomName = "PLAYERMARKER"; geom.setName(geomName); geom.setUserData(geomName, pos); playerNodes.attachChild(geom); displayLocationName(number.toString(), new Vector3f(pos.x, 1, pos.z)); } }
/** A cube object for target practice */ protected Geometry makeCube(String name, float x, float y, float z) { Box box = new Box(new Vector3f(x, y, z), 1, 1, 1); Geometry cube = new Geometry(name, box); Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat1.setColor("Color", ColorRGBA.randomColor()); cube.setMaterial(mat1); return cube; }
Range(int start, String colorStr) { this.start = start; this.color = new ColorRGBA(); if (colorStr.length() >= 6) { color.set( Integer.parseInt(colorStr.subSequence(0, 2).toString(), 16) / 255f, Integer.parseInt(colorStr.subSequence(2, 4).toString(), 16) / 255f, Integer.parseInt(colorStr.subSequence(4, 6).toString(), 16) / 255f, 1); if (colorStr.length() == 8) { color.a = Integer.parseInt(colorStr.subSequence(6, 8).toString(), 16) / 255f; } } else { color.set( Integer.parseInt(Character.toString(colorStr.charAt(0)), 16) / 15f, Integer.parseInt(Character.toString(colorStr.charAt(1)), 16) / 15f, Integer.parseInt(Character.toString(colorStr.charAt(2)), 16) / 15f, 1); if (colorStr.length() == 4) { color.a = Integer.parseInt(Character.toString(colorStr.charAt(3)), 16) / 15f; } } }
protected void createMaterial() { Material material; if (alpha < 1f && transparent) { diffuse.a = alpha; } if (shadeless) { material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); material.setColor("Color", diffuse.clone()); material.setTexture("ColorMap", diffuseMap); // TODO: Add handling for alpha map? } else { material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); material.setBoolean("UseMaterialColors", true); material.setColor("Ambient", ambient.clone()); material.setColor("Diffuse", diffuse.clone()); material.setColor("Specular", specular.clone()); material.setFloat("Shininess", shininess); // prevents "premature culling" bug if (diffuseMap != null) material.setTexture("DiffuseMap", diffuseMap); if (specularMap != null) material.setTexture("SpecularMap", specularMap); if (normalMap != null) material.setTexture("NormalMap", normalMap); if (alphaMap != null) material.setTexture("AlphaMap", alphaMap); } if (transparent) { material.setTransparent(true); material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); material.getAdditionalRenderState().setAlphaTest(true); material.getAdditionalRenderState().setAlphaFallOff(0.01f); } material.setName(matName); matList.put(matName, material); }
Line(AssetManager assetmanager_) { assetmanager = assetmanager_; Mesh mesh = new Mesh(); mesh.setMode(Mesh.Mode.Lines); mesh.setBuffer(VertexBuffer.Type.Position, 3, new float[] {0, 0, 0, 0, 0, 0}); mesh.setBuffer(VertexBuffer.Type.Index, 2, new short[] {0, 1}); // Box b = new Box(Vector3f.ZERO, 1, 1, 1); geom = new Geometry("Box", mesh); Material mat = new Material(assetmanager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.randomColor()); geom.setMaterial(mat); geom.getMesh().getBuffer(Type.Position).setUpdateNeeded(); // geom.getMesh().getBuffer(Type.TexCoord).setUpdateNeeded(); geom.getMesh().getBuffer(Type.Index).setUpdateNeeded(); geom.getMesh().updateBound(); geom.getMesh().updateCounts(); geom.updateModelBound(); }
protected ColorRGBA readColor() { ColorRGBA v = new ColorRGBA(); v.set(scan.nextFloat(), scan.nextFloat(), scan.nextFloat(), 1.0f); return v; }
protected boolean readLine() { if (!scan.hasNext()) { return false; } String cmd = scan.next().toLowerCase(); if (cmd.startsWith("#")) { // skip entire comment until next line return skipLine(); } else if (cmd.equals("newmtl")) { String name = scan.next(); startMaterial(name); } else if (cmd.equals("ka")) { ambient.set(readColor()); } else if (cmd.equals("kd")) { diffuse.set(readColor()); } else if (cmd.equals("ks")) { specular.set(readColor()); } else if (cmd.equals("ns")) { float shiny = scan.nextFloat(); if (shiny >= 1) { shininess = shiny; /* (128f / 1000f)*/ if (specular.equals(ColorRGBA.Black)) { specular.set(ColorRGBA.White); } } else { // For some reason blender likes to export Ns 0 statements // Ignore Ns 0 instead of setting it } } else if (cmd.equals("d") || cmd.equals("tr")) { float tempAlpha = scan.nextFloat(); if (tempAlpha != 0) { alpha = tempAlpha; transparent = true; } } else if (cmd.equals("map_ka")) { // ignore it for now return skipLine(); } else if (cmd.equals("map_kd")) { String path = nextStatement(); diffuseMap = loadTexture(path); } else if (cmd.equals("map_bump") || cmd.equals("bump")) { if (normalMap == null) { String path = nextStatement(); normalMap = loadTexture(path); } } else if (cmd.equals("map_ks")) { String path = nextStatement(); specularMap = loadTexture(path); if (specularMap != null) { // NOTE: since specular color is modulated with specmap // make sure we have it set if (specular.equals(ColorRGBA.Black)) { specular.set(ColorRGBA.White); } } } else if (cmd.equals("map_d")) { String path = scan.next(); alphaMap = loadTexture(path); transparent = true; } else if (cmd.equals("illum")) { int mode = scan.nextInt(); switch (mode) { case 0: // no lighting shadeless = true; break; case 1: disallowSpecular = true; break; case 2: case 3: case 5: case 8: break; case 4: case 6: case 7: case 9: // Enable transparency // Works best if diffuse map has an alpha channel transparent = true; break; } } else if (cmd.equals("ke") || cmd.equals("ni")) { // Ni: index of refraction - unsupported in jME // Ke: emission color return skipLine(); } else { logger.log(Level.WARNING, "Unknown statement in MTL! {0}", cmd); return skipLine(); } return true; }
public void setLighting(LightList list) { // XXX: This is abuse of setLighting() to // apply fixed function bindings // and do other book keeping. if (list == null || list.size() == 0) { glDisable(GL_LIGHTING); applyFixedFuncBindings(false); setModelView(worldMatrix, viewMatrix); return; } // Number of lights set previously int numLightsSetPrev = lightList.size(); // If more than maxLights are defined, they will be ignored. // The GL1 renderer is not permitted to crash due to a // GL1 limitation. It must render anything that the GL2 renderer // can render (even incorrectly). lightList.clear(); materialAmbientColor.set(0, 0, 0, 0); for (int i = 0; i < list.size(); i++) { Light l = list.get(i); if (l.getType() == Light.Type.Ambient) { // Gather materialAmbientColor.addLocal(l.getColor()); } else { // Add to list lightList.add(l); // Once maximum lights reached, exit loop. if (lightList.size() >= maxLights) { break; } } } applyFixedFuncBindings(true); glEnable(GL_LIGHTING); fb16.clear(); fb16.put(materialAmbientColor.r) .put(materialAmbientColor.g) .put(materialAmbientColor.b) .put(1) .flip(); glLightModel(GL_LIGHT_MODEL_AMBIENT, fb16); if (context.matrixMode != GL_MODELVIEW) { glMatrixMode(GL_MODELVIEW); context.matrixMode = GL_MODELVIEW; } // Lights are already in world space, so just convert // them to view space. glLoadMatrix(storeMatrix(viewMatrix, fb16)); for (int i = 0; i < lightList.size(); i++) { int glLightIndex = GL_LIGHT0 + i; Light light = lightList.get(i); Light.Type lightType = light.getType(); ColorRGBA col = light.getColor(); Vector3f pos; // Enable the light glEnable(glLightIndex); // OGL spec states default value for light ambient is black switch (lightType) { case Directional: DirectionalLight dLight = (DirectionalLight) light; fb16.clear(); fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip(); glLight(glLightIndex, GL_DIFFUSE, fb16); glLight(glLightIndex, GL_SPECULAR, fb16); pos = tempVec.set(dLight.getDirection()).negateLocal().normalizeLocal(); fb16.clear(); fb16.put(pos.x).put(pos.y).put(pos.z).put(0.0f).flip(); glLight(glLightIndex, GL_POSITION, fb16); glLightf(glLightIndex, GL_SPOT_CUTOFF, 180); break; case Point: PointLight pLight = (PointLight) light; fb16.clear(); fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip(); glLight(glLightIndex, GL_DIFFUSE, fb16); glLight(glLightIndex, GL_SPECULAR, fb16); pos = pLight.getPosition(); fb16.clear(); fb16.put(pos.x).put(pos.y).put(pos.z).put(1.0f).flip(); glLight(glLightIndex, GL_POSITION, fb16); glLightf(glLightIndex, GL_SPOT_CUTOFF, 180); if (pLight.getRadius() > 0) { // Note: this doesn't follow the same attenuation model // as the one used in the lighting shader. glLightf(glLightIndex, GL_CONSTANT_ATTENUATION, 1); glLightf(glLightIndex, GL_LINEAR_ATTENUATION, pLight.getInvRadius() * 2); glLightf( glLightIndex, GL_QUADRATIC_ATTENUATION, pLight.getInvRadius() * pLight.getInvRadius()); } else { glLightf(glLightIndex, GL_CONSTANT_ATTENUATION, 1); glLightf(glLightIndex, GL_LINEAR_ATTENUATION, 0); glLightf(glLightIndex, GL_QUADRATIC_ATTENUATION, 0); } break; case Spot: SpotLight sLight = (SpotLight) light; fb16.clear(); fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip(); glLight(glLightIndex, GL_DIFFUSE, fb16); glLight(glLightIndex, GL_SPECULAR, fb16); pos = sLight.getPosition(); fb16.clear(); fb16.put(pos.x).put(pos.y).put(pos.z).put(1.0f).flip(); glLight(glLightIndex, GL_POSITION, fb16); Vector3f dir = sLight.getDirection(); fb16.clear(); fb16.put(dir.x).put(dir.y).put(dir.z).put(1.0f).flip(); glLight(glLightIndex, GL_SPOT_DIRECTION, fb16); float outerAngleRad = sLight.getSpotOuterAngle(); float innerAngleRad = sLight.getSpotInnerAngle(); float spotCut = outerAngleRad * FastMath.RAD_TO_DEG; float spotExpo = 0.0f; if (outerAngleRad > 0) { spotExpo = (1.0f - (innerAngleRad / outerAngleRad)) * 128.0f; } glLightf(glLightIndex, GL_SPOT_CUTOFF, spotCut); glLightf(glLightIndex, GL_SPOT_EXPONENT, spotExpo); if (sLight.getSpotRange() > 0) { glLightf(glLightIndex, GL_LINEAR_ATTENUATION, sLight.getInvSpotRange()); } else { glLightf(glLightIndex, GL_LINEAR_ATTENUATION, 0); } break; default: throw new UnsupportedOperationException("Unrecognized light type: " + lightType); } } // Disable lights after the index for (int i = lightList.size(); i < numLightsSetPrev; i++) { glDisable(GL_LIGHT0 + i); } // This will set view matrix as well. setModelView(worldMatrix, viewMatrix); }
protected void renderMultipassLighting(Shader shader, Geometry g, RenderManager rm) { Renderer r = rm.getRenderer(); LightList lightList = g.getWorldLightList(); 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; for (int i = 0; i < lightList.size(); i++) { Light l = lightList.get(i); if (l instanceof AmbientLight) { continue; } if (isFirstLight) { // set ambient color for first light only ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList)); isFirstLight = false; isSecondLight = true; } else if (isSecondLight) { ambientColor.setValue(VarType.Vector4, ColorRGBA.Black); // apply additive blending for 2nd and future lights r.applyRenderState(additiveLight); isSecondLight = false; } TempVars vars = TempVars.get(); Quaternion tmpLightDirection = vars.quat1; Quaternion tmpLightPosition = vars.quat2; ColorRGBA tmpLightColor = vars.color; Vector4f tmpVec = vars.vect4f; 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(); 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 directoin 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 happen now in the frag shader. tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0); rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec); tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos); lightDir.setValue(VarType.Vector4, tmpLightDirection); break; default: throw new UnsupportedOperationException("Unknown type of light: " + l.getType()); } vars.release(); r.setShader(shader); r.renderMesh(g.getMesh(), g.getLodLevel(), 1); } if (isFirstLight && lightList.size() > 0) { // There are only ambient lights in the scene. Render // a dummy "normal light" so we can see the ambient ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList)); lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha); lightPos.setValue(VarType.Vector4, nullDirLight); r.setShader(shader); r.renderMesh(g.getMesh(), g.getLodLevel(), 1); } }
private ColorRGBA convertColor(Color inColor, ColorRGBA outColor) { return outColor.set( inColor.getRed(), inColor.getGreen(), inColor.getBlue(), inColor.getAlpha()); }
/** * Uploads the lights in the light list as two uniform arrays.<br> * <br> * * * * <p><code>uniform vec4 g_LightColor[numLights];</code><br> * // g_LightColor.rgb is the diffuse/specular color of the light.<br> * // g_Lightcolor.a is the type of light, 0 = Directional, 1 = Point, <br> * // 2 = Spot. <br> * <br> * <code>uniform vec4 g_LightPosition[numLights];</code><br> * // g_LightPosition.xyz is the position of the light (for point lights)<br> * // or the direction of the light (for directional lights).<br> * // g_LightPosition.w is the inverse radius (1/r) of the light (for attenuation) <br> */ protected void updateLightListUniforms(Shader shader, Geometry g, int numLights) { if (numLights == 0) { // this shader does not do lighting, ignore. return; } LightList lightList = g.getWorldLightList(); Uniform lightColor = shader.getUniform("g_LightColor"); Uniform lightPos = shader.getUniform("g_LightPosition"); Uniform lightDir = shader.getUniform("g_LightDirection"); lightColor.setVector4Length(numLights); lightPos.setVector4Length(numLights); lightDir.setVector4Length(numLights); Uniform ambientColor = shader.getUniform("g_AmbientLightColor"); ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList)); int lightIndex = 0; for (int i = 0; i < numLights; i++) { if (lightList.size() <= i) { lightColor.setVector4InArray(0f, 0f, 0f, 0f, lightIndex); lightPos.setVector4InArray(0f, 0f, 0f, 0f, lightIndex); } else { Light l = lightList.get(i); ColorRGBA color = l.getColor(); lightColor.setVector4InArray( color.getRed(), color.getGreen(), color.getBlue(), l.getType().getId(), i); switch (l.getType()) { case Directional: DirectionalLight dl = (DirectionalLight) l; Vector3f dir = dl.getDirection(); lightPos.setVector4InArray(dir.getX(), dir.getY(), dir.getZ(), -1, lightIndex); break; case Point: PointLight pl = (PointLight) l; Vector3f pos = pl.getPosition(); float invRadius = pl.getInvRadius(); lightPos.setVector4InArray(pos.getX(), pos.getY(), pos.getZ(), invRadius, lightIndex); break; case Spot: SpotLight sl = (SpotLight) l; Vector3f pos2 = sl.getPosition(); Vector3f dir2 = sl.getDirection(); float invRange = sl.getInvSpotRange(); float spotAngleCos = sl.getPackedAngleCos(); lightPos.setVector4InArray(pos2.getX(), pos2.getY(), pos2.getZ(), invRange, lightIndex); lightDir.setVector4InArray( dir2.getX(), dir2.getY(), dir2.getZ(), spotAngleCos, lightIndex); break; case Ambient: // skip this light. Does not increase lightIndex continue; default: throw new UnsupportedOperationException("Unknown type of light: " + l.getType()); } } lightIndex++; } while (lightIndex < numLights) { lightColor.setVector4InArray(0f, 0f, 0f, 0f, lightIndex); lightPos.setVector4InArray(0f, 0f, 0f, 0f, lightIndex); lightIndex++; } }
void setColor(ColorRGBA color) { this.colorInt = color.asIntRGBA(); invalidate(); }