Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Mesh {
- ..some variables...
- public Mesh() {
- this.vaoID = glGenVertexArrays();
- this.posVBOID = glGenBuffers();
- this.textureVBOID = glGenBuffers();
- this.normalVBOID = glGenBuffers();
- this.indicesVBOID = glGenBuffers();
- }
- public void render() {
- System.out.println("1");
- Texture texture = material.getTexture();
- if(texture != null){
- //Activate the first texture unit
- glActiveTexture(GL_TEXTURE0);
- //Bind the texture
- texture.bind();
- }
- System.out.println("2");
- //Draw the mesh
- glBindVertexArray(vaoID);
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- System.out.println("3");
- glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_INT, 0);
- System.out.println("4");
- //Restore state
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(2);
- glBindVertexArray(0);
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- public Mesh flushToBuffers(){
- this.indicesCount = this.indices.length;
- glBindVertexArray(this.vaoID);
- if(vertices != null){
- //Position VBO
- FloatBuffer posBuffer = BufferUtils.createFloatBuffer(this.vertices.length * 3);
- posBuffer.put(CollectionUtils.Vector3fArrayToFloatArray(this.vertices)).flip();
- glBindBuffer(GL_ARRAY_BUFFER, this.posVBOID);
- glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
- }
- if(textureCoords != null){
- //Texture Coordinates VBO
- FloatBuffer textCoordsBuffer = BufferUtils.createFloatBuffer(this.textureCoords.length * 2);
- textCoordsBuffer.put(CollectionUtils.Vector2fArrayToFloatArray(this.textureCoords)).flip();
- glBindBuffer(GL_ARRAY_BUFFER, this.textureVBOID);
- glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW);
- glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
- }
- if(normals != null){
- //Normals VBO
- FloatBuffer vecNormalsBuffer = BufferUtils.createFloatBuffer(this.normals.length * 3);
- vecNormalsBuffer.put(CollectionUtils.Vector3fArrayToFloatArray(this.normals)).flip();
- glBindBuffer(GL_ARRAY_BUFFER, this.normalVBOID);
- glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW);
- glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0);
- }
- if(indices != null){
- //Indices VBO
- IntBuffer indicesBuffer = BufferUtils.createIntBuffer(this.indices.length);
- indicesBuffer.put(this.indices).flip();
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.indicesVBOID);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
- }
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- return this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement