Search in sources :

Example 1 with ShaderSource

use of com.jme3.shader.Shader.ShaderSource in project jmonkeyengine by jMonkeyEngine.

the class GLRenderer method updateShaderData.

public void updateShaderData(Shader shader) {
    int id = shader.getId();
    boolean needRegister = false;
    if (id == -1) {
        // create program
        id = gl.glCreateProgram();
        if (id == 0) {
            throw new RendererException("Invalid ID (" + id + ") received when trying to create shader program.");
        }
        shader.setId(id);
        needRegister = true;
    }
    // If using GLSL 1.5, we bind the outputs for the user
    // For versions 3.3 and up, user should use layout qualifiers instead.
    boolean bindFragDataRequired = false;
    for (ShaderSource source : shader.getSources()) {
        if (source.isUpdateNeeded()) {
            updateShaderSourceData(source);
        }
        if (source.getType() == ShaderType.Fragment && source.getLanguage().equals("GLSL150")) {
            bindFragDataRequired = true;
        }
        gl.glAttachShader(id, source.getId());
    }
    if (bindFragDataRequired) {
        // Check if GLSL version is 1.5 for shader
        gl3.glBindFragDataLocation(id, 0, "outFragColor");
        // For MRT
        for (int i = 0; i < limits.get(Limits.FrameBufferMrtAttachments); i++) {
            gl3.glBindFragDataLocation(id, i, "outFragData[" + i + "]");
        }
    }
    // Link shaders to program
    gl.glLinkProgram(id);
    // Check link status
    gl.glGetProgram(id, GL.GL_LINK_STATUS, intBuf1);
    boolean linkOK = intBuf1.get(0) == GL.GL_TRUE;
    String infoLog = null;
    if (VALIDATE_SHADER || !linkOK) {
        gl.glGetProgram(id, GL.GL_INFO_LOG_LENGTH, intBuf1);
        int length = intBuf1.get(0);
        if (length > 3) {
            // get infos
            infoLog = gl.glGetProgramInfoLog(id, length);
        }
    }
    if (linkOK) {
        if (infoLog != null) {
            logger.log(Level.WARNING, "Shader linked successfully. Linker warnings: \n{0}", infoLog);
        } else {
            logger.fine("Shader linked successfully.");
        }
        shader.clearUpdateNeeded();
        if (needRegister) {
            // Register shader for clean up if it was created in this method.
            objManager.registerObject(shader);
            statistics.onNewShader();
        } else {
            // OpenGL spec: uniform locations may change after re-link
            resetUniformLocations(shader);
        }
    } else {
        if (infoLog != null) {
            throw new RendererException("Shader failed to link, shader:" + shader + "\n" + infoLog);
        } else {
            throw new RendererException("Shader failed to link, shader:" + shader + "\ninfo: <not provided>");
        }
    }
}
Also used : ShaderSource(com.jme3.shader.Shader.ShaderSource)

Example 2 with ShaderSource

use of com.jme3.shader.Shader.ShaderSource in project jmonkeyengine by jMonkeyEngine.

the class GLRenderer method deleteShader.

public void deleteShader(Shader shader) {
    if (shader.getId() == -1) {
        logger.warning("Shader is not uploaded to GPU, cannot delete.");
        return;
    }
    for (ShaderSource source : shader.getSources()) {
        if (source.getId() != -1) {
            gl.glDetachShader(shader.getId(), source.getId());
            deleteShaderSource(source);
        }
    }
    gl.glDeleteProgram(shader.getId());
    statistics.onDeleteShader();
    shader.resetObject();
}
Also used : ShaderSource(com.jme3.shader.Shader.ShaderSource)

Example 3 with ShaderSource

use of com.jme3.shader.Shader.ShaderSource in project jmonkeyengine by jMonkeyEngine.

the class TestShaderNodes method simpleInitApp.

@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(20);
    Logger.getLogger("com.jme3").setLevel(Level.WARNING);
    Box boxshape1 = new Box(1f, 1f, 1f);
    Geometry cube_tex = new Geometry("A Textured Box", boxshape1);
    Texture tex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/UnshadedNodes.j3md");
    mat.selectTechnique(TechniqueDef.DEFAULT_TECHNIQUE_NAME, renderManager);
    Technique t = mat.getActiveTechnique();
    //        for (Shader.ShaderSource shaderSource : t.getShader().getSources()) {
    //            System.out.println(shaderSource.getSource());
    //        }
    mat.setColor("Color", ColorRGBA.Yellow);
    mat.setTexture("ColorMap", tex);
    cube_tex.setMaterial(mat);
    rootNode.attachChild(cube_tex);
}
Also used : Geometry(com.jme3.scene.Geometry) Box(com.jme3.scene.shape.Box) Material(com.jme3.material.Material) Technique(com.jme3.material.Technique) Texture(com.jme3.texture.Texture)

Aggregations

ShaderSource (com.jme3.shader.Shader.ShaderSource)2 Material (com.jme3.material.Material)1 Technique (com.jme3.material.Technique)1 Geometry (com.jme3.scene.Geometry)1 Box (com.jme3.scene.shape.Box)1 Texture (com.jme3.texture.Texture)1