Organisation
This commit is contained in:
75
src/engine/graphics/Shader.java
Normal file
75
src/engine/graphics/Shader.java
Normal file
@ -0,0 +1,75 @@
|
||||
package engine.graphics;
|
||||
|
||||
import engine.math.Matrix4f;
|
||||
import engine.utils.ShaderUtils;
|
||||
import engine.math.Vector3f;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
public class Shader {
|
||||
|
||||
private boolean enabled = false;
|
||||
|
||||
//Identifiant du programme resultat de la compilation des shaders
|
||||
private final int ID;
|
||||
private Map<String, Integer> locationCache = new HashMap<String, Integer>();
|
||||
|
||||
/*
|
||||
Crée le fragment et le vertex shader les lie dans un programme dont il renvoie l'identifiant.
|
||||
*/
|
||||
public Shader(String vertex, String fragment) {
|
||||
ID = ShaderUtils.load(vertex, fragment);
|
||||
}
|
||||
|
||||
public int getUniform(String name){
|
||||
if (locationCache.containsKey(name)) return locationCache.get(name);
|
||||
int result = glGetUniformLocation(ID, name);
|
||||
if (result == -1) System.err.println("Could not find uniform variable " + name);
|
||||
else locationCache.put(name, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setUniform1i(String name, int value) {
|
||||
if (!enabled) enable();
|
||||
glUniform1i(getUniform(name), value);
|
||||
}
|
||||
|
||||
public void setUniform1f(String name, float value) {
|
||||
if (!enabled) enable();
|
||||
glUniform1f(getUniform(name), value);
|
||||
}
|
||||
|
||||
public void setUniform2f(String name, float x, float y) {
|
||||
if (!enabled) enable();
|
||||
glUniform2f(getUniform(name), x, y);
|
||||
}
|
||||
|
||||
public void setUniform3f(String name, Vector3f vector) {
|
||||
if (!enabled) enable();
|
||||
glUniform3f(getUniform(name), vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
public void setUniform4f(String name, float x, float y, float z, float w) {
|
||||
if (!enabled) enable();
|
||||
glUniform4f(getUniform(name), x, y, z, w);
|
||||
}
|
||||
|
||||
public void setUniformMat4f(String name, Matrix4f matrix){
|
||||
if (!enabled) enable();
|
||||
glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer());
|
||||
}
|
||||
|
||||
public void enable() {
|
||||
glUseProgram(ID);
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
glUseProgram(0);
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
}
|
67
src/engine/graphics/Texture.java
Normal file
67
src/engine/graphics/Texture.java
Normal file
@ -0,0 +1,67 @@
|
||||
package engine.graphics;
|
||||
|
||||
import engine.utils.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL30.*;
|
||||
|
||||
public class Texture {
|
||||
|
||||
private int width, height;
|
||||
private int texture;
|
||||
private int index;
|
||||
|
||||
public Texture(String path, int index) {
|
||||
this.index = index;
|
||||
texture = load(path);
|
||||
}
|
||||
|
||||
private int load(String path) {
|
||||
int[] pixels = null;
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(new FileInputStream(path));
|
||||
width = image.getWidth();
|
||||
height = image.getHeight();
|
||||
pixels = new int[width * height];
|
||||
image.getRGB(0, 0, width, height, pixels, 0, width);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
int[] data = new int[width * height];
|
||||
for (int i = 0; i < width * height; i++) {
|
||||
int a = (pixels[i] & 0xff000000) >> 24;
|
||||
int r = (pixels[i] & 0xff0000) >> 16;
|
||||
int g = (pixels[i] & 0xff00) >> 8;
|
||||
int b = (pixels[i] & 0xff);
|
||||
|
||||
data[i] = a << 24 | b << 16 | g << 8 | r;
|
||||
}
|
||||
|
||||
int result = glGenTextures();
|
||||
glBindTexture(GL_TEXTURE_2D, result);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, BufferUtils.createIntBuffer(data));
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void bind() {
|
||||
glActiveTexture(GL_TEXTURE0 + this.index);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
}
|
||||
|
||||
public void unbind() {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
}
|
121
src/engine/graphics/VertexArray.java
Normal file
121
src/engine/graphics/VertexArray.java
Normal file
@ -0,0 +1,121 @@
|
||||
package engine.graphics;
|
||||
|
||||
import engine.utils.BufferUtils;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL15.*;
|
||||
import static org.lwjgl.opengl.GL30.*;
|
||||
|
||||
public class VertexArray {
|
||||
|
||||
private int VAO ,VBO, EBO, CBO, TBO;
|
||||
private int count;
|
||||
|
||||
public VertexArray(float[] vertices, byte[] indices){
|
||||
count = indices.length;
|
||||
// VERTEX ARRAY OBJECT
|
||||
VAO = glGenVertexArrays();
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
// VERTEX BUFFER OBJECT
|
||||
createVertexBufferObject(vertices);
|
||||
|
||||
EBO = glGenBuffers();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
public VertexArray(float[] vertices, byte[] indices, float[] color) {
|
||||
count = indices.length;
|
||||
// VERTEX ARRAY OBJECT
|
||||
VAO = glGenVertexArrays();
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// VERTEX BUFFER OBJECT
|
||||
createVertexBufferObject(vertices);
|
||||
// COLOR BUFFER OBJECT
|
||||
createColorBufferObject(color);
|
||||
|
||||
EBO = glGenBuffers();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
public VertexArray(float[] vertices, byte[] indices, float[] color, float[] texture) {
|
||||
count = indices.length;
|
||||
// VERTEX ARRAY OBJECT
|
||||
VAO = glGenVertexArrays();
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// VERTEX BUFFER OBJECT
|
||||
createVertexBufferObject(vertices);
|
||||
// COLOR BUFFER OBJECT
|
||||
createColorBufferObject(color);
|
||||
// TEXTURE BUFFER OBJECT
|
||||
createTextureBufferObject(texture);
|
||||
|
||||
EBO = glGenBuffers();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
private void createVertexBufferObject(float[] vertices){
|
||||
VBO = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
||||
glEnableVertexAttribArray(0);
|
||||
}
|
||||
|
||||
private void createColorBufferObject(float[] color){
|
||||
CBO = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, CBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(color), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
|
||||
glEnableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
private void createTextureBufferObject(float[] texture){
|
||||
TBO = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, TBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(texture), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, false, 0, 0);
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
|
||||
public void bind(){
|
||||
glBindVertexArray(this.VAO);
|
||||
}
|
||||
|
||||
public void unbind(){
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
public void draw(){
|
||||
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_BYTE, 0);
|
||||
}
|
||||
|
||||
public void render(){
|
||||
bind();
|
||||
draw();
|
||||
unbind();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user