- Point centroid = t.centroid();
- gl.glBegin(GL.GL_LINES);
- gl.glColor3f(1, 1, 1);
- /*
- centroid.glVertex(gl);
- centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
- */
- /*
- if (mesh==goal)
- for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
- gl.glDisable(GL.GL_LIGHTING);
- gl.glBegin(GL.GL_LINES);
- gl.glColor3f(1, 1, 1);
- p.p.glVertex(gl);
- //p.p.plus(p.norm().times(p.score())).glVertex(gl);
- if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
- //tile.nearest(p).centroid().glVertex(gl);
- gl.glEnd();
- gl.glEnable(GL.GL_LIGHTING);
+ projection = Matrix.getProjectionMatrix(gl);
+
+ if (updateVisibilities) {
+ updateVisibilities = false;
+ // update vertex visibilities
+ updateVisibility(gl);
+ }
+
+ if (updateClosest) {
+ updateClosest = false;
+ double dist = Double.MAX_VALUE;
+ double distE = Double.MAX_VALUE;
+ closest = null;
+ closestOriginallyAt = null;
+ closestEdge = null;
+ for(Mesh mesh : meshes)
+ if (mesh.option_selectable)
+ for(Mesh.Vertex v : mesh.vertices()) {
+ if (!v.visible) continue;
+ Point p = projection.times(v.getPoint());
+ int x = (int)p.x;
+ int y = (int)p.y;
+ int mousex = (int)getMouse().x;
+ int mousey = (int)getMouse().y;
+ if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
+ dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
+ closest = v;
+ }
+ for(Mesh.E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
+ if (!e.v2.visible) continue;
+ Segment s =
+ new Segment(projection.times(e.v1.getPoint()),
+ projection.times(e.v2.getPoint()));
+ double dist2 = s.distance(getMouse());
+ if (dist2 < distE) {
+ distE = dist2;
+ closestEdge = e;
+ }
+ }
+ }
+ }
+ }
+
+ protected synchronized void updateVisibility(GL gl) {
+ IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
+ gl.glFlush();
+ gl.glDrawBuffer(GL.GL_BACK);
+ gl.glReadBuffer(GL.GL_BACK);
+ gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
+ gl.glFlush();
+ gl.glDisable(GL.GL_LIGHTING);
+ gl.glShadeModel(GL.GL_FLAT);
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glColor3f(0,0,0);
+ for(Mesh mesh : meshes) mesh.render(gl, Matrix.ONE, true);
+ for(Mesh mesh : meshes)
+ if (mesh.option_selectable)
+ for(Mesh.Vertex v : mesh.vertices()) {
+ Point p = v.getPoint();
+ Point projected = projection.times(p);
+ boolean vis = false;
+
+ gl.glColor3f(1,1,1);
+ gl.glBegin(gl.GL_POINTS);
+ p.glVertex(gl);
+ gl.glEnd();
+ gl.glFlush();
+
+ vis = false;
+ gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
+ for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
+
+ v.visible = vis;
+ if (vis) {
+ gl.glColor3f(0,0,0);
+ gl.glBegin(gl.GL_POINTS);
+ p.glVertex(gl);
+ gl.glEnd();
+ }