Small refactors and Log::Success function
This commit is contained in:
@@ -39,6 +39,7 @@ public class Shader {
|
|||||||
@Getter @Setter private int vertexID;
|
@Getter @Setter private int vertexID;
|
||||||
@Getter @Setter private int fragmentID;
|
@Getter @Setter private int fragmentID;
|
||||||
@Getter @Setter private int shaderProgramID;
|
@Getter @Setter private int shaderProgramID;
|
||||||
|
@Getter @Setter private int vaoID, vboID, eboID;
|
||||||
|
|
||||||
private float[] vertexArray = {
|
private float[] vertexArray = {
|
||||||
0.5f, -0.5f, 0.0f, /* */ 1.0f, 0.0f, 0.0f, 1.0f, //
|
0.5f, -0.5f, 0.0f, /* */ 1.0f, 0.0f, 0.0f, 1.0f, //
|
||||||
@@ -52,8 +53,6 @@ public class Shader {
|
|||||||
0, 1, 3
|
0, 1, 3
|
||||||
};
|
};
|
||||||
|
|
||||||
@Getter private int vaoID, vboID, eboID;
|
|
||||||
|
|
||||||
public Shader(String fragPath, String vertPath) {
|
public Shader(String fragPath, String vertPath) {
|
||||||
this.fragmentSource = readFromFile(fragPath);
|
this.fragmentSource = readFromFile(fragPath);
|
||||||
this.vertexSource = readFromFile(vertPath);
|
this.vertexSource = readFromFile(vertPath);
|
||||||
@@ -97,21 +96,23 @@ public class Shader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createProgram() {
|
private void createProgram() {
|
||||||
setShaderProgramID(glCreateProgram());
|
int id = glCreateProgram();
|
||||||
glAttachShader(shaderProgramID, getVertexID());
|
glAttachShader(id, getVertexID());
|
||||||
glAttachShader(shaderProgramID, getFragmentID());
|
glAttachShader(id, getFragmentID());
|
||||||
glLinkProgram(shaderProgramID);
|
glLinkProgram(id);
|
||||||
|
|
||||||
int success = glGetProgrami(getShaderProgramID(), GL_LINK_STATUS);
|
int success = glGetProgrami(id, GL_LINK_STATUS);
|
||||||
if (success == GL_FALSE) {
|
if (success == GL_FALSE) {
|
||||||
int len = glGetProgrami(getShaderProgramID(), GL_INFO_LOG_LENGTH);
|
int len = glGetProgrami(id, GL_INFO_LOG_LENGTH);
|
||||||
|
|
||||||
Log.error(
|
Log.error(
|
||||||
"Shader initialisation",
|
"Shader initialisation",
|
||||||
String.format(
|
String.format("Failed to create Shader Program: %s", glGetShaderInfoLog(id, len)));
|
||||||
"Failed to create Shader Program: %s",
|
} else {
|
||||||
glGetShaderInfoLog(getShaderProgramID(), len)));
|
Log.success("Shader initialisation", "Shader Program created.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setShaderProgramID(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readFromFile(String stringFilePath) {
|
private String readFromFile(String stringFilePath) {
|
||||||
@@ -134,21 +135,21 @@ public class Shader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void thingy() {
|
private void thingy() {
|
||||||
this.vaoID = glGenVertexArrays();
|
setVaoID(glGenVertexArrays());
|
||||||
glBindVertexArray(this.vaoID);
|
glBindVertexArray(getVaoID());
|
||||||
|
|
||||||
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(this.vertexArray.length);
|
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(this.vertexArray.length);
|
||||||
vertexBuffer.put(this.vertexArray).flip();
|
vertexBuffer.put(this.vertexArray).flip();
|
||||||
|
|
||||||
this.vboID = glGenBuffers();
|
setVboID(glGenBuffers());
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, this.vboID);
|
glBindBuffer(GL_ARRAY_BUFFER, getVboID());
|
||||||
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
|
||||||
|
|
||||||
IntBuffer elementBuffer = BufferUtils.createIntBuffer(this.elementArray.length);
|
IntBuffer elementBuffer = BufferUtils.createIntBuffer(this.elementArray.length);
|
||||||
elementBuffer.put(this.elementArray).flip();
|
elementBuffer.put(this.elementArray).flip();
|
||||||
|
|
||||||
this.eboID = glGenBuffers();
|
setEboID(glGenBuffers());
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.eboID);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getEboID());
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
|
||||||
|
|
||||||
int positionsSize = 3;
|
int positionsSize = 3;
|
||||||
@@ -163,9 +164,17 @@ public class Shader {
|
|||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void use() {
|
||||||
|
glUseProgram(getShaderProgramID());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void detach() {
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
glUseProgram(this.shaderProgramID);
|
glUseProgram(getShaderProgramID());
|
||||||
glBindVertexArray(this.vaoID);
|
glBindVertexArray(getVaoID());
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
glDrawElements(GL_TRIANGLES, this.elementArray.length, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, this.elementArray.length, GL_UNSIGNED_INT, 0);
|
||||||
|
|||||||
@@ -25,6 +25,13 @@ public final class Log {
|
|||||||
System.err.println(fancyError + fancyErrorStage + errorDescription);
|
System.err.println(fancyError + fancyErrorStage + errorDescription);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void success(String successStage, String successDescription) {
|
||||||
|
String fancySuccess = String.format("[%s] ", colourisedString(Colours.GREEN, "SUCCESS"));
|
||||||
|
String fancySucessStage =
|
||||||
|
colourisedString(Colours.YELLOW, String.format("<%s> ", successStage));
|
||||||
|
System.out.println(fancySuccess + fancySucessStage + successDescription);
|
||||||
|
}
|
||||||
|
|
||||||
private static String colourisedString(String colour, String string) {
|
private static String colourisedString(String colour, String string) {
|
||||||
return colour + string + Colours.ANSI_RESET;
|
return colour + string + Colours.ANSI_RESET;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user