Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 84 additions & 15 deletions src/main/java/gcewing/sg/renderers/SGBaseTERenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static java.lang.Math.min;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COMPILE;
import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
import static org.lwjgl.opengl.GL11.GL_LIGHTING;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
Expand All @@ -18,13 +19,16 @@
import static org.lwjgl.opengl.GL11.GL_TRIANGLE_FAN;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glCallList;
import static org.lwjgl.opengl.GL11.glColor3d;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glDepthMask;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glEndList;
import static org.lwjgl.opengl.GL11.glNewList;
import static org.lwjgl.opengl.GL11.glNormal3d;
import static org.lwjgl.opengl.GL11.glNormal3f;
import static org.lwjgl.opengl.GL11.glPopMatrix;
Expand All @@ -36,8 +40,10 @@
import static org.lwjgl.opengl.GL11.glVertex3d;
import static org.lwjgl.opengl.GL12.GL_RESCALE_NORMAL;

import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;

import org.joml.Vector3d;

Expand Down Expand Up @@ -99,8 +105,37 @@ public class SGBaseTERenderer extends BaseTileEntityRenderer {
}
}

static final ResourceLocation stargateTexture = SGCraft.mod.resourceLocation("textures/tileentity/stargate.png");
static final ResourceLocation eventHorizonTexture = SGCraft.mod
.resourceLocation("textures/tileentity/eventhorizon.png");
static final ResourceLocation irisTexture = SGCraft.mod.resourceLocation("textures/tileentity/iris.png");

// static data for display lists
static final int LIST_OUTER_RING = 0;
static final int LIST_INNER_RING = 1;
static final int LIST_CHEVRON_BODY = 2;
static final int LIST_CHEVRON_LIGHT = 3;
static final int NUM_LISTS = 4;
static int listBase = -1;

double u0, v0;

void compileDisplayLists() {
listBase = GLAllocation.generateDisplayLists(NUM_LISTS);
glNewList(listBase + LIST_OUTER_RING, GL_COMPILE);
renderRing(ringMidRadius - ringOverlap, ringOuterRadius, RingType.Outer, ringZOffset);
glEndList();
glNewList(listBase + LIST_INNER_RING, GL_COMPILE);
renderRing(ringInnerRadius, ringMidRadius, RingType.Inner, 0);
glEndList();
glNewList(listBase + LIST_CHEVRON_BODY, GL_COMPILE);
chevronBody();
glEndList();
glNewList(listBase + LIST_CHEVRON_LIGHT, GL_COMPILE);
chevronLight();
glEndList();
}

@Override
public void renderTileEntityAt(TileEntity te, double x, double y, double z, float t, int destroyStage) {
SGBaseTE tesg = (SGBaseTE) te;
Expand All @@ -112,20 +147,31 @@ public void renderTileEntityAt(TileEntity te, double x, double y, double z, floa
if (SGBaseTE.transparency) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else glDisable(GL_BLEND);
} else {
glDisable(GL_BLEND);
}

glEnable(GL_RESCALE_NORMAL);
glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
glTranslated(x + 0.5, y + 2.5, z + 0.5);

renderStargate(tesg, t);

glDisable(GL_RESCALE_NORMAL);
glPopMatrix();
}

void renderStargate(SGBaseTE te, float t) {
BaseGLUtils.glMultMatrix(te.localToGlobalTransformation(new Vector3d()));
bindTexture(SGCraft.mod.resourceLocation("textures/tileentity/stargate.png"));

if (listBase < 0) {
// lazily generate vertices once
compileDisplayLists();
}

bindTexture(stargateTexture);
glNormal3f(0, 1, 0);
renderRing(ringMidRadius - ringOverlap, ringOuterRadius, RingType.Outer, ringZOffset);
glCallList(listBase + LIST_OUTER_RING);
renderInnerRing(te, t);
renderChevrons(te);
if (te.hasIrisUpgrade) renderIris(te, t);
Expand All @@ -135,7 +181,7 @@ void renderStargate(SGBaseTE te, float t) {
void renderInnerRing(SGBaseTE te, float t) {
glPushMatrix();
glRotatef((float) (te.interpolatedRingAngle(t) + SGBaseTE.ringSymbolAngle / 2), 0, 0, 1);
renderRing(ringInnerRadius, ringMidRadius, RingType.Inner, 0);
glCallList(listBase + LIST_INNER_RING);
glPopMatrix();
}

Expand Down Expand Up @@ -212,6 +258,27 @@ void renderChevronAtPosition(int i, float a, boolean engaged) {
}

void chevron(boolean engaged) {
if (engaged) {
glTranslated(-chevronMotionDistance, 0, 0);
}
glCallList(listBase + LIST_CHEVRON_BODY);

// Draw light
if (engaged) {
glDisable(GL_LIGHTING);
setLightingDisabled(true);
} else {
glColor3d(0.5, 0.5, 0.5);
}
glCallList(listBase + LIST_CHEVRON_LIGHT);

// Reset state
glColor3f(1, 1, 1);
glEnable(GL_LIGHTING);
setLightingDisabled(false);
}

void chevronBody() {
double r1 = chevronInnerRadius;
double r2 = chevronOuterRadius;
double z2 = ringDepth / 2;
Expand All @@ -221,7 +288,6 @@ void chevron(boolean engaged) {
double x1 = r1, y1 = chevronWidth / 4;
double x2 = r2, y2 = chevronWidth / 2;

if (engaged) glTranslated(-chevronMotionDistance, 0, 0);
glBegin(GL_QUADS);

selectTile(chevronTextureIndex);
Expand Down Expand Up @@ -281,13 +347,19 @@ void chevron(boolean engaged) {
vertex(x2, y2, z2, 16, 0);

glEnd();
}

void chevronLight() {
double r1 = chevronInnerRadius;
double r2 = chevronOuterRadius;
double z2 = ringDepth / 2;
double z1 = z2 + chevronDepth;
double w1 = chevronBorderWidth;
double w2 = w1 * 1.25;
double x1 = r1, y1 = chevronWidth / 4;
double x2 = r2, y2 = chevronWidth / 2;

selectTile(chevronLitTextureIndex);
if (!engaged) glColor3d(0.5, 0.5, 0.5);
else {
glDisable(GL_LIGHTING);
setLightingDisabled(true);
}
glBegin(GL_QUADS);

// Face 4
Expand All @@ -307,10 +379,7 @@ void chevron(boolean engaged) {
vertex(x2, -y2 + w2, z1, 16, 4);
vertex(x2, -y2 + w2, z2, 16, 0);

glColor3f(1, 1, 1);
glEnd();
glEnable(GL_LIGHTING);
setLightingDisabled(false);
}

protected static void setLightingDisabled(boolean off) {
Expand All @@ -321,7 +390,7 @@ protected static void setLightingDisabled(boolean off) {
}

void renderEventHorizon(SGBaseTE te) {
bindTexture(SGCraft.mod.resourceLocation("textures/tileentity/eventhorizon.png"));
bindTexture(eventHorizonTexture);
glDisable(GL_CULL_FACE);
glNormal3d(0, 0, 1);
double[][] grid = te.getEventHorizonGrid()[0];
Expand Down Expand Up @@ -358,7 +427,7 @@ void ehVertex(double[][] grid, int i, int j, double rclip) {
}

void renderIris(SGBaseTE te, double t) {
bindTexture(SGCraft.mod.resourceLocation("textures/tileentity/iris.png"));
bindTexture(irisTexture);
double a = 0.8 * te.getIrisAperture(t);
for (int i = 0; i < numIrisBlades; i++) {
glPushMatrix();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gcewing/sg/utils/BaseGLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

public class BaseGLUtils {

private static final FloatBuffer matrixBuffer = GLAllocation.createDirectFloatBuffer(16);

public static void glMultMatrix(Trans3 t) {
FloatBuffer b = GLAllocation.createDirectFloatBuffer(16);
FloatBuffer b = matrixBuffer;
Vector3d v = t.offset;
Matrix3d m = t.rotation;

Expand Down