Search in sources :

Example 1 with RenderState

use of com.jme3.material.RenderState in project jmonkeyengine by jMonkeyEngine.

the class J3MLoader method readForcedRenderState.

private void readForcedRenderState(List<Statement> renderStates) throws IOException {
    renderState = new RenderState();
    for (Statement statement : renderStates) {
        readRenderStateStatement(statement);
    }
    technique.setForcedRenderState(renderState);
    renderState = null;
}
Also used : Statement(com.jme3.util.blockparser.Statement)

Example 2 with RenderState

use of com.jme3.material.RenderState in project jmonkeyengine by jMonkeyEngine.

the class BatchNode method mergeGeometries.

/**
     * Merges all geometries in the collection into
     * the output mesh. Does not take into account materials.
     *
     * @param geometries
     * @param outMesh
     */
private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
    int[] compsForBuf = new int[VertexBuffer.Type.values().length];
    VertexBuffer.Format[] formatForBuf = new VertexBuffer.Format[compsForBuf.length];
    boolean[] normForBuf = new boolean[VertexBuffer.Type.values().length];
    int totalVerts = 0;
    int totalTris = 0;
    int totalLodLevels = 0;
    int maxWeights = -1;
    Mesh.Mode mode = null;
    float lineWidth = 1f;
    for (Geometry geom : geometries) {
        totalVerts += geom.getVertexCount();
        totalTris += geom.getTriangleCount();
        totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
        if (maxVertCount < geom.getVertexCount()) {
            maxVertCount = geom.getVertexCount();
        }
        Mesh.Mode listMode;
        //float listLineWidth = 1f;
        int components;
        switch(geom.getMesh().getMode()) {
            case Points:
                listMode = Mesh.Mode.Points;
                components = 1;
                break;
            case LineLoop:
            case LineStrip:
            case Lines:
                listMode = Mesh.Mode.Lines;
                //listLineWidth = geom.getMesh().getLineWidth();
                components = 2;
                break;
            case TriangleFan:
            case TriangleStrip:
            case Triangles:
                listMode = Mesh.Mode.Triangles;
                components = 3;
                break;
            default:
                throw new UnsupportedOperationException();
        }
        for (VertexBuffer vb : geom.getMesh().getBufferList().getArray()) {
            int currentCompsForBuf = compsForBuf[vb.getBufferType().ordinal()];
            if (vb.getBufferType() != VertexBuffer.Type.Index && currentCompsForBuf != 0 && currentCompsForBuf != vb.getNumComponents()) {
                throw new UnsupportedOperationException("The geometry " + geom + " buffer " + vb.getBufferType() + " has different number of components than the rest of the meshes " + "(this: " + vb.getNumComponents() + ", expected: " + currentCompsForBuf + ")");
            }
            compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents();
            formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat();
            normForBuf[vb.getBufferType().ordinal()] = vb.isNormalized();
        }
        maxWeights = Math.max(maxWeights, geom.getMesh().getMaxNumWeights());
        if (mode != null && mode != listMode) {
            throw new UnsupportedOperationException("Cannot combine different" + " primitive types: " + mode + " != " + listMode);
        }
        mode = listMode;
        //Not needed anymore as lineWidth is now in RenderState and will be taken into account when merging according to the material
        //            if (mode == Mesh.Mode.Lines) {
        //                if (lineWidth != 1f && listLineWidth != lineWidth) {
        //                    throw new UnsupportedOperationException("When using Mesh Line mode, cannot combine meshes with different line width "
        //                            + lineWidth + " != " + listLineWidth);
        //                }
        //                lineWidth = listLineWidth;
        //            }
        compsForBuf[VertexBuffer.Type.Index.ordinal()] = components;
    }
    outMesh.setMaxNumWeights(maxWeights);
    outMesh.setMode(mode);
    //outMesh.setLineWidth(lineWidth);
    if (totalVerts >= 65536) {
        // make sure we create an UnsignedInt buffer so we can fit all of the meshes
        formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedInt;
    } else {
        formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedShort;
    }
    // generate output buffers based on retrieved info
    for (int i = 0; i < compsForBuf.length; i++) {
        if (compsForBuf[i] == 0) {
            continue;
        }
        Buffer data;
        if (i == VertexBuffer.Type.Index.ordinal()) {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
        } else {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
        }
        VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
        vb.setupData(VertexBuffer.Usage.Dynamic, compsForBuf[i], formatForBuf[i], data);
        vb.setNormalized(normForBuf[i]);
        outMesh.setBuffer(vb);
    }
    int globalVertIndex = 0;
    int globalTriIndex = 0;
    for (Geometry geom : geometries) {
        Mesh inMesh = geom.getMesh();
        if (!isBatch(geom)) {
            geom.associateWithGroupNode(this, globalVertIndex);
        }
        int geomVertCount = inMesh.getVertexCount();
        int geomTriCount = inMesh.getTriangleCount();
        for (int bufType = 0; bufType < compsForBuf.length; bufType++) {
            VertexBuffer inBuf = inMesh.getBuffer(VertexBuffer.Type.values()[bufType]);
            VertexBuffer outBuf = outMesh.getBuffer(VertexBuffer.Type.values()[bufType]);
            if (outBuf == null) {
                continue;
            }
            if (VertexBuffer.Type.Index.ordinal() == bufType) {
                int components = compsForBuf[bufType];
                IndexBuffer inIdx = inMesh.getIndicesAsList();
                IndexBuffer outIdx = outMesh.getIndexBuffer();
                for (int tri = 0; tri < geomTriCount; tri++) {
                    for (int comp = 0; comp < components; comp++) {
                        int idx = inIdx.get(tri * components + comp) + globalVertIndex;
                        outIdx.put((globalTriIndex + tri) * components + comp, idx);
                    }
                }
            } else if (VertexBuffer.Type.Position.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doCopyBuffer(inPos, globalVertIndex, outPos, 3);
            } else if (VertexBuffer.Type.Normal.ordinal() == bufType || VertexBuffer.Type.Tangent.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doCopyBuffer(inPos, globalVertIndex, outPos, compsForBuf[bufType]);
                if (VertexBuffer.Type.Tangent.ordinal() == bufType) {
                    useTangents = true;
                }
            } else {
                if (inBuf == null) {
                    throw new IllegalArgumentException("Geometry " + geom.getName() + " has no " + outBuf.getBufferType() + " buffer whereas other geoms have. all geometries should have the same types of buffers.\n Try to use GeometryBatchFactory.alignBuffer() on the BatchNode before batching");
                } else if (outBuf == null) {
                    throw new IllegalArgumentException("Geometry " + geom.getName() + " has a " + outBuf.getBufferType() + " buffer whereas other geoms don't. all geometries should have the same types of buffers.\n Try to use GeometryBatchFactory.alignBuffer() on the BatchNode before batching");
                } else {
                    inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
                }
            }
        }
        globalVertIndex += geomVertCount;
        globalTriIndex += geomTriCount;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Buffer(java.nio.Buffer) FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer)

