📄 joglview.java
字号:
w = subCanvasXSize; } if ( viewportRequestedSizeYInPixels < subCanvasYSize ) { h = viewportRequestedSizeYInPixels; } else { h = subCanvasYSize; } viewportStartX += (subCanvasXSize - w) / 2; viewportStartY += (subCanvasYSize - h) / 2; viewportSizeX = w; viewportSizeY = h; } camera.updateViewportResize(viewportSizeX, viewportSizeY); } public void activateViewportGL(GL gl, int canvasXSize, int canvasYSize) { if ( !active ) { return; } updateViewportConfiguration(canvasXSize, canvasYSize); gl.glViewport(viewportStartX, viewportStartY, viewportSizeX, viewportSizeY); } /** PRE: glViewport is set to full canvas area, and projection and modelview matrices are set to identity. */ public void drawBorderGL(GL gl, int viewportXsize, int viewportYsize) { if ( !active ) { return; } gl.glPushAttrib(gl.GL_DEPTH_TEST); gl.glPushAttrib(gl.GL_TEXTURE_2D); gl.glPushAttrib(gl.GL_LIGHTING); gl.glDisable(gl.GL_LIGHTING); gl.glDisable(gl.GL_TEXTURE_2D); gl.glDisable(gl.GL_DEPTH_TEST); double x1, y1, x2, y2; double epsilonx = 2.0 / ((double)viewportXsize); double epsilony = 2.0 / ((double)viewportYsize); double dx = (viewportBorder) * epsilonx; double dy = (viewportBorder) * epsilony; x1 = viewportStartXPercent*2 - 1; y1 = viewportStartYPercent*2 - 1; x2 = x1 + viewportSizeXPercent*2; y2 = y1 + viewportSizeYPercent*2; gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL); gl.glBegin(gl.GL_QUADS); if ( selected ) { gl.glColor3d(1, 0.96, 0); } else { gl.glColor3d(0.21, 0.25, 0.29); } gl.glVertex3d(x1, y1, 0); gl.glVertex3d(x2, y1, 0); gl.glVertex3d(x2, y2, 0); gl.glVertex3d(x1, y2, 0); gl.glColor3d(0, 0, 0); gl.glVertex3d(x1+2*dx, y1+2*dy, 0); gl.glVertex3d(x2-2*dx, y1+2*dy, 0); gl.glVertex3d(x2-2*dx, y2-2*dy, 0); gl.glVertex3d(x1+2*dx, y2-2*dy, 0); gl.glEnd(); gl.glPopAttrib(); gl.glPopAttrib(); gl.glPopAttrib(); } public RGBAImage calculateLabelImage(String label, ColorRgb color) { RGBAImage labelImage; //----------------------------------------------------------------- labelImage = new RGBAImage(); labelImage.init(200, 30); BufferedImage bi; Graphics offlineContext; bi = AwtRGBAImageRenderer.exportToAwtBufferedImage(labelImage); offlineContext = bi.getGraphics(); FontMetrics fm = offlineContext.getFontMetrics(); Rectangle2D r = fm.getStringBounds(label, offlineContext); Rectangle ri = r.getBounds(); //----------------------------------------------------------------- labelImage.init(ri.width-ri.x+10, (ri.height-ri.y)/2+5); bi = AwtRGBAImageRenderer.exportToAwtBufferedImage(labelImage); offlineContext = bi.getGraphics(); //----------------------------------------------------------------- offlineContext.setColor( new Color((float)color.r, (float)color.g, (float)color.b)); offlineContext.setFont(font); offlineContext.drawString(label, -ri.x, -ri.y); AwtRGBAImageRenderer.importFromAwtBufferedImage(bi, labelImage); return labelImage; } public void setTitle(String name) { title = new String(name); titleImage = calculateLabelImage(title, new ColorRgb(0.76, 0.76, 0.76)); } public void drawReferenceBase(GL gl) { //----------------------------------------------------------------- int basesize = 64; gl.glPushAttrib(gl.GL_VIEWPORT_BIT); gl.glPushAttrib(gl.GL_DEPTH_TEST); gl.glPushAttrib(gl.GL_TEXTURE_2D); gl.glPushAttrib(gl.GL_LIGHTING); gl.glViewport(viewportStartX, viewportStartY, basesize, basesize); gl.glMatrixMode(gl.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glPushMatrix(); gl.glDisable(gl.GL_LIGHTING); gl.glDisable(gl.GL_TEXTURE_2D); gl.glDisable(gl.GL_DEPTH_TEST); //----------------------------------------------------------------- Matrix4x4 R = camera.getRotation(); gl.glLoadIdentity(); R.invert(); gl.glRotated(90, -1, 0, 0); gl.glRotated(90, 0, 0, 1); JoglMatrixRenderer.activate(gl, R); gl.glPushMatrix(); gl.glTranslated(2, 1, 0); drawTextureString3D(gl, xLabelImage); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(1, 2, 0); drawTextureString3D(gl, yLabelImage); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslated(1, 1, 1); drawTextureString3D(gl, zLabelImage); gl.glPopMatrix(); //gl.glLoadIdentity(); gl.glBegin(gl.GL_LINES); gl.glColor3d(0.78, 0, 0); gl.glVertex3d(0, 0, 0); gl.glVertex3d(1, 0, 0); gl.glColor3d(0, 0.61, 0); gl.glVertex3d(0, 0, 0); gl.glVertex3d(0, 1, 0); gl.glColor3d(0, 0, 0.76); gl.glVertex3d(0, 0, 0); gl.glVertex3d(0, 0, 1); gl.glEnd(); //----------------------------------------------------------------- gl.glPopMatrix(); gl.glMatrixMode(gl.GL_PROJECTION); gl.glPopMatrix(); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glPopAttrib(); gl.glPopAttrib(); gl.glPopAttrib(); gl.glPopAttrib(); } private void drawGridRectangle(GL gl) { gl.glPushMatrix(); Matrix4x4 R; double yaw, pitch; R = camera.getRotation(); yaw = Math.toDegrees(R.obtainEulerYawAngle()); pitch = Math.toDegrees(R.obtainEulerPitchAngle()); if ( camera.getProjectionMode() == camera.PROJECTION_MODE_ORTHOGONAL && (pitch > -45 && pitch < 45) ) { if ( (yaw > 45 && yaw < 135) || (yaw < -45 && yaw > -135) ) { gl.glRotated(90, 1, 0, 0); } else { gl.glRotated(90, 0, 1, 0); } } //----------------------------------------------------------------- int nx = 14; // Must be an even number int ny = 14; // Must be an even number double dx = 1.0; double dy = 1.0; int x, y; double minx = -(((double)nx)/2) * dx; double maxx = (((double)nx)/2) * dx; double miny = -(((double)ny)/2) * dy; double maxy = (((double)ny)/2) * dy; gl.glDisable(gl.GL_LIGHTING); gl.glDisable(gl.GL_TEXTURE_2D); gl.glLineWidth(1.0f); gl.glBegin(GL.GL_LINES); gl.glColor3d(0.37, 0.37, 0.37); for ( x = 0; x <= nx; x++ ) { if ( x == nx/2 ) continue; gl.glVertex3d(minx + ((double)x)*dx, miny, 0); gl.glVertex3d(minx + ((double)x)*dx, maxy, 0); } for ( y = 0; y <= ny; y++ ) { if ( y == ny/2 ) continue; gl.glVertex3d(minx, minx + ((double)y)*dy, 0); gl.glVertex3d(maxx, minx + ((double)y)*dy, 0); } gl.glColor3d(0, 0, 0); gl.glVertex3d(minx + ((double)(nx/2))*dx, miny, 0); gl.glVertex3d(minx + ((double)(nx/2))*dx, maxy, 0); gl.glVertex3d(minx, minx + ((double)(ny/2))*dy, 0); gl.glVertex3d(maxx, minx + ((double)(ny/2))*dy, 0); gl.glEnd(); gl.glPopMatrix(); } public void toggleGrid() { if ( showGrid ) { showGrid = false; } else { showGrid = true; } } public void drawGrid(GL gl) { //- Draw reference grid plane ------------------------------------- if ( showGrid ) drawGridRectangle(gl); } public void drawTextureString2D(GL gl, int x, int y, RGBAImage i) { double dx; double dy; dx = ((double)(2*x)) / ((double)viewportSizeX); dy = ((double)(2*(viewportSizeY - y))) / ((double)viewportSizeY); gl.glMatrixMode(gl.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glPushMatrix(); gl.glLoadIdentity(); gl.glTranslated(dx, dy, 0); drawTextureString3D(gl, i); gl.glPopMatrix(); gl.glMatrixMode(gl.GL_PROJECTION); gl.glPopMatrix(); gl.glMatrixMode(gl.GL_MODELVIEW); } private void drawTextureString3D(GL gl, RGBAImage i) { gl.glRasterPos3d(-1, -1, 0); gl.glEnable(gl.GL_TEXTURE_2D); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); // First: activate texture, Second: set texture parameters gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST); gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST); gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, gl.GL_BLEND); JoglImageRenderer.draw(gl, i); } public void drawTitle(GL gl) { int borderx = 4; int bordery = 1; drawTextureString2D(gl, borderx, titleImage.getYSize() + bordery, titleImage); } public boolean inside(double x, double y) { if ( x >= viewportStartXPercent && x <= viewportStartXPercent + viewportSizeXPercent && y >= viewportStartYPercent && y <= viewportStartYPercent + viewportSizeYPercent ) { return true; } return false; } public void updateMouseEvent(MouseEvent e, int globalViewportXSize, int globalViewportYSize) { e.translatePoint(-getViewportStartX(), getViewportSizeY() - (globalViewportYSize-getViewportStartY())); } public void hintConfig(int numViews, int id) { if ( numViews >= 4 ) { switch ( id ) { case 0: camera = cameraLeft; quality.setSurfaces(false); quality.setWires(true); break; case 1: camera = cameraPerspective; break; case 2: camera = cameraTop; quality.setSurfaces(false); quality.setWires(true); break; case 3: default: camera = cameraFront; quality.setSurfaces(false); quality.setWires(true); break; } setTitle(camera.getName()); } } public void drawLabelsForTranslateGizmo(GL gl, TranslateGizmo gizmo) { ArrayList<SimpleBody> things = gizmo.getElements(); int i; Vector3D lv = new Vector3D(); Vector3D p; Vector3D tp = new Vector3D(); Matrix4x4 R; boolean yellow; ColorRgb c = new ColorRgb(1, 1, 0); for ( i = 0; i < things.size() && i < 3; i++ ) { SimpleBody r = things.get(i); Geometry g = r.getGeometry(); Vector3D position; if ( g != null ) { gl.glPushMatrix(); gl.glLoadIdentity(); lv.x = lv.y = 0; if ( g instanceof Arrow ) { Arrow a = ((Arrow)g); lv.z = (a.getHeadLength() + a.getBaseLength()) * 1.1; } else { lv.z = 1; } R = new Matrix4x4(); R.translation(r.getPosition()); R = R.multiply(r.getRotation()); p = R.multiply(lv); camera.projectPoint(p, tp); //--------------------------------------------- yellow = false; if ( VSDK.colorDistance(c, r.getMaterial().getDiffuse()) < VSDK.EPSILON ) { yellow = true; } //--------------------------------------------- switch ( i ) { case 0: if ( yellow ) { drawTextureString2D(gl, (int)tp.x-3, (int)tp.y+12, xLabelImageSelected); } else { drawTextureString2D(gl, (int)tp.x-3, (int)tp.y+12, xLabelImage); } break; case 1: if ( yellow ) { drawTextureString2D(gl, (int)tp.x-3, (int)tp.y+12, yLabelImageSelected); } else { drawTextureString2D(gl, (int)tp.x-3, (int)tp.y+12, yLabelImage); } break; case 2: if ( yellow ) { drawTextureString2D(gl, (int)tp.x-3, (int)tp.y+12, zLabelImageSelected); } else { drawTextureString2D(gl, (int)tp.x-3, (int)tp.y+12, zLabelImage); } break; } gl.glPopMatrix(); } } }}//===========================================================================//= EOF =//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -