All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
main.io.github.seba244c.icespire.graphics.Mesh Maven / Gradle / Ivy
package io.github.seba244c.icespire.graphics;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
import static org.lwjgl.opengl.GL30.*;
import org.lwjgl.system.MemoryUtil;
/**
* The graphical representation of an entity. Requires an model
* @author Sebsa
* @since 1.0.2
*/
public class Mesh {
private final int vaoId;
private final List vboIdList;
private final int vertexCount;
private Material material;
/**
* @param positions All vertex position
* @param textCoords Texture coordinates
* @param normals The normals
* @param indices The indices
*/
public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) {
FloatBuffer posBuffer = null;
FloatBuffer textCoordsBuffer = null;
FloatBuffer vecNormalsBuffer = null;
IntBuffer indicesBuffer = null;
try {
vertexCount = indices.length;
vboIdList = new ArrayList<>();
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
// Position VBO
int vboId = glGenBuffers();
vboIdList.add(vboId);
posBuffer = MemoryUtil.memAllocFloat(positions.length);
posBuffer.put(positions).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
// Texture coordinates VBO
vboId = glGenBuffers();
vboIdList.add(vboId);
textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length);
textCoordsBuffer.put(textCoords).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
// Vertex normals VBO
vboId = glGenBuffers();
vboIdList.add(vboId);
vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length);
vecNormalsBuffer.put(normals).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0);
// Index VBO
vboId = glGenBuffers();
vboIdList.add(vboId);
indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
} finally {
if (posBuffer != null) {
MemoryUtil.memFree(posBuffer);
}
if (textCoordsBuffer != null) {
MemoryUtil.memFree(textCoordsBuffer);
}
if (vecNormalsBuffer != null) {
MemoryUtil.memFree(vecNormalsBuffer);
}
if (indicesBuffer != null) {
MemoryUtil.memFree(indicesBuffer);
}
}
}
/**
* Gets the material used when rendering the mesh
* @return The material
*/
public Material getMaterial() {
return material;
}
/**
* Sets the material used when rendering the mesh
* @param material The material
*/
public void setMaterial(Material material) {
this.material = material;
}
/**
* Gets the vao id
* @return The vao id
*/
public int getVaoId() {
return vaoId;
}
/**
* Gets the amount of vertexes in a mesh
* @return The vertexes in a mesh
*/
public int getVertexCount() {
return vertexCount;
}
/**
* Clean up and unbinds the mesh, making it un-usable
*/
public void cleanup() {
glDisableVertexAttribArray(0);
// Delete the VBOs
glBindBuffer(GL_ARRAY_BUFFER, 0);
for (int vboId : vboIdList) {
glDeleteBuffers(vboId);
}
// Delete the texture
Texture texture = material.getTexture();
if (texture != null) {
texture.cleanup();
}
// Delete the VAO
glBindVertexArray(0);
glDeleteVertexArrays(vaoId);
}
public void deleteBuffers() {
glDisableVertexAttribArray(0);
// Delete the VBOs
glBindBuffer(GL_ARRAY_BUFFER, 0);
for (int vboId : vboIdList) {
glDeleteBuffers(vboId);
}
// Delete the VAO
glBindVertexArray(0);
glDeleteVertexArrays(vaoId);
}
}