-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.java
105 lines (86 loc) · 3.63 KB
/
Shader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This code is from exercise sheet written by Dr. Steve Maddock */
import gmaths.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.Charset;
import com.jogamp.opengl.*;
import com.jogamp.opengl.util.glsl.*;
public class Shader {
private static final boolean DISPLAY_SHADERS = false;
private int ID;
private String vertexShaderSource;
private String fragmentShaderSource;
/* The constructor */
public Shader(GL3 gl3, String vertexPath, String fragmentPath) {
try {
vertexShaderSource = new String(Files.readAllBytes(Paths.get(vertexPath)), Charset.defaultCharset());
fragmentShaderSource = new String(Files.readAllBytes(Paths.get(fragmentPath)), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
if (DISPLAY_SHADERS) display();
ID = compileAndLink(gl3);
}
public int getID() {
return ID;
}
public void use(GL3 gl3) {
gl3.glUseProgram(ID);
}
public void setInt(GL3 gl3, String name, int value) {
int location = gl3.glGetUniformLocation(ID, name);
gl3.glUniform1i(location, value);
}
public void setFloat(GL3 gl3, String name, float value) {
int location = gl3.glGetUniformLocation(ID, name);
gl3.glUniform1f(location, value);
}
public void setFloat(GL3 gl3, String name, float f1, float f2) {
int location = gl3.glGetUniformLocation(ID, name);
gl3.glUniform2f(location, f1, f2);
}
public void setFloat(GL3 gl3, String name, float f1, float f2, float f3) {
int location = gl3.glGetUniformLocation(ID, name);
gl3.glUniform3f(location, f1, f2, f3);
}
public void setFloat(GL3 gl, String name, float f1, float f2, float f3, float f4) {
int location = gl.glGetUniformLocation(ID, name);
gl.glUniform4f(location, f1, f2, f3, f4);
}
public void setFloatArray(GL3 gl3, String name, float[] f) {
int location = gl3.glGetUniformLocation(ID, name);
gl3.glUniformMatrix4fv(location, 1, false, f, 0);
}
public void setVec3(GL3 gl3, String name, Vec3 v) {
int location = gl3.glGetUniformLocation(ID, name);
gl3.glUniform3f(location, v.x, v.y, v.z);
}
private void display() {
System.out.println("***Vertex shader***");
System.out.println(vertexShaderSource);
System.out.println("\n***Fragment shader***");
System.out.println(fragmentShaderSource);
}
private int compileAndLink(GL3 gl3) {
String[][] sources = new String[1][1];
sources[0] = new String[]{vertexShaderSource};
ShaderCode vertexShaderCode = new ShaderCode(GL3.GL_VERTEX_SHADER, sources.length, sources);
boolean compiled = vertexShaderCode.compile(gl3, System.err);
if (!compiled)
System.err.println("[error] Unable to compile vertex shader: " + sources[0][0]);
sources[0] = new String[]{fragmentShaderSource};
ShaderCode fragmentShaderCode = new ShaderCode(GL3.GL_FRAGMENT_SHADER, sources.length, sources);
compiled = fragmentShaderCode.compile(gl3, System.err);
if (!compiled)
System.err.println("[error] Unable to compile fragment shader: " + sources[0][0]);
ShaderProgram program = new ShaderProgram();
program.init(gl3);
program.add(vertexShaderCode);
program.add(fragmentShaderCode);
program.link(gl3, System.out);
if (!program.validateProgram(gl3, System.out))
System.err.println("[error] Unable to link program");
return program.program();
}
}