Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.lesson.graphics;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.nio.FloatBuffer;
- import java.awt.*;
- import java.nio.IntBuffer;
- import java.util.ArrayList;
- import java.util.List;
- import net.lesson.graphics.models.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 org.newdawn.slick.opengl.Texture;
- import org.newdawn.slick.opengl.TextureLoader;
- public class Loader {
- private List<Integer> vaos = new ArrayList<Integer>();
- private List<Integer> vbos = new ArrayList<Integer>();
- private List<Integer> textures = new ArrayList<Integer>();
- public RawModel loadToVAO(float[] positions,float[] textureCoords, int[] indices){
- int vaoID = createVao();
- bindIndicesBuffer(indices);
- storeDataInAttributeList(0,3, positions);
- storeDataInAttributeList(1,2, positions);
- unbindVAO();
- return new RawModel(vaoID, indices.length);
- }
- public int loadTexture(String fileName){
- Texture texture = null;
- try{
- texture = TextureLoader.getTexture("PNG", new FileInputStream("res/"+fileName+".png"));
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }catch (IOException e){
- e.printStackTrace();
- }
- int textureID = texture.getTextureID();
- textures.add(textureID);
- return textureID;
- }
- public void cleanUp(){
- for(int vao:vaos){
- GL30.glDeleteVertexArrays(vao);
- }
- for(int vbo:vbos){
- GL15.glDeleteBuffers(vbo);
- }
- for(int texture:textures){
- GL11.glDeleteTextures(texture);
- }
- }
- private int createVao(){
- int vaoID = GL30.glGenVertexArrays();
- vaos.add(vaoID);
- GL30.glBindVertexArray(vaoID);
- return vaoID;
- }
- private void storeDataInAttributeList(int attributeNumber,int coridinateSize, float[] data){
- int vboID = GL15.glGenBuffers();
- vbos.add(vboID);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
- FloatBuffer buffer = storeDataInFloatBuffer(data);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
- GL20.glVertexAttribPointer(attributeNumber, coridinateSize, GL11.GL_FLOAT, false, 0,0);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- }
- public void unbindVAO(){
- GL30.glBindVertexArray(0);
- }
- private void bindIndicesBuffer(int[] indices){
- int vboID = GL15.glGenBuffers();
- vbos.add(vboID);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
- IntBuffer buffer = storeDataInIntBuffer(indices);
- GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
- }
- private IntBuffer storeDataInIntBuffer(int[] data){
- IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
- buffer.put(data);
- buffer.flip();
- return buffer;
- }
- private FloatBuffer storeDataInFloatBuffer(float[] data){
- FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
- buffer.put(data);
- buffer.flip();
- return buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement