// Creates program object, attaches shaders, and links into pipeline protected int createProgram(String vertexSource, String fragmentSource) { // Load the vertex and fragment shaders int vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource); int fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource); // Create the program object int program = glCreateProgram(); if (vertexShader == 0 || fragmentShader == 0 || program == 0) return 0; if (program != 0) { glAttachShader(program, vertexShader); ctx.checkGLError("createProgram Attaching vertex shader"); glAttachShader(program, fragmentShader); ctx.checkGLError("createProgram Attaching fragment shader"); glLinkProgram(program); int linkStatus = glGetProgram(program, GL_LINK_STATUS); if (linkStatus != GL_TRUE) { log().error("Could not link program: "); log().error(glGetProgramInfoLog(program, SHADER_INFO_LOG_LEN)); glDeleteProgram(program); program = 0; } } return program; }
protected boolean prepare(int fbufWidth, int fbufHeight) { if (ctx.useShader(this) && glIsProgram(program)) { glUseProgram(program); ctx.checkGLError("Shader.prepare useProgram"); glUniform2f(uScreenSizeLoc, fbufWidth, fbufHeight); // ctx.checkGLError("Shader.prepare uScreenSizeLoc set to " + fbufWidth + " " + fbufHeight); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); ctx.checkGLError("Shader.prepare BindBuffer"); glEnableVertexAttribArray(aMatrix); glEnableVertexAttribArray(aTranslation); glEnableVertexAttribArray(aPosition); if (aTexture != -1) glEnableVertexAttribArray(aTexture); ctx.checkGLError("Shader.prepare AttribArrays enabled"); glVertexAttribPointer(aMatrix, 4, GL_FLOAT, false, VERTEX_STRIDE, 0); glVertexAttribPointer(aTranslation, 2, GL_FLOAT, false, VERTEX_STRIDE, 16); glVertexAttribPointer(aPosition, 2, GL_FLOAT, false, VERTEX_STRIDE, 24); if (aTexture != -1) glVertexAttribPointer(aTexture, 2, GL_FLOAT, false, VERTEX_STRIDE, 32); ctx.checkGLError("Shader.prepare AttribPointer"); return true; } return false; }
@Override public void flush() { if (vertexOffset == 0) { return; } ctx.checkGLError("Shader.flush"); vertexData.position(0); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STREAM_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementData, GL_STREAM_DRAW); ctx.checkGLError("Shader.flush BufferData"); glDrawElements(GL_TRIANGLES, elementOffset, GL_UNSIGNED_SHORT, 0); vertexOffset = elementOffset = 0; ctx.checkGLError("Shader.flush DrawElements"); }