Example 3 with RenderState

use of com.jme3.material.RenderState in project jmonkeyengine by jMonkeyEngine.

the class Material method read.

public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    name = ic.readString("name", null);
    additionalState = (RenderState) ic.readSavable("render_state", null);
    transparent = ic.readBoolean("is_transparent", false);
    // Load the material def
    String defName = ic.readString("material_def", null);
    HashMap<String, MatParam> params = (HashMap<String, MatParam>) ic.readStringSavableMap("parameters", null);
    boolean enableVcolor = false;
    boolean separateTexCoord = false;
    boolean applyDefaultValues = false;
    boolean guessRenderStateApply = false;
    int ver = ic.getSavableVersion(Material.class);
    if (ver < 1) {
        applyDefaultValues = true;
    }
    if (ver < 2) {
        guessRenderStateApply = true;
    }
    if (im.getFormatVersion() == 0) {
        // Enable compatibility with old models
        if (defName.equalsIgnoreCase("Common/MatDefs/Misc/VertexColor.j3md")) {
            // Using VertexColor, switch to Unshaded and set VertexColor=true
            enableVcolor = true;
            defName = "Common/MatDefs/Misc/Unshaded.j3md";
        } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/SimpleTextured.j3md") || defName.equalsIgnoreCase("Common/MatDefs/Misc/SolidColor.j3md")) {
            // Using SimpleTextured/SolidColor, just switch to Unshaded
            defName = "Common/MatDefs/Misc/Unshaded.j3md";
        } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/WireColor.j3md")) {
            // Using WireColor, set wireframe renderstate = true and use Unshaded
            getAdditionalRenderState().setWireframe(true);
            defName = "Common/MatDefs/Misc/Unshaded.j3md";
        } else if (defName.equalsIgnoreCase("Common/MatDefs/Misc/Unshaded.j3md")) {
            // Uses unshaded, ensure that the proper param is set
            MatParam value = params.get("SeperateTexCoord");
            if (value != null && ((Boolean) value.getValue()) == true) {
                params.remove("SeperateTexCoord");
                separateTexCoord = true;
            }
        }
        assert applyDefaultValues && guessRenderStateApply;
    }
    def = (MaterialDef) im.getAssetManager().loadAsset(new AssetKey(defName));
    paramValues = new ListMap<String, MatParam>();
    // load the textures and update nextTexUnit
    for (Map.Entry<String, MatParam> entry : params.entrySet()) {
        MatParam param = entry.getValue();
        if (param instanceof MatParamTexture) {
            MatParamTexture texVal = (MatParamTexture) param;
            // do not add to param values
            if (texVal.getTextureValue() == null || texVal.getTextureValue().getImage() == null) {
                continue;
            }
        }
        if (im.getFormatVersion() == 0 && param.getName().startsWith("m_")) {
            // Ancient version of jME3 ...
            param.setName(param.getName().substring(2));
        }
        if (def.getMaterialParam(param.getName()) == null) {
            logger.log(Level.WARNING, "The material parameter is not defined: {0}. Ignoring..", param.getName());
        } else {
            checkSetParam(param.getVarType(), param.getName());
            paramValues.put(param.getName(), param);
        }
    }
    if (applyDefaultValues) {
        // not available
        for (MatParam param : def.getMaterialParams()) {
            if (param.getValue() != null && paramValues.get(param.getName()) == null) {
                setParam(param.getName(), param.getVarType(), param.getValue());
            }
        }
    }
    if (guessRenderStateApply && additionalState != null) {
        // Try to guess values of "apply" render state based on defaults
        // if value != default then set apply to true
        additionalState.applyPolyOffset = additionalState.offsetEnabled;
        additionalState.applyBlendMode = additionalState.blendMode != BlendMode.Off;
        additionalState.applyColorWrite = !additionalState.colorWrite;
        additionalState.applyCullMode = additionalState.cullMode != FaceCullMode.Back;
        additionalState.applyDepthTest = !additionalState.depthTest;
        additionalState.applyDepthWrite = !additionalState.depthWrite;
        additionalState.applyStencilTest = additionalState.stencilTest;
        additionalState.applyWireFrame = additionalState.wireframe;
    }
    if (enableVcolor) {
        setBoolean("VertexColor", true);
    }
    if (separateTexCoord) {
        setBoolean("SeparateTexCoord", true);
    }
}
Also used : AssetKey(com.jme3.asset.AssetKey) ListMap(com.jme3.util.ListMap)

