Last active
January 25, 2020 10:42
-
-
Save matyklug18/8d44bf2b990a853b952d073be8888062 to your computer and use it in GitHub Desktop.
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 opencraft.engine.renderers; | |
import opencraft.data.GUIMesh; | |
import opencraft.data.Vertex; | |
import opencraft.engine.Window; | |
import opencraft.engine.shaders.QuadShader; | |
import opencraft.util.MatrixUtil; | |
import org.joml.Matrix4f; | |
import org.joml.Vector2f; | |
import org.joml.Vector3f; | |
import static org.lwjgl.opengl.GL11.*; | |
import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; | |
import static org.lwjgl.opengl.GL15.glBindBuffer; | |
import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; | |
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; | |
import static org.lwjgl.opengl.GL30.glBindVertexArray; | |
public class QuadRenderer { | |
QuadShader shader; | |
Window win; | |
private static final float Z_NEAR = 0.01f; | |
private static final float Z_FAR = 5001.f; | |
public QuadRenderer(Window win, String frag) { | |
this.win = win; | |
this.shader = new QuadShader(frag).create(); | |
} | |
GUIMesh mesh = new GUIMesh(new Vertex[] { | |
//Back face | |
new Vertex(new Vector3f(-1, 1,0), new Vector3f(0,0,-1), new Vector2f(0,0)), | |
new Vertex(new Vector3f(-1,-1,0), new Vector3f(0,0,-1), new Vector2f(0,1)), | |
new Vertex(new Vector3f( 1,-1,0), new Vector3f(0,0,-1), new Vector2f(1,1)), | |
new Vertex(new Vector3f( 1, 1,0), new Vector3f(0,0,-1), new Vector2f(1,0)), | |
}, new int[] { | |
//Back face | |
0, 1, 3, | |
3, 1, 2, | |
}).create(); | |
public void renderMesh() { | |
glBindVertexArray(mesh.getVAOID()); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.getIBOID()); | |
shader.bind(); | |
Matrix4f projectionMatrix; | |
projectionMatrix = new Matrix4f().ortho(0, win.getXSize(), 0 , win.getYSize(), Z_NEAR, Z_FAR); | |
shader.setUniform("project", projectionMatrix); | |
Matrix4f modelViewMatrix = MatrixUtil.getModelViewMatrix(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); | |
shader.setUniform("model", modelViewMatrix); | |
glDrawElements(GL_TRIANGLES, mesh.getInds().size(), GL_UNSIGNED_INT, 0); | |
shader.unbind(); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
glDisableVertexAttribArray(0); | |
glDisableVertexAttribArray(1); | |
glBindVertexArray(0); | |
} | |
public void destroy() { | |
shader.destroy(); | |
} | |
} |
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
#version 460 core | |
out vec4 outColor; | |
void main() { | |
outColor = vec4(1, 0, 1, 1); | |
} |
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 opencraft.engine.shaders; | |
import opencraft.util.FileUtils; | |
import org.joml.Matrix4f; | |
import org.joml.Vector2f; | |
import org.joml.Vector3f; | |
import org.lwjgl.system.MemoryUtil; | |
import java.io.FileNotFoundException; | |
import java.nio.FloatBuffer; | |
import static org.lwjgl.opengl.GL30.*; | |
public class QuadShader { | |
private String vertexFile, fragmentFile; | |
private int vertexID, fragmentID, programID; | |
public QuadShader(String frag) { | |
try { | |
vertexFile = FileUtils.loadAsString("guiVertex.glsl"); | |
fragmentFile = FileUtils.loadAsString(frag); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
public QuadShader create() { | |
programID = glCreateProgram(); | |
vertexID = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vertexID, vertexFile); | |
glCompileShader(vertexID); | |
if (glGetShaderi(vertexID, GL_COMPILE_STATUS) == GL_FALSE) { | |
System.err.println("Vertex Shader: " + glGetShaderInfoLog(vertexID)); | |
return this; | |
} | |
fragmentID = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fragmentID, fragmentFile); | |
glCompileShader(fragmentID); | |
if (glGetShaderi(fragmentID, GL_COMPILE_STATUS) == GL_FALSE) { | |
System.err.println("Fragment Shader: " + glGetShaderInfoLog(fragmentID)); | |
return this; | |
} | |
glAttachShader(programID, vertexID); | |
glAttachShader(programID, fragmentID); | |
glLinkProgram(programID); | |
if (glGetProgrami(programID, GL_LINK_STATUS) == GL_FALSE) { | |
System.err.println("Program Linking: " + glGetProgramInfoLog(programID)); | |
return this; | |
} | |
glValidateProgram(programID); | |
if (glGetProgrami(programID, GL_VALIDATE_STATUS) == GL_FALSE) { | |
System.err.println("Program Validation: " + glGetProgramInfoLog(programID)); | |
return this; | |
} | |
glDeleteShader(vertexID); | |
glDeleteShader(fragmentID); | |
return this; | |
} | |
public void bind() { | |
glUseProgram(programID); | |
} | |
public void unbind() { | |
glUseProgram(0); | |
} | |
public void destroy() { | |
glDeleteProgram(programID); | |
} | |
public int getUniformLocation(String name) { | |
return glGetUniformLocation(programID, name); | |
} | |
public void setUniform(String name, float value) { | |
glUniform1f(getUniformLocation(name), value); | |
} | |
public void setUniform(String name, int value) { | |
glUniform1i(getUniformLocation(name), value); | |
} | |
public void setUniform(String name, boolean value) { | |
glUniform1i(getUniformLocation(name), value ? 1 : 0); | |
} | |
public void setUniform(String name, Vector2f value) { | |
glUniform2f(getUniformLocation(name), value.x, value.y); | |
} | |
public void setUniform(String name, Vector3f value) { | |
glUniform3f(getUniformLocation(name), value.x, value.y, value.z); | |
} | |
public void setUniform(String name, Matrix4f value) { | |
FloatBuffer matrix = MemoryUtil.memAllocFloat(16); | |
value.get(matrix); | |
glUniformMatrix4fv(getUniformLocation(name), false, matrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment