Organisation

This commit is contained in:
Antoine
2021-05-17 05:39:23 +02:00
parent 5d93325b3d
commit 202c141e4d
13 changed files with 51 additions and 18 deletions

View File

@ -0,0 +1,32 @@
package engine.utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
public class BufferUtils {
private BufferUtils() {
}
public static ByteBuffer createByteBuffer(byte[] array){
ByteBuffer result = ByteBuffer.allocateDirect(array.length).order(ByteOrder.nativeOrder());
result.put(array).flip();
return result;
}
public static FloatBuffer createFloatBuffer(float[] array){
FloatBuffer result = ByteBuffer.allocateDirect(array.length << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
result.put(array).flip();
return result;
}
public static IntBuffer createIntBuffer(int[] array){
IntBuffer result = ByteBuffer.allocateDirect(array.length << 2).order(ByteOrder.nativeOrder()).asIntBuffer();
result.put(array).flip();
return result;
}
}

View File

@ -0,0 +1,29 @@
package engine.utils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileUtils {
private FileUtils() {
}
public static String loadAsString(String file){
StringBuilder result = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String buffer = "";
while ((buffer = reader.readLine()) != null) {
result.append(buffer +"\n");
}
reader.close();
} catch (IOException e){
e.printStackTrace();
}
return result.toString();
}
}

View File

@ -0,0 +1,60 @@
package engine.utils;
import engine.utils.FileUtils;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
public class ShaderUtils {
private ShaderUtils(){
}
public static int load(String vertPath, String fragPath){
String vert = FileUtils.loadAsString(vertPath);
String frag = FileUtils.loadAsString(fragPath);
return create(vert, frag);
}
public static int create(String vert, String frag){
// On crée et compile le vertex et le fragment shader
int vertID = glCreateShader(GL_VERTEX_SHADER);
int fragID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertID, vert);
glShaderSource(fragID, frag);
glCompileShader(vertID);
if (glGetShaderi(vertID, GL_COMPILE_STATUS) == GL_FALSE){
System.err.println("Failed to compile vertex shader");
System.err.println(glGetShaderInfoLog(vertID));
return -1;
}
glCompileShader(fragID);
if (glGetShaderi(fragID, GL_COMPILE_STATUS) == GL_FALSE){
System.err.println("Failed to compile fragment shader");
System.err.println(glGetShaderInfoLog(fragID));
return -1;
}
//on lie les shaders au programme
int program = glCreateProgram();
glAttachShader(program, vertID);
glAttachShader(program, fragID);
glLinkProgram(program);
if(glGetProgrami(program, GL_LINK_STATUS) == GL_FALSE) {
System.err.println("Failed to link vertex and fragment shader");
System.err.println(glGetShaderInfoLog(program));
return -1;
}
glValidateProgram(program);
glDeleteShader(vertID);
glDeleteShader(fragID);
return program;
}
}