package engine.model;
import engine.Vec2f;
import java.util.Vector;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import static engine.GfxEngine.loadMaterial;
import org.lwjgl.opengl.ARBVertexBufferObject;
public class OverlayMap extends Model{
private Vector<Vec2f> vertice;
private int gridSize;
private float offset[];
public OverlayMap(int grid[][],int gridSize){
super();
vertice = new Vector<>();
this.gridSize = gridSize;
offset = new float[2];
processGrid(grid);
offset[0] /= vertice.size();
offset[1] /= vertice.size();
offsetVertice();
createVBO();
bindBuffer();
defaultmaterial = loadMaterial(null);
}
private void processGrid(int grid[][]){
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
if(grid[x][y] == 1) {
this.addTile(x, y);
offset[0] += x;
offset[1] += y;
}
}
}
}
private void offsetVertice(){
for(int i=0;i<this.vertice.size();i++){
this.vertice.get(i).x -= this.offset[0]*5;
this.vertice.get(i).y -= this.offset[1]*5;
}
}
private void addTile(int x, int y){
this.vertice.add(new Vec2f(x-1,y+1));
this.vertice.add(new Vec2f(x,y+1));
this.vertice.add(new Vec2f(x,y));
this.vertice.add(new Vec2f(x-1,y+1));
this.vertice.add(new Vec2f(x,y));
this.vertice.add(new Vec2f(x-1,y));
}
private void createVBO(){
this.texcoords_enabled = true;
this.normals_enabled = false;
this.VBO = BufferUtils.createFloatBuffer(this.vertice.size()*5);
for(Vec2f tile:this.vertice){
this.VBO.put(tile.x);
this.VBO.put(tile.y);
this.VBO.put(0f);
this.VBO.put(0f);
this.VBO.put(1f);
}
this.VBO.flip();
this.tri_vertcount = this.VBO.capacity();
}
public Matrix4f getModelMatrix() {
Matrix4f mat4 = new Matrix4f();
mat4.setIdentity();
mat4.translate(new Vector3f(0,0,0));
mat4.scale(new Vector3f(1,1,1));
return mat4;
}
public void draw(int currentCellx, int currentCellz, float camRotY){
float cellx,cellz;
cellx = (currentCellx+1)*this.gridSize-this.offset[0]*5;
cellz = (currentCellz+1)*this.gridSize-this.offset[1]*5;
glPushMatrix();
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glTranslatef(300,300,0f);
glRotatef(-camRotY,0.0f,0.0f,1.0f);
glScalef(1.5f, 1.5f, 1.5f);
this.draw_glsl(this.defaultmaterial, this.getModelMatrix());
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(cellx-this.gridSize*2f, cellz+this.gridSize);
glVertex2f(cellx+this.gridSize, cellz+this.gridSize);
glVertex2f(cellx+this.gridSize, cellz-this.gridSize*2f);
glVertex2f(cellx-this.gridSize*2f, cellz-this.gridSize*2f);
glEnd();
glLineWidth(2f);
glBegin(GL_LINES);
glVertex2f(cellx-this.gridSize*2f, cellz-this.gridSize);
glVertex2f(cellx+this.gridSize, cellz-this.gridSize);
glVertex2f(cellx-this.gridSize*2f, cellz);
glVertex2f(cellx+this.gridSize, cellz);
glVertex2f(cellx-this.gridSize, cellz+this.gridSize);
glVertex2f(cellx-this.gridSize, cellz-this.gridSize*2f);
glVertex2f(cellx, cellz+this.gridSize);
glVertex2f(cellx, cellz-this.gridSize*2f);
glVertex2f(cellx-this.gridSize*2.2f, cellz-this.gridSize*2f);
glVertex2f(cellx-this.gridSize*2.2f, cellz+this.gridSize*1.2f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glVertex2f(cellx-this.gridSize*2.2f, cellz+this.gridSize*1.2f);
glVertex2f(cellx+this.gridSize, cellz+this.gridSize*1.2f);
glEnd();
glLineWidth(1f);
glPopMatrix();
}
public void draw2(int currentCellx, int currentCellz){
currentCellx++;
currentCellz++;
glPushMatrix();
glTranslatef(100f,100f,0f);
glScalef(1f, 1f, 1f);
glBegin(GL_TRIANGLES);
for(Vec2f tile:this.vertice){
if(currentCellx*this.gridSize-this.gridSize*2 < tile.x*0.2 && currentCellx*this.gridSize+this.gridSize > tile.x*0.2 &&
currentCellz*this.gridSize-this.gridSize*2 < tile.y*0.2 && currentCellz*this.gridSize+this.gridSize > tile.y*0.2){
glColor4f(0.0f, 0.5f, 0.0f, 1.0f);
}
else{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
glVertex2f(tile.x, tile.y);
}
glEnd();
glPopMatrix();
}
}