protected void readUniformsFromSource(GL2 gl, String source) { // read the uniforms defined by the shader source int samplerNumber = 0; Pattern uniformPattern = Pattern.compile("uniform\\s+(\\w+)\\s+([^;]+);"); Matcher matcher = uniformPattern.matcher(source); while (matcher.find()) { String[] names = matcher.group(2).split("\\s*,\\s*"); if (matcher.group(1).equals("sampler2D")) { // a sampler2D was found; set them to incremental integers (assume that they are defined by // texture order) for (String name : names) { gl.glUniform1i(gl.glGetUniformLocation(shaderProgram, name), samplerNumber++); } } else { for (String name : names) { // check if we have a setter for this uniform name IUniformSetter setter = setters.get(name); if (setter != null) { int uniformLocation = gl.glGetUniformLocation(shaderProgram, name); uniforms.put(uniformLocation, setter); } } } } }
@Override public void getLocations(GL2 gl) { vertexLocation = gl.glGetAttribLocation(id, "vertexin"); posdimLocation = gl.glGetUniformLocation(id, "posdim"); textureLocation = gl.glGetUniformLocation(id, "tex"); }
public void init(GLAutoDrawable drawable) { drawable.setGL(new DebugGL2(drawable.getGL().getGL2())); final GL2 gl = drawable.getGL().getGL2(); // drawable.getGL().getGL2(); gl.glViewport(0, 0, SCREENW, SCREENH); // Clear color buffer with black // gl.glClearColor(1.0f, 0.5f, 1.0f, 1.0f); gl.glClearColor(.0f, .0f, .0f, 1.0f); gl.glClearDepth(1.0f); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glEnable(GL2.GL_DEPTH_TEST); gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); gl.glCreateShader(GL2GL3.GL_VERTEX_SHADER); shader.init(gl); int programName = shader.getID(); gl.glBindAttribLocation(programName, Object3D.VERTEXPOSITION, "inposition"); gl.glBindAttribLocation(programName, Object3D.VERTEXCOLOR, "incolor"); gl.glBindAttribLocation(programName, Object3D.VERTEXNORMAL, "innormal"); gl.glBindAttribLocation(programName, Object3D.VERTEXTEXCOORD0, "intexcoord0"); shader.link(gl); uniformMat = gl.glGetUniformLocation(programName, "mat"); uniformLight = gl.glGetUniformLocation(programName, "lightdir"); gl.glUseProgram(programName); gl.glUniform3f(uniformLight, 0f, 10f, -10f); obj.init(gl, mats, programName); gl.glUseProgram(0); }
public void execute() { GL2 gl = GLContext.getCurrentGL().getGL2(); if (id == null) { id = gl.glGetUniformLocation(program, name); } if (id == -1) { return; } if (update) { update = false; } else { return; } if (ints != null) { switch (ints.length / size) { case 1: gl.glUniform1iv(id, size, ints, 0); break; case 2: gl.glUniform2iv(id, size, ints, 0); break; case 3: gl.glUniform3iv(id, size, ints, 0); break; case 4: gl.glUniform4iv(id, size, ints, 0); break; } } else if (floats != null) { switch (floats.length / size) { case 1: gl.glUniform1fv(id, size, floats, 0); break; case 2: gl.glUniform2fv(id, size, floats, 0); break; case 3: gl.glUniform3fv(id, size, floats, 0); break; case 4: gl.glUniform4fv(id, size, floats, 0); break; } } else if (vectors != null) { gl.glUniform4fv(id, size, vectorToArray(vectors), 0); } else if (matrices != null) { gl.glUniformMatrix4fv(id, size, false, matrixToArray(matrices), 0); } }
public void setTextureUnit(GL2 gl, String texname, int texunit) { int[] params = new int[] {0}; gl.glGetProgramiv(_progId, GL2.GL_LINK_STATUS, params, 0); if (params[0] != 1) { System.err.println("Error: setTextureUnit needs program to be linked."); } int id = gl.glGetUniformLocation(_progId, texname); if (id == -1) { System.err.println("Warning: Invalid texture " + texname); return; } gl.glUniform1i(id, texunit); }
public void setUniform(GL2 gl, String name, float[] val, int count) { int id = gl.glGetUniformLocation(_progId, name); if (id == -1) { System.err.println("Warning: Invalid uniform parameter " + name); return; } switch (count) { case 1: gl.glUniform1fv(id, 1, val, 0); break; case 2: gl.glUniform2fv(id, 1, val, 0); break; case 3: gl.glUniform3fv(id, 1, val, 0); break; case 4: gl.glUniform4fv(id, 1, val, 0); break; } }