Created
January 27, 2015 23:31
-
-
Save sugiartocokrowibowo/554a9ec318a28752dcd2 to your computer and use it in GitHub Desktop.
CG[LWJGL0017] || Shader Dasar
This file contains hidden or 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 latihanShader00; | |
| import java.io.BufferedReader; | |
| import java.io.FileReader; | |
| import org.lwjgl.opengl.GL11; | |
| import org.lwjgl.opengl.GL20; | |
| import static org.lwjgl.opengl.GL20.*; | |
| // class ini digunakan untuk meload dan mengcompile shader | |
| public class ShaderProgram { | |
| // OpenGL handle yang menunjuk ke executable shader program | |
| private int programId; | |
| public void init(String vertexShaderFilename, String fragmentShaderFilename){ | |
| // buat shader program. jika berhasil OK, buat vertex dan fragment shaders | |
| programId = glCreateProgram(); | |
| // load and compile shaders (vertex dan fragment) | |
| int vertShader = loadAndCompileShader(vertexShaderFilename, GL_VERTEX_SHADER); | |
| int fragShader = loadAndCompileShader(fragmentShaderFilename, GL_FRAGMENT_SHADER); | |
| // attach shaders yg sudah dikompilasi ke program | |
| glAttachShader(programId, vertShader); | |
| glAttachShader(programId, fragShader); | |
| // melakukan link program | |
| glLinkProgram(programId); | |
| //validasi link shader | |
| if (glGetProgrami(programId, GL_LINK_STATUS) == GL11.GL_FALSE){ | |
| throw new RuntimeException("tidak dapat melakukan link shader. Reason: " + glGetProgramInfoLog(programId, 1000)); | |
| } | |
| //melakukan validasi shader | |
| glValidateProgram(programId); | |
| if (glGetProgrami(programId, GL_VALIDATE_STATUS) == GL11.GL_FALSE){ | |
| throw new RuntimeException("Tidak dapat memvalidasi shader. Reason: " + glGetProgramInfoLog(programId, 1000)); | |
| } | |
| } | |
| /** | |
| * With the exception of syntax, setting up vertex and fragment shaders | |
| * is the same. | |
| * @param the name and path to the vertex shader | |
| */ | |
| private int loadAndCompileShader(String filename, int shaderType){ | |
| //vertShader akan bernilai != 0 juka shader berhasil dibuat | |
| int handle = glCreateShader(shaderType); | |
| if( handle == 0 ){ | |
| throw new RuntimeException("Tidak dapat membuat shader dengan type "+shaderType+" untuk file "+filename+". "+ glGetProgramInfoLog(programId, 1000)); | |
| } | |
| // me-load code dari file menjadi String Shader | |
| String code = loadFile(filename); | |
| // upload code ke OpenGL dan menggabungkan code dengan shader | |
| glShaderSource(handle, code); | |
| // meng-compile source code ke binary | |
| glCompileShader(handle); | |
| // acquire compilation status | |
| int shaderStatus = glGetShaderi(handle, GL20.GL_COMPILE_STATUS); | |
| //memeriksa apakah proses compile berhasil | |
| if( shaderStatus == GL11.GL_FALSE){ | |
| throw new IllegalStateException("kesalahan kompilasi shader ["+filename+"]. Info: " + glGetShaderInfoLog(handle, 1000)); | |
| } | |
| return handle; | |
| } | |
| /** | |
| * me-load sebuah file teks dan me-return String isi file yang berisi script shader. | |
| */ | |
| private String loadFile(String filename){ | |
| StringBuilder vertexCode = new StringBuilder(); | |
| String line = null ; | |
| try{ | |
| BufferedReader reader = new BufferedReader(new FileReader(filename)); | |
| while( (line = reader.readLine()) !=null ){ | |
| vertexCode.append(line); | |
| vertexCode.append('\n'); | |
| } | |
| reader.close(); | |
| }catch(Exception e){ | |
| throw new IllegalArgumentException("gagal me-load shader dari file ["+filename+"]", e); | |
| } | |
| return vertexCode.toString(); | |
| } | |
| public int getProgramId(){ | |
| return programId; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment