From a6a6adcfbdbc26c4649b21d0274f4e5b7a8f1e94 Mon Sep 17 00:00:00 2001 From: LemADEC Date: Sat, 4 Mar 2017 20:54:18 +0100 Subject: [PATCH] Fixed planet spining while in orbit Added star colorization --- .../cr0s/warpdrive/render/RenderSpaceSky.java | 177 ++++++++++++------ 1 file changed, 123 insertions(+), 54 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/render/RenderSpaceSky.java b/src/main/java/cr0s/warpdrive/render/RenderSpaceSky.java index 32f947a4..3490b7c1 100644 --- a/src/main/java/cr0s/warpdrive/render/RenderSpaceSky.java +++ b/src/main/java/cr0s/warpdrive/render/RenderSpaceSky.java @@ -4,6 +4,7 @@ import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.CelestialObject; +import java.awt.Color; import java.util.Random; import net.minecraft.client.Minecraft; @@ -282,8 +283,8 @@ public class RenderSpaceSky extends IRenderHandler { final double transitionOrbit = Math.max(0.0D, Math.min(PLANET_ORBIT, distanceToBorder)) / PLANET_ORBIT; // relative position above celestialObject - final double offsetX = (1.0 - transitionOrbit) * (celestialObject.borderRadiusX - distanceToCenterX) / celestialObject.borderRadiusX; - final double offsetZ = (1.0 - transitionOrbit) * (celestialObject.borderRadiusZ - distanceToCenterZ) / celestialObject.borderRadiusZ; + final double offsetX = (1.0 - transitionOrbit) * (distanceToCenterX / celestialObject.borderRadiusX); + final double offsetZ = (1.0 - transitionOrbit) * (distanceToCenterZ / celestialObject.borderRadiusZ); // simulating a non-planar universe... final double planetY_far = (celestialObject.dimensionId + 99 % 100 - 50) * Math.log(distanceToCenter) / 4.0D; @@ -307,7 +308,13 @@ public class RenderSpaceSky extends IRenderHandler { @SuppressWarnings("SuspiciousNameCombination") final double angleV_far = Math.atan2(Math.sqrt(distanceToCenterX * distanceToCenterX + distanceToCenterZ * distanceToCenterZ), planetY); final double angleV = Math.PI * (1.0D - transitionOrbit) + angleV_far * transitionOrbit; - final double angleS = 0.15D * celestialObject.dimensionId * transitionApproaching; // + (world.getTotalWorldTime() + partialTicks) * Math.PI / 6000.0D; + final double angleS = 0.15D * celestialObject.dimensionId * transitionApproaching // + (world.getTotalWorldTime() + partialTicks) * Math.PI / 6000.0D; + + angleH * (1.0D - transitionApproaching); + + if (celestialObject.dimensionId == 1 && (Minecraft.getSystemTime() / 10) % 100 == 0) { + WarpDrive.logger.info(String.format("transition Far %.2f Approaching %.2f Orbit %.2f distanceToCenter %.3f %.3f offset %.3f %.3f angle H %.3f V_far %.3f V %.3f S %.3f", + transitionFar, transitionApproaching, transitionOrbit, distanceToCenterX, distanceToCenterZ, offsetX, offsetZ, angleH, angleV_far, angleV, angleS)); + } // pre-computations final double sinH = Math.sin(angleH); @@ -328,9 +335,9 @@ public class RenderSpaceSky extends IRenderHandler { final double valH = offset2 * cosS + offset1 * sinS; final double y = valV * sinV + renderRange * cosV; final double valD = renderRange * sinV - valV * cosV; - final double x = valD * sinH - valH * cosH; - final double z = valH * sinH + valD * cosH; - tessellator.addVertexWithUV(100 * (1 - offsetX) + x, y, 100 * (1 - offsetZ) + z, (indexVertex & 2) / 2, (indexVertex + 1 & 2) / 2); + final double x = valD * sinH - valH * cosH + renderSize * offsetX; + final double z = valH * sinH + valD * cosH + renderSize * offsetZ; + tessellator.addVertexWithUV(x, y, z, (indexVertex & 2) / 2, (indexVertex + 1 & 2) / 2); } tessellator.draw(); @@ -341,59 +348,121 @@ public class RenderSpaceSky extends IRenderHandler { final Random rand = new Random(10842L); final boolean hasMoreStars = rand.nextBoolean() || rand.nextBoolean(); final Tessellator tessellator = Tessellator.instance; - tessellator.startDrawingQuads(); - for (int indexStars = 0; indexStars < (hasMoreStars ? 20000 : 6000); indexStars++) { - double randomX = rand.nextDouble() * 2.0D - 1.0D; - double randomY = rand.nextDouble() * 2.0D - 1.0D; - double randomZ = rand.nextDouble() * 2.0D - 1.0D; - final double lambda = 1.2D; - final double renderSize = 0.10F + 0.03F * Math.log(1.0D - rand.nextDouble()) / (-lambda); // random.nextFloat() * 0.5F; - double randomLength = randomX * randomX + randomY * randomY + randomZ * randomZ; + final double renderRangeMax = 200.0D; + for (int indexStars = 0; indexStars < (hasMoreStars ? 20000 : 2000); indexStars++) { + double randomX; + double randomY; + double randomZ; + double randomLength; + do { + randomX = rand.nextDouble() * 2.0D - 1.0D; + randomY = rand.nextDouble() * 2.0D - 1.0D; + randomZ = rand.nextDouble() * 2.0D - 1.0D; + randomLength = randomX * randomX + randomY * randomY + randomZ * randomZ; + } while (randomLength >= 1.0D || randomLength <= 0.90D); - if (randomLength < 1.0D && randomLength > 0.01D) { - // forcing Z-order - randomLength = 1.0D / Math.sqrt(randomLength); - randomX *= randomLength; - randomY *= randomLength; - randomZ *= randomLength; - - // scaling - final double x0 = randomX * 100.0D; - final double y0 = randomY * 100.0D; - final double z0 = randomZ * 100.0D; - - // angles - @SuppressWarnings("SuspiciousNameCombination") - final double angleH = Math.atan2(randomX, randomZ); - @SuppressWarnings("SuspiciousNameCombination") - final double angleV = Math.atan2(Math.sqrt(randomX * randomX + randomZ * randomZ), randomY); - final double angleS = rand.nextDouble() * Math.PI * 2.0D; - - // pre-computations - final double sinH = Math.sin(angleH); - final double cosH = Math.cos(angleH); - final double sinV = Math.sin(angleV); - final double cosV = Math.cos(angleV); - final double sinS = Math.sin(angleS); - final double cosS = Math.cos(angleS); - - for (int indexVertex = 0; indexVertex < 4; indexVertex++) { - final double valZero = 0.0D; - final double offset1 = ((indexVertex & 2) - 1) * renderSize; - final double offset2 = ((indexVertex + 1 & 2) - 1) * renderSize; - final double valV = offset1 * cosS - offset2 * sinS; - final double valH = offset2 * cosS + offset1 * sinS; - final double y1 = valV * sinV + valZero * cosV; - final double valD = valZero * sinV - valV * cosV; - final double x1 = valD * sinH - valH * cosH; - final double z1 = valH * sinH + valD * cosH; - tessellator.addVertex(x0 + x1, y0 + y1, z0 + z1); - } + final double renderSize = 0.4F + 0.05F * Math.log(1.1D - rand.nextDouble()); + + // forcing Z-order + randomLength = 1.0D / Math.sqrt(randomLength); + randomX *= randomLength; + randomY *= randomLength; + randomZ *= randomLength; + + // scaling + final double x0 = randomX * renderRangeMax; + final double y0 = randomY * renderRangeMax; + final double z0 = randomZ * renderRangeMax; + + // angles + @SuppressWarnings("SuspiciousNameCombination") + final double angleH = Math.atan2(randomX, randomZ); + @SuppressWarnings("SuspiciousNameCombination") + final double angleV = Math.atan2(Math.sqrt(randomX * randomX + randomZ * randomZ), randomY); + final double angleS = rand.nextDouble() * Math.PI * 2.0D; + + // colorization + final int rgb = getStarColorRGB(rand); + GL11.glColor4f(((rgb >> 16) & 0xFF) / 255.0F, ((rgb >> 8) & 0xFF) / 255.0F, (rgb & 0xFF) / 255.0F, 1.0F /* isSpace ? 1.0F : 0.2F /**/); + + // pre-computations + final double sinH = Math.sin(angleH); + final double cosH = Math.cos(angleH); + final double sinV = Math.sin(angleV); + final double cosV = Math.cos(angleV); + final double sinS = Math.sin(angleS); + final double cosS = Math.cos(angleS); + + tessellator.startDrawingQuads(); + for (int indexVertex = 0; indexVertex < 4; indexVertex++) { + final double valZero = 0.0D; + final double offset1 = ((indexVertex & 2) - 1) * renderSize; + final double offset2 = ((indexVertex + 1 & 2) - 1) * renderSize; + final double valV = offset1 * cosS - offset2 * sinS; + final double valH = offset2 * cosS + offset1 * sinS; + final double y1 = valV * sinV + valZero * cosV; + final double valD = valZero * sinV - valV * cosV; + final double x1 = valD * sinH - valH * cosH; + final double z1 = valH * sinH + valD * cosH; + tessellator.addVertex(x0 + x1, y0 + y1, z0 + z1); } + tessellator.draw(); } - tessellator.draw(); + } + + // colorization loosely inspired from Hertzsprung-Russell diagram + // (we're using it for non-star objects too, so yeah...) + private static int getStarColorRGB(Random rand) { + final double colorType = rand.nextDouble(); + float hue; + float saturation; + float brightness = 1.0F - 0.8F * rand.nextFloat(); // distance effect + + if (colorType <= 0.08D) {// 8% light blue (young star) + hue = 0.48F + 0.08F * rand.nextFloat(); + saturation = 0.18F + 0.22F * rand.nextFloat(); + + } else if (colorType <= 0.24D) {// 22% pure white (early age) + hue = 0.126F + 0.040F * rand.nextFloat(); + saturation = 0.00F + 0.15F * rand.nextFloat(); + brightness *= 0.95F; + + } else if (colorType <= 0.45D) {// 21% yellow white + hue = 0.126F + 0.040F * rand.nextFloat(); + saturation = 0.15F + 0.15F * rand.nextFloat(); + brightness *= 0.90F; + + } else if (colorType <= 0.67D) {// 22% yellow + hue = 0.126F + 0.040F * rand.nextFloat(); + saturation = 0.80F + 0.15F * rand.nextFloat(); + if (rand.nextInt(3) == 1) {// yellow giant + brightness *= 0.90F; + } else { + brightness *= 0.85F; + } + + } else if (colorType <= 0.92D) {// 25% orange + hue = 0.055F + 0.055F * rand.nextFloat(); + saturation = 0.85F + 0.15F * rand.nextFloat(); + if (rand.nextInt(3) == 1) {// (orange giant) + brightness *= 0.90F; + } else { + brightness *= 0.80F; + } + + } else {// red (mostly giants) + hue = 0.95F + 0.05F * rand.nextFloat(); + if (rand.nextInt(3) == 1) {// (red giant) + saturation = 0.80F + 0.20F * rand.nextFloat(); + brightness *= 0.95F; + } else { + saturation = 0.70F + 0.20F * rand.nextFloat(); + brightness *= 0.65F; + } + } + return Color.HSBtoRGB(hue, saturation, brightness); } private static Vec3 getCustomSkyColor() {