00001 package org.hfbk.vis.visnode;
00002
00003 import java.util.ArrayList;
00004 import java.util.List;
00005
00006 import org.dronus.gl.Buffers;
00007 import org.dronus.gl.Convexiser;
00008 import org.dronus.gl.GLFont;
00009 import org.dronus.gl.GLUtil;
00010 import org.dronus.graph.Node;
00011 import org.hfbk.vis.Prefs;
00012 import org.lwjgl.opengl.GL11;
00013 import org.lwjgl.util.vector.Vector3f;
00020 public class VisUgl extends VisNodeDraggable {
00021
00022 int dl;
00023 float w,h,l,r,xd,yd,zd;
00024 int rec;
00025
00026 Vector3f origin=new Vector3f();
00027 Vector3f size=new Vector3f();
00028
00029
00030
00031 Vector3f epicenter;
00032
00033
00034 List<Vector3f> verticies=new ArrayList<Vector3f>();
00035 List<Integer> faces=new ArrayList<Integer>();
00036 List<Integer> lines=new ArrayList<Integer>();
00037 List<List<Vector3f>> triangles=new ArrayList<List<Vector3f>>();
00038 List<String> groups=new ArrayList<String>();
00039
00040
00041
00042
00043 public VisUgl(Node n, Vector3f position) {
00044
00045 super(n, new Vector3f());
00046
00047
00048 }
00049
00050 Vector3f getCenter(List<Vector3f> poly){
00051 Vector3f c=new Vector3f();
00052 for (Vector3f v: poly){
00053 Vector3f.add(c, v, c);
00054 }
00055 c.scale(1f/poly.size());
00056
00057
00058 return c;
00059 }
00060
00061 void drawFaces(){
00062 int n;
00063 for (int i=0; i<faces.size(); i+=n+1){
00064
00065 float group=faces.get(i++)*13;
00066
00067 n=0;
00068 while (faces.get(i+n)!=-1) n++;
00069
00070 if (n<3) continue;
00071
00072 List<Vector3f> polygon=new ArrayList<Vector3f>();
00073
00074 for (int k=0; k<n; k++) {
00075 Vector3f v=verticies.get(faces.get(i+k)-1);
00076 polygon.add(v);
00077 }
00078
00079 List<List<Vector3f>> convexPolys;
00080 if (n<4){
00081 convexPolys=new ArrayList<List<Vector3f>>();
00082 convexPolys.add(polygon);
00083 }else{
00084 Convexiser bfec=new Convexiser();
00085 convexPolys=bfec.convexise(polygon);
00086 }
00087
00088 for (List<Vector3f> t: convexPolys){
00089 groupColor(group);
00090 GL11.glBegin(GL11.GL_POLYGON);
00091 for (Vector3f v: t)
00092 GL11.glVertex3f(v.x,v.y,v.z);
00093 GL11.glEnd();
00094
00095 triangles.add(t);
00096 }
00097 }
00098 }
00099
00100 void groupColor(float g){
00101 if (g>0) {
00102 g=g*41;
00103 GL11.glColor3f((g/3)%1f, (g/5)%1f, (g/7)%1f);
00104 }
00105 }
00106
00107
00108 void makeBox(float x,float y,float w,float h,int level)
00109 {
00110
00111 x+=.1f;y+=.1f;w-=.2f;h-=.2f;
00112
00113
00114
00115
00116
00117
00118 float depth;
00119 if (level!=7)
00120 {
00121 depth=level*-.1f;
00122
00123 GL11.glColor4f(level*.1f,level*.1f,level*.1f,.8f);
00124
00125 GL11.glBegin(GL11.GL_QUADS);
00126
00127
00129 GL11.glVertex3f(x,y,0.01f);
00130 GL11.glVertex3f(x+w,y,0.01f);
00131 GL11.glVertex3f(x+w,y+h,0.01f);
00132 GL11.glVertex3f(x,y+h,0.01f);
00134 GL11.glVertex3f(x,y,depth);
00135 GL11.glVertex3f(x+w,y,depth);
00136 GL11.glVertex3f(x+w,y+h,depth);
00137 GL11.glVertex3f(x,y+h,depth);
00139 GL11.glVertex3f(x,y,0.01f);
00140 GL11.glVertex3f(x,y+h,0.01f);
00141 GL11.glVertex3f(x,y+h,depth);
00142 GL11.glVertex3f(x,y,depth);
00144 GL11.glVertex3f(x+w,y,0.01f);
00145 GL11.glVertex3f(x+w,y+h,0.01f);
00146 GL11.glVertex3f(x+w,y+h,depth);
00147 GL11.glVertex3f(x+w,y,depth);
00149 GL11.glVertex3f(x,y,0.01f);
00150 GL11.glVertex3f(x+w,y,0.01f);
00151 GL11.glVertex3f(x+w,y,depth);
00152 GL11.glVertex3f(x,y,depth);
00154 GL11.glVertex3f(x,y+h,0.01f);
00155 GL11.glVertex3f(x+w,y+h,0.01f);
00156 GL11.glVertex3f(x+w,y+h,depth);
00157 GL11.glVertex3f(x,y+h,depth);
00158
00159
00160 GL11.glEnd();
00161
00162 GL11.glColor4f(1,1,1,1);
00163
00164 GL11.glBegin(GL11.GL_LINE_STRIP);
00165
00166
00168 GL11.glVertex3f(x,y,0.01f);
00169 GL11.glVertex3f(x+w,y,0.01f);
00170 GL11.glVertex3f(x+w,y+h,0.01f);
00171 GL11.glVertex3f(x,y+h,0.01f);
00172 GL11.glVertex3f(x,y,0.01f);
00173 GL11.glEnd();
00174 GL11.glBegin(GL11.GL_LINE_STRIP);
00175
00177 GL11.glVertex3f(x,y,depth);
00178 GL11.glVertex3f(x+w,y,depth);
00179 GL11.glVertex3f(x+w,y+h,depth);
00180 GL11.glVertex3f(x,y+h,depth);
00181 GL11.glVertex3f(x,y,depth);
00182 GL11.glEnd();
00183 GL11.glBegin(GL11.GL_LINE_STRIP);
00185 GL11.glVertex3f(x,y,0.01f);
00186 GL11.glVertex3f(x,y+h,0.01f);
00187 GL11.glVertex3f(x,y+h,depth);
00188 GL11.glVertex3f(x,y,depth);
00189 GL11.glVertex3f(x,y,0.01f);
00190 GL11.glEnd();
00191 GL11.glBegin(GL11.GL_LINE_STRIP);
00193 GL11.glVertex3f(x+w,y,0.01f);
00194 GL11.glVertex3f(x+w,y+h,0.01f);
00195 GL11.glVertex3f(x+w,y+h,depth);
00196 GL11.glVertex3f(x+w,y,depth);
00197 GL11.glVertex3f(x+w,y,0.01f);
00198 GL11.glEnd();
00199 GL11.glBegin(GL11.GL_LINE_STRIP);
00201 GL11.glVertex3f(x,y,0.01f);
00202 GL11.glVertex3f(x+w,y,0.01f);
00203 GL11.glVertex3f(x+w,y,depth);
00204 GL11.glVertex3f(x,y,depth);
00205 GL11.glVertex3f(x,y,0.01f);
00206 GL11.glEnd();
00207 GL11.glBegin(GL11.GL_LINE_STRIP);
00209 GL11.glVertex3f(x,y+h,0.01f);
00210 GL11.glVertex3f(x+w,y+h,0.01f);
00211 GL11.glVertex3f(x+w,y+h,depth);
00212 GL11.glVertex3f(x,y+h,depth);
00213 GL11.glVertex3f(x,y+h,0.01f);
00214
00215
00216 GL11.glEnd();
00217 }
00218 }
00219
00220
00221 void drawAuto(float x, float y,float w,float h, int level)
00222 {
00223
00224
00225
00226 if(level > 1) {
00227 level = level - 1;
00228
00229
00230 double ra=Math.random();
00231
00232
00233
00234 if (ra<.33f)
00235 {
00236
00237 makeBox(x,y,w,h,level);
00238
00239 }
00240 else
00241
00242 if (ra>.66f)
00243 {
00244
00245 drawAuto(x, y,w/3,h, level);
00246 drawAuto(x+w/3,y,w*2/3,h,level);
00247
00248 }
00249 else
00250 {
00251
00252
00253
00254 drawAuto(x, y,w,h/3, level);
00255 drawAuto(x,y+h/3,w,h*2/3,level);
00256
00257
00258 }
00259
00260
00261 }
00262
00263 }
00264
00265 void drawLines(){
00266 for (int i=0; i<lines.size(); i++){
00267 int group=lines.get(i++);
00268 if (group>0)
00269 GL11.glColor3f((group%1.0f), (group*2)%1.0f, (group*7)%1.0f);
00270
00271 GL11.glBegin(GL11.GL_LINE_STRIP);
00272 while (lines.get(i)!=-1){
00273 Vector3f v=verticies.get(lines.get(i)-1);
00274 GL11.glVertex3f(v.x,v.y,v.z);
00275 i++;
00276 }
00277 GL11.glEnd();
00278 }
00279 }
00280
00284 void renderSelf() {
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 if (dl==0){
00297 dl=GL11.glGenLists(1);
00298 GL11.glNewList(dl, GL11.GL_COMPILE);
00299
00300 GL11.glDisable(GL11.GL_TEXTURE_2D);
00301
00302 GL11.glPolygonOffset(1,1);
00303
00304
00305
00306
00307
00308
00309 GL11.glMaterial(GL11.GL_FRONT, GL11.GL_DIFFUSE, Buffers.f4(.9f,.5f,.0f,.8f));
00310
00311
00312
00313
00314
00315 GL11.glPushMatrix();
00316 GL11.glTranslatef(0,-15,0);
00317 GL11.glRotatef(90,1,0,0);
00318 GL11.glScalef(50,50,50);
00319
00320
00321
00322
00323 radius=1000;
00324 rec=0;
00325
00326 w=l=h=1;
00327
00328 zd=0;
00329
00330
00331
00332 for (xd=0;xd<8;xd=xd+1)
00333 {
00334 for (yd=0;yd>-8;yd=yd-1)
00335 {
00336
00337
00338 drawAuto(xd,yd,w,h,8);
00339 }
00340 }
00341 GL11.glDisable(GL11.GL_LIGHTING);
00342
00343
00344
00345
00346 GL11.glPolygonOffset(0,0);
00347
00348 GL11.glColor3f(1,1,1);
00349
00350
00351
00352
00353
00354 GL11.glPopMatrix();
00355
00356 GL11.glEndList();
00357
00358 if (epicenter!=null)
00359 getRoot().epicenter=epicenter;
00360 }
00361
00362
00363 GL11.glCallList(dl);
00364
00365 if (isHoovered) {
00366 GL11.glColor3f(1,0,0);
00367
00368 }
00369
00370 if (Prefs.current.debug){
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 }
00384 }
00385
00386 void drawIndicies(){
00387 GLFont.getDefault().render();
00388
00389 for (int i=0; i<faces.size(); i++){
00390 i++;
00391 while (faces.get(i)!=-1){
00392 Vector3f v=verticies.get(faces.get(i)-1);
00393 GL11.glPushMatrix();
00394 GL11.glTranslatef(v.x, v.y, v.z);
00395 GLUtil.billboardCylinder();
00396 GL11.glColor3f(1,1,1);
00397 GLFont.getDefault().print(""+(faces.get(i)-1));
00398
00399 GL11.glPopMatrix();
00400 i++;
00401 }
00402 }
00403 GL11.glDisable(GL11.GL_TEXTURE_2D);
00404 }
00405
00406
00410 boolean hitTriangle(Ray ray, List<Vector3f> polygon) {
00411
00412 final double EPSILON=0.000001;
00413
00414 Vector3f vert0=polygon.get(0);
00415 Vector3f vert1=polygon.get(1);
00416 Vector3f vert2=polygon.get(2);
00417
00418 Vector3f edge1=new Vector3f(), edge2=new Vector3f(), tvec=new Vector3f(), pvec=new Vector3f(), qvec=new Vector3f();
00419
00420 Vector3f.sub(vert1, vert0, edge1);
00421 Vector3f.sub(vert2, vert0, edge2);
00422
00423 Vector3f.cross(ray.dir, edge2, pvec);
00424
00425
00426 double det =Vector3f.dot(edge1,pvec);
00427
00428 if (det > -EPSILON && det < EPSILON)
00429 return false;
00430
00431 double inv_det = 1.0 / det;
00432
00433
00434 Vector3f.sub(ray.start, vert0, tvec);
00435
00436
00437 double u = Vector3f.dot(tvec, pvec) * inv_det;
00438 if (u < 0.0 || u > 1.0)
00439 return false;
00440
00441
00442
00443 Vector3f.cross(tvec,edge1,qvec);
00444 double v = Vector3f.dot(ray.dir, qvec) * inv_det;
00445 if (v < 0.0 || u + v > 1.0)
00446 return false;
00447
00448
00449
00450
00451 return true;
00452 }
00453
00460 boolean hit(Ray mouseray, Vector3f hit) {
00461 float distance;
00462
00463 if(handle==null) distance=GLUtil.getPosition().length();
00464 else distance=hitDepth;
00465 Vector3f projector=(Vector3f)new Vector3f(mouseray.dir).scale(distance);
00466 Vector3f.add(mouseray.start, projector, hit);
00467
00468 if (hit.length()>radius) return false;
00469
00470 for (List<Vector3f> t:triangles)
00471 if (hitTriangle(mouseray, t))
00472 return true;
00473
00474 return false;
00475 }
00476 }