SHOW:
|
|
- or go back to the newest paste.
| 1 | package entities; | |
| 2 | ||
| 3 | import java.nio.FloatBuffer; | |
| 4 | import java.nio.IntBuffer; | |
| 5 | ||
| 6 | import org.lwjgl.BufferUtils; | |
| 7 | import org.lwjgl.opengl.GL11; | |
| 8 | import org.lwjgl.opengl.GL15; | |
| 9 | import org.lwjgl.opengl.GL20; | |
| 10 | import org.lwjgl.opengl.GL30; | |
| 11 | import org.lwjgl.util.vector.Vector3f; | |
| 12 | import org.lwjgl.util.vector.Vector4f; | |
| 13 | ||
| 14 | import base.Matrices; | |
| 15 | ||
| 16 | import model.Model; | |
| 17 | import model.OBJLoader; | |
| 18 | import model.ShaderHandler; | |
| 19 | ||
| 20 | public class Cube {
| |
| 21 | Model cube = new Model(); | |
| 22 | int vaoId, vertexVboId, indexVboId; | |
| 23 | int vsId, fsId, pId; | |
| 24 | public Cube(){
| |
| 25 | cube = OBJLoader.loadObj("res/cube.obj");
| |
| 26 | } | |
| 27 | ||
| 28 | public void setup(){
| |
| 29 | int size = cube.vertices.size(); | |
| 30 | FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(size * 3); | |
| 31 | for(int i = 0; i < size; i++){
| |
| 32 | vertexBuffer.put(cube.vertices.get(i).x); | |
| 33 | vertexBuffer.put(cube.vertices.get(i).y); | |
| 34 | vertexBuffer.put(cube.vertices.get(i).z); | |
| 35 | } | |
| 36 | vertexBuffer.flip(); | |
| 37 | ||
| 38 | size = cube.indices.size(); | |
| 39 | IntBuffer indexBuffer = BufferUtils.createIntBuffer(size * 3); | |
| 40 | for(int i = 0; i < size; i++){
| |
| 41 | indexBuffer.put((int)cube.indices.get(i).x); | |
| 42 | indexBuffer.put((int)cube.indices.get(i).y); | |
| 43 | indexBuffer.put((int)cube.indices.get(i).z); | |
| 44 | } | |
| 45 | indexBuffer.flip(); | |
| 46 | ||
| 47 | vaoId = GL30.glGenVertexArrays(); | |
| 48 | GL30.glBindVertexArray(vaoId); | |
| 49 | ||
| 50 | vertexVboId = GL15.glGenBuffers(); | |
| 51 | GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexVboId); | |
| 52 | GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW); | |
| 53 | GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0); | |
| 54 | GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); | |
| 55 | GL30.glBindVertexArray(0); | |
| 56 | ||
| 57 | indexVboId = GL15.glGenBuffers(); | |
| 58 | GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexVboId); | |
| 59 | GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_STATIC_DRAW); | |
| 60 | GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); | |
| 61 | } | |
| 62 | ||
| 63 | public void setupShaders(){
| |
| 64 | vsId = ShaderHandler.loadShader("res/cube_vs.glsl", GL20.GL_VERTEX_SHADER);
| |
| 65 | fsId = ShaderHandler.loadShader("res/cube_fs.glsl", GL20.GL_FRAGMENT_SHADER);
| |
| 66 | ||
| 67 | pId = GL20.glCreateProgram(); | |
| 68 | GL20.glAttachShader(pId, vsId); | |
| 69 | GL20.glAttachShader(pId, fsId); | |
| 70 | GL20.glLinkProgram(pId); | |
| 71 | ||
| 72 | //GL20.glBindAttribLocation(pId, 0, "position"); | |
| 73 | ||
| 74 | Matrices.perspectiveMatrixLocation = GL20.glGetUniformLocation(pId, "perspectiveMatrix"); | |
| 75 | Matrices.viewMatrixLocation = GL20.glGetUniformLocation(pId, "viewMatrix"); | |
| 76 | Matrices.modelMatrixLocation = GL20.glGetUniformLocation(pId, "modelMatrix"); | |
| 77 | ||
| 78 | GL20.glValidateProgram(pId); | |
| 79 | } | |
| 80 | ||
| 81 | public void render(){
| |
| 82 | Matrices.uploadMatrices(pId, Matrices.perspectiveMatrixLocation, Matrices.viewMatrixLocation, Matrices.modelMatrixLocation); | |
| 83 | GL20.glUseProgram(pId); | |
| 84 | GL30.glBindVertexArray(vaoId); | |
| 85 | GL20.glEnableVertexAttribArray(0); | |
| 86 | GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indexVboId); | |
| 87 | GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); | |
| 88 | GL11.glDrawElements(GL11.GL_TRIANGLES, cube.indices.size() * 3, GL11.GL_UNSIGNED_INT, 0); | |
| 89 | GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); | |
| 90 | GL20.glDisableVertexAttribArray(0); | |
| 91 | GL30.glBindVertexArray(0); | |
| 92 | GL20.glUseProgram(0); | |
| 93 | } | |
| 94 | } |