Skip to content

Commit

Permalink
Merge branch 'master' into configurable-channels
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchej123 authored Jul 7, 2024
2 parents 4e4cb76 + 8a44856 commit ed47060
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 165 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

dependencies {
api('com.github.GTNewHorizons:StructureLib:1.3.1:dev')
implementation("com.github.GTNewHorizons:GT5-Unofficial:5.09.48.27:dev")
implementation("com.github.GTNewHorizons:GT5-Unofficial:5.09.48.64:dev")
compileOnly("com.github.GTNewHorizons:NotEnoughEnergistics:1.6.0:dev")

runtimeOnlyNonPublishable("com.github.GTNewHorizons:StructureCompat:0.6.3:dev")
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.22'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.23'
}


3 changes: 1 addition & 2 deletions src/main/java/blockrenderer6343/BlockRenderer6343.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
version = Tags.VERSION,
name = BlockRenderer6343.MOD_NAME,
acceptedMinecraftVersions = "[1.7.10]",
dependencies = " required-after:CodeChickenLib;" + " required-after:NotEnoughItems;"
+ "required-after:structurelib")
dependencies = "required-after:NotEnoughItems;required-after:structurelib")
public class BlockRenderer6343 {

public static final String MOD_ID = "blockrenderer6343";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public void render(int x, int y, int width, int height, int mouseX, int mouseY)
&& mouseX < positionedRect.position.x + positionedRect.size.width
&& mouseY > positionedRect.position.y
&& mouseY < positionedRect.position.y + positionedRect.size.height) {
Vector3f hitPos = ProjectionUtils.unProject(mouseX, mouseY);
MovingObjectPosition result = rayTrace(hitPos);
Vector3f lookVec = ProjectionUtils.unProject(positionedRect, eyePos, lookAt, mouseX, mouseY);
MovingObjectPosition result = rayTrace(lookVec);
if (result != null) {
this.lastTraceResult = result;
onLookingAt.accept(result);
Expand Down Expand Up @@ -357,55 +357,13 @@ public static void setDefaultPassRenderState(int pass) {
}
}

public MovingObjectPosition rayTrace(Vector3f hitPos) {
public MovingObjectPosition rayTrace(Vector3f lookVec) {
Vec3 startPos = Vec3.createVectorHelper(this.eyePos.x, this.eyePos.y, this.eyePos.z);
hitPos.scale(2); // Double view range to ensure pos can be seen.
lookVec.scale(100); // range: 100 Blocks
Vec3 endPos = Vec3.createVectorHelper(
(hitPos.x - startPos.xCoord),
(hitPos.y - startPos.yCoord),
(hitPos.z - startPos.zCoord));
(lookVec.x + startPos.xCoord),
(lookVec.y + startPos.yCoord),
(lookVec.z + startPos.zCoord));
return ((TrackedDummyWorld) this.world).rayTraceBlockswithTargetMap(startPos, endPos, renderedBlocks);
}

/***
* For better performance, You'd better handle the event setOnLookingAt(Consumer) or getLastTraceResult()
*
* @param mouseX xPos in Texture
* @param mouseY yPos in Texture
* @return RayTraceResult Hit
*/
protected MovingObjectPosition screenPos2BlockPosFace(int mouseX, int mouseY, int x, int y, int width, int height) {
// render a frame
glEnable(GL_DEPTH_TEST);
setupCamera(getPositionedRect(x, y, width, height));

drawWorld();

Vector3f hitPos = ProjectionUtils.unProject(mouseX, mouseY);
MovingObjectPosition result = rayTrace(hitPos);

resetCamera();

return result;
}

/***
* For better performance, You'd better do project in setOnWorldRender(Consumer)
*
* @param pos BlockPos
* @param depth should pass Depth Test
* @return x, y, z
*/
protected Vector3f blockPos2ScreenPos(BlockPosition pos, boolean depth, int x, int y, int width, int height) {
// render a frame
glEnable(GL_DEPTH_TEST);
setupCamera(getPositionedRect(x, y, width, height));

drawWorld();
Vector3f winPos = ProjectionUtils.project(pos);

resetCamera();

return winPos;
}
}
152 changes: 48 additions & 104 deletions src/main/java/blockrenderer6343/client/utils/ProjectionUtils.java
Original file line number Diff line number Diff line change
@@ -1,117 +1,61 @@
package blockrenderer6343.client.utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;

import blockrenderer6343.api.utils.BlockPosition;
import blockrenderer6343.api.utils.PositionedRect;

public class ProjectionUtils {

private static final FloatBuffer MODELVIEW_MATRIX_BUFFER = ByteBuffer.allocateDirect(16 * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
private static final FloatBuffer PROJECTION_MATRIX_BUFFER = ByteBuffer.allocateDirect(16 * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
private static final IntBuffer VIEWPORT_BUFFER = ByteBuffer.allocateDirect(16 * 4).order(ByteOrder.nativeOrder())
.asIntBuffer();
protected static final FloatBuffer PIXEL_DEPTH_BUFFER = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder())
.asFloatBuffer();
protected static final FloatBuffer OBJECT_POS_BUFFER = ByteBuffer.allocateDirect(3 * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();

public static Vector3f project(BlockPosition pos) {
// read current rendering parameters
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, MODELVIEW_MATRIX_BUFFER);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, PROJECTION_MATRIX_BUFFER);
GL11.glGetInteger(GL11.GL_VIEWPORT, VIEWPORT_BUFFER);

// rewind buffers after write by OpenGL glGet calls
MODELVIEW_MATRIX_BUFFER.rewind();
PROJECTION_MATRIX_BUFFER.rewind();
VIEWPORT_BUFFER.rewind();

// call gluProject with retrieved parameters
GLU.gluProject(
pos.x + 0.5f,
pos.y + 0.5f,
pos.z + 0.5f,
MODELVIEW_MATRIX_BUFFER,
PROJECTION_MATRIX_BUFFER,
VIEWPORT_BUFFER,
OBJECT_POS_BUFFER);

// rewind buffers after read by gluProject
VIEWPORT_BUFFER.rewind();
PROJECTION_MATRIX_BUFFER.rewind();
MODELVIEW_MATRIX_BUFFER.rewind();

// rewind buffer after write by gluProject
OBJECT_POS_BUFFER.rewind();

// obtain position in Screen
float winX = OBJECT_POS_BUFFER.get();
float winY = OBJECT_POS_BUFFER.get();
float winZ = OBJECT_POS_BUFFER.get();

// rewind buffer after read
OBJECT_POS_BUFFER.rewind();

return new Vector3f(winX, winY, winZ);
public static Vector3f unProject(PositionedRect rect, Vector3f eyePos, Vector3f lookat, int mouseX, int mouseY) {
int width = rect.size.width;
int height = rect.size.height;

double aspectRatio = ((double) width / (double) height);
double fov = ((60 / 2d)) * (Math.PI / 180);

double a = -((double) (mouseX - rect.position.x) / (double) width - 0.5) * 2;
double b = -((double) (height - (mouseY - rect.position.y)) / (double) height - 0.5) * 2;
double tanf = Math.tan(fov);

Vector3f lookVec = new Vector3f();
Vector3f.sub(eyePos, lookat, lookVec);
float yawn = (float) Math.atan2(lookVec.x, -lookVec.z);
float pitch = (float) Math.atan2(lookVec.y, Math.sqrt(lookVec.x * lookVec.x + lookVec.z * lookVec.z));

Matrix4f rot = new Matrix4f();
rot.rotate(yawn, new Vector3f(0, -1, 0));
rot.rotate(pitch, new Vector3f(1, 0, 0));
Vector4f foward = new Vector4f(0, 0, 1, 0);
Vector4f up = new Vector4f(0, 1, 0, 0);
Vector4f left = new Vector4f(1, 0, 0, 0);
Matrix4f.transform(rot, foward, foward);
Matrix4f.transform(rot, up, up);
Matrix4f.transform(rot, left, left);

Vector3f result = new Vector3f(foward.x, foward.y, foward.z);
Vector3f.add(
result,
new Vector3f(
(float) (left.x * tanf * aspectRatio * a),
(float) (left.y * tanf * aspectRatio * a),
(float) (left.z * tanf * aspectRatio * a)),
result);
Vector3f.add(
result,
new Vector3f((float) (up.x * tanf * b), (float) (up.y * tanf * b), (float) (up.z * tanf * b)),
result);
return normalize(result);
}

public static Vector3f unProject(int mouseX, int mouseY) {
// read depth of pixel under mouse
GL11.glReadPixels(mouseX, mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, PIXEL_DEPTH_BUFFER);

// rewind buffer after write by glReadPixels
PIXEL_DEPTH_BUFFER.rewind();

// retrieve depth from buffer (0.0-1.0f)
float pixelDepth = PIXEL_DEPTH_BUFFER.get();

// rewind buffer after read
PIXEL_DEPTH_BUFFER.rewind();

// read current rendering parameters
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, MODELVIEW_MATRIX_BUFFER);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, PROJECTION_MATRIX_BUFFER);
GL11.glGetInteger(GL11.GL_VIEWPORT, VIEWPORT_BUFFER);

// rewind buffers after write by OpenGL glGet calls
MODELVIEW_MATRIX_BUFFER.rewind();
PROJECTION_MATRIX_BUFFER.rewind();
VIEWPORT_BUFFER.rewind();

// call gluUnProject with retrieved parameters
GLU.gluUnProject(
mouseX,
mouseY,
pixelDepth,
MODELVIEW_MATRIX_BUFFER,
PROJECTION_MATRIX_BUFFER,
VIEWPORT_BUFFER,
OBJECT_POS_BUFFER);

// rewind buffers after read by gluUnProject
VIEWPORT_BUFFER.rewind();
PROJECTION_MATRIX_BUFFER.rewind();
MODELVIEW_MATRIX_BUFFER.rewind();

// rewind buffer after write by gluUnProject
OBJECT_POS_BUFFER.rewind();
public static Vector3f normalize(Vector3f vec) {
float length = (float) Math.sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);

// obtain absolute position in world
float posX = OBJECT_POS_BUFFER.get();
float posY = OBJECT_POS_BUFFER.get();
float posZ = OBJECT_POS_BUFFER.get();
vec.x /= length;
vec.y /= length;
vec.z /= length;

// rewind buffer after read
OBJECT_POS_BUFFER.rewind();
return new Vector3f(posX, posY, posZ);
return vec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void tryLoadingMultiblock(ItemStack candidate) {
Block block = ib.field_150939_a;
if (block.hasTileEntity(candidate.getItemDamage())) {
TileEntity te = block.createTileEntity(DummyWorld.INSTANCE, ib.getMetadata(candidate.getItemDamage()));
if (IMultiblockInfoContainer.contains(te.getClass())) {
if (te != null && IMultiblockInfoContainer.contains(te.getClass())) {
baseHandler.setOnIngredientChanged(ingredients -> {
this.ingredients = ingredients;
resetPositionedIngredients();
Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
"credits": "",
"logoFile": "",
"screenshots": [],
"parent": "",
"requiredMods": [],
"dependencies": [],
"dependants": [],
"useDependencyInformation": true
"parent": ""
}]
}

0 comments on commit ed47060

Please sign in to comment.