Java Code Examples for org.lwjgl.opengl.GL20#glAttachShader()

The following examples show how to use org.lwjgl.opengl.GL20#glAttachShader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericShader.java    From LWJGUI with MIT License 6 votes vote down vote up
protected static int createProgram(int vertexShaderId, int[] fragmentShaderIds, String[] attrs, int[] indices) {

		// build the shader program
		int id = GL20.glCreateProgram();
		GL20.glAttachShader(id, vertexShaderId);
		for (int fragmentShaderId : fragmentShaderIds) {
			GL20.glAttachShader(id, fragmentShaderId);
		}

		assert (attrs.length == indices.length);
		for (int i=0; i<attrs.length; i++) {
			GL20.glBindAttribLocation(id, indices[i], attrs[i]);
		}

		GL20.glLinkProgram(id);
		boolean isSuccess = GL20.glGetProgrami(id, GL20.GL_LINK_STATUS) == GL11.GL_TRUE;
		if (!isSuccess) {
			throw new RuntimeException("Shader program did not link:\n" + GL20.glGetProgramInfoLog(id, 4096));
		}

		return id;
	}
 
Example 2
Source File: ShaderProgram.java    From LowPolyWater with The Unlicense 6 votes vote down vote up
public ShaderProgram(MyFile vertexFile, MyFile geometryFile, MyFile fragmentFile, String... inVariables) {
	int vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
	int geometryShaderID = loadShader(geometryFile, GL32.GL_GEOMETRY_SHADER);
	int fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
	programID = GL20.glCreateProgram();
	GL20.glAttachShader(programID, vertexShaderID);
	GL20.glAttachShader(programID, geometryShaderID);
	GL20.glAttachShader(programID, fragmentShaderID);
	bindAttributes(inVariables);
	GL20.glLinkProgram(programID);
	GL20.glDetachShader(programID, vertexShaderID);
	GL20.glDetachShader(programID, geometryShaderID);
	GL20.glDetachShader(programID, fragmentShaderID);
	GL20.glDeleteShader(vertexShaderID);
	GL20.glDeleteShader(geometryShaderID);
	GL20.glDeleteShader(fragmentShaderID);
}
 
Example 3
Source File: OpenGL3_TheQuadTextured.java    From ldparteditor with MIT License 6 votes vote down vote up
private void setupShaders() {       
    // Load the vertex shader
    vsId = this.loadShader("src/thequad/vertex_textured.glsl", GL20.GL_VERTEX_SHADER);
    // Load the fragment shader
    fsId = this.loadShader("src/thequad/fragment_textured.glsl", GL20.GL_FRAGMENT_SHADER);
     
    // Create a new shader program that links both shaders
    pId = GL20.glCreateProgram();
    GL20.glAttachShader(pId, vsId);
    GL20.glAttachShader(pId, fsId);
 
    // Position information will be attribute 0
    GL20.glBindAttribLocation(pId, 0, "in_Position");
    // Color information will be attribute 1
    GL20.glBindAttribLocation(pId, 1, "in_Color");
    // Textute information will be attribute 2
    GL20.glBindAttribLocation(pId, 2, "in_TextureCoord");
     
    GL20.glLinkProgram(pId);
    GL20.glValidateProgram(pId);
     
    this.exitOnGLError("setupShaders");
}
 
Example 4
Source File: GLShader.java    From ldparteditor with MIT License 6 votes vote down vote up
public GLShader(final String vertexPath, final String fragmentPath) {
    final int vertex = createAndCompile(vertexPath, GL20.GL_VERTEX_SHADER);
    final int fragment = createAndCompile(fragmentPath, GL20.GL_FRAGMENT_SHADER);

    program = GL20.glCreateProgram();
    GL20.glAttachShader(program, fragment);
    GL20.glAttachShader(program, vertex);

    GL20.glLinkProgram(program);

    // FIXME Extract parameter locations
    // int baseImageLoc = GL20.glGetUniformLocation(program, "colorMap"); //$NON-NLS-1$

    if (GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
        NLogger.error(GLShader.class, "Could not link shader: " + GL20.glGetProgramInfoLog(program, 1024)); //$NON-NLS-1$;
    }

    GL20.glDetachShader(program, fragment);
    GL20.glDetachShader(program, vertex);
    GL20.glDeleteShader(fragment);
    GL20.glDeleteShader(vertex);
}
 
Example 5
Source File: DesktopComputeJobManager.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
@Override
public void createAndLinkComputeProgram() {
  program = GL20.glCreateProgram();
  checkError();
  GL20.glAttachShader(program, shader);
  checkError();
  GL20.glLinkProgram(program);
  checkError();
}
 
