Skip to content

Instantly share code, notes, and snippets.

@Gutawer
Last active July 21, 2018 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gutawer/389966d49eb9a253fbfca5687cdbf52c to your computer and use it in GitHub Desktop.
Save Gutawer/389966d49eb9a253fbfca5687cdbf52c to your computer and use it in GitHub Desktop.
class ####_ProjectionTestHandler : StaticEventHandler {
override void renderOverlay(RenderEvent e) {
if (automapactive != 0) return;
double fovH = (e.camera.player != NULL) ? e.camera.player.FOV : e.camera.cameraFOV;
####_Matrix worldToClip = ####_Matrix.worldToClip(e.viewPos, e.viewAngle, e.viewPitch, e.viewRoll, fovH);
PlayerInfo player = players[consoleplayer];
if (player != NULL) {
####_ProjectionTestPlayer mo = ####_ProjectionTestPlayer(player.mo);
if (mo != NULL) {
ThinkerIterator iter = ThinkerIterator.create("Actor", Thinker.STAT_DEFAULT);
Actor projActor;
while ((projActor = Actor(iter.next())) != NULL) {
// this moves the position of the actor into absolute coordinates relative to the viewPos
// (which the view matrix is expecting)
Vector3 adjustedWorldPos = e.viewPos + LevelLocals.vec3Diff(e.viewPos, projActor.pos);
Vector3 positions[8];
positions[0] = (adjustedWorldPos + ( projActor.radius, projActor.radius, 0 ));
positions[1] = (adjustedWorldPos + (-projActor.radius, projActor.radius, 0 ));
positions[2] = (adjustedWorldPos + ( projActor.radius, -projActor.radius, 0 ));
positions[3] = (adjustedWorldPos + (-projActor.radius, -projActor.radius, 0 ));
positions[4] = (adjustedWorldPos + ( projActor.radius, projActor.radius, projActor.height));
positions[5] = (adjustedWorldPos + (-projActor.radius, projActor.radius, projActor.height));
positions[6] = (adjustedWorldPos + ( projActor.radius, -projActor.radius, projActor.height));
positions[7] = (adjustedWorldPos + (-projActor.radius, -projActor.radius, projActor.height));
Vector3 ndcPositions[8];
// ndcCounter tracks how many valid ndcPositions were found, if it's not 8 by the end of the loop then
// we won't draw anything
int ndcCounter = 0;
bool anyOnScreen = false;
for (int i = 0; i < 8; i++) {
Vector3 worldPos = positions[i];
Vector3 ndcPos = worldToClip.multiplyVector3(worldPos).asVector3();
if (ndcPos.x <= 1 && ndcPos.x >= -1 &&
ndcPos.y <= 1 && ndcPos.y >= -1 ) {
anyOnScreen = true;
}
if (ndcPos.z <= 1 && ndcPos.z >= -1) {
ndcCounter++;
ndcPositions[i] = ndcPos;
}
}
if (anyOnScreen && ndcCounter >= 8) {
// the infinities are used here since any real number will be automatically lower/higher than infinity/-infinity
double lowestX = double.infinity;
double lowestY = double.infinity;
double highestX = -double.infinity;
double highestY = -double.infinity;
// create a bounding box from the projected positions
for (int i = 0; i < 8; i++) {
Vector2 ndc = ndcPositions[i].xy;
if (ndc.x < lowestX ) lowestX = ndc.x;
if (ndc.y < lowestY ) lowestY = ndc.y;
if (ndc.x > highestX) highestX = ndc.x;
if (ndc.y > highestY) highestY = ndc.y;
}
Vector3 corners[4];
corners[0] = (clamp(lowestX , -0.95, 0.95), clamp(lowestY , -0.95, 0.95), 0);
corners[1] = (clamp(highestX, -0.95, 0.95), clamp(lowestY , -0.95, 0.95), 0);
corners[2] = (clamp(lowestX , -0.95, 0.95), clamp(highestY, -0.95, 0.95), 0);
corners[3] = (clamp(highestX, -0.95, 0.95), clamp(highestY, -0.95, 0.95), 0);
string cornerTextures[4];
cornerTextures[0] = "PRJBOXBL";
cornerTextures[1] = "PRJBOXBR";
cornerTextures[2] = "PRJBOXTL";
cornerTextures[3] = "PRJBOXTR";
Vector2 cornerDirs[4];
cornerDirs[0] = (-1, 1);
cornerDirs[1] = ( 1, 1);
cornerDirs[2] = (-1, -1);
cornerDirs[3] = ( 1, -1);
double frac = (gametic + e.fracTic) / 35.0;
for (int i = 0; i < 4; i++) {
Vector2 viewportPos = ####_GlobalMaths.ndcToViewPort(corners[i]);
viewportPos += (2 * abs(2 * (frac - floor(frac + 0.5))) - 1) * 6 * cornerDirs[i];
Screen.drawTexture(TexMan.checkForTexture(cornerTextures[i], TexMan.Type_Any), false, viewportPos.x, viewportPos.y);
}
Vector2 tlCornerPos = ####_GlobalMaths.ndcToViewPort(corners[2]) + (2 * abs(2 * (frac - floor(frac + 0.5))) - 1) * 6 * cornerDirs[2];
Screen.drawText(smallfont, Font.CR_WHITE, tlCornerPos.x + 8, tlCornerPos.y + 8, projActor.getTag());
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment