📄 joglrgbaimagerenderer.java
字号:
//===========================================================================//=-------------------------------------------------------------------------=//= Module history: =//= - November 25 2005 - Oscar Chavarro: Original base version =//= - December 7 2005 - Fabio Aroca / Eduardo Mendoza: importJOGLimage and =//= getImageJOGL methods added =//= - March 14 2006 - Oscar Chavarro: quality check =//===========================================================================package vsdk.toolkit.render.jogl;// Java base classesimport java.io.FileInputStream;import java.nio.ByteBuffer;import java.util.ArrayList;// JOGL classesimport javax.media.opengl.GL;//import javax.media.opengl.glu.GLU;import com.sun.opengl.util.texture.Texture;import com.sun.opengl.util.texture.TextureData;import com.sun.opengl.util.texture.TextureIO;import com.sun.opengl.cg.CGparameter;import com.sun.opengl.cg.CgGL;// VitralSDK classesimport vsdk.toolkit.media.RGBAImage;import vsdk.toolkit.common.RendererConfiguration;class _JoglRGBAImageRendererImageAssociation extends JoglRenderer{ public int glList; public Texture renderer; public RGBAImage image;}public class JoglRGBAImageRenderer extends JoglRenderer { private static ArrayList<_JoglRGBAImageRendererImageAssociation> compiledImages = new ArrayList<_JoglRGBAImageRendererImageAssociation>(); //private static GLU glu = null; /** This method generates an OpenGL/JOGL MipMap structure, assoiates it with the given image reference and activates. The method keeps track of all images activated, and take that history into account to pass the image data to the graphics hardware only once. Note that this method creates and use an OpenGL/JOGL compilation list for each image, to ensure optimal performance. \todo In applications with changing images, the memory list of compiled lists and the list themselves should be cleared, or not used. This will lead to the creation of new methods. */ private static int activateBase(GL gl, RGBAImage img) { //- 1. Initialization of texture parameters ----------------------- int x_tam = img.getXSize(); int y_tam = img.getYSize(); int lists[] = new int[1]; if ( (x_tam % 4) == 0 ) { gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 4); } else if ( (x_tam % 2) == 0 ) { gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 2); } else { gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1); } /* if ( glu == null ) { glu = new GLU(); } */ //- 2. Seek if there is a precompiled glList for this image ------- boolean glListIsCompiled = false; _JoglRGBAImageRendererImageAssociation item = null; int i; for ( i = 0; i < compiledImages.size(); i++ ) { item = compiledImages.get(i); if ( item.image == img ) { glListIsCompiled = true; break; } } //- 3. If there is no glList, create it --------------------------- gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glEnable(GL.GL_BLEND); if ( glListIsCompiled == false ) { //---- item = new _JoglRGBAImageRendererImageAssociation(); item.image = img; item.glList = 1; compiledImages.add(item); //---- gl.glGenTextures(1, lists, 0); item.glList=lists[0]; //gl.glBindTexture(gl.GL_TEXTURE_2D, item.glList); //---- try { TextureData textureData; textureData = new TextureData( 4, // int internalFormat (number of components) x_tam, // int width y_tam, // int height 0, // int border gl.GL_RGBA, // int pixelFormat gl.GL_UNSIGNED_BYTE, // int pixelType true, // boolean mipmap false, // boolean dataIsCompressed false, // boolean mustFlipVertically ByteBuffer.wrap(img.getRawImage()), // Buffer buffer null // TextureData.Flusher flusher ); item.renderer = TextureIO.newTexture(textureData); } catch ( Exception e ) { System.err.println(e); } //---- /* //glu.gluBuild2DMipmaps(gl.GL_TEXTURE_2D, 4, x_tam, y_tam, gl.GL_RGBA, // gl.GL_UNSIGNED_BYTE, ByteBuffer.wrap(img.getRawImage())); gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, 4, x_tam, y_tam, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, ByteBuffer.wrap(img.getRawImage())); */ } //- 4. Use the image's glList -------------------------------------// if ( glListIsCompiled == false ) { item.renderer.bind(); item.renderer.enable();/* } else { gl.glCallList(item.glList); }*/ /* if ( item != null ) { gl.glBindTexture(gl.GL_TEXTURE_2D, item.glList); } */ gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_MODULATE); return item.glList; } public static int activate(GL gl, RGBAImage img) { int list = activateBase(gl, img); //----------------------------------------------------------------- CGparameter param; if ( nvidiaCgAutomaticMode ) { param = CgGL.cgGetNamedParameter(JoglRenderer.NvidiaGpuPixelProgramTexture, "textureMap"); CgGL.cgGLSetTextureParameter(param, list); param = CgGL.cgGetNamedParameter(JoglRenderer.NvidiaGpuPixelProgramTextureBump, "textureMap"); CgGL.cgGLSetTextureParameter(param, list); } return list; } public static int activateAsNormalMap(GL gl, RGBAImage img, RendererConfiguration quality) { int list = -1; //----------------------------------------------------------------- CGparameter param; if ( nvidiaCgAutomaticMode && !nvidiaCgErrorReported && needCg(quality) ) { list = activateBase(gl, img); param = CgGL.cgGetNamedParameter(JoglRenderer.NvidiaGpuPixelProgramTextureBump, "normalMap"); CgGL.cgGLSetTextureParameter(param, list); } return list; } public static void draw(GL gl, RGBAImage img) { gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1); gl.glRasterPos2f(-1, -1); gl.glDrawPixels(img.getXSize(), img.getYSize(), gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, ByteBuffer.wrap(img.getRawImage())); } public static ByteBuffer importJOGLimage(GL gl) { int[] view= new int[4]; //IntBuffer vpBuffer = BufferUtils.newIntBuffer(16); gl.glGetIntegerv(GL.GL_VIEWPORT, view,0); int width = view[2], height = view[3]; ByteBuffer bb = ByteBuffer.allocateDirect(3 * width * height); gl.glReadBuffer(GL.GL_FRONT_LEFT); gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1); gl.glReadPixels( -1, -1, width, height, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, bb); gl.glFlush(); return bb; } public static RGBAImage getImageJOGL(GL gl) { RGBAImage image = new RGBAImage(); int[] view= new int[4]; gl.glGetIntegerv(GL.GL_VIEWPORT, view,0); int width = view[2], height = view[3]; image.init(width, height); // TODO: Check if this can be done without duplication! ByteBuffer bb = importJOGLimage(gl).duplicate(); int pos = 0; for (int y =image.getYSize()-1; y >=0; y--) { for (int x = 0; x < image.getXSize(); x++) { image.putPixel(x,y, bb.get(pos), bb.get(pos + 1), bb.get(pos + 2)); pos += 3; } } return image; }}//===========================================================================//= EOF =//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -