VisClient/org/dronus/gl/GLUtil.java

Go to the documentation of this file.
00001 /*
00002  * Created on 19.03.2005
00003  *
00004  */
00005 package org.dronus.gl;
00006 import java.nio.FloatBuffer;
00007 import java.nio.IntBuffer;
00008 
00009 import org.hfbk.vis.Prefs;
00010 import org.lwjgl.BufferUtils;
00011 import org.lwjgl.opengl.*;
00012 import org.lwjgl.util.Color;
00013 import org.lwjgl.util.glu.GLU;
00014 import org.lwjgl.util.vector.Matrix4f;
00015 import org.lwjgl.util.vector.Vector3f;
00016 import org.lwjgl.util.vector.Vector4f;
00017 
00030 public class GLUtil{
00031         //TODO get aspect at runtime
00032         public static float aspect=4/3f, fov=.5f;
00033         
00043         static float detail=1000;
00044         
00045         //show objects extends for debugging purposes
00046         public static boolean showExtents=false; 
00047         
00048         public static Color backgroundcolor=new Color(25,25,25);
00049         public static Color extentcolor    =new Color(255,80,80,25);
00050         
00055         public static void debugLighting(){
00056                 GL11.glDisable(GL11.GL_TEXTURE_2D);
00057                 //GL11.glDisable(GL11.GL_LIGHTING);
00058                 GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, Buffers.f4(1,0,0,1));
00059                 GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, Buffers.f4(1,0,0,1));
00060                 GL11.glMaterial(GL11.GL_FRONT, GL11.GL_EMISSION, Buffers.f4(0,0,.7f,1));
00061         }
00062         
00076         public static void renderToScreen(float bw, float bh){          
00077                 GL11.glMatrixMode(GL11.GL_PROJECTION);
00078                 GL11.glLoadIdentity();
00079                 float tw=aspect*2, th=2f;
00080                 bw*=tw; bh*=th;
00081                 GL11.glOrtho(-bw,tw+bw,-bh,th+bh,0,1);
00082         GL11.glDisable(GL11.GL_LIGHTING);
00083         GL11.glDisable(GL11.GL_DEPTH_TEST);
00084         GL11.glDisable(GL11.GL_CULL_FACE);
00085         GL11.glEnable(GL11.GL_TEXTURE_2D);              
00086         GL11.glEnable(GL11.GL_BLEND);
00087         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
00088         GL11.glMatrixMode(GL11.GL_MODELVIEW);
00089                 GL11.glLoadIdentity();          
00090         GL11.glColor3f(1, 1, 1);
00091         GL11.glTranslatef(0f,th,-1f);              
00092         }
00093         
00099         public static void renderQuad(float x1, float y1, float x2, float y2, float tw, float th) {             
00100             GL11.glBegin(GL11.GL_QUADS);
00101                 GL11.glNormal3f(0,0,1);
00102            
00103                 GL11.glTexCoord2f(0, 0);
00104                 GL11.glVertex3f(x1, y2, 0.0f);
00105                 GL11.glTexCoord2f(0, th);
00106                 GL11.glVertex3f(x1, y1, 0.0f);
00107                 GL11.glTexCoord2f(tw, th);
00108                 GL11.glVertex3f(x2, y1, 0.0f);          
00109                 GL11.glTexCoord2f(tw, 0);
00110                 GL11.glVertex3f(x2, y2, 0.0f);
00111                 
00112         GL11.glEnd();
00113         }
00114         
00119         public static void renderQuad(float x1, float y1, float x2, float y2) {         
00120             GL11.glBegin(GL11.GL_QUADS);
00121                 GL11.glNormal3f(0,0,1);
00122            
00123                 GL11.glTexCoord2f(0, 0);
00124                 GL11.glVertex3f(x1, y2, 0.0f);
00125                 GL11.glTexCoord2f(0, 1);
00126                 GL11.glVertex3f(x1, y1, 0.0f);
00127                 GL11.glTexCoord2f(1, 1);
00128                 GL11.glVertex3f(x2, y1, 0.0f);          
00129                 GL11.glTexCoord2f(1, 0);
00130                 GL11.glVertex3f(x2, y2, 0.0f);
00131                 
00132         GL11.glEnd();
00133         }
00134         
00139         public static void renderFrame(float x1, float y1, float x2, float y2) {                
00140             GL11.glBegin(GL11.GL_LINE_LOOP);
00141                 GL11.glNormal3f(0,0,1);
00142                 GL11.glVertex3f(x1, y2, 0.0f);
00143                 GL11.glVertex3f(x2, y2, 0.0f);
00144                 GL11.glVertex3f(x2, y1, 0.0f);
00145                 GL11.glVertex3f(x1, y1, 0.0f);
00146                 //GL11.glVertex3f(0, h, 0.0f);
00147         GL11.glEnd();
00148         }
00149         
00150         
00151         public static void init(){              
00152                 //vis client angepasst..
00153            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
00154                 GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
00155                 
00156                 //depth buffer
00157                 GL11.glEnable(GL11.GL_DEPTH_TEST);
00158                 GL11.glDepthFunc(GL11.GL_LEQUAL);
00159                 GL11.glEnable(GL11.GL_POLYGON_OFFSET_FILL); //prepare for coplanar rendering
00160         
00161                 // we want things and textures to be translucent at some areas
00162                 GL11.glEnable(GL11.GL_BLEND);
00163                 GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);       
00164                 GL11.glEnable(GL11.GL_ALPHA_TEST);
00165                 GL11.glAlphaFunc(GL11.GL_GREATER, .2f);
00166                 
00167                 //we want lines nifty 
00168                 GL11.glEnable(GL11.GL_LINE_SMOOTH);
00169                 GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);
00170                 GL11.glLineWidth(2);
00171                 GL11.glEnable(GL11.GL_POLYGON_SMOOTH);
00172                 GL11.glHint(GL11.GL_POLYGON_SMOOTH_HINT, GL11.GL_NICEST);
00173                 
00174                 
00175                 //if we once use light..
00176                 GL11.glLightModeli(GL11.GL_LIGHT_MODEL_LOCAL_VIEWER,GL11.GL_TRUE); //BEI SPECULAR EINSCHLATEN
00177                 GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, Buffers.f4(0,0,0,1));            
00178 
00179                 GL11.glEnable(GL11.GL_LIGHT0);
00180                 GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, Buffers.f4(.1f,.01f,0,1));                
00181                 GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, Buffers.f4(1f,1f,1f,1));
00182                 GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, Buffers.f4(1f,1f,1f,1));         
00183                 GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, Buffers.f4(100,300,300,0));
00184         }
00185         
00189         public static Matrix4f getTransform(Matrix4f xform) {
00190                 FloatBuffer mb=Buffers.matrixBuffer();
00191                 mb.rewind();
00192                 GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mb);
00193                 xform.load(mb);
00194                 return xform;
00195         }
00196                 
00203         public static Vector4f getPosition() {
00204                 FloatBuffer mb=Buffers.matrixBuffer();
00205                 mb.rewind();
00206                 GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mb);
00207                 Vector4f x=new Vector4f(0,0,0,1);
00208                 mb.position(12);
00209                 x.load(mb);
00210                 return x;
00211         }
00212         
00213         public static void initFrustum(float fieldOfView, double near, double far) {
00214                 //DEBUG GLText.print("rendered #:"+rendered+"/"+(culled+rendered));
00215                 rendered=culled=0;
00216                 
00217                 GL11.glMatrixMode(GL11.GL_PROJECTION);
00218                 GL11.glLoadIdentity();
00219                 fov=fieldOfView;
00220                 GL11.glFrustum(-aspect*near*fov, aspect*near*fov, -near*fov, near*fov, near, far*far);
00221                 
00222                 GL11.glMatrixMode(GL11.GL_MODELVIEW);
00223         }
00224 
00225         public static int genTexture(){
00226                 IntBuffer idbuf = BufferUtils.createIntBuffer(1); 
00227                 GL11.glGenTextures(idbuf);
00228                 return idbuf.get(0);
00229         }
00230         
00231         //some stats
00232         static int rendered=0, culled=0;
00239         public static boolean inFrustum(float radius) {
00240                 Vector4f pos=GLUtil.getPosition();
00241                 boolean in=(Math.abs(pos.z)<radius*detail/fov &&
00242                                 pos.x-radius<=-(pos.z-radius)*fov*aspect  && 
00243                                 pos.x+radius>=(pos.z-radius)*fov*aspect && 
00244                                 pos.y-radius<= -(pos.z-radius)*fov && 
00245                                 pos.y+radius>=(pos.z-radius)*fov);
00246                 if (!in) culled++;
00247                 else rendered++;
00248                 return in;
00249         }
00250 
00251         
00252         static Matrix4f invTrans=new Matrix4f();
00257         public static void billboardCylinder() {
00258                 //rotate label towards viewer
00259                 GLUtil.getTransform(invTrans);
00260                 invTrans.invert();
00261                 Vector4f pos=new Vector4f(0,0,0,1);             
00262                 pos=Matrix4f.transform(invTrans,pos,null);
00263                 float atan=(float)Math.atan(pos.x/pos.z);
00264                 if (pos.z<0) atan+=Math.PI;
00265                 GL11.glRotatef((float)(180*atan/Math.PI),0,1,0);
00266                 
00267                 //a cheaper billboarding, slightly different: 
00268                 // parallels to the camera plane. better readability
00269                 // but much more static look if viewed from above.
00270                 /*Matrix4f t=GLUtil.getTransform();
00271                 t.m00=1; t.m01=0; t.m02=0;
00272                 t.m20=0; t.m21=0; t.m22=1;
00273                 FloatBuffer b=getMatrixBuffer();
00274                 t.store(b);
00275                 b.flip();
00276                 GL11.glLoadMatrix(b);*/
00277         }
00278         
00286         public static void alignDirection(Vector3f dir) {
00287                 //GL11.glRotatef(-90,0,1,0);
00288                 GLU.gluLookAt(0, 0, 0,  dir.x,dir.y,dir.z,  0,1,0);
00289                 //GL11.glRotatef(-90,0,0,1);    
00290         }
00291         
00292         public static void drawLine(Vector3f p1, Vector3f p2) {
00293                 GL11.glBegin(GL11.GL_LINES);
00294                 GL11.glVertex3f(p1.x,p1.y,p1.z);
00295                 GL11.glVertex3f(p2.x,p2.y,p2.z);
00296                 GL11.glEnd();
00297         }
00298 
00301         public static void debugDoubleSphere(float r1, float r2) {
00302                 GL11.glDisable(GL11.GL_LIGHTING);
00303                 GL11.glDisable(GL11.GL_TEXTURE_2D);
00304                 GL11.glDisable(GL11.GL_ALPHA_TEST);
00305                 GL11.glDisable(GL11.GL_DEPTH_TEST);
00306                 GL11.glEnable(GL11.GL_CULL_FACE);
00307                 
00308                 Color c=extentcolor;            
00309                 GL11.glColor4f(c.getRed()/256f, c.getGreen()/256f, c.getBlue()/256f, c.getAlpha()/256f);                
00310                 
00311                 GLSphereRenderer.renderSphere(r1);
00312                 
00313                 if (Prefs.current.debug) {
00314                         GL11.glColor4f(0,1,1,.3f);
00315                         GLSphereRenderer.renderSphere(r2);                      
00316                 }
00317 
00318                 GL11.glEnable(GL11.GL_ALPHA_TEST);
00319                 GL11.glEnable(GL11.GL_DEPTH_TEST);
00320                 GL11.glDisable(GL11.GL_CULL_FACE);              
00321         }       
00322         
00323 }

Generated on Tue Apr 7 17:57:19 2009 for visclient by  doxygen 1.5.1