Example 6
Source File: ShaderProgram.java    From LowPolyWater with The Unlicense 5 votes vote down vote up
public ShaderProgram(MyFile vertexFile, MyFile fragmentFile, String... inVariables) {
	int vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
	int fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
	programID = GL20.glCreateProgram();
	GL20.glAttachShader(programID, vertexShaderID);
	GL20.glAttachShader(programID, fragmentShaderID);
	bindAttributes(inVariables);
	GL20.glLinkProgram(programID);
	GL20.glDetachShader(programID, vertexShaderID);
	GL20.glDetachShader(programID, fragmentShaderID);
	GL20.glDeleteShader(vertexShaderID);
	GL20.glDeleteShader(fragmentShaderID);
}
 
Example 7
Source File: ShaderProgram.java    From OpenGL-Animation with The Unlicense 5 votes vote down vote up
public ShaderProgram(MyFile vertexFile, MyFile fragmentFile, String... inVariables) {
	int vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
	int fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
	programID = GL20.glCreateProgram();
	GL20.glAttachShader(programID, vertexShaderID);
	GL20.glAttachShader(programID, fragmentShaderID);
	bindAttributes(inVariables);
	GL20.glLinkProgram(programID);
	GL20.glDetachShader(programID, vertexShaderID);
	GL20.glDetachShader(programID, fragmentShaderID);
	GL20.glDeleteShader(vertexShaderID);
	GL20.glDeleteShader(fragmentShaderID);
}
 
Example 8
Source File: OpenGLRenderer20.java    From ldparteditor with MIT License 4 votes vote down vote up
/**
 * Initializes the Scene and gives OpenGL-Hints
 */
@Override
public void init() {
    // MARK OpenGL Hints and Initialization
    GL11.glDepthMask(true);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    GL11.glEnable(GL11.GL_TEXTURE_2D);

    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glDepthFunc(GL11.GL_LESS);

    GL11.glClearDepth(1.0f);
    GL11.glClearColor(View.background_Colour_r[0], View.background_Colour_g[0], View.background_Colour_b[0], 1.0f);

    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glPointSize(5);
    // GL11.glLineWidth(2);

    GL11.glEnable(GL11.GL_LIGHTING);
    GL11.glShadeModel(GL11.GL_SMOOTH);

    GL11.glEnable(GL11.GL_NORMALIZE);

    GL11.glEnable(GL11.GL_LIGHT0);
    GL11.glEnable(GL11.GL_LIGHT1);
    GL11.glEnable(GL11.GL_LIGHT2);
    GL11.glEnable(GL11.GL_LIGHT3);

    GL11.glLightModelfv(GL11.GL_LIGHT_MODEL_AMBIENT, BufferFactory.floatBuffer(new float[] { 0.1f, 0.1f, 0.1f, 1f }));

    GL11.glLightfv(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, BufferFactory.floatBuffer(new float[] { View.light1_Colour_r[0], View.light1_Colour_g[0], View.light1_Colour_b[0], 1f }));
    GL11.glLightfv(GL11.GL_LIGHT0, GL11.GL_SPECULAR, BufferFactory.floatBuffer(new float[] { View.light1_specular_Colour_r[0], View.light1_specular_Colour_g[0], View.light1_specular_Colour_b[0], 1f }));
    GL11.glLightf(GL11.GL_LIGHT0, GL11.GL_LINEAR_ATTENUATION, .001f);

    GL11.glLightfv(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, BufferFactory.floatBuffer(new float[] { View.light2_Colour_r[0], View.light2_Colour_g[0], View.light2_Colour_b[0], 1f }));
    GL11.glLightfv(GL11.GL_LIGHT1, GL11.GL_SPECULAR, BufferFactory.floatBuffer(new float[] { View.light2_specular_Colour_r[0], View.light2_specular_Colour_g[0], View.light2_specular_Colour_b[0], 1f }));
    GL11.glLightf(GL11.GL_LIGHT1, GL11.GL_LINEAR_ATTENUATION, .001f);

    GL11.glLightfv(GL11.GL_LIGHT2, GL11.GL_DIFFUSE, BufferFactory.floatBuffer(new float[] { View.light3_Colour_r[0], View.light3_Colour_g[0], View.light3_Colour_b[0], 1f }));
    GL11.glLightfv(GL11.GL_LIGHT2, GL11.GL_SPECULAR, BufferFactory.floatBuffer(new float[] { View.light3_specular_Colour_r[0], View.light3_specular_Colour_g[0], View.light3_specular_Colour_b[0], 1f }));
    GL11.glLightf(GL11.GL_LIGHT2, GL11.GL_LINEAR_ATTENUATION, .001f);

    GL11.glLightfv(GL11.GL_LIGHT3, GL11.GL_DIFFUSE, BufferFactory.floatBuffer(new float[] { View.light4_Colour_r[0], View.light4_Colour_g[0], View.light4_Colour_b[0], 1f }));
    GL11.glLightfv(GL11.GL_LIGHT3, GL11.GL_SPECULAR, BufferFactory.floatBuffer(new float[] { View.light4_specular_Colour_r[0], View.light4_specular_Colour_g[0], View.light4_specular_Colour_b[0], 1f }));
    GL11.glLightf(GL11.GL_LIGHT3, GL11.GL_LINEAR_ATTENUATION, .001f);

    GL11.glEnable(GL11.GL_COLOR_MATERIAL);
    GL11.glColorMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE);

    GL11.glMaterialfv(GL11.GL_FRONT, GL11.GL_SPECULAR, BufferFactory.floatBuffer(new float[] { 1.0f, 1.0f, 1.0f, 1.0f }));
    GL11.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS, 128f);


    GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

    if (fsGlossId == -1) {
        vsGlossId = loadGlossVertexShader();
        fsGlossId = loadGlossFragmentShader();
        if (pGlossId == -1 && vsGlossId != -1 && fsGlossId != -1) {
            pGlossId = GL20.glCreateProgram();
            GL20.glAttachShader(pGlossId, vsGlossId);
            GL20.glAttachShader(pGlossId, fsGlossId);
            GL20.glLinkProgram(pGlossId);
            GL20.glValidateProgram(pGlossId);
            baseImageLoc = GL20.glGetUniformLocation(pGlossId, "colorMap"); //$NON-NLS-1$
            glossMapLoc = GL20.glGetUniformLocation(pGlossId, "glossMap"); //$NON-NLS-1$
            cubeMapLoc = GL20.glGetUniformLocation(pGlossId, "cubeMap"); //$NON-NLS-1$
            cubeMapMatteLoc = GL20.glGetUniformLocation(pGlossId, "cubeMapMatte"); //$NON-NLS-1$
            cubeMapMetalLoc = GL20.glGetUniformLocation(pGlossId, "cubeMapMetal"); //$NON-NLS-1$
            alphaSwitchLoc = GL20.glGetUniformLocation(pGlossId, "alphaSwitch"); //$NON-NLS-1$
            normalSwitchLoc = GL20.glGetUniformLocation(pGlossId, "normalSwitch"); //$NON-NLS-1$
            noTextureSwitch = GL20.glGetUniformLocation(pGlossId, "noTextureSwitch"); //$NON-NLS-1$
            noGlossMapSwitch = GL20.glGetUniformLocation(pGlossId, "noGlossMapSwitch"); //$NON-NLS-1$
            cubeMapSwitch = GL20.glGetUniformLocation(pGlossId, "cubeMapSwitch"); //$NON-NLS-1$
            noLightSwitch = GL20.glGetUniformLocation(pGlossId, "noLightSwitch"); //$NON-NLS-1$
        }
    }
}
 
