Last active
December 10, 2019 18:14
-
-
Save matyklug18/91ff4aca9db683b9c06ca747d8e8bb1e 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
#version 460 core | |
in vec2 passTexCoord; | |
out vec4 outColor; | |
uniform sampler2D tex; | |
void main() { | |
outColor = texture(tex, passTexCoord); | |
if(outColor.a == 0) | |
discard; | |
} |
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
public class GUIMesh { | |
ArrayList<Vertex> verts = new ArrayList<>(); | |
ArrayList<Integer> inds = new ArrayList<>(); | |
private int vaoID, pboID, iboID, tboID; | |
public GUIMesh() { | |
} | |
public GUIMesh(ArrayList<Vertex> verts, ArrayList<Integer> inds) { | |
this.verts = verts; | |
this.inds = inds; | |
} | |
public GUIMesh(Vertex[] verts, int[] inds) { | |
this.verts.addAll(Arrays.asList(verts)); | |
this.inds.addAll(Arrays.stream(inds).boxed().collect(Collectors.toList())); | |
} | |
public GUIMesh create() { | |
//create the VAO | |
vaoID = glGenVertexArrays(); | |
glBindVertexArray(vaoID); | |
//create the buffer | |
FloatBuffer posBuff = MemoryUtil.memAllocFloat(verts.size() * 3); | |
float[] posData = new float[verts.size() * 3]; | |
for (int i = 0; i < verts.size(); i++) { | |
posData[i * 3] = verts.get(i).getPos().x; | |
posData[i * 3 + 1] = verts.get(i).getPos().y; | |
posData[i * 3 + 2] = verts.get(i).getPos().z; | |
} | |
posBuff.put(posData).flip(); | |
pboID = storeFloatBuffer(posBuff, 0, 3); | |
FloatBuffer textureBuffer = MemoryUtil.memAllocFloat(verts.size() * 2); | |
float[] textureData = new float[verts.size() * 2]; | |
for (int i = 0; i < verts.size(); i++) { | |
textureData[i * 2] = verts.get(i).getTex().x; | |
textureData[i * 2 + 1] = verts.get(i).getTex().y; | |
} | |
textureBuffer.put(textureData).flip(); | |
tboID = storeFloatBuffer(textureBuffer, 1, 2); | |
IntBuffer indsBuff = MemoryUtil.memAllocInt(inds.size()); | |
indsBuff.put(inds.stream().mapToInt(i -> i).toArray()).flip(); | |
iboID = plainStoreIntBuffer(indsBuff); | |
return this; | |
} | |
public void destroy() { | |
glDeleteBuffers(pboID); | |
glDeleteBuffers(iboID); | |
glDeleteBuffers(tboID); | |
glDeleteVertexArrays(vaoID); | |
} | |
int storeFloatBuffer(FloatBuffer buff, int index, int size) { | |
int vboID = glGenBuffers(); | |
glBindBuffer(GL_ARRAY_BUFFER, vboID); | |
glBufferData(GL_ARRAY_BUFFER, buff, GL_STATIC_DRAW); | |
glVertexAttribPointer(index, size, GL_FLOAT, false, 0, 0); | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
return vboID; | |
} | |
int plainStoreIntBuffer(IntBuffer data) { | |
int boID = glGenBuffers(); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, boID); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, data, GL_STATIC_DRAW); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
return boID; | |
} | |
public int getVAOID() { | |
return vaoID; | |
} | |
public int getIBOID() { | |
return iboID; | |
} | |
public ArrayList<Integer> getInds() { | |
return inds; | |
} | |
public GUIMesh combine(GUIMesh mesh) { | |
this.inds.addAll(mesh.inds); | |
this.verts.addAll(mesh.verts); | |
return this; | |
} | |
} |
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
public class GUIRenderer { | |
GUIShader shader = new GUIShader().create(); | |
FileUtils.Texture tex; | |
Window win; | |
private static final float FOV = (float) Math.toRadians(60.0f); | |
private static final float Z_NEAR = 0.01f; | |
private static final float Z_FAR = 1000.f; | |
public GUIRenderer(FileUtils.Texture tex, Window win) { | |
this.tex = tex; | |
this.win = win; | |
} | |
public void renderMesh(GUIMesh mesh, Vector3f pos, Vector3f rot, Vector3f scale) { | |
glBindVertexArray(mesh.getVAOID()); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.getIBOID()); | |
glActiveTexture(GL_TEXTURE0); | |
glBindTexture(GL_TEXTURE_2D, tex.getID()); | |
shader.bind(); | |
Matrix4f projectionMatrix; | |
float aspectRatio = (float) win.getXSize() / win.getYSize(); | |
projectionMatrix = new Matrix4f().perspective(FOV, aspectRatio, | |
Z_NEAR, Z_FAR); | |
shader.setUniform("project", projectionMatrix); | |
Matrix4f modelViewMatrix = MatrixUtil.getModelViewMatrix(pos, rot, scale); | |
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(); | |
glDeleteTextures(tex.getID()); | |
} | |
} |
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 | |
in vec3 position; | |
in vec2 texCoord; | |
out vec2 passTexCoord; | |
uniform mat4 project; | |
uniform mat4 model; | |
void main() { | |
gl_Position = project * model * vec4(position, 1.0); | |
passTexCoord = texCoord; | |
} |
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
public class InvBlockMeshMaker { | |
static int index = 0; | |
final static int xAmount = 8; | |
final static int yAmount = 1; | |
final static float xStep = 1.0f / ((float) xAmount); | |
final static float yStep = 1.0f / ((float) yAmount); | |
public static GUIMesh genMesh(Block block) { | |
GUIMesh mesh = new GUIMesh(); | |
front(mesh, block); | |
back(mesh, block); | |
left(mesh, block); | |
right(mesh, block); | |
top(mesh, block); | |
bottom(mesh, block); | |
return mesh; | |
} | |
static GUIMesh back(GUIMesh guiMesh, Block block) { | |
guiMesh.combine(new GUIMesh(new Vertex[] { | |
//Back face | |
new Vertex(new Vector3f(0,1,0), new Vector3f(0,0,-1), new Vector2f(xStep * (block.getAtlasX(BlockSide.BACK) - 1), yStep * (block.getAtlasY(BlockSide.BACK) - 1))), | |
new Vertex(new Vector3f(0,0,0), new Vector3f(0,0,-1), new Vector2f(xStep * (block.getAtlasX(BlockSide.BACK) - 1), yStep * block.getAtlasY(BlockSide.BACK))), | |
new Vertex(new Vector3f(1,0,0), new Vector3f(0,0,-1), new Vector2f(xStep * block.getAtlasX(BlockSide.BACK), yStep * block.getAtlasY(BlockSide.BACK))), | |
new Vertex(new Vector3f(1,1,0), new Vector3f(0,0,-1), new Vector2f(xStep * block.getAtlasX(BlockSide.BACK), yStep * (block.getAtlasY(BlockSide.BACK) - 1))), | |
}, new int[] { | |
//Back face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return guiMesh; | |
} | |
static GUIMesh front(GUIMesh guiMesh, Block block) { | |
guiMesh.combine(new GUIMesh(new Vertex[] { | |
//Front face | |
new Vertex(new Vector3f(0,1,1), new Vector3f(0,0,1), new Vector2f(xStep * (block.getAtlasX(BlockSide.FRONT) - 1), yStep * (block.getAtlasY(BlockSide.FRONT) - 1))), | |
new Vertex(new Vector3f(0,0,1), new Vector3f(0,0,1), new Vector2f(xStep * (block.getAtlasX(BlockSide.FRONT) - 1), yStep * block.getAtlasY(BlockSide.FRONT))), | |
new Vertex(new Vector3f(1,0,1), new Vector3f(0,0,1), new Vector2f(xStep * block.getAtlasX(BlockSide.FRONT), yStep * block.getAtlasY(BlockSide.FRONT))), | |
new Vertex(new Vector3f(1,1,1), new Vector3f(0,0,1), new Vector2f(xStep * block.getAtlasX(BlockSide.FRONT), yStep * (block.getAtlasY(BlockSide.FRONT) - 1))), | |
}, new int[] { | |
//Front face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return guiMesh; | |
} | |
static GUIMesh right(GUIMesh guiMesh, Block block) { | |
guiMesh.combine(new GUIMesh(new Vertex[] { | |
//Right face | |
new Vertex(new Vector3f(1,1,0), new Vector3f(1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.RIGHT) - 1), yStep * (block.getAtlasY(BlockSide.RIGHT) - 1))), | |
new Vertex(new Vector3f(1,0,0), new Vector3f(1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.RIGHT) - 1), yStep * block.getAtlasY(BlockSide.RIGHT))), | |
new Vertex(new Vector3f(1,0,1), new Vector3f(1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.RIGHT), yStep * block.getAtlasY(BlockSide.RIGHT))), | |
new Vertex(new Vector3f(1,1,1), new Vector3f(1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.RIGHT), yStep * (block.getAtlasY(BlockSide.RIGHT) - 1))), | |
}, new int[] { | |
//Right face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return guiMesh; | |
} | |
static GUIMesh left(GUIMesh guiMesh, Block block) { | |
guiMesh.combine(new GUIMesh(new Vertex[] { | |
//Left face | |
new Vertex(new Vector3f(0,1,0), new Vector3f(-1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.LEFT) - 1), yStep * (block.getAtlasY(BlockSide.LEFT) - 1))), | |
new Vertex(new Vector3f(0,0,0), new Vector3f(-1,0,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.LEFT) - 1), yStep * block.getAtlasY(BlockSide.LEFT))), | |
new Vertex(new Vector3f(0,0,1), new Vector3f(-1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.LEFT), yStep * block.getAtlasY(BlockSide.LEFT))), | |
new Vertex(new Vector3f(0,1,1), new Vector3f(-1,0,0), new Vector2f(xStep * block.getAtlasX(BlockSide.LEFT), yStep * (block.getAtlasY(BlockSide.LEFT) - 1))), | |
}, new int[] { | |
//Left face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return guiMesh; | |
} | |
static GUIMesh top(GUIMesh guiMesh, Block block) { | |
guiMesh.combine(new GUIMesh(new Vertex[] { | |
//Top face | |
new Vertex(new Vector3f(0,1,1), new Vector3f(0,1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.TOP) - 1), yStep * (block.getAtlasY(BlockSide.TOP) - 1))), | |
new Vertex(new Vector3f(0,1,0), new Vector3f(0,1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.TOP) - 1), yStep * block.getAtlasY(BlockSide.TOP))), | |
new Vertex(new Vector3f(1,1,0), new Vector3f(0,1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.TOP), yStep * block.getAtlasY(BlockSide.TOP))), | |
new Vertex(new Vector3f(1,1,1), new Vector3f(0,1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.TOP), yStep * (block.getAtlasY(BlockSide.TOP) - 1))), | |
}, new int[] { | |
//Top face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return guiMesh; | |
} | |
static GUIMesh bottom(GUIMesh guiMesh, Block block) { | |
guiMesh.combine(new GUIMesh(new Vertex[] { | |
//Bottom face | |
new Vertex(new Vector3f(0, 0, 1), new Vector3f(0,-1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.BOTTOM) - 1), yStep * (block.getAtlasY(BlockSide.BOTTOM) - 1))), | |
new Vertex(new Vector3f(0, 0, 0), new Vector3f(0,-1,0), new Vector2f(xStep * (block.getAtlasX(BlockSide.BOTTOM) - 1), yStep * block.getAtlasY(BlockSide.BOTTOM))), | |
new Vertex(new Vector3f(1, 0, 0), new Vector3f(0,-1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.BOTTOM), yStep * block.getAtlasY(BlockSide.BOTTOM))), | |
new Vertex(new Vector3f(1, 0, 1), new Vector3f(0,-1,0), new Vector2f(xStep * block.getAtlasX(BlockSide.BOTTOM), yStep * (block.getAtlasY(BlockSide.BOTTOM) - 1))), | |
}, new int[] { | |
//Bottom face | |
0 + index * 4, 1 + index * 4, 3 + index * 4, | |
3 + index * 4, 1 + index * 4, 2 + index * 4, | |
})); | |
index ++; | |
return guiMesh; | |
} | |
} |
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
guiRenderer.renderMesh(InvBlockMeshMaker.genMesh(new BlockGrass()).create(), new Vector3f(0,0,0), new Vector3f(0,0,0), new Vector3f(1,1,1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment