public/utils/RenderAdapter.java

270 lines
8.1 KiB
Java

package com.baseband.client.util.render;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.opengl.GL11;
import java.awt.*;
import static org.lwjgl.opengl.GL11.*;
public class RenderAdapter {
static int mode;
static int color;
static Vec3d translated;
static boolean depth;
private static byte[] splitIntBE(int i) {
return new byte[]{(byte)(i >> 24 & 0xff), (byte)(i >> 16 & 0xff), (byte)(i >> 8 & 0xff), (byte)(i >> 0 & 0xff)};
}
public static void ready() {
glPushMatrix();
}
public static void translate(double x, double y, double z) {
glTranslated(x,y,z);
translated = new Vec3d(x,y,z);
}
public static void begin(int modeIn) {
glBegin(mode = modeIn);
}
public static void color(int argb) {
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
changeColor(argb);
}
public static void changeColor(int argb) {
byte[] bytes = splitIntBE(argb);
glColor4ub(bytes[1], bytes[2], bytes[3], bytes[0]);
color = argb;
}
public static void depth(boolean b) {
depth = b;
if(b) {
glEnable(GL_DEPTH_TEST);
} else {
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);
}
}
public static void put(double x, double y, double z) {
glVertex3d(x,y,z);
}
public static void end() {
translated = null;
color = 0;
depth = false;
mode = 0;
glEnd();
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glPopMatrix();
}
public static void next() {
// end current
glEnd();
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glPopMatrix();
// start new
glPushMatrix();
glTranslated(translated.x, translated.y, translated.z);
color(color);
depth(depth);
glBegin(mode);
}
/**
* [redacted in BaseBand/public]
*/
public static void drawLinesAroundBlock(BlockPos pos, int color, Vec3d eyePos) {
try {
ready();
translate(-eyePos.x, -eyePos.y, -eyePos.z);
color(color);
depth(false);
begin(GL11.GL_LINES);
drawLinesAroundBlockNow(pos.getX(), pos.getY(), pos.getZ());
end();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void drawLinesAroundBlockNow(int x, int y, int z) {
// bottom [redacted in BaseBand/public]
put(x, y, z);
put(x + 1, y, z);
put(x, y, z);
put(x, y, z + 1);
put(x + 1, y, z);
put(x + 1, y, z + 1);
put(x, y, z + 1);
put(x + 1, y, z + 1);
//sides [redacted in BaseBand/public]
put(x, y, z);
put(x, y + 1, z);
put(x + 1, y, z);
put(x + 1, y + 1, z);
put(x, y, z + 1);
put(x, y + 1, z + 1);
put(x + 1, y, z + 1);
put(x + 1, y + 1, z + 1);
put(x + 1, y, z);
put(x + 1, y + 1, z);
// top [redacted in BaseBand/public]
put(x, y + 1, z);
put(x + 1, y + 1, z);
put(x, y + 1, z);
put(x, y + 1, z + 1);
put(x + 1, y + 1, z);
put(x + 1, y + 1, z + 1);
put(x, y + 1, z + 1);
put(x + 1, y + 1, z + 1);
}
public static void drawLinesAroundBlock(BlockPos pos, Color color, Vec3d eyePos) {
drawLinesAroundBlock(pos, color.getRGB(), eyePos);
}
public static void drawBlockFaces(BlockPos pos, int color, Vec3d eyePos) {
try {
ready();
translate(-eyePos.x, -eyePos.y, -eyePos.z);
color(color);
depth(false);
begin(GL_QUADS);
drawBlockFacesNow(pos.getX(), pos.getY(), pos.getZ());
end();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void drawBlockFacesNow(int x, int y, int z) {
//bottom
put(x, y, z);
put(x + 1, y, z);
put(x + 1, y, z + 1);
put(x, y, z + 1);
//z- face
put(x, y, z);
put(x, y + 1, z);
put(x + 1, y + 1, z);
put(x + 1, y, z);
//z+ face
put(x, y, z + 1);
put(x, y + 1, z + 1);
put(x + 1, y + 1, z + 1);
put(x + 1, y, z + 1);
//x- face
put(x, y, z);
put(x, y + 1, z);
put(x, y + 1, z + 1);
put(x, y, z + 1);
//x+ face
put(x + 1, y, z);
put(x + 1, y + 1, z);
put(x + 1, y + 1, z + 1);
put(x + 1, y, z + 1);
//top
put(x, y + 1, z);
put(x + 1, y + 1, z);
put(x + 1, y + 1, z + 1);
put(x, y + 1, z + 1);
}
public static void drawBlockFaces(BlockPos pos, Color color, Vec3d eyePos) {
drawBlockFaces(pos, color.getRGB(), eyePos);
}
public static void drawAABB(AxisAlignedBB box, int color, Vec3d eyePos) {
try {
ready();
translate(-eyePos.x, -eyePos.y, -eyePos.z);
color(color);
depth(false);
begin(GL11.GL_QUADS);
double entityHalfed = (box.maxX - box.minX) / 2;
double entityHeight = (box.maxY - box.minY);
Vec3d pos = new Vec3d(box.maxX - entityHalfed, box.minY, box.maxZ - entityHalfed);
drawAABBNow(pos, entityHalfed, entityHeight);
end();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void drawAABBNow(Vec3d halfPos, double entityHalfed, double entityHeight) {
// bottom
put(halfPos.x - entityHalfed, halfPos.y - 0.01, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y - 0.01, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y - 0.01, halfPos.z - entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y - 0.01, halfPos.z - entityHalfed);
// top
put(halfPos.x - entityHalfed, halfPos.y + entityHeight, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y + entityHeight, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y + entityHeight, halfPos.z - entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y + entityHeight, halfPos.z - entityHalfed);
// z -
put(halfPos.x - entityHalfed, halfPos.y + entityHeight, halfPos.z - entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y + entityHeight, halfPos.z - entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y - 0.01, halfPos.z - entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y - 0.01, halfPos.z - entityHalfed);
// z +
put(halfPos.x - entityHalfed, halfPos.y + entityHeight, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y + entityHeight, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y - 0.01, halfPos.z + entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y - 0.01, halfPos.z + entityHalfed);
// x -
put(halfPos.x - entityHalfed, halfPos.y + entityHeight, halfPos.z - entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y + entityHeight, halfPos.z + entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y - 0.01, halfPos.z + entityHalfed);
put(halfPos.x - entityHalfed, halfPos.y - 0.01, halfPos.z - entityHalfed);
// y +
put(halfPos.x + entityHalfed, halfPos.y + entityHeight, halfPos.z - entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y + entityHeight, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y - 0.01, halfPos.z + entityHalfed);
put(halfPos.x + entityHalfed, halfPos.y - 0.01, halfPos.z - entityHalfed);
}
}