Created
January 28, 2012 07:15
-
-
Save fkaa/1693188 to your computer and use it in GitHub Desktop.
Loads a specified shader from "/minecraft/shaders/"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.minecraft.src; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.nio.IntBuffer; | |
import static org.lwjgl.opengl.GL11.*; | |
import static org.lwjgl.opengl.GL20.*; | |
import org.lwjgl.opengl.ContextCapabilities; | |
import org.lwjgl.BufferUtils; | |
public class ShaderHelper { | |
private int vertex_shader = 0; | |
private int fragment_shader = 0; | |
private int program = 0; | |
public ShaderHelper() { | |
program = glCreateProgram(); | |
} | |
public ShaderHelper addShader(String shader, int type) { | |
String line = ""; | |
String source = ""; | |
String filepath = ModLoader.getMinecraftInstance().getMinecraftDir().getAbsolutePath() + "/shaders/" + shader; | |
BufferedReader reader; | |
try { | |
reader = new BufferedReader(new FileReader(filepath)); | |
while((line = reader.readLine()) != null) { | |
source += line + "\n"; | |
} | |
} catch(Exception e) { | |
throw new RuntimeException("Unable to load shader from: " + filepath); | |
} | |
if(type == GL_VERTEX_SHADER) { | |
vertex_shader = compile(GL_VERTEX_SHADER, source); | |
if (this.vertex_shader == 0) System.out.println("Error Vertex Shader!"); | |
link(filepath, vertex_shader); | |
} else if(type == GL_FRAGMENT_SHADER) { | |
fragment_shader = compile(GL_FRAGMENT_SHADER, source); | |
if (this.fragment_shader == 0) System.out.println("Error Fragment Shader!"); | |
link(filepath, fragment_shader); | |
} else { | |
System.out.println("Shader type \"" + type + "\" cannot be resolved."); | |
} | |
return this; | |
} | |
private int compile(int type, String shader_source) { | |
System.out.println("Compiling: " + type); | |
int shader = glCreateShader(type); | |
if (shader == 0) { | |
System.out.println("Cannot create shader."); | |
return 0; | |
} | |
glShaderSource(shader, shader_source); | |
glCompileShader(shader); | |
System.out.println(shader); | |
return shader; | |
} | |
private void link(String source, int sort) { | |
System.out.println("linking: " + sort); | |
glAttachShader(this.program, sort); | |
glLinkProgram(this.program); | |
IntBuffer result = BufferUtils.createIntBuffer(1); | |
System.out.println(result); | |
glGetProgram(this.program, GL_LINK_STATUS, result); | |
if (result.get(0) != 1) { | |
throw new RuntimeException("Error while compiling shader. \n\nin: " + source + "\n\n" + glGetProgramInfoLog(this.program, 5000)); | |
} | |
System.out.println(glGetProgramInfoLog(this.program, 5000)); | |
} | |
public void bind() { | |
glUseProgram(this.program); | |
} | |
public void unbind() { | |
glUseProgram(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment