Detect face direction of clicked edge.

origin
Joshua Sigona 3 years ago
parent e5e42d4f88
commit a1147ce151
  1. 6
      src/sig/Cube.java
  2. 6
      src/sig/MouseHandler.java
  3. 2
      src/sig/Panel.java
  4. 23
      src/sig/SigRenderer.java
  5. 8
      src/sig/Triangle.java
  6. 4
      src/sig/utils/DrawUtils.java

@ -9,16 +9,22 @@ public class Cube extends Mesh{
this.triangles=OBJReader.ReadOBJFile("cube.obj",true); this.triangles=OBJReader.ReadOBJFile("cube.obj",true);
triangles.get(0).tex=type.getTexture(BlockType.FRONT); triangles.get(0).tex=type.getTexture(BlockType.FRONT);
triangles.get(1).tex=type.getTexture(BlockType.FRONT); triangles.get(1).tex=type.getTexture(BlockType.FRONT);
triangles.get(0).dir=triangles.get(1).dir=BlockType.FRONT;
triangles.get(2).tex=type.getTexture(BlockType.RIGHT); triangles.get(2).tex=type.getTexture(BlockType.RIGHT);
triangles.get(3).tex=type.getTexture(BlockType.RIGHT); triangles.get(3).tex=type.getTexture(BlockType.RIGHT);
triangles.get(2).dir=triangles.get(3).dir=BlockType.RIGHT;
triangles.get(4).tex=type.getTexture(BlockType.BACK); triangles.get(4).tex=type.getTexture(BlockType.BACK);
triangles.get(5).tex=type.getTexture(BlockType.BACK); triangles.get(5).tex=type.getTexture(BlockType.BACK);
triangles.get(4).dir=triangles.get(5).dir=BlockType.BACK;
triangles.get(6).tex=type.getTexture(BlockType.LEFT); triangles.get(6).tex=type.getTexture(BlockType.LEFT);
triangles.get(7).tex=type.getTexture(BlockType.LEFT); triangles.get(7).tex=type.getTexture(BlockType.LEFT);
triangles.get(6).dir=triangles.get(7).dir=BlockType.LEFT;
triangles.get(8).tex=type.getTexture(BlockType.TOP); triangles.get(8).tex=type.getTexture(BlockType.TOP);
triangles.get(9).tex=type.getTexture(BlockType.TOP); triangles.get(9).tex=type.getTexture(BlockType.TOP);
triangles.get(8).dir=triangles.get(9).dir=BlockType.TOP;
triangles.get(10).tex=type.getTexture(BlockType.BOTTOM); triangles.get(10).tex=type.getTexture(BlockType.BOTTOM);
triangles.get(11).tex=type.getTexture(BlockType.BOTTOM); triangles.get(11).tex=type.getTexture(BlockType.BOTTOM);
triangles.get(10).dir=triangles.get(11).dir=BlockType.BOTTOM;
} }
} }

@ -4,9 +4,9 @@ import java.awt.event.MouseEvent;
public class MouseHandler { public class MouseHandler {
public MouseEvent e; public MouseEvent e;
public Block b; public Triangle t;
public MouseHandler(MouseEvent e,Block b) { public MouseHandler(MouseEvent e,Triangle t) {
this.e=e; this.e=e;
this.b=b; this.t=t;
} }
} }

@ -396,6 +396,7 @@ public class Panel extends JPanel implements Runnable {
triTransformed.tex = t.tex; triTransformed.tex = t.tex;
triTransformed.b=t.b; triTransformed.b=t.b;
triTransformed.unmodifiedTri=t; triTransformed.unmodifiedTri=t;
triTransformed.dir=t.dir;
Vector normal=new Vector(),line1=new Vector(),line2=new Vector(); Vector normal=new Vector(),line1=new Vector(),line2=new Vector();
line1 = Vector.subtract(triTransformed.B,triTransformed.A); line1 = Vector.subtract(triTransformed.B,triTransformed.A);
@ -458,6 +459,7 @@ public class Panel extends JPanel implements Runnable {
triProjected.V = (Vector2)clipped[i].V.clone(); triProjected.V = (Vector2)clipped[i].V.clone();
triProjected.b=clipped[i].b; triProjected.b=clipped[i].b;
triProjected.unmodifiedTri=clipped[i].unmodifiedTri; triProjected.unmodifiedTri=clipped[i].unmodifiedTri;
triProjected.dir=clipped[i].dir;
triProjected.T.u = triProjected.T.u/triProjected.A.w; triProjected.T.u = triProjected.T.u/triProjected.A.w;
triProjected.U.u = triProjected.U.u/triProjected.B.w; triProjected.U.u = triProjected.U.u/triProjected.B.w;

@ -94,10 +94,29 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
} }
if (answer!=null) { if (answer!=null) {
if (answer.e.getButton()==MouseEvent.BUTTON1) { if (answer.e.getButton()==MouseEvent.BUTTON1) {
addBlock(Vector.add(answer.b.pos,new Vector(0,1,0)),BlockType.PLANKS); switch (answer.t.dir) {
case BlockType.FRONT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),BlockType.PLANKS);
}break;
case BlockType.BACK:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),BlockType.PLANKS);
}break;
case BlockType.LEFT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),BlockType.PLANKS);
}break;
case BlockType.RIGHT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),BlockType.PLANKS);
}break;
case BlockType.TOP:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),BlockType.PLANKS);
}break;
case BlockType.BOTTOM:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),BlockType.PLANKS);
}break;
}
} else } else
if (answer.e.getButton()==MouseEvent.BUTTON3) { if (answer.e.getButton()==MouseEvent.BUTTON3) {
removeBlock(answer.b.pos); removeBlock(answer.t.b.pos);
} }
answer=null; answer=null;
} }

@ -10,6 +10,7 @@ public class Triangle {
public long nextRenderTime = -1l; public long nextRenderTime = -1l;
public long nextRenderTime2 = -1l; public long nextRenderTime2 = -1l;
public Triangle unmodifiedTri; public Triangle unmodifiedTri;
public int dir = -1;
public Triangle() { public Triangle() {
this(new Vector(),new Vector(),new Vector()); this(new Vector(),new Vector(),new Vector());
} }
@ -32,6 +33,7 @@ public class Triangle {
targetTriangle.tex=this.tex; targetTriangle.tex=this.tex;
targetTriangle.b=this.b; targetTriangle.b=this.b;
targetTriangle.unmodifiedTri=this.unmodifiedTri; targetTriangle.unmodifiedTri=this.unmodifiedTri;
targetTriangle.dir=this.dir;
} }
@Override @Override
protected Object clone(){ protected Object clone(){
@ -40,12 +42,12 @@ public class Triangle {
t.tex=tex; t.tex=tex;
t.b=b; t.b=b;
t.unmodifiedTri=unmodifiedTri; t.unmodifiedTri=unmodifiedTri;
t.dir=dir;
return t; return t;
} }
@Override @Override
public String toString() { public String toString() {
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", T=" + T + ", U=" + U + ", V=" + V + ", col=" + col return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", T=" + T + ", U=" + U + ", V=" + V + ", col=" + col + ", dir=" + dir + "]";
+ "]";
} }
public int getColor() { public int getColor() {
return col; return col;
@ -106,6 +108,7 @@ public class Triangle {
out_tri[0].tex = in.tex; out_tri[0].tex = in.tex;
out_tri[0].b=in.b; out_tri[0].b=in.b;
out_tri[0].unmodifiedTri=in.unmodifiedTri; out_tri[0].unmodifiedTri=in.unmodifiedTri;
out_tri[0].dir=in.dir;
out_tri[0].A = inside_points[0]; out_tri[0].A = inside_points[0];
out_tri[0].T = inside_tex[0]; out_tri[0].T = inside_tex[0];
out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0],t); out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0],t);
@ -123,6 +126,7 @@ public class Triangle {
out_tri[0].col=out_tri[1].col=in.col; out_tri[0].col=out_tri[1].col=in.col;
out_tri[0].tex=out_tri[1].tex=in.tex; out_tri[0].tex=out_tri[1].tex=in.tex;
out_tri[0].b=out_tri[1].b=in.b; out_tri[0].b=out_tri[1].b=in.b;
out_tri[0].dir=out_tri[1].dir=in.dir;
out_tri[0].unmodifiedTri=out_tri[1].unmodifiedTri=in.unmodifiedTri; out_tri[0].unmodifiedTri=out_tri[1].unmodifiedTri=in.unmodifiedTri;
out_tri[0].A = inside_points[0]; out_tri[0].A = inside_points[0];
out_tri[0].B = inside_points[1]; out_tri[0].B = inside_points[1];

@ -96,7 +96,7 @@ public class DrawUtils {
if (tex_w>SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]) { if (tex_w>SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]) {
SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]=tex_w; SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]=tex_w;
if (j==SigRenderer.temp_request.getX()&&i==SigRenderer.temp_request.getY()) { if (j==SigRenderer.temp_request.getX()&&i==SigRenderer.temp_request.getY()) {
SigRenderer.tempAnswer=new MouseHandler(SigRenderer.temp_request,ref.b); SigRenderer.tempAnswer=new MouseHandler(SigRenderer.temp_request,ref);
} }
} }
} }
@ -172,7 +172,7 @@ public class DrawUtils {
if (tex_w>SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]) { if (tex_w>SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]) {
SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]=tex_w; SigRenderer.depthBuffer_noTransparency[i*SigRenderer.SCREEN_WIDTH+j]=tex_w;
if (j==SigRenderer.temp_request.getX()&&i==SigRenderer.temp_request.getY()) { if (j==SigRenderer.temp_request.getX()&&i==SigRenderer.temp_request.getY()) {
SigRenderer.tempAnswer=new MouseHandler(SigRenderer.temp_request,ref.b); SigRenderer.tempAnswer=new MouseHandler(SigRenderer.temp_request,ref);
} }
} }
} }

Loading…
Cancel
Save