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:lessur.util.shader.ShaderManager.java

License:GNU General Public License

/**
 * Fetches a shader id for a given fragment shader, generating
 * a new id if necessary//from w w  w  . jav  a 2 s.co m
 * @param fragmentShaderFilePath The path to the fragment shader
 * @returns shaderID for a given fragment shader
 */
public int getFragmentShaderID(String fragmentShaderFilePath) throws Exception {
    Integer id = shaderMap.get(fragmentShaderFilePath);
    if (id == null) {
        if (has_opengl2) {
            id = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
            GL20.glShaderSource(id, getProgramCode(fragmentShaderFilePath));
            GL20.glCompileShader(id);
            shaderMap.put(fragmentShaderFilePath, id);
        } else if (has_arb) {
            id = ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
            ARBShaderObjects.glShaderSourceARB(id, getProgramCode(fragmentShaderFilePath));
            ARBShaderObjects.glCompileShaderARB(id);
            shaderMap.put(fragmentShaderFilePath, id);
        }
        StringBuilder errorMessage = new StringBuilder();
        //Check for errors with shader
        if (!compiledSuccessfully(id)) {
            errorMessage.append("Fragment Shader ");
            errorMessage.append(fragmentShaderFilePath);
            errorMessage.append(" failed to compile.\n");
            errorMessage.append(getShaderInfoLog(id));
            errorMessage.append("\n\n");
            errorMessage.insert(0, "Could not compile shader.\n");
            throw new Exception(errorMessage.toString());
        }
    }
    return id;
}

From source file:lessur.util.shader.ShaderManager.java

License:GNU General Public License

/**
 * Fetches a shader id for a given vertex shader, generating
 * a new id if necessary.//from   w  ww  .  j  a va 2 s . co  m
 * @param fragmentFileName The path to the vertex shader
 * @returns shaderID The shader ID of the [new] fragment shader
 */
public int getVertexShaderID(String vertexShaderFilePath) throws Exception {
    Integer id = shaderMap.get(vertexShaderFilePath);
    if (id == null) {
        if (has_opengl2) {
            id = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
            GL20.glShaderSource(id, getProgramCode(vertexShaderFilePath));
            GL20.glCompileShader(id);
            shaderMap.put(vertexShaderFilePath, id);
        } else if (has_arb) {
            id = ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
            ARBShaderObjects.glShaderSourceARB(id, getProgramCode(vertexShaderFilePath));
            ARBShaderObjects.glCompileShaderARB(id);
            shaderMap.put(vertexShaderFilePath, id);
        }
        StringBuilder errorMessage = new StringBuilder();
        //Check for errors with shader
        if (!compiledSuccessfully(id)) {
            errorMessage.append("Vertex Shader ");
            errorMessage.append(vertexShaderFilePath);
            errorMessage.append(" failed to compile.\n");
            errorMessage.append(getShaderInfoLog(id));
            errorMessage.append("\n\n");
            errorMessage.insert(0, "Could not compile shader.\n");
            throw new Exception(errorMessage.toString());
        }
    }
    return id;
}

From source file:main.java.com.YeAJG.game.io.FileIOHandler.java

License:Open Source License

public static int loadShader(String filename, int type) throws IOException {
    StringBuilder shaderSource = new StringBuilder();
    int shaderID = 0;

    try {//from www.  j av a2  s .co m
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = reader.readLine()) != null) {
            shaderSource.append(line).append("\n");
        }
        reader.close();
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    }

    shaderID = GL20.glCreateShader(type);
    GL20.glShaderSource(shaderID, shaderSource);
    GL20.glCompileShader(shaderID);

    if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
        throw new IOException("Could not compile shader.");
    }

    Game.exitOnGLError("loadShader");
    return shaderID;
}

From source file:me.thehutch.fusion.engine.render.opengl.gl20.OpenGL20Program.java

License:Open Source License