Example 4 with RenderState

use of com.jme3.material.RenderState in project jmonkeyengine by jMonkeyEngine.

the class RenderDeviceJme method renderFont.

@Override
public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY) {
    if (str.length() == 0) {
        return;
    }
    RenderFontJme jmeFont = (RenderFontJme) font;
    ColorRGBA colorRgba = convertColor(color, tempColor);
    CachedTextKey key = new CachedTextKey(jmeFont.getFont(), str);
    BitmapText text = textCacheLastFrame.get(key);
    if (text == null) {
        text = jmeFont.createText();
        text.setText(str);
        text.updateLogicalState(0);
    }
    textCacheCurrentFrame.put(key, text);
    //        float width = text.getLineWidth();
    //        float height = text.getLineHeight();
    //+ 0.5f * width * (1f - sizeX);
    float x0 = x;
    // + 0.5f * height * (1f - sizeY);
    float y0 = y;
    tempMat.loadIdentity();
    tempMat.setTranslation(x0, getHeight() - y0, 0);
    tempMat.setScale(sizeX, sizeY, 0);
    rm.setWorldMatrix(tempMat);
    rm.setForcedRenderState(renderState);
    text.setColor(colorRgba);
    text.updateLogicalState(0);
    text.render(rm, colorRgba);
//        System.out.format("renderFont(%s, %s, %d, %d, %s, %f, %f)\n", jmeFont.getFont(), str, x, y, color.toString(), sizeX, sizeY);
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) BitmapText(com.jme3.font.BitmapText)

Example 5 with RenderState

use of com.jme3.material.RenderState in project jmonkeyengine by jMonkeyEngine.

the class Material method render.

