private static int setupProgram(String vShaderPath, String fShaderPath) { int program = glCreateProgramObjectARB(); int vShader = 0; int fShader = 0; if (program != 0) { vShader = createVertShader(vShaderPath); fShader = createFragShader(fShaderPath); } if (vShader != 0 || fShader != 0) { if (vShader != 0) { glAttachObjectARB(program, vShader); } if (fShader != 0) { glAttachObjectARB(program, fShader); } if (entityAttrib >= 0) { glBindAttribLocationARB(program, entityAttrib, "mc_Entity"); } glLinkProgramARB(program); glValidateProgramARB(program); printLogInfo(program); } else if (program != 0) { glDeleteObjectARB(program); program = 0; } return program; }
public static void destroy() { for (int i = 0; i < ProgramCount; ++i) { if (programs[i] != 0) { glDeleteObjectARB(programs[i]); programs[i] = 0; } } }
private static int createVertShader(String filename) { int vertShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); if (vertShader == 0) { return 0; } String vertexCode = ""; String line; BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader((Shaders.class).getResourceAsStream(filename))); } catch (Exception e) { try { reader = new BufferedReader(new FileReader(new File(filename))); } catch (Exception e2) { System.out.println("Couldn't open " + filename + "!"); glDeleteObjectARB(vertShader); return 0; } } try { while ((line = reader.readLine()) != null) { vertexCode += line + "\n"; if (line.matches("attribute [_a-zA-Z0-9]+ mc_Entity.*")) { entityAttrib = 10; } } } catch (Exception e) { System.out.println("Couldn't read " + filename + "!"); glDeleteObjectARB(vertShader); return 0; } glShaderSourceARB(vertShader, vertexCode); glCompileShaderARB(vertShader); printLogInfo(vertShader); return vertShader; }
private static int createFragShader(String filename) { int fragShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); if (fragShader == 0) { return 0; } String fragCode = ""; String line; BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader((Shaders.class).getResourceAsStream(filename))); } catch (Exception e) { try { reader = new BufferedReader(new FileReader(new File(filename))); } catch (Exception e2) { System.out.println("Couldn't open " + filename + "!"); glDeleteObjectARB(fragShader); return 0; } } try { while ((line = reader.readLine()) != null) { fragCode += line + "\n"; if (colorAttachments < 5 && line.matches("uniform [ _a-zA-Z0-9]+ gaux1;.*")) { colorAttachments = 5; } else if (colorAttachments < 6 && line.matches("uniform [ _a-zA-Z0-9]+ gaux2;.*")) { colorAttachments = 6; } else if (colorAttachments < 7 && line.matches("uniform [ _a-zA-Z0-9]+ gaux3;.*")) { colorAttachments = 7; } else if (colorAttachments < 8 && line.matches("uniform [ _a-zA-Z0-9]+ shadow;.*")) { shadowPassInterval = 1; colorAttachments = 8; } else if (line.matches("/\\* SHADOWRES:[0-9]+ \\*/.*")) { String[] parts = line.split("(:| )", 4); System.out.println("Shadow map resolution: " + parts[2]); shadowMapWidth = shadowMapHeight = Integer.parseInt(parts[2]); } else if (line.matches("/\\* SHADOWFOV:[0-9\\.]+ \\*/.*")) { String[] parts = line.split("(:| )", 4); System.out.println("Shadow map field of view: " + parts[2]); shadowMapFOV = Float.parseFloat(parts[2]); shadowMapIsOrtho = false; } else if (line.matches("/\\* SHADOWHPL:[0-9\\.]+ \\*/.*")) { String[] parts = line.split("(:| )", 4); System.out.println("Shadow map half-plane: " + parts[2]); shadowMapHalfPlane = Float.parseFloat(parts[2]); shadowMapIsOrtho = true; } } } catch (Exception e) { System.out.println("Couldn't read " + filename + "!"); glDeleteObjectARB(fragShader); return 0; } glShaderSourceARB(fragShader, fragCode); glCompileShaderARB(fragShader); printLogInfo(fragShader); return fragShader; }