Example 9
Source File: LWJGL20DrawContext.java    From settlers-remake with MIT License 4 votes vote down vote up
private ShaderProgram(String name) {
	int vertexShader = -1;
	int fragmentShader;

	String vname = name;
	if(name.contains("-")) vname = name.split("-")[0];

	try {
		vertexShader = createShader(vname+".vert", GL20.GL_VERTEX_SHADER);
		fragmentShader = createShader(name+".frag", GL20.GL_FRAGMENT_SHADER);
	} catch (IOException e) {
		e.printStackTrace();

		if(vertexShader != -1) GL20.glDeleteShader(vertexShader);
		throw new Error("could not read shader files", e);
	}

	program = GL20.glCreateProgram();
	setObjectLabel(KHRDebug.GL_PROGRAM, program, name);

	GL20.glAttachShader(program, vertexShader);
	GL20.glAttachShader(program, fragmentShader);

	GL20.glBindAttribLocation(program, 0, "vertex");
	GL20.glBindAttribLocation(program, 1, "texcoord");
	GL20.glBindAttribLocation(program, 2, "color");

	GL20.glLinkProgram(program);
	GL20.glValidateProgram(program);

	GL20.glDetachShader(program, vertexShader);
	GL20.glDetachShader(program, fragmentShader);
	GL20.glDeleteShader(vertexShader);
	GL20.glDeleteShader(fragmentShader);

	String log = GL20.glGetProgramInfoLog(program);
	if(debugOutput != null && !log.isEmpty()) System.out.print("info log of " + name + "=====\n" + log + "==== end\n");

	if(GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) == 0) {

		GL20.glDeleteProgram(program);
		throw new Error("Could not link " + name);
	}

	for(int i = 0;i != ufs.length;i++) {
		int uf = GL20.glGetUniformLocation(program, uniform_names[i]);
		ufs[i] = uf;
	}
	shaders.add(this);
}
 
Example 10
Source File: ShaderHelper.java    From OpenModsLib with MIT License 4 votes vote down vote up
@Override
public void glAttachShader(int program, int shader) {
	GL20.glAttachShader(program, shader);
}