Example usage for org.lwjgl.opengl GL20 glShaderSource

List of usage examples for org.lwjgl.opengl GL20 glShaderSource

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL20 glShaderSource.

Prototype

public static void glShaderSource(@NativeType("GLuint") int shader,
        @NativeType("GLchar const **") CharSequence string) 

Source Link

Document

Sets the source code in shader to the source code in the array of strings specified by strings .

Usage

From source file:org.terasology.rendering.assets.MaterialShader.java

License:Apache License

private void compileShader(int type, String shaderCode) {

    int shaderId = GL20.glCreateShader(type);

    if (type == GL20.GL_FRAGMENT_SHADER) {
        fragmentProgram = shaderId;//ww  w . ja  va2  s  . c om
    } else if (type == GL20.GL_VERTEX_SHADER) {
        vertexProgram = shaderId;
    }

    GL20.glShaderSource(shaderId, shaderCode);
    GL20.glCompileShader(shaderId);

    verifyCompile(shaderId);
}

From source file:org.terasology.rendering.opengl.GLSLShader.java

License:Apache License

private void compileShader(int type, Set<ShaderProgramFeature> features) {
    int shaderId = GL20.glCreateShader(type);
    StringBuilder shader = createShaderBuilder();

    // Add the activated features for this shader
    for (ShaderProgramFeature feature : features) {
        shader.append("#define ").append(feature.name()).append("\n");
    }//from   w  w w .j a v a  2 s.c om

    shader.append("\n");

    shader.append(includedDefines);
    shader.append(includedUniforms);

    if (type == GL20.GL_FRAGMENT_SHADER) {
        shader.append(includedFunctionsFragment).append("\n");
    } else {
        shader.append(includedFunctionsVertex).append("\n");
    }

    if (type == GL20.GL_FRAGMENT_SHADER) {
        shader.append(shaderProgramBase.getFragmentProgram());
    } else if (type == GL20.GL_VERTEX_SHADER) {
        shader.append(shaderProgramBase.getVertexProgram());
    }

    String debugShaderType = "UNKNOWN";
    int featureHash = ShaderProgramFeature.getBitset(features);
    if (type == GL20.GL_FRAGMENT_SHADER) {
        fragmentPrograms.put(featureHash, shaderId);
        debugShaderType = "FRAGMENT";
    } else if (type == GL20.GL_VERTEX_SHADER) {
        vertexPrograms.put(featureHash, shaderId);
        debugShaderType = "VERTEX";
    }

    // Dump all final shader sources to the log directory
    final String strippedTitle = getURI().toString().replace(":", "-");

    Path path = PathManager.getInstance().getShaderLogPath()
            .resolve(debugShaderType.toLowerCase() + "_" + strippedTitle + "_" + featureHash + ".glsl");
    try (BufferedWriter writer = Files.newBufferedWriter(path, TerasologyConstants.CHARSET)) {
        writer.write(shader.toString());
    } catch (IOException e) {
        logger.error("Failed to dump shader source.");
    }

    GL20.glShaderSource(shaderId, shader.toString());
    GL20.glCompileShader(shaderId);

    StringBuilder error = new StringBuilder();
    boolean success = printLogInfo(shaderId, error);

    String errorLine = "";
    if (error.length() > 0) {
        try {
            Pattern p = Pattern.compile("-?\\d+");
            Matcher m = p.matcher(error.toString());

            int counter = 0;
            while (m.find()) {
                if (counter++ % 2 == 1) {
                    int lineNumberInt = Integer.valueOf(m.group());

                    try (Scanner reader = new Scanner(shader.toString())) {
                        for (int i = 0; i < lineNumberInt - 1; ++i) {
                            reader.nextLine();
                        }

                        errorLine = reader.nextLine();
                        errorLine = "Error prone line: '" + errorLine + "'";

                        logger.warn("{} \n Line: {}", error, errorLine);
                    }
                    break;
                }
            }

        } catch (Exception e) {
            logger.error("Error parsing shader compile error: {}", error, e);
        }
    }

    if (!success) {
        String errorMessage = debugShaderType + " Shader '" + getURI()
                + "' failed to compile. Terasology might not look quite as good as it should now...\n\n" + error
                + "\n\n" + errorLine;

        logger.error(errorMessage);
        JOptionPane.showMessageDialog(null, errorMessage, "Shader compilation error",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:org.terasology.rendering.shader.ShaderProgram.java

License:Apache License

private void compileShader(int type) {

    int shaderId = GL20.glCreateShader(type);

    StringBuilder shader = Shader.createShaderBuilder();

    if (type == GL20.GL_FRAGMENT_SHADER)
        shader.append(Shader.getIncludedFunctionsFragment()).append("\n");
    else/*w w w . jav  a2s  . c  om*/
        shader.append(Shader.getIncludedFunctionsVertex()).append("\n");

    String filename = title;

    if (type == GL20.GL_FRAGMENT_SHADER) {
        filename += "_frag.glsl";
    } else if (type == GL20.GL_VERTEX_SHADER) {
        filename += "_vert.glsl";
    }

    logger.debug("Loading shader {} ({}, type = {})", title, filename, String.valueOf(type));

    // Read in the shader code
    shader.append(readShader(filename));

    if (type == GL20.GL_FRAGMENT_SHADER) {
        fragmentProgram = shaderId;
    } else if (type == GL20.GL_VERTEX_SHADER) {
        vertexProgram = shaderId;
    }

    GL20.glShaderSource(shaderId, shader.toString());
    GL20.glCompileShader(shaderId);

    String error;
    if ((error = printLogInfo(shaderId)) != null) {
        JOptionPane.showMessageDialog(null,
                "Shader '" + title
                        + "' failed to compile. Terasology might not look quite as good as it should now...\n\n"
                        + error,
                "Shader compilation error", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:ovh.tgrhavoc.gameengine.core.Shader.java

License:Open Source License

private void addProgram(String text, int type) {
    int shader = GL20.glCreateShader(type);
    if (shader == 0) {
        System.err.println("Could not find valid memory location when adding shader");
        System.exit(-1);//w w w  .  j av  a2  s  . co m
    }

    GL20.glShaderSource(shader, text);
    GL20.glCompileShader(shader);

    if (GL20.glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == 0) {
        System.err.println(GL20.glGetShaderInfoLog(shader, 1024));
        System.exit(-1);
    }

    GL20.glAttachShader(pointer, shader);
}

From source file:playn.java.JavaGL20.java

License:Apache License

@Override
public void glShaderSource(int shader, String string) {
    GL20.glShaderSource(shader, string);
}

From source file:playn.java.JavaGL20.java

License:Apache License

@Override
public void glShaderSource(int shader, int count, String[] strings, int[] length, int lengthOff) {
    for (final String str : strings)
        GL20.glShaderSource(shader, str);
}

From source file:playn.java.JavaGL20.java

License:Apache License

@Override
public void glShaderSource(int shader, int count, String[] strings, IntBuffer length) {
    for (final String str : strings)
        GL20.glShaderSource(shader, str);
}

From source file:processing.lwjgl.PGL.java

License:Open Source License

public void shaderSource(int id, String source) {
    GL20.glShaderSource(id, source);
}

From source file:processing.lwjgl.PLWJGL.java

License:Open Source License

public void shaderSource(int shader, String source) {
    GL20.glShaderSource(shader, source);
}

From source file:processing.opengl.PLWJGL.java

License:Open Source License

@Override
public void shaderSource(int shader, String source) {
    GL20.glShaderSource(shader, source);
}