@Override
public void attachShader(CharSequence source, int type) {
    ensureCreated("Program must be created to attach a shader.");
    // Generate a shader handle
    final int shaderId = GL20.glCreateShader(type);
    // Upload the shader's source to the GPU
    GL20.glShaderSource(shaderId, source);
    // Compile the shader
    GL20.glCompileShader(shaderId);//w  ww  .j  av a 2s  .c  o  m
    // Check for a shader compile error
    if (GL20.glGetShaderi(shaderId, GL_COMPILE_STATUS) == GL_FALSE) {
        // Get the shader info log length
        final int logLength = GL20.glGetShaderi(shaderId, GL20.GL_INFO_LOG_LENGTH);
        throw new IllegalStateException(
                "OpenGL Error: Could not compile shader\n" + GL20.glGetShaderInfoLog(shaderId, logLength));
    }
    // Attach the shader
    GL20.glAttachShader(id, shaderId);
    // Add the shader to the program
    this.shaders.add(shaderId);
    // Check for errors
    RenderUtil.checkGLError();
}

From source file:net.betabears.the2dlibrary.graphics.shader.Shader.java

public void createShader() {
    id = GL20.glCreateShader(type);//w w  w.  j  a v a 2  s.  c o  m
    GL20.glShaderSource(id, text);
    GL20.glCompileShader(id);

    int i;
    while ((i = GL11.glGetError()) != 0) {
        LOGGER.log(Level.WARNING, "Error happened during creation of ShaderProgram. GL error code: {0}", i);
    }
}

From source file:net.neilcsmith.praxis.video.opengl.internal.ShaderProgram.java

License:Apache License

private int loadShader(int type, String source) {
    //      GL20 gl = Gdx.graphics.getGL20();
    ByteBuffer tmp = ByteBuffer.allocateDirect(4);
    tmp.order(ByteOrder.nativeOrder());
    IntBuffer intbuf = tmp.asIntBuffer();

    int shader = GL20.glCreateShader(type);
    if (shader == 0)
        return -1;

    GL20.glShaderSource(shader, source);
    GL20.glCompileShader(shader);//  w  w w  .ja va  2s . c  o  m
    GL20.glGetShader(shader, GL20.GL_COMPILE_STATUS, intbuf);

    int compiled = intbuf.get(0);
    if (compiled == 0) {
        GL20.glGetShader(shader, GL20.GL_INFO_LOG_LENGTH, intbuf);
        int infoLogLength = intbuf.get(0);
        if (infoLogLength > 1) {
            String infoLog = getShaderInfoLog(shader);
            log += infoLog;
        }
        return -1;
    }

    return shader;
}

From source file:net.smert.frameworkgl.opengl.helpers.ShaderHelper.java

License:Apache License

public void setSource(int shaderID, String code) {
    GL20.glShaderSource(shaderID, code);
}

From source file:opengl.test.object.object.java

protected final void vertexShader(String file) {
    this.vertexID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
    GL20.glShaderSource(this.vertexID, object.sourceLoader(file));
    GL20.glCompileShader(this.vertexID);
    if (GL20.glGetShaderi(this.vertexID, GL20.GL_COMPILE_STATUS) != GL11.GL_TRUE) {
        throw new RuntimeException("Khong the compile vertexShader");
    }//from  w ww.  j  a  v  a 2 s .co  m
    GL20.glAttachShader(this.programID, this.vertexID);
}

From source file:opengl.test.object.object.java

protected final void fragmentShader(String file) {
    this.fragmentID = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
    GL20.glShaderSource(this.fragmentID, object.sourceLoader(file));
    GL20.glCompileShader(this.fragmentID);
    if (GL20.glGetShaderi(this.fragmentID, GL20.GL_COMPILE_STATUS) != GL11.GL_TRUE) {
        throw new RuntimeException("Khong the compile fragmentShader");
    }//  w w  w.jav  a  2  s . co m
    GL20.glAttachShader(this.programID, this.fragmentID);

}

From source file:opengl.test.object.tree.testobject.leaf.java

private void vertexShader(String file) {
    this.vertexID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
    GL20.glShaderSource(this.vertexID, leaf.sourceLoader(file));
    GL20.glCompileShader(this.vertexID);
    if (GL20.glGetShaderi(this.vertexID, GL20.GL_COMPILE_STATUS) != GL11.GL_TRUE) {
        throw new RuntimeException("Khong the compile vertexShader");
    }//w w  w  .  j a  va  2  s.  c om
    GL20.glAttachShader(this.programID, this.vertexID);
}