📄 renderer.java
字号:
/* * J3DME Fast 3D software rendering for small devices * Copyright (C) 2001 Onno Hommes * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package net.jscience.j3dme;/** * This abstart class defines the methods to be implemented * by platform specific renderers. The renderer is responsible for * drawing on the display of the device the engine is running on. * This abstract class disconnects the 3D engine from the platform that * it is runnning on. The platform specific renderer will use the platform * specific drawing capabilities to implement the abstract drawing methods. */public abstract class Renderer { private int[] transformation; /** * Draws a line on the screen<p> * @param x1 Start x coordinate * @param y1 Start y coordinate * @param x2 End x coordinate * @param y2 End y coordinate */ public abstract void drawLine(int x1,int y1,int x2,int y2); /** * Clears a portion of the screen<p> * @param x Top left x coordinate of area to clear * @param y Top left y coordinate * @param w Width of the area * @param h Height ot the area */ public abstract void clearArea(int x,int y,int w,int h); /** * Define the drawregion on the screen<p> * @param x Top left x coordinate * @param y Top left y coordinate * @param w Width of the area * @param h Height of the area */ public abstract void setDrawRegion(int x,int y,int w,int h); /** * * @param x Top left x coordinate * @param y Top left y coordinate * @param w Width of the area * @param h Height of the area */ public void drawRectangle(int x,int y,int w,int h){ drawLine(x,y,x+w,y); drawLine(x+w,y,x+w,y+h); drawLine(x+w,y+h,x,y+h); drawLine(x,y+h,x,y); } public void drawScene(ViewPort view){ boolean[] visible_faces; int cx = view.cx; int cy = view.cy; int offset=0; Camera camera = view.camera; World world = camera.getWorld(); // Take a Picture camera.captureScene(); // Clear Display //setDrawRegion(view.x,view.y,view.width,view.height); clearArea(view.x,view.y,view.width,view.height); int size = world.getNumberOfModels(); for (int mi=0;mi<size;mi++){ // Get current Model by index Model model = world.getModel(mi); // Get model geometry Wireframe geom = (Wireframe) model.getGeometry(); // Determine number of wires int wires = geom.wires_from.length; // View culling if ((camera.visibility_mask & 1 << mi) != 0){ // Determine view scale int viewscale = camera.fieldOfView / camera.getModelDistance(model); // For each wire do for (int wi=0;wi<wires;wi++){ // Determine wire visibility if (geom instanceof Surface) if (isWireVisible(camera.surface_masks[mi],wi,(Surface)geom) == false) continue; // Projecting Wire onto Screen int si = offset + geom.wires_from[wi]; int ei = offset + geom.wires_to[wi]; int sx = camera.x_tfx[si] * viewscale >> 8; int sy = camera.y_tfx[si] * viewscale >> 8; int ex = camera.x_tfx[ei] * viewscale >> 8; int ey = camera.y_tfx[ei] * viewscale >> 8; drawLine(cx+sx, cy-sy, cx+ex, cy-ey); } } offset += wires; } } public boolean isWireVisible(int surface_mask,int w_index, Surface geometry){ int[] wires = geometry.wires; for (int wi=0;wi<wires.length;wi++){ // Find polygon for wire if (wires[wi] == w_index){ for (int si=0;si<geometry.polygons_from.length;si++){ // Find Surface for polygon if (wi >= geometry.polygons_from[si] && wi <= geometry.polygons_to[si]){ // Determine bit_mask int bit_mask = 1 << si; // If visible return true otherwise keep on searching if ((surface_mask & bit_mask) == bit_mask) return true; } } } } // Wire not visible return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -