📄 renderer.java
字号:
/* * $RCSfile: Renderer.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Use is subject to license terms. * * $Revision: 1.23 $ * $Date: 2007/05/15 21:31:43 $ * $State: Exp $ *//* * Portions of this code were derived from work done by the Blackdown * group (www.blackdown.org), who did the initial Linux implementation * of the Java 3D API. */package javax.media.j3d;import java.util.logging.Level;import javax.vecmath.*;import java.awt.*;import java.awt.image.*;import java.util.*;class Renderer extends J3dThread { // This action causes this thread to wait static final int WAIT = 0; // This action causes this thread to notify the view, and then wait. static final int NOTIFY_AND_WAIT = 1; // This action causes this thread to be notified static final int NOTIFY = 2; // The following are DecalGroup rendering states static final int DECAL_NONE = 0; static final int DECAL_1ST_CHILD = 1; static final int DECAL_NTH_CHILD = 2; // stuff for scene antialiasing static final int NUM_ACCUMULATION_SAMPLES = 8; static final float ACCUM_SAMPLES_X[] = { -0.54818f, 0.56438f, 0.39462f, -0.54498f, -0.83790f, -0.39263f, 0.32254f, 0.84216f}; static final float ACCUM_SAMPLES_Y[] = { 0.55331f, -0.53495f, 0.41540f, -0.52829f, 0.82102f, -0.27383f, 0.09133f, -0.84399f}; static final float accumValue = 1.0f / NUM_ACCUMULATION_SAMPLES; // The following are Render arguments static final int RENDER = 0; static final int SWAP = 1; static final int REQUESTRENDER = 2; static final int REQUESTCLEANUP = 3; // Renderer Structure used for the messaging to the renderer RendererStructure rendererStructure = new RendererStructure(); // vworldtoVpc matrix for background geometry Transform3D bgVworldToVpc = new Transform3D(); private static int numInstances = 0; private int instanceNum = -1; // Local copy of sharedStereZBuffer flag boolean sharedStereoZBuffer; // This is the id for the underlying sharable graphics context Context sharedCtx = null; // since the sharedCtx id can be the same as the previous one, // we need to keep a time stamp to differentiate the contexts with the // same id long sharedCtxTimeStamp = 0; // display and drawable, used to free shared context private long sharedCtxDisplay = 0; private Drawable sharedCtxDrawable = null; /** * This is the id of the current rendering context */ Context currentCtx = null; /** * This is the id of the current rendering drawable */ Drawable currentDrawable = null; // an unique bit to identify this renderer int rendererBit = 0; // an unique number to identify this renderer : ( rendererBit = 1 << rendererId) int rendererId = 0; // List of renderMolecules that are dirty due to additions // or removal of renderAtoms from their display list set // of renderAtoms ArrayList dirtyRenderMoleculeList = new ArrayList(); // List of individual dlists that need to be rebuilt ArrayList dirtyRenderAtomList = new ArrayList(); // List of (Rm, rInfo) pair of individual dlists that need to be rebuilt ArrayList dirtyDlistPerRinfoList = new ArrayList(); // Texture and display list that should be freed ArrayList textureIdResourceFreeList = new ArrayList(); ArrayList displayListResourceFreeList = new ArrayList(); // Texture that should be reload ArrayList textureReloadList = new ArrayList(); J3dMessage[] renderMessage; // The screen for this Renderer. Note that this renderer may share // by both on screen and off screen. When view unregister, we need // to set both reference to null. Screen3D onScreen; Screen3D offScreen; // full screen anti-aliasing projection matrices Transform3D accumLeftProj = new Transform3D(); Transform3D accumRightProj = new Transform3D(); Transform3D accumInfLeftProj = new Transform3D(); Transform3D accumInfRightProj = new Transform3D(); // rendering messages J3dMessage m[]; int nmesg = 0; // List of contexts created ArrayList<Context> listOfCtxs = new ArrayList<Context>(); // Parallel list of canvases ArrayList<Canvas3D> listOfCanvases = new ArrayList<Canvas3D>(); boolean needToRebuildDisplayList = false; boolean needToResendTextureDown = false; // True when either one of dirtyRenderMoleculeList, // dirtyDlistPerRinfoList, dirtyRenderAtomList size > 0 boolean dirtyDisplayList = false; // Remember OGL context resources to free // before context is destroy. // It is used when sharedCtx = true; ArrayList textureIDResourceTable = new ArrayList(5); // Instrumentation of Java 3D renderer private long lastSwapTime = System.nanoTime(); private synchronized int newInstanceNum() { return (++numInstances); } int getInstanceNum() { if (instanceNum == -1) instanceNum = newInstanceNum(); return instanceNum; } /** * Constructs a new Renderer */ Renderer(ThreadGroup t) { super(t); setName("J3D-Renderer-" + getInstanceNum()); type = J3dThread.RENDER_THREAD; rendererId = VirtualUniverse.mc.getRendererId(); rendererBit = (1 << rendererId); renderMessage = new J3dMessage[1]; } /** * The main loop for the renderer. */ void doWork(long referenceTime) { RenderAtom ra; RenderBin renderBin = null; Canvas3D cv, canvas=null; Object firstArg; View view = null; Color3f col; int stereo_mode; int num_stereo_passes, num_render_passes, num_accum_passes = 1; int pass, apass, i, j, k; boolean doAccum = false; double accumDx = 0.0f, accumDy = 0.0f; double accumDxFactor = 1.0f, accumDyFactor = 1.0f; double accumLeftX = 0.0, accumLeftY = 0.0, accumRightX = 0.0, accumRightY = 0.0, accumInfLeftX = 0.0, accumInfLeftY = 0.0, accumInfRightX = 0.0, accumInfRightY = 0.0; int opArg, status; boolean done = false; Transform3D t3d = null; opArg = ((Integer)args[0]).intValue(); try { if (opArg == SWAP) { Object [] swapArray = (Object[])args[2]; view = (View)args[3]; for (i=0; i<swapArray.length; i++) { cv = (Canvas3D) swapArray[i]; if (!cv.isRunning) { continue; } doneSwap: try { if (!cv.validCanvas) { continue; } if (cv.active && (cv.ctx != null) && (cv.view != null) && (cv.imageReady)) { if (cv.useDoubleBuffer) { synchronized (cv.drawingSurfaceObject) { if (cv.validCtx) { if (VirtualUniverse.mc.doDsiRenderLock) { // Set doDsiLock flag for rendering based on system // property, If we force DSI lock for swap // buffer, we lose most of the parallelism that having // multiple renderers gives us. if (!cv.drawingSurfaceObject.renderLock()) { break doneSwap; } cv.makeCtxCurrent(); cv.syncRender(cv.ctx, true); status = cv.swapBuffers(cv.ctx, cv.screen.display, cv.drawable); if (status != Canvas3D.NOCHANGE) { cv.resetRendering(status); } cv.drawingSurfaceObject.unLock(); } else { cv.makeCtxCurrent(); cv.syncRender(cv.ctx, true); status = cv.swapBuffers(cv.ctx, cv.screen.display, cv.drawable); if (status != Canvas3D.NOCHANGE) { cv.resetRendering(status); } } } } } cv.view.inCanvasCallback = true; try { cv.postSwap(); } catch (RuntimeException e) { System.err.println("Exception occurred during Canvas3D callback:"); e.printStackTrace(); } catch (Error e) { // Issue 264 - catch Error so Renderer doesn't die System.err.println("Error occurred during Canvas3D callback:"); e.printStackTrace(); } // reset flag cv.imageReady = false; cv.view.inCanvasCallback = false; // Clear canvasDirty bit ONLY when postSwap() success if (MasterControl.isStatsLoggable(Level.INFO)) { // Instrumentation of Java 3D renderer long currSwapTime = System.nanoTime(); long deltaTime = currSwapTime - lastSwapTime; lastSwapTime = currSwapTime; VirtualUniverse.mc.recordTime(MasterControl.TimeType.TOTAL_FRAME, deltaTime); } // Set all dirty bits except environment set and lightbin // they are only set dirty if the last used light bin or // environment set values for this canvas change between // one frame and other if (!cv.ctxChanged) { cv.canvasDirty = (0xffff & ~(Canvas3D.LIGHTBIN_DIRTY | Canvas3D.LIGHTENABLES_DIRTY | Canvas3D.AMBIENTLIGHT_DIRTY | Canvas3D.MODELCLIP_DIRTY | Canvas3D.VIEW_MATRIX_DIRTY | Canvas3D.FOG_DIRTY)); // Force reload of transform next frame cv.modelMatrix = null; // Force the cached renderAtom to null cv.ra = null; } else { cv.ctxChanged = false; } } } catch (NullPointerException ne) { // Ignore NPE if (VirtualUniverse.mc.doDsiRenderLock) { cv.drawingSurfaceObject.unLock(); } } catch (RuntimeException ex) { ex.printStackTrace(); if (VirtualUniverse.mc.doDsiRenderLock) { cv.drawingSurfaceObject.unLock(); } // Issue 260 : indicate fatal error and notify error listeners cv.setFatalError(); RenderingError err = new RenderingError(RenderingError.UNEXPECTED_RENDERING_ERROR, J3dI18N.getString("Renderer0")); err.setCanvas3D(cv); err.setGraphicsDevice(cv.graphicsConfiguration.getDevice()); notifyErrorListeners(err); } cv.releaseCtx(); } if (view != null) { // STOP_TIMER // incElapsedFrames() is delay until MC:updateMirroObject if (view.viewCache.getDoHeadTracking()) { VirtualUniverse.mc.sendRunMessage(view, J3dThread.RENDER_THREAD); } } } else if (opArg == REQUESTCLEANUP) { Integer mtype = (Integer) args[2];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -