1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
package tectech.thing.block;
import static tectech.rendering.EOH.EOHTileEntitySR.STAR_LAYER_0;
import static tectech.rendering.EOH.EOHTileEntitySR.STAR_LAYER_1;
import static tectech.rendering.EOH.EOHTileEntitySR.STAR_LAYER_2;
import static tectech.thing.casing.TTCasingsContainer.GodforgeCasings;
import java.nio.FloatBuffer;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.AdvancedModelLoader;
import net.minecraftforge.client.model.IModelCustom;
import org.joml.Matrix4fStack;
import org.joml.Vector3f;
import org.joml.Vector4f;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import com.gtnewhorizon.gtnhlib.client.renderer.shader.ShaderProgram;
import com.gtnewhorizon.gtnhlib.client.renderer.vbo.VertexBuffer;
import tectech.Reference;
import tectech.thing.metaTileEntity.multi.ForgeOfGodsRingsStructureString;
import tectech.thing.metaTileEntity.multi.ForgeOfGodsStructureString;
import tectech.util.StructureVBO;
import tectech.util.TextureUpdateRequester;
public class RenderForgeOfGods extends TileEntitySpecialRenderer {
private static ShaderProgram starProgram;
private static IModelCustom starModel;
private static float modelNormalize = .0067f * 2;
private static boolean initialized = false;
private static int u_Color = -1, u_ModelMatrix = -1, u_Gamma = -1;
private Matrix4fStack starModelMatrix = new Matrix4fStack(3);
private static ShaderProgram beamProgram;
private static int a_VertexID = -1;
private static int u_BeamModelMatrix = -1;
private static int u_CameraPosition = -1, u_SegmentArray = -1, u_SegmentQuads = -1;
private static int u_BeamIntensity = -1, u_BeamColor = -1, u_BeamTime = -1;
private static int beam_vboID = -1;
private static int maxSegments = -1;
private static final int beamSegmentQuads = 16;
private static Matrix4fStack beamModelMatrix = new Matrix4fStack(2);
private VertexBuffer ringOne, ringTwo, ringThree;
// These are nudges/translations for each ring to align with the structure
private static final Vector3f ringOneNudge = new Vector3f(0, -1, 0);
private static final Vector3f ringTwoNudge = new Vector3f(0, -1, 0);
private static final Vector3f ringThreeNudge = new Vector3f(.5f, -1, 0);
private static ShaderProgram fadeBypassProgram;
private static TextureUpdateRequester textureUpdater;
private void init() {
try {
starProgram = new ShaderProgram(Reference.MODID, "shaders/star.vert.glsl", "shaders/star.frag.glsl");
u_Color = starProgram.getUniformLocation("u_Color");
u_Gamma = starProgram.getUniformLocation("u_Gamma");
u_ModelMatrix = starProgram.getUniformLocation("u_ModelMatrix");
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
starModel = AdvancedModelLoader.loadModel(new ResourceLocation(Reference.MODID, "models/Star.obj"));
try {
beamProgram = new ShaderProgram(
Reference.MODID,
"shaders/gorgeBeam.vert.glsl",
"shaders/gorgeBeam.frag.glsl");
u_BeamModelMatrix = beamProgram.getUniformLocation("u_ModelMatrix");
u_CameraPosition = beamProgram.getUniformLocation("u_CameraPosition");
u_SegmentQuads = beamProgram.getUniformLocation("u_SegmentQuads");
u_SegmentArray = beamProgram.getUniformLocation("u_SegmentArray");
u_BeamColor = beamProgram.getUniformLocation("u_Color");
u_BeamIntensity = beamProgram.getUniformLocation("u_Intensity");
u_BeamTime = beamProgram.getUniformLocation("u_Time");
a_VertexID = beamProgram.getAttribLocation("a_VertexID");
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
beamProgram.use();
GL20.glUniform1f(u_SegmentQuads, (float) beamSegmentQuads);
maxSegments = 10;// GL20.glGetActiveUniformSize(beamProgram.getProgram(), u_SegmentArray);
FloatBuffer buffer = BufferUtils.createFloatBuffer(maxSegments * beamSegmentQuads * 6 * 3);
for (int i = 0; i < maxSegments; i++) {
for (int j = 0; j < beamSegmentQuads; j++) {
for (int v = 0; v < 6; v++) {
int segID = i * beamSegmentQuads * 6;
int quadID = j * 6;
int vertID = segID + quadID + v;
buffer.put(vertID);
buffer.put(0);
buffer.put(0);
}
}
}
buffer.flip();
beam_vboID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, beam_vboID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(a_VertexID, 1, GL11.GL_FLOAT, false, 3 * Float.BYTES, 0);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
ShaderProgram.clear();
initialized = true;
}
private void initRings() {
StructureVBO ringStructure = (new StructureVBO()).addMapping('H', BlockGodforgeGlass.INSTANCE, 0)
.addMapping('B', GodforgeCasings, 0)
.addMapping('C', GodforgeCasings, 1)
.addMapping('D', GodforgeCasings, 2)
.addMapping('E', GodforgeCasings, 3)
.addMapping('G', GodforgeCasings, 5)
.addMapping('K', GodforgeCasings, 6)
.addMapping('I', GodforgeCasings, 7);
ringOne = ringStructure.assignStructure(ForgeOfGodsStructureString.FIRST_RING)
.build();
ringTwo = ringStructure.assignStructure(ForgeOfGodsRingsStructureString.SECOND_RING)
.build();
ringThree = ringStructure.assignStructure(ForgeOfGodsRingsStructureString.THIRD_RING)
.build();
fadeBypassProgram = new ShaderProgram(
Reference.MODID,
"shaders/fadebypass.vert.glsl",
"shaders/fadebypass.frag.glsl");
textureUpdater = ringStructure.getTextureUpdateRequestor();
}
public void RenderStarLayer(Vector4f color, ResourceLocation texture, float size, Vector3f rotationAxis,
float degrees) {
starModelMatrix.pushMatrix();
starModelMatrix.rotate((degrees / 180f * ((float) Math.PI)), rotationAxis.x, rotationAxis.y, rotationAxis.z);
starModelMatrix.scale(size, size, size);
this.bindTexture(texture);
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
// Enable transparency if needed
if (color.w < 1 && color.w > 0) {
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(false); // Disable depth writing for transparency
}
FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
GL20.glUniformMatrix4(u_ModelMatrix, false, starModelMatrix.get(matrixBuffer));
GL20.glUniform4f(u_Color, color.x, color.y, color.z, color.w);
starModel.renderAll();
GL11.glPopAttrib();
starModelMatrix.popMatrix();
}
public void RenderEntireStar(TileEntityForgeOfGods tile, double x, double y, double z, float timer) {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glDisable(GL11.GL_LIGHTING);
starProgram.use();
float cx = (float) x + .5f;
float cy = (float) y + .5f;
float cz = (float) z + .5f;
float size = modelNormalize;
starModelMatrix.clear();
starModelMatrix.translate(cx, cy, cz);
size *= tile.getStarRadius();
timer *= tile.getRotationSpeed();
float r = tile.getColorR(), g = tile.getColorG(), b = tile.getColorB();
GL20.glUniform1f(u_Gamma, tile.getGamma());
RenderStarLayer(
new Vector4f(r, g, b, 1f),
STAR_LAYER_0,
size,
new Vector3f(0F, 1F, 1).normalize(),
130 + (timer) % 360000);
RenderStarLayer(
new Vector4f(r, g, b, 0.4f),
STAR_LAYER_1,
size * 1.02f,
new Vector3f(1F, 1F, 0F).normalize(),
-49 + (timer) % 360000);
RenderStarLayer(
new Vector4f(r, g, b, 0.2f),
STAR_LAYER_2,
size * 1.04f,
new Vector3f(1F, 0F, 1F).normalize(),
67 + (timer) % 360000);
ShaderProgram.clear();
GL11.glPopAttrib();
}
public void bufferSoftBeam(TileEntityForgeOfGods tile) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(maxSegments * 3);
float angle = tile.getStartAngle();
float radius = tile.getStarRadius() * 1.1f;
float startx = -radius * ((float) Math.cos(angle));
float starty = radius * ((float) Math.sin(angle));
buffer.put(starty);
buffer.put(startx);
buffer.put(0);
for (int i = tile.getRingCount() - 1; i >= 0; i--) {
buffer.put(tile.getLenRadius(i));
buffer.put(tile.getLensDistance(i));
buffer.put(1f);
}
buffer.put(TileEntityForgeOfGods.BACK_PLATE_RADIUS);
buffer.put(TileEntityForgeOfGods.BACK_PLATE_DISTANCE);
buffer.put(-.05f);
buffer.rewind();
GL20.glUniform3(u_SegmentArray, buffer);
}
public void bufferIntenseBeam(TileEntityForgeOfGods tile) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(maxSegments * 3);
float angle = tile.getStartAngle();
float radius = tile.getStarRadius() * 1.05f;
float startx = -radius * ((float) Math.cos(angle));
float starty = radius * ((float) Math.sin(angle));
// first lens means the one closest to the star
int firstLens = tile.getRingCount() - 1;
float nextx = tile.getLensDistance(firstLens);
float nexty = tile.getLenRadius(firstLens) * .75f;
float backx = Math.max(-radius, (nextx + radius) / 2);
float backy = TileEntityForgeOfGods.interpolate(startx, nextx, starty, nexty, backx);
buffer.put(backy);
buffer.put(backx);
buffer.put(0);
float transparency = .2f;
for (int i = tile.getRingCount() - 1; i >= 0; i--) {
buffer.put(tile.getLenRadius(i) / 2);
buffer.put(tile.getLensDistance(i));
buffer.put(transparency);
transparency += .3f;
}
float currx = tile.getLensDistance(0);
float curry = tile.getLenRadius(0) / 2;
float lastx = TileEntityForgeOfGods.BACK_PLATE_DISTANCE;
float lasty = Math.min(tile.getLenRadius(firstLens), TileEntityForgeOfGods.BACK_PLATE_RADIUS);
float midx = lastx + 8f;
float midy = TileEntityForgeOfGods.interpolate(currx, lastx, curry, lasty, midx);
buffer.put(midy);
buffer.put(midx);
buffer.put(transparency);
buffer.put(lasty);
buffer.put(lastx);
buffer.put(0f);
buffer.rewind();
GL20.glUniform3(u_SegmentArray, buffer);
// return buffer;
}
public void RenderBeamSegment(TileEntityForgeOfGods tile, double x, double y, double z, float timer) {
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
// GL11.glDisable(GL11.GL_TEXTURE_2D);
// GL11.glDisable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_ALPHA_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
this.bindTexture(new ResourceLocation(Reference.MODID, "models/spaceLayer.png"));
float cx = (float) x + .5f;
float cy = (float) y + .5f;
float cz = (float) z + .5f;
beamModelMatrix.clear();
beamModelMatrix.translate(cx, cy, cz);
beamModelMatrix.rotate(
tile.getRotAngle() / 180 * ((float) Math.PI),
tile.getRotAxisX(),
tile.getRotAxisY(),
tile.getRotAxisZ());
beamModelMatrix.rotate((float) Math.PI / 2f, 0, 1, 0);
beamProgram.use();
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthMask(false); // Disable depth writing for transparency
bufferSoftBeam(tile);
FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
GL20.glUniformMatrix4(u_BeamModelMatrix, false, beamModelMatrix.get(matrixBuffer));
beamModelMatrix.invert();
Vector4f cameraPosition = new Vector4f(
ActiveRenderInfo.objectX,
ActiveRenderInfo.objectY,
ActiveRenderInfo.objectZ,
1);
cameraPosition = beamModelMatrix.transform(cameraPosition);
GL20.glUniform3f(u_CameraPosition, cameraPosition.x, cameraPosition.y, cameraPosition.z);
GL20.glUniform3f(u_BeamColor, tile.getColorR(), tile.getColorG(), tile.getColorB());
GL20.glUniform1f(u_BeamIntensity, 2);
GL20.glUniform1f(u_BeamTime, timer);
GL20.glEnableVertexAttribArray(a_VertexID);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, maxSegments * beamSegmentQuads * 6);
GL20.glUniform3f(u_BeamColor, 1, 1, 1);
GL20.glUniform1f(u_BeamIntensity, 4);
bufferIntenseBeam(tile);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, maxSegments * beamSegmentQuads * 6);
GL20.glDisableVertexAttribArray(a_VertexID);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glPopAttrib();
ShaderProgram.clear();
}
private void RenderRings(TileEntityForgeOfGods tile, double x, double y, double z, float timer) {
bindTexture(TextureMap.locationBlocksTexture);
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
textureUpdater.requestUpdate();
fadeBypassProgram.use();
GL11.glPushMatrix();
GL11.glTranslated(x + .5f, y + .5f, z + .5f);
GL11.glRotatef(tile.getRotAngle(), tile.getRotAxisX(), tile.getRotAxisY(), tile.getRotAxisZ());
GL11.glRotatef(timer / 6 * 7, 1, 0, 0);
GL11.glTranslated(ringOneNudge.x, ringOneNudge.y, ringOneNudge.z);
ringOne.render();
GL11.glPopMatrix();
if (tile.getRingCount() > 1) {
GL11.glPushMatrix();
GL11.glTranslated(x + .5f, y + .5f, z + .5f);
GL11.glRotatef(tile.getRotAngle(), tile.getRotAxisX(), tile.getRotAxisY(), tile.getRotAxisZ());
GL11.glRotatef(-timer / 4 * 5, 1, 0, 0);
GL11.glTranslated(ringTwoNudge.x, ringTwoNudge.y, ringTwoNudge.z);
ringTwo.render();
GL11.glPopMatrix();
if (tile.getRingCount() > 2) {
GL11.glPushMatrix();
GL11.glTranslated(x + .5f, y + .5f, z + .5f);
GL11.glRotatef(tile.getRotAngle(), tile.getRotAxisX(), tile.getRotAxisY(), tile.getRotAxisZ());
GL11.glRotatef(timer * 3, 1, 0, 0);
GL11.glTranslated(ringThreeNudge.x, ringThreeNudge.y, ringThreeNudge.z);
ringThree.render();
GL11.glPopMatrix();
}
}
ShaderProgram.clear();
GL11.glPopAttrib();
}
@Override
public void renderTileEntityAt(TileEntity tile, double x, double y, double z, float timeSinceLastTick) {
if (!(tile instanceof TileEntityForgeOfGods forgeTile)) return;
if (forgeTile.getRingCount() < 1) return;
if (!initialized) {
init();
initRings();
if (!initialized) return;
}
// Based on system time to prevent tps issues from causing stutters
// Need to look into different timing system to prevent stutters based on tps issues
// But prevent bypassing the pause menu
long millis = System.currentTimeMillis() % (1000 * 36000);
float timer = millis / (50f); // to ticks
RenderEntireStar(forgeTile, x, y, z, timer);
RenderRings(forgeTile, x, y, z, timer);
RenderBeamSegment(forgeTile, x, y, z, timer);
}
public static void enablePseudoTransparentColorInversion() {
GL11.glEnable(GL11.GL_COLOR_LOGIC_OP);
GL11.glLogicOp(GL11.GL_OR_INVERTED);
}
public static void enableOpaqueColorInversion() {
GL11.glEnable(GL11.GL_COLOR_LOGIC_OP);
GL11.glLogicOp(GL11.GL_COPY_INVERTED);
}
public static void disableOpaqueColorInversion() {
GL11.glDisable(GL11.GL_COLOR_LOGIC_OP);
}
}
|