/**
     * Called by {@link RenderManager} to render the geometry by
     * using this material.
     * <p>
     * The material is rendered as follows:
     * <ul>
     * <li>Determine which technique to use to render the material -
     * either what the user selected via
     * {@link #selectTechnique(java.lang.String, com.jme3.renderer.RenderManager)
     * Material.selectTechnique()},
     * or the first default technique that the renderer supports
     * (based on the technique's {@link TechniqueDef#getRequiredCaps() requested rendering capabilities})<ul>
     * <li>If the technique has been changed since the last frame, then it is notified via
     * {@link Technique#makeCurrent(com.jme3.asset.AssetManager, boolean, java.util.EnumSet)
     * Technique.makeCurrent()}.
     * If the technique wants to use a shader to render the model, it should load it at this part -
     * the shader should have all the proper defines as declared in the technique definition,
     * including those that are bound to material parameters.
     * The technique can re-use the shader from the last frame if
     * no changes to the defines occurred.</li></ul>
     * <li>Set the {@link RenderState} to use for rendering. The render states are
     * applied in this order (later RenderStates override earlier RenderStates):<ol>
     * <li>{@link TechniqueDef#getRenderState() Technique Definition's RenderState}
     * - i.e. specific renderstate that is required for the shader.</li>
     * <li>{@link #getAdditionalRenderState() Material Instance Additional RenderState}
     * - i.e. ad-hoc renderstate set per model</li>
     * <li>{@link RenderManager#getForcedRenderState() RenderManager's Forced RenderState}
     * - i.e. renderstate requested by a {@link com.jme3.post.SceneProcessor} or
     * post-processing filter.</li></ol>
     * <li>If the technique {@link TechniqueDef#isUsingShaders() uses a shader}, then the uniforms of the shader must be updated.<ul>
     * <li>Uniforms bound to material parameters are updated based on the current material parameter values.</li>
     * <li>Uniforms bound to world parameters are updated from the RenderManager.
     * Internally {@link UniformBindingManager} is used for this task.</li>
     * <li>Uniforms bound to textures will cause the texture to be uploaded as necessary.
     * The uniform is set to the texture unit where the texture is bound.</li></ul>
     * <li>If the technique uses a shader, the model is then rendered according
     * to the lighting mode specified on the technique definition.<ul>
     * <li>{@link LightMode#SinglePass single pass light mode} fills the shader's light uniform arrays
     * with the first 4 lights and renders the model once.</li>
     * <li>{@link LightMode#MultiPass multi pass light mode} light mode renders the model multiple times,
     * for the first light it is rendered opaque, on subsequent lights it is
     * rendered with {@link BlendMode#AlphaAdditive alpha-additive} blending and depth writing disabled.</li>
     * </ul>
     * <li>For techniques that do not use shaders,
     * fixed function OpenGL is used to render the model (see {@link GL1Renderer} interface):<ul>
     * <li>OpenGL state ({@link FixedFuncBinding}) that is bound to material parameters is updated. </li>
     * <li>The texture set on the material is uploaded and bound.
     * Currently only 1 texture is supported for fixed function techniques.</li>
     * <li>If the technique uses lighting, then OpenGL lighting state is updated
     * based on the light list on the geometry, otherwise OpenGL lighting is disabled.</li>
     * <li>The mesh is uploaded and rendered.</li>
     * </ul>
     * </ul>
     *
     * @param geometry The geometry to render
     * @param lights Presorted and filtered light list to use for rendering
     * @param renderManager The render manager requesting the rendering
     */
public void render(Geometry geometry, LightList lights, RenderManager renderManager) {
    if (technique == null) {
        selectTechnique(TechniqueDef.DEFAULT_TECHNIQUE_NAME, renderManager);
    }
    TechniqueDef techniqueDef = technique.getDef();
    Renderer renderer = renderManager.getRenderer();
    EnumSet<Caps> rendererCaps = renderer.getCaps();
    if (techniqueDef.isNoRender()) {
        return;
    }
    // Apply render state
    updateRenderState(renderManager, renderer, techniqueDef);
    // Get world overrides
    SafeArrayList<MatParamOverride> overrides = geometry.getWorldMatParamOverrides();
    // Select shader to use
    Shader shader = technique.makeCurrent(renderManager, overrides, renderManager.getForcedMatParams(), lights, rendererCaps);
    // Begin tracking which uniforms were changed by material.
    clearUniformsSetByCurrent(shader);
    // Set uniform bindings
    renderManager.updateUniformBindings(shader);
    // Set material parameters
    int unit = updateShaderMaterialParameters(renderer, shader, overrides, renderManager.getForcedMatParams());
    // Clear any uniforms not changed by material.
    resetUniformsNotSetByCurrent(shader);
    // Delegate rendering to the technique
    technique.render(renderManager, shader, geometry, lights, unit);
}
Also used : Renderer(com.jme3.renderer.Renderer) Shader(com.jme3.shader.Shader) Caps(com.jme3.renderer.Caps)

Aggregations

Statement (com.jme3.util.blockparser.Statement)3 Material (com.jme3.material.Material)2 RenderState (com.jme3.material.RenderState)2 FloatBuffer (java.nio.FloatBuffer)2 AssetKey (com.jme3.asset.AssetKey)1 BitmapText (com.jme3.font.BitmapText)1 LightList (com.jme3.light.LightList)1 MaterialDef (com.jme3.material.MaterialDef)1 Technique (com.jme3.material.Technique)1 ColorRGBA (com.jme3.math.ColorRGBA)1 Caps (com.jme3.renderer.Caps)1 Renderer (com.jme3.renderer.Renderer)1 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)1 Shader (com.jme3.shader.Shader)1 Texture2D (com.jme3.texture.Texture2D)1 ListMap (com.jme3.util.ListMap)1 Buffer (java.nio.Buffer)1