79 lines
1.9 KiB
Java
79 lines
1.9 KiB
Java
package projectzombie.model;
|
|
|
|
import gl_engine.MathHelpers;
|
|
import gl_engine.texture.TextureRef3D;
|
|
|
|
import static org.lwjgl.opengl.GL33.*;
|
|
|
|
import org.lwjgl.opengl.GL33;
|
|
|
|
public abstract class Model
|
|
{
|
|
int vao, size;
|
|
boolean loaded = false;
|
|
private static final int SIZE = 9;
|
|
private float[] verticies;
|
|
|
|
public int getSize() {
|
|
return size;
|
|
}
|
|
|
|
// px, py, pz, tx, ty
|
|
protected abstract float[] getVerticies();
|
|
protected abstract TextureRef3D[] getTextures();
|
|
|
|
public float[] getLoadedVerticies() {
|
|
return verticies;
|
|
}
|
|
|
|
public void bind()
|
|
{
|
|
if(loaded) {
|
|
glBindVertexArray(vao);
|
|
}
|
|
|
|
else
|
|
{
|
|
verticies = this.getVerticies();
|
|
TextureRef3D[] refs = this.getTextures();
|
|
|
|
if(verticies.length % SIZE != 0 || refs.length * 3 != verticies.length / SIZE) {
|
|
System.err.println("Invalid model");
|
|
System.exit(1);
|
|
return;
|
|
}
|
|
|
|
size = verticies.length/SIZE;
|
|
double k = 0.001;
|
|
|
|
for(int i=0;i<size;i++) {
|
|
TextureRef3D ref = refs[i / 3];
|
|
verticies[i*SIZE + 3] = (float)MathHelpers.map(verticies[i*SIZE + 3], 0-k, 1+k, ref.sx, ref.ex);
|
|
verticies[i*SIZE + 4] = (float)MathHelpers.map(verticies[i*SIZE + 4], 0-k, 1+k, ref.sy, ref.ey);
|
|
verticies[i*SIZE + 5] = ref.z;
|
|
}
|
|
|
|
vao = glGenVertexArrays();
|
|
glBindVertexArray(vao);
|
|
|
|
int vbo = glGenBuffers();
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
glBufferData(GL_ARRAY_BUFFER, verticies, GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, false, Float.BYTES * SIZE, 0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, false, Float.BYTES * SIZE, Float.BYTES * 3);
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glVertexAttribPointer(2, 3, GL_FLOAT, false, Float.BYTES * SIZE, Float.BYTES * 6);
|
|
glEnableVertexAttribArray(2);
|
|
|
|
loaded = true;
|
|
}
|
|
}
|
|
|
|
public void render() {
|
|
GL33.glDrawArrays(GL33.GL_TRIANGLES, 0, getSize());
|
|
}
|
|
} |