jeu-de-combat/src/engine/object/ObjectGlTex.java
2021-05-20 19:02:28 +02:00

77 lines
2.1 KiB
Java

package engine.object;
import engine.math.Primitive;
import engine.graphics.Shader;
import engine.graphics.Texture;
import engine.graphics.VertexArray;
import engine.math.Matrix4f;
import engine.math.Vector3f;
import java.util.ArrayList;
import java.util.List;
public class ObjectGlTex extends ObjectGl{
protected List<Texture> textures;
public ObjectGlTex(){
}
public ObjectGlTex(float x, float y, float z, float h, float w, List<String> texPath, float[] texCoord) {
super();
this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, null, texCoord);
this.transform = Matrix4f.identity();
this.shader = new Shader("shaders/ObjectGlTex/vert.glsl","shaders/ObjectGlTex/frag.glsl");
this.zPos = z;
this.setTexture(texPath);
}
public void setTexture(List<String> texPath){
this.textures = new ArrayList<>();
int count = 0;
for (String path : texPath){
textures.add(new Texture(path, count));
count++;
}
}
public void setTextureWrap(float x, float y, float w, float h){
int texWidth = this.textures.get(0).getWidth();
int texHeight = this.textures.get(0).getHeight();
float[] result = {
x / texWidth, y / texHeight,
x + w / texWidth, y / texHeight,
x + w / texWidth, y + h / texHeight,
x / texWidth, y + h / texHeight,
};
// TODO scaling object
this.setTextureWrap(result);
}
public void setTextureWrap(float[] texture){
this.vertexArray.swapTextureBufferObject(texture);
}
@Override
public void render() {
this.shader.enable();
this.shader.setUniformMat4f("projection", projection);
this.shader.setUniformMat4f("view", view);
this.shader.setUniformMat4f("transform", this.transform);
for (Texture t : textures){
t.bind();
}
this.vertexArray.render();
for (Texture t : textures){
t.unbind();
}
this.shader.disable();
}
}