package Connectivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;
import Builder.MainCamera;
import Command.LDrawPart;
import Command.PartTypeT;
import Common.Box3;
import Common.Matrix4;
import Common.Vector3f;
import LDraw.Support.ConnectivityLibrary;
import Window.MOCBuilder;
public class GlobalConnectivityRenderer {
public MainCamera camera;
private boolean isVisible = false;
private GLU glu;
public GlobalConnectivityRenderer(MainCamera cam) {
glu = new GLU();
camera = cam;
}
public void drawConnectivityItem(GL2 gl2, Connectivity conn) {
Vector3f pos = conn.getCurrentPos();
float originX, originY, originZ;
originX = pos.getX();
originY = pos.getY();
originZ = pos.getZ();
float size = 3;
gl2.glBegin(GL2.GL_QUADS); // draw using triangles
float redValue = conn.getName().hashCode() % 255;
float greenValue = conn.gettype() % 255;
gl2.glColor3d(redValue / 255, greenValue / 255, 0);
gl2.glVertex3f(originX - size, originY - size, originZ - size);
gl2.glVertex3f(originX - size, originY + size, originZ - size);
gl2.glVertex3f(originX + size, originY + size, originZ - size);
gl2.glVertex3f(originX + size, originY - size, originZ - size);
gl2.glVertex3f(originX - size, originY - size, originZ - size);
gl2.glVertex3f(originX - size, originY - size, originZ + size);
gl2.glVertex3f(originX - size, originY + size, originZ + size);
gl2.glVertex3f(originX - size, originY + size, originZ - size);
gl2.glVertex3f(originX - size, originY - size, originZ - size);
gl2.glVertex3f(originX + size, originY - size, originZ - size);
gl2.glVertex3f(originX + size, originY - size, originZ + size);
gl2.glVertex3f(originX - size, originY - size, originZ + size);
gl2.glVertex3f(originX - size, originY - size, originZ + size);
gl2.glVertex3f(originX - size, originY + size, originZ + size);
gl2.glVertex3f(originX + size, originY + size, originZ + size);
gl2.glVertex3f(originX + size, originY - size, originZ + size);
gl2.glVertex3f(originX + size, originY - size, originZ - size);
gl2.glVertex3f(originX + size, originY - size, originZ + size);
gl2.glVertex3f(originX + size, originY + size, originZ + size);
gl2.glVertex3f(originX + size, originY + size, originZ - size);
gl2.glVertex3f(originX - size, originY + size, originZ - size);
gl2.glVertex3f(originX + size, originY + size, originZ - size);
gl2.glVertex3f(originX + size, originY + size, originZ + size);
gl2.glVertex3f(originX - size, originY + size, originZ + size);
gl2.glEnd();
}
public void draw(GL2 gl2) {
// drawBoundingBoxForPartOfNoConnectivity(gl2);
if (isVisible == false)
return;
gl2.glDisable(GL2.GL_LIGHTING);
gl2.glUseProgram(0);
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glPushMatrix();
gl2.glLoadMatrixf(camera.getProjection(), 0);
gl2.glMatrixMode(GL2.GL_MODELVIEW);
gl2.glPushMatrix();
gl2.glLoadMatrixf(camera.getModelView(), 0);
drawBoundingBoxForPartOfNoConnectivity(gl2);
drawConnectivity(gl2);
drawCollisionBoxes(gl2);
drawBoundingBoxes(gl2);
gl2.glMatrixMode(GL2.GL_MODELVIEW);
gl2.glPopMatrix();
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glPopMatrix();
gl2.glEnable(GL2.GL_LIGHTING);
}
public void drawBoundingBoxForPartOfNoConnectivity(GL2 gl2) {
GlobalConnectivityManager gcm = GlobalConnectivityManager.getInstance();
ArrayList<LDrawPart> partList = gcm.getPartList();
for (LDrawPart part : partList) {
if (part.getConnectivityList().isEmpty())
if (part.isPartDataExist())
drawBoundingBox(gl2, part.boundingBox3());
}
}
private void drawBoundingBox(GL2 gl2, Box3 box3) {
gl2.glLoadMatrixf(camera.getModelView(), 0);
gl2.glColor3d(0, 0, 0);
gl2.glBegin(GL2.GL_LINE_LOOP); // draw using line loop
gl2.glVertex3f(box3.getMin().getX(), box3.getMin().getY(), box3
.getMin().getZ());
gl2.glVertex3f(box3.getMin().getX(), box3.getMin().getY(), box3
.getMax().getZ());
gl2.glVertex3f(box3.getMin().getX(), box3.getMax().getY(), box3
.getMax().getZ());
gl2.glVertex3f(box3.getMin().getX(), box3.getMax().getY(), box3
.getMin().getZ());
gl2.glEnd();
gl2.glBegin(GL2.GL_LINE_LOOP); // draw using triangles
gl2.glVertex3f(box3.getMax().getX(), box3.getMin().getY(), box3
.getMin().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMin().getY(), box3
.getMax().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMax().getY(), box3
.getMax().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMax().getY(), box3
.getMin().getZ());
gl2.glEnd();
gl2.glBegin(GL2.GL_LINE_LOOP); // draw using triangles
gl2.glVertex3f(box3.getMin().getX(), box3.getMin().getY(), box3
.getMin().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMin().getY(), box3
.getMin().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMin().getY(), box3
.getMax().getZ());
gl2.glVertex3f(box3.getMin().getX(), box3.getMin().getY(), box3
.getMax().getZ());
gl2.glEnd();
gl2.glBegin(GL2.GL_LINE_LOOP); // draw using triangles
gl2.glVertex3f(box3.getMin().getX(), box3.getMax().getY(), box3
.getMin().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMax().getY(), box3
.getMin().getZ());
gl2.glVertex3f(box3.getMax().getX(), box3.getMax().getY(), box3
.getMax().getZ());
gl2.glVertex3f(box3.getMin().getX(), box3.getMax().getY(), box3
.getMax().getZ());
gl2.glEnd();
}
public void drawCollisionBox(GL2 gl2, Vector3f[] pos) {
gl2.glBegin(GL2.GL_QUADS); // draw using triangles
gl2.glColor4d(0, 1, 0, 0.3f);
gl2.glVertex3f(pos[0].x, pos[0].y, pos[0].z);
gl2.glVertex3f(pos[1].x, pos[1].y, pos[1].z);
gl2.glVertex3f(pos[2].x, pos[2].y, pos[2].z);
gl2.glVertex3f(pos[3].x, pos[3].y, pos[3].z);
gl2.glVertex3f(pos[7].x, pos[7].y, pos[7].z);
gl2.glVertex3f(pos[6].x, pos[6].y, pos[6].z);
gl2.glVertex3f(pos[5].x, pos[5].y, pos[5].z);
gl2.glVertex3f(pos[4].x, pos[4].y, pos[4].z);
gl2.glVertex3f(pos[0].x, pos[0].y, pos[0].z);
gl2.glVertex3f(pos[4].x, pos[4].y, pos[4].z);
gl2.glVertex3f(pos[5].x, pos[5].y, pos[5].z);
gl2.glVertex3f(pos[1].x, pos[1].y, pos[1].z);
gl2.glVertex3f(pos[6].x, pos[6].y, pos[6].z);
gl2.glVertex3f(pos[2].x, pos[2].y, pos[2].z);
gl2.glVertex3f(pos[1].x, pos[1].y, pos[1].z);
gl2.glVertex3f(pos[5].x, pos[5].y, pos[5].z);
gl2.glVertex3f(pos[6].x, pos[6].y, pos[6].z);
gl2.glVertex3f(pos[2].x, pos[2].y, pos[2].z);
gl2.glVertex3f(pos[3].x, pos[3].y, pos[3].z);
gl2.glVertex3f(pos[7].x, pos[7].y, pos[7].z);
gl2.glVertex3f(pos[7].x, pos[7].y, pos[7].z);
gl2.glVertex3f(pos[3].x, pos[3].y, pos[3].z);
gl2.glVertex3f(pos[0].x, pos[0].y, pos[0].z);
gl2.glVertex3f(pos[4].x, pos[4].y, pos[4].z);
gl2.glEnd();
}
private void drawBoundingBox(GL2 gl2, Vector3f[] pos) {
gl2.glBegin(GL2.GL_LINES); // draw using triangles
gl2.glColor4d(1, 0, 0, 1.0f);
gl2.glVertex3f(pos[0].x, pos[0].y, pos[0].z);
gl2.glVertex3f(pos[1].x, pos[1].y, pos[1].z);
gl2.glVertex3f(pos[1].x, pos[1].y, pos[1].z);
gl2.glVertex3f(pos[2].x, pos[2].y, pos[2].z);
gl2.glVertex3f(pos[2].x, pos[2].y, pos[2].z);
gl2.glVertex3f(pos[3].x, pos[3].y, pos[3].z);
gl2.glVertex3f(pos[3].x, pos[3].y, pos[3].z);
gl2.glVertex3f(pos[0].x, pos[0].y, pos[0].z);
gl2.glVertex3f(pos[4].x, pos[4].y, pos[4].z);
gl2.glVertex3f(pos[5].x, pos[5].y, pos[5].z);
gl2.glVertex3f(pos[5].x, pos[5].y, pos[5].z);
gl2.glVertex3f(pos[6].x, pos[6].y, pos[6].z);
gl2.glVertex3f(pos[6].x, pos[6].y, pos[6].z);
gl2.glVertex3f(pos[7].x, pos[7].y, pos[7].z);
gl2.glVertex3f(pos[7].x, pos[7].y, pos[7].z);
gl2.glVertex3f(pos[4].x, pos[4].y, pos[4].z);
gl2.glVertex3f(pos[0].x, pos[0].y, pos[0].z);
gl2.glVertex3f(pos[4].x, pos[4].y, pos[4].z);
gl2.glVertex3f(pos[1].x, pos[1].y, pos[1].z);
gl2.glVertex3f(pos[5].x, pos[5].y, pos[5].z);
gl2.glVertex3f(pos[2].x, pos[2].y, pos[2].z);
gl2.glVertex3f(pos[6].x, pos[6].y, pos[6].z);
gl2.glVertex3f(pos[3].x, pos[3].y, pos[3].z);
gl2.glVertex3f(pos[7].x, pos[7].y, pos[7].z);
gl2.glEnd();
}
private void drawCollisionBoxes(GL2 gl2) {
GlobalConnectivityManager gcm = GlobalConnectivityManager.getInstance();
ArrayList<LDrawPart> partList = gcm.getPartList();
gl2.glLoadMatrixf(camera.getModelView(), 0);
for (int i = 0; i < partList.size(); i++) {
LDrawPart part = partList.get(i);
// ArrayList<CollisionBox> boxes = part.getCollisionBoxList(Matrix4
// .getIdentityMatrix4(), new Box3(new Vector3f(-100, -100,
// -100), new Vector3f(100, 100, 100)));
ArrayList<CollisionBox> boxes = part.getCollisionBoxList();
if (boxes == null || boxes.size() == 0)
continue;
for (int j = 0; j < boxes.size(); j++) {
CollisionBox collisionBox = boxes.get(j);
Vector3f[] boxPos = new Vector3f[8];
boxPos[0] = new Vector3f(-collisionBox.getsX(),
-collisionBox.getsY(), -collisionBox.getsZ());
boxPos[1] = new Vector3f(collisionBox.getsX(),
-collisionBox.getsY(), -collisionBox.getsZ());
boxPos[2] = new Vector3f(collisionBox.getsX(),
-collisionBox.getsY(), collisionBox.getsZ());
boxPos[3] = new Vector3f(-collisionBox.getsX(),
-collisionBox.getsY(), collisionBox.getsZ());
boxPos[4] = new Vector3f(-collisionBox.getsX(),
collisionBox.getsY(), -collisionBox.getsZ());
boxPos[5] = new Vector3f(collisionBox.getsX(),
collisionBox.getsY(), -collisionBox.getsZ());
boxPos[6] = new Vector3f(collisionBox.getsX(),
collisionBox.getsY(), collisionBox.getsZ());
boxPos[7] = new Vector3f(-collisionBox.getsX(),
collisionBox.getsY(), collisionBox.getsZ());
for (int k = 0; k < 8; k++) {
boxPos[k] = collisionBox.getTransformMatrix()
.transformPoint(boxPos[k]);
boxPos[k] = part.transformationMatrix().transformPoint(
boxPos[k]);
}
drawCollisionBox(gl2, boxPos);
}
}
}
private void drawBoundingBoxes(GL2 gl2) {
GlobalConnectivityManager gcm = GlobalConnectivityManager.getInstance();
ArrayList<LDrawPart> partList = gcm.getPartList();
gl2.glLoadMatrixf(camera.getModelView(), 0);
for (int i = 0; i < partList.size(); i++) {
LDrawPart part = (LDrawPart) partList.get(i);
Box3 boundingBox = part.boundingBox3();
Vector3f max = boundingBox.getMax();
Vector3f min = boundingBox.getMin();
if (boundingBox != null) {
Vector3f[] boxPos = new Vector3f[8];
boxPos[0] = new Vector3f(min.x, min.y, min.z);
boxPos[1] = new Vector3f(max.x, min.y, min.z);
boxPos[2] = new Vector3f(max.x, min.y, max.z);
boxPos[3] = new Vector3f(min.x, min.y, max.z);
boxPos[4] = new Vector3f(min.x, max.y, min.z);
boxPos[5] = new Vector3f(max.x, max.y, min.z);
boxPos[6] = new Vector3f(max.x, max.y, max.z);
boxPos[7] = new Vector3f(min.x, max.y, max.z);
drawBoundingBox(gl2, boxPos);
}
}
}
private void drawConnectivity(GL2 gl2) {
// draw connectivity
ArrayList<LDrawPart> allPart = MOCBuilder.getInstance()
.getAllPartInActiveModel(false);
for (LDrawPart part : allPart) {
if (part.getConnectivityList() != null) {
for (Connectivity conn : part.getConnectivityList()) {
if (conn instanceof ICustom2DField)
continue;
drawConnectivityItem(gl2, conn);
}
}
}
for (LDrawPart part : allPart) {
if (part.getConnectivityMatrixItemList() != null) {
for (MatrixItem item : part.getConnectivityMatrixItemList()) {
if (item.getConnectedConnectivity() == null) {
if (item.getParent() instanceof Stud)
drawStudMatrix(gl2, item);
else
drawHoleMatrix(gl2, item);
}
}
}
}
}
private void drawStudMatrix(GL2 gl2, MatrixItem matrixItem) {
gl2.glColor3d(1, matrixItem.getAltitude() / 29.0,
matrixItem.getAltitude() / 29.0);
Vector3f pos = matrixItem.getCurrentPos();
// pos = LDrawGridTypeT.getSnappedPos(pos, LDrawGridTypeT.Medium);
GLUquadric earth = glu.gluNewQuadric();
glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);
final float radius = 2;
final int slices = 4;
final int stacks = 4;
gl2.glLoadMatrixf(camera.getModelView(), 0);
gl2.glTranslatef(pos.getX(), pos.getY(), pos.getZ());
glu.gluSphere(earth, radius, slices, stacks);
glu.gluDeleteQuadric(earth);
}
private void drawHoleMatrix(GL2 gl2, MatrixItem matrixItem) {
gl2.glColor3d(matrixItem.getAltitude() / 29.0, 1,
matrixItem.getAltitude() / 29.0);
Vector3f pos = matrixItem.getCurrentPos();
// pos = LDrawGridTypeT.getSnappedPos(pos, LDrawGridTypeT.Medium);
// Draw sphere (possible styles: FILL, LINE, POINT).
GLUquadric earth = glu.gluNewQuadric();
glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);
final float radius = 2;
final int slices = 4;
final int stacks = 4;
gl2.glLoadMatrixf(camera.getModelView(), 0);
gl2.glTranslatef(pos.getX(), pos.getY(), pos.getZ());
glu.gluSphere(earth, radius, slices, stacks);
glu.gluDeleteQuadric(earth);
}
public void isVisible(boolean flag) {
this.isVisible = flag;
}
}