Render white rectangle
This commit is contained in:
		
							parent
							
								
									d989edee91
								
							
						
					
					
						commit
						1303d0d05e
					
				| @ -1,5 +1,6 @@ | ||||
| package core.engine; | ||||
| 
 | ||||
| import core.engine.model.RawModel; | ||||
| import org.lwjgl.Version; | ||||
| import org.lwjgl.glfw.GLFWErrorCallback; | ||||
| import org.lwjgl.glfw.GLFWVidMode; | ||||
| @ -19,6 +20,8 @@ import static org.lwjgl.system.MemoryUtil.NULL; | ||||
| public class Engine { | ||||
| 
 | ||||
|     private long window; | ||||
|     private final Loader loader = new Loader(); | ||||
|     private final Renderer renderer = new Renderer(); | ||||
| 
 | ||||
|     public Engine() { | ||||
| 
 | ||||
| @ -99,18 +102,29 @@ public class Engine { | ||||
| 
 | ||||
|         // Set the clear color | ||||
|         glClearColor(1.0f, 0.0f, 0.0f, 0.0f); | ||||
| 
 | ||||
|         float[] vertices = { | ||||
|                 -0.5f, 0.5f, 0f, | ||||
|                 -0.5f, -0.5f, 0f, | ||||
|                 0.5f, -0.5f, 0f, | ||||
|                 0.5f, -0.5f, 0f, | ||||
|                 0.5f, 0.5f, 0f, | ||||
|                 -0.5f, 0.5f, 0f | ||||
|         }; | ||||
|         RawModel model = loader.loadToVAO(vertices); | ||||
|         // Run the rendering loop until the user has attempted to close | ||||
|         // the window or has pressed the ESCAPE key. | ||||
|         while ( !glfwWindowShouldClose(window) ) { | ||||
|             renderer.prepare(); | ||||
|             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer | ||||
| 
 | ||||
|             renderer.render(model); | ||||
|             glfwSwapBuffers(window); // swap the color buffers | ||||
| 
 | ||||
|             // Poll for window events. The key callback above will only be | ||||
|             // invoked during this call. | ||||
|             glfwPollEvents(); | ||||
|         } | ||||
| 
 | ||||
|         loader.cleanUp(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
							
								
								
									
										64
									
								
								client/src/main/java/core/engine/Loader.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								client/src/main/java/core/engine/Loader.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,64 @@ | ||||
| package core.engine; | ||||
| 
 | ||||
| import core.engine.model.RawModel; | ||||
| import org.lwjgl.BufferUtils; | ||||
| import org.lwjgl.opengl.GL11; | ||||
| import org.lwjgl.opengl.GL15; | ||||
| import org.lwjgl.opengl.GL20; | ||||
| import org.lwjgl.opengl.GL30; | ||||
| 
 | ||||
| import java.nio.FloatBuffer; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Loader { | ||||
| 
 | ||||
|     private List<Integer> vaos = new ArrayList<>(); | ||||
|     private List<Integer> vbos = new ArrayList<>(); | ||||
| 
 | ||||
|     public RawModel loadToVAO(float[] positions) { | ||||
|         int vaoID = createVAO(); | ||||
|         storeDataInAttributeList(0, positions); | ||||
|         unbindVAO(); | ||||
|         return new RawModel(vaoID, positions.length/3); | ||||
|     } | ||||
| 
 | ||||
|     private int createVAO() { | ||||
|         int vao = GL30.glGenVertexArrays(); | ||||
|         vaos.add(vao); | ||||
|         GL30.glBindVertexArray(vao); | ||||
|         return vao; | ||||
|     } | ||||
| 
 | ||||
|     private void storeDataInAttributeList(int attributeNumber, float[] data) { | ||||
|         int vboID = GL15.glGenBuffers(); | ||||
|         vbos.add(vboID); | ||||
|         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); | ||||
|         FloatBuffer floatBuffer = storeInFloatBuffer(data); | ||||
|         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, floatBuffer, GL15.GL_STATIC_DRAW); | ||||
|         GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0,0); | ||||
|         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); | ||||
|     } | ||||
| 
 | ||||
|     private void unbindVAO() { | ||||
|         GL30.glBindVertexArray(0); | ||||
|     } | ||||
| 
 | ||||
|     private FloatBuffer storeInFloatBuffer(float[] data) { | ||||
|         FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(data.length); | ||||
|         floatBuffer.put(data); | ||||
|         //Buffer is until know expected to be written to. Flip it, so it can be read | ||||
|         floatBuffer.flip(); | ||||
|         return floatBuffer; | ||||
|     } | ||||
| 
 | ||||
|     public void cleanUp() { | ||||
|         for(int vao: vaos) { | ||||
|             GL30.glDeleteVertexArrays(vao); | ||||
|         } | ||||
| 
 | ||||
|         for(int vbo: vbos) { | ||||
|             GL15.glDeleteBuffers(vbo); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										21
									
								
								client/src/main/java/core/engine/Renderer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								client/src/main/java/core/engine/Renderer.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| package core.engine; | ||||
| 
 | ||||
| import core.engine.model.RawModel; | ||||
| import org.lwjgl.opengl.GL11; | ||||
| import org.lwjgl.opengl.GL20; | ||||
| import org.lwjgl.opengl.GL30; | ||||
| 
 | ||||
| public class Renderer { | ||||
| 
 | ||||
|     public void prepare() { | ||||
|         GL11.glClearColor(1,0,0,1); | ||||
|     } | ||||
| 
 | ||||
|     public void render(RawModel rawModel) { | ||||
|         GL30.glBindVertexArray(rawModel.getVaoID()); | ||||
|         GL20.glEnableVertexAttribArray(0); | ||||
|         GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, rawModel.getVertexCount()); | ||||
|         GL20.glDisableVertexAttribArray(0); | ||||
|         GL30.glBindVertexArray(0); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										20
									
								
								client/src/main/java/core/engine/model/RawModel.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								client/src/main/java/core/engine/model/RawModel.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | ||||
| package core.engine.model; | ||||
| 
 | ||||
| public class RawModel { | ||||
| 
 | ||||
|     private final int vaoID; | ||||
|     private final int vertexCount; | ||||
| 
 | ||||
|     public RawModel(int vaoID, int vertexCount) { | ||||
|         this.vaoID = vaoID; | ||||
|         this.vertexCount = vertexCount; | ||||
|     } | ||||
| 
 | ||||
|     public int getVaoID() { | ||||
|         return vaoID; | ||||
|     } | ||||
| 
 | ||||
|     public int getVertexCount() { | ||||
|         return vertexCount; | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user