Advertisement
Guest User

Lucho

a guest
Dec 9th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. package renderEngine;
  2.  
  3. import org.lwjgl.LWJGLException;
  4. import org.lwjgl.opengl.ContextAttribs;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.opengl.PixelFormat;
  9.  
  10. public class DisplayManager {
  11. private static final int WIDTH = 1280;
  12. private static final int HIGHT = 720;
  13. private static final int FPS = 120;
  14.  
  15. public static void createDisplay() {
  16.  
  17. ContextAttribs attribs = new ContextAttribs(3,2)
  18. .withForwardCompatible(true)
  19. .withProfileCore(true);
  20.  
  21. try {
  22. Display.setDisplayMode(new DisplayMode(WIDTH, HIGHT));
  23. Display.create(new PixelFormat(), attribs);
  24. Display.setTitle("My first JavaOpenGL windows");
  25. } catch (LWJGLException e) {
  26. e.printStackTrace();
  27. }
  28.  
  29. GL11.glViewport(0, 0, WIDTH, HIGHT);
  30. }
  31.  
  32. public static void updateDisplay() {
  33. Display.sync(FPS);
  34. Display.update();
  35. }
  36.  
  37. public static void closeDisplay() {
  38. Display.destroy();
  39. }
  40.  
  41. }
  42.  
  43.  
  44. package renderEngine;
  45.  
  46. import java.nio.FloatBuffer;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49.  
  50. import org.lwjgl.BufferUtils;
  51. import org.lwjgl.opengl.GL11;
  52. import org.lwjgl.opengl.GL15;
  53. import org.lwjgl.opengl.GL20;
  54. import org.lwjgl.opengl.GL30;
  55.  
  56. import models.RawModel;
  57.  
  58.  
  59. public class Loader {
  60.  
  61. private List<Integer> vaos = new ArrayList<Integer>();
  62. private List<Integer> vbos = new ArrayList<Integer>();
  63.  
  64. public RawModel loadToVAO(float[] positions) {
  65. int vaoID = createVAO();
  66. storeDataInAttributeList(0, positions);
  67. unbindVAO();
  68. return new RawModel (vaoID, positions.length/3);
  69. }
  70. private void storeDataInAttributeList (int attributeNumber, float[] data) {
  71. int vboID = GL15.glGenBuffers();
  72. vbos.add(vboID);
  73. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
  74. FloatBuffer buffer = storeDataInFloatBuffer(data);
  75. GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer,GL15.GL_STATIC_DRAW);
  76. GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT,false, 0,0);
  77. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);
  78. }
  79.  
  80.  
  81. private int createVAO() {
  82. int vaoID = GL30.glGenVertexArrays();
  83. vaos.add(vaoID);
  84. GL30.glBindVertexArray(vaoID);
  85. return vaoID;
  86. }
  87.  
  88. private FloatBuffer storeDataInFloatBuffer(float[] data) {
  89. FloatBuffer buffer =BufferUtils.createFloatBuffer(data.length);
  90. buffer.put(data);
  91. buffer.flip();
  92. return buffer;
  93. }
  94.  
  95. private void unbindVAO() {
  96. GL30.glBindVertexArray(0);
  97. }
  98.  
  99. public void cleanUp() {
  100. for(int vao:vaos) {
  101. GL30.glDeleteVertexArrays(vao);
  102. }
  103.  
  104. for(int vbo:vbos) {
  105. GL15.glDeleteBuffers(vbo);
  106. }
  107. }
  108. }
  109.  
  110.  
  111. package models;
  112.  
  113. public class RawModel {
  114.  
  115. private int vaoID;
  116. private int vetexCount;
  117.  
  118. public RawModel(int vaoID, int vertexCount) {
  119. this.vaoID = vaoID;
  120. this.vetexCount = vertexCount;
  121. }
  122.  
  123. public int getVaoID() {
  124. return vaoID;
  125. }
  126.  
  127. public int getVetexCount() {
  128. return vetexCount;
  129. }
  130.  
  131. }
  132.  
  133.  
  134. package engineTester;
  135.  
  136. import org.lwjgl.opengl.Display;
  137.  
  138. import models.RawModel;
  139. import renderEngine.DisplayManager;
  140. import renderEngine.Loader;
  141. import renderEngine.Render;
  142.  
  143. public class MainGameLoop {
  144.  
  145. public static void main(String[] args) {
  146.  
  147. DisplayManager.createDisplay();
  148.  
  149. Loader loader = new Loader();
  150. Render render = new Render();
  151.  
  152. float[] vertices = {
  153. -0.5f,0.5f,0f,
  154. -0.5f,-0.5f,0f,
  155. 0.5f,-0.5f,0f,
  156.  
  157. 0.5f,-0.5f,0f,
  158. 0.5f,0.5f,0f,
  159. -0.5f,0.5f,0f
  160. };
  161.  
  162. RawModel model = loader.loadToVAO(vertices);
  163.  
  164. while(!Display.isCloseRequested()) {
  165.  
  166. //game logic
  167. render.prepare();
  168. render.render(model);
  169.  
  170. DisplayManager.updateDisplay();
  171. }
  172. DisplayManager.closeDisplay();
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement