made progress in block selection from third person view
This commit is contained in:
parent
2f1eb04d5d
commit
bcd31a1f96
5 changed files with 106 additions and 58 deletions
|
@ -9,15 +9,20 @@
|
|||
package buildcraft;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.renderer.GLAllocation;
|
||||
import net.minecraft.entity.EntityList;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraftforge.client.event.RenderWorldLastEvent;
|
||||
import net.minecraftforge.client.event.TextureStitchEvent;
|
||||
import net.minecraftforge.common.IPlantable;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
@ -26,6 +31,11 @@ import net.minecraftforge.common.config.Property;
|
|||
import net.minecraftforge.fluids.BlockFluidBase;
|
||||
import net.minecraftforge.fluids.IFluidBlock;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.glu.GLU;
|
||||
|
||||
import buildcraft.api.core.BuildCraftAPI;
|
||||
import buildcraft.api.core.IIconProvider;
|
||||
import buildcraft.api.gates.ActionManager;
|
||||
|
@ -357,4 +367,55 @@ public class BuildCraftCore extends BuildCraftMod {
|
|||
public void processIMCRequests(FMLInterModComms.IMCEvent event) {
|
||||
InterModComms.processIMC(event);
|
||||
}
|
||||
|
||||
public static float diffX, diffY, diffZ;
|
||||
|
||||
static FloatBuffer modelviewF = GLAllocation.createDirectFloatBuffer(16);
|
||||
static FloatBuffer projectionF = GLAllocation.createDirectFloatBuffer(16);
|
||||
static IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
|
||||
static FloatBuffer pos = ByteBuffer.allocateDirect(3 * 4).asFloatBuffer();
|
||||
|
||||
@SubscribeEvent
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void renderLast (RenderWorldLastEvent evt) {
|
||||
/**
|
||||
* Note (SpaceToad): Why on earth this thing eventually worked out is a
|
||||
* mystery to me. In particular, all the examples I got computed y in
|
||||
* a different way. Anyone with further OpenGL understanding would be
|
||||
* welcome to explain.
|
||||
*
|
||||
* Anyway, the purpose of this code is to store the block position
|
||||
* pointed by the mouse at each frame, relative to the entity that has
|
||||
* the camera.
|
||||
*
|
||||
* It got heavily inspire from the two following sources:
|
||||
* http://nehe.gamedev.net/article/using_gluunproject/16013/
|
||||
* #ActiveRenderInfo.updateRenderInfo.
|
||||
*
|
||||
* See EntityUrbanist#rayTraceMouse for a usage example.
|
||||
*/
|
||||
|
||||
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelviewF);
|
||||
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionF);
|
||||
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
|
||||
float f = (viewport.get(0) + viewport.get(2)) / 2;
|
||||
float f1 = (viewport.get(1) + viewport.get(3)) / 2;
|
||||
|
||||
float x = Mouse.getX();
|
||||
float y = Mouse.getY();
|
||||
|
||||
// TODO: Minecraft seems to instist to have this winZ re-created at
|
||||
// each frame - looks like a memory leak to me but I couldn't use a
|
||||
// static variable instead, as for the rest.
|
||||
FloatBuffer winZ = GLAllocation.createDirectFloatBuffer(1);
|
||||
GL11.glReadPixels((int) x, (int) y, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);
|
||||
|
||||
GLU.gluUnProject(x, y, winZ.get(), modelviewF, projectionF, viewport,
|
||||
pos);
|
||||
|
||||
diffX = pos.get(0);
|
||||
diffY = pos.get(1);
|
||||
diffZ = pos.get(2);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,13 @@
|
|||
*/
|
||||
package buildcraft.builders.urbanism;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.DoubleBuffer;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GLAllocation;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.MovingObjectPosition;
|
||||
|
@ -16,8 +22,8 @@ import net.minecraft.util.Vec3;
|
|||
import net.minecraft.world.World;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.input.Mouse;
|
||||
|
||||
import buildcraft.BuildCraftCore;
|
||||
import buildcraft.api.core.Position;
|
||||
import buildcraft.core.EntityEnergyLaser;
|
||||
|
||||
|
@ -26,7 +32,7 @@ public class EntityUrbanist extends EntityLivingBase {
|
|||
/**
|
||||
* To be used only in debug sessions to adjust the mouse pointer parameters.
|
||||
*/
|
||||
private boolean debugPointer = false;
|
||||
private boolean debugPointer = true;
|
||||
private EntityEnergyLaser laser = null;
|
||||
|
||||
public EntityLivingBase player;
|
||||
|
@ -109,67 +115,48 @@ public class EntityUrbanist extends EntityLivingBase {
|
|||
return null;
|
||||
}
|
||||
|
||||
static FloatBuffer modelviewF = GLAllocation.createDirectFloatBuffer(16);
|
||||
static FloatBuffer projectionF = GLAllocation.createDirectFloatBuffer(16);
|
||||
static DoubleBuffer modelviewD = ByteBuffer.allocateDirect(16 * 8).asDoubleBuffer();
|
||||
static DoubleBuffer projectionD = ByteBuffer.allocateDirect(16 * 8).asDoubleBuffer();
|
||||
static IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
|
||||
static FloatBuffer winZ = ByteBuffer.allocateDirect(1 * 4).asFloatBuffer();
|
||||
static FloatBuffer pos = ByteBuffer.allocateDirect(3 * 4).asFloatBuffer();
|
||||
|
||||
public MovingObjectPosition rayTraceMouse() {
|
||||
// These three parameters have been determined experimentally. Proper
|
||||
// ray tracing would involve matrix inversion through e.g. gluUnProject.
|
||||
// This works just fine right now, and can be re-adjusted using the
|
||||
// debug pointer (a laser instance). Note: laser parameters are not yet
|
||||
// synchronized with the client, not needs to tweaks to be used
|
||||
// properly.
|
||||
double posAdjust = -1F;
|
||||
float diffScale = 3.67F;
|
||||
float diffLScale = 5.2F;
|
||||
|
||||
double distance = 1000;
|
||||
|
||||
float width = Minecraft.getMinecraft().displayWidth;
|
||||
float height = Minecraft.getMinecraft().displayHeight;
|
||||
|
||||
float x = Mouse.getX();
|
||||
float y = Mouse.getY();
|
||||
|
||||
// the height determines the view scale, hence / height in both cases
|
||||
float diffX = (x - (width / 2F)) / height * 2F;
|
||||
float diffY = (y - (height / 2F)) / height * 2F;
|
||||
|
||||
diffX *= diffScale;
|
||||
diffY *= diffScale;
|
||||
|
||||
float diffXL = diffX / diffLScale;
|
||||
float diffYL = diffY / diffLScale;
|
||||
|
||||
Vec3 pos = this.getPosition(1.0F);
|
||||
Vec3 look = this.getLook(1.0F).normalize();
|
||||
|
||||
Vec3 worldUp = worldObj.getWorldVec3Pool().getVecFromPool(0, 1, 0);
|
||||
Vec3 side = worldUp.crossProduct(look).normalize();
|
||||
Vec3 up = side.crossProduct(look).normalize();
|
||||
|
||||
pos = pos.addVector(up.xCoord * posAdjust, up.yCoord * posAdjust, up.zCoord * posAdjust);
|
||||
pos = pos.addVector(side.xCoord * -diffX, side.yCoord * -diffX, side.zCoord * -diffX);
|
||||
pos = pos.addVector(up.xCoord * -diffY, up.yCoord * -diffY, up.zCoord * -diffY);
|
||||
|
||||
look = look.addVector(side.xCoord * -diffXL, side.yCoord * -diffXL, side.zCoord * -diffXL);
|
||||
look = look.addVector(up.xCoord * -diffYL, up.yCoord * -diffYL, up.zCoord * -diffYL);
|
||||
pos.xCoord += BuildCraftCore.diffX;
|
||||
pos.yCoord += BuildCraftCore.diffY;
|
||||
pos.zCoord += BuildCraftCore.diffZ;
|
||||
|
||||
Vec3 vec32 = pos.addVector(look.xCoord * distance, look.yCoord * distance, look.zCoord * distance);
|
||||
|
||||
MovingObjectPosition result = this.worldObj.rayTraceBlocks(pos, vec32);
|
||||
|
||||
// TODO: work on pos not on result!!!
|
||||
System.out.println ("POS = " + pos.xCoord + ", " + pos.yCoord + ", " + pos.zCoord);
|
||||
System.out.println ("RESULT = " + result.blockX + ", " + result.blockY + ", " + result.blockZ);
|
||||
|
||||
if (debugPointer) {
|
||||
if (laser == null) {
|
||||
// note: as this is on the client, it will only work if the
|
||||
// server client update is deactivated in the server updateentity.
|
||||
laser = new EntityEnergyLaser(worldObj, new Position(posX,
|
||||
posY, posZ), new Position(posX, posY, posZ));
|
||||
|
||||
worldObj.spawnEntityInWorld(laser);
|
||||
}
|
||||
|
||||
pos = this.getPosition(1.0F);
|
||||
pos = pos.addVector(up.xCoord * posAdjust, up.yCoord * posAdjust,
|
||||
up.zCoord * posAdjust);
|
||||
pos = pos.addVector(side.xCoord * -diffX, side.yCoord * -diffX,
|
||||
side.zCoord * -diffX);
|
||||
pos = pos.addVector(up.xCoord * -diffY, up.yCoord * -diffY,
|
||||
up.zCoord * -diffY);
|
||||
pos.xCoord += BuildCraftCore.diffX;
|
||||
pos.yCoord += BuildCraftCore.diffY + 0.1F;
|
||||
pos.zCoord += BuildCraftCore.diffZ;
|
||||
|
||||
look = this.getLook(1.0F).normalize();
|
||||
|
||||
Vec3 aimed = worldObj.getWorldVec3Pool().getVecFromPool(
|
||||
pos.xCoord + look.xCoord * 200,
|
||||
|
|
|
@ -176,8 +176,9 @@ public class GuiUrbanist extends GuiAdvancedInterface {
|
|||
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) {
|
||||
super.mouseClicked(mouseX, mouseY, mouseButton);
|
||||
|
||||
if (!onInterface(mouseX, mouseY)) {
|
||||
System.out.println ("CLICKED");
|
||||
|
||||
if (!onInterface(mouseX, mouseY)) {
|
||||
if (selectedTool != -1) {
|
||||
tools [selectedTool].worldClicked(this, urbanist.urbanist.rayTraceMouse());
|
||||
}
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
*/
|
||||
package buildcraft.core;
|
||||
|
||||
import buildcraft.api.core.Position;
|
||||
import buildcraft.core.proxy.CoreProxy;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
import buildcraft.api.core.Position;
|
||||
|
||||
public abstract class EntityLaser extends Entity {
|
||||
|
||||
|
@ -72,9 +71,9 @@ public abstract class EntityLaser extends Entity {
|
|||
needsUpdate = false;
|
||||
}
|
||||
|
||||
if (worldObj.isRemote) {
|
||||
updateDataClient();
|
||||
}
|
||||
//if (worldObj.isRemote) {
|
||||
// updateDataClient();
|
||||
//}
|
||||
|
||||
boundingBox.minX = Math.min(data.head.x, data.tail.x);
|
||||
boundingBox.minY = Math.min(data.head.y, data.tail.y);
|
||||
|
|
|
@ -10,10 +10,6 @@ package buildcraft.core.render;
|
|||
|
||||
import java.util.Date;
|
||||
|
||||
import buildcraft.api.core.Position;
|
||||
import buildcraft.core.EntityLaser;
|
||||
import buildcraft.core.LaserData;
|
||||
import buildcraft.core.render.RenderEntityBlock.RenderInfo;
|
||||
import net.minecraft.client.model.ModelBase;
|
||||
import net.minecraft.client.model.ModelRenderer;
|
||||
import net.minecraft.client.renderer.GLAllocation;
|
||||
|
@ -24,6 +20,11 @@ import net.minecraft.util.ResourceLocation;
|
|||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import buildcraft.api.core.Position;
|
||||
import buildcraft.core.EntityLaser;
|
||||
import buildcraft.core.LaserData;
|
||||
import buildcraft.core.render.RenderEntityBlock.RenderInfo;
|
||||
|
||||
public class RenderLaser extends Render {
|
||||
|
||||
public static final float STEP = 0.04F;
|
||||
|
@ -64,8 +65,8 @@ public class RenderLaser extends Render {
|
|||
|
||||
RenderInfo block = new RenderInfo();
|
||||
|
||||
float minSize = 0.2F * (float) size / 100F;
|
||||
float maxSize = 0.4F * (float) size / 100F;
|
||||
float minSize = 0.2F * size / 100F;
|
||||
float maxSize = 0.4F * size / 100F;
|
||||
//float minSize = 0.1F;
|
||||
//float maxSize = 0.2F;
|
||||
|
||||
|
@ -93,9 +94,8 @@ public class RenderLaser extends Render {
|
|||
|
||||
@Override
|
||||
public void doRender(Entity entity, double x, double y, double z, float f, float f1) {
|
||||
|
||||
doRender((EntityLaser) entity, x, y, z, f, f1);
|
||||
entity.setAngles(45, 180);
|
||||
//entity.setAngles(45, 180);
|
||||
}
|
||||
|
||||
private void doRender(EntityLaser laser, double x, double y, double z, float f, float f1) {
|
||||
|
|
Loading…
Reference in a new issue