import gl4java.*; import gl4java.awt.*; import gl4java.utils.textures.*; import java.awt.*; import java.awt.event.*; import java.net.*; public class GLImageWorld1 extends GLCanvas implements MouseListener,MouseMotionListener { float []mPosObjTrans; float []mPosObjRot; Point mousePoint; Point oldMousePoint; boolean mouseMoveFlag; TextureGrabber textGrab4Snapshot = null; String textGrab4SnapshotFName = null; TextureLoader txtLoader = null; int texName[] = {0}; public GLImageWorld1 (int w, int h) { super(w, h); } /** * Creates a snapshot (save texture/image) of the current * GL-Context ! * * The snapshot itself is created delayed, * so no return value is avaiable. * Because this is a non critical path, I hope its enough ! * * @param tg The TextureGrabber * @param fname The filename * @see TextureGrabber */ public void snapshot(TextureGrabber tg, String fname) { textGrab4Snapshot=tg; textGrab4SnapshotFName=fname; repaint(); } public void setOriginalPerspective() { setOriginalPerspective(true); } private void setOriginalPerspective(boolean fetchGL) { for(int i=0;i<16;i++) mPosObjTrans[i]=0f; mPosObjTrans[0]=mPosObjTrans[5]=mPosObjTrans[10]=mPosObjTrans[15]=1f; for(int i=0;i<16;i++) mPosObjRot[i]=0f; mPosObjRot[0]=mPosObjRot[5]=mPosObjRot[10]=mPosObjRot[15]=1f; if(fetchGL) { if( glj.gljMakeCurrent() == false ) { System.out.println("problem in use() method"); return; } } TranlateObj(0f,0f,-10f); SetCamera(); if(fetchGL) { glj.gljCheckGL(); glj.gljFree(); repaint(); } } public void preInit() { // createOwnWindow = true; } public void init() { gl.glEnable(GL_TEXTURE_2D); gl.glGenTextures(1,texName); gl.glBindTexture(GL_TEXTURE_2D,texName[0]); //gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); //gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); //gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); gl.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // cameraMatrix init mPosObjTrans=new float[16]; mPosObjRot=new float[16]; setOriginalPerspective(false); gl.glShadeModel (GL_SMOOTH); gl.glEnable(GL_DEPTH_TEST); gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f); gl.glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glj.gljCheckGL(); addMouseListener(this); addMouseMotionListener(this); mouseMoveFlag=false; reshape(getSize().width, getSize().height); } /** * These variables are very important * to respect the texture-size ratio ! */ float texMaxPosX = 1f; float texMaxPosY = 1f; public boolean loadTexture(String name, String type) { boolean ok = true; if( glj.gljMakeCurrent() == false ) { System.out.println("problem in use() method"); return false; } // texture laden if(type.equals("png")) txtLoader = new PngTextureLoader(gl, glu); else if(type.equals("ppm")) txtLoader = new PPMAsciiTextureLoader(gl, glu); else if(type.equals("tga")) txtLoader = new TGATextureLoader(gl, glu); else if(type.equals("any")) txtLoader = new AWTTextureLoader(this, gl, glu); else { System.out.println("Imagetype: "+type+" is currently not supported !"); ok = false; } if(ok) { try { txtLoader.readTexture(name); } catch (Exception ex) { ex.printStackTrace(); } } if(ok && txtLoader.isOk()) { gl.glEnable(GL_TEXTURE_2D); txtLoader.texImage2DScaled4BestSize(); texMaxPosX = 1f; texMaxPosY = 1f; float texAspect = txtLoader.getTextureWidth()/ txtLoader.getTextureHeight(); if(txtLoader.getTextureWidth()>txtLoader.getTextureHeight()) texMaxPosY = 1f/texAspect; else texMaxPosX = texAspect; System.out.println("texture succesfully loaded !"); System.out.println("texture: "+txtLoader); } glj.gljCheckGL(); glj.gljFree(); if(ok) repaint(); return ok; } public void display() { int i; /* Standard GL4Java Init */ if( glj.gljMakeCurrent() == false ) { System.out.println("problem in use() method"); return; } // just render it gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SetCamera(); DrawScene(); if(textGrab4Snapshot!=null) { textGrab4Snapshot.grabPixels(GL_BACK, 0, 0, cvsGetWidth(), cvsGetHeight()); if(textGrab4SnapshotFName!=null) textGrab4Snapshot.write2File(textGrab4SnapshotFName); textGrab4Snapshot =null; textGrab4SnapshotFName=null; } /* For your animation dutys ;-) */ glj.gljSwap(); glj.gljCheckGL(); glj.gljFree(); } public void reshape(int w, int h) { gl.glMatrixMode (GL_MODELVIEW); gl.glViewport (0, 0, w, h); gl.glLoadIdentity(); SetCamera(); } public void drawGrid(float x0, float y0, float width, float height, float step) { float i,j; /* draw grid */ gl.glBegin(GL_LINES); for(i=x0;i