⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 render.java

📁 一个J2ME的3D 一个J2ME的3D 一个J2ME的3D
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 
 */
package render;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import object3d.Poly_4D;

import Math.*;

/**
 * @author PanXu
 *
 */
public class Render {
	/**
	 * 成员变量
	 */
	Clip clip = new Clip ();//渲染裁减区域
	int ScreenBuffer[] = null;
	int [] ImgBuf = null ;
	int ScreenW , ScreenH ;
	int ScreenWSub1 , ScreenHSub1;
	
	//行地址索引
	int [] RowIndex = null;
	public Render() {
		super();
		// TODO 自动生成构造函数存根
	}
	/**
	 * 初始化渲染器
	 * @param x 渲染裁减区域
	 * @param y
	 * @param w
	 * @param h
	 */
	public void InitRender (int x, int y , int w , int h , 
			int ScreenW , int ScreenH)
	{
		this .clip .ClipLTPoint .M[0] = x;
		this .clip .ClipLTPoint .M[1] = y;
		this .clip .ClipW = w ;
		this .clip .ClipH = h ;
		this .ScreenW = ScreenW ;
		this .ScreenH = ScreenH ;
		this .ScreenHSub1 = this .ScreenH - 1 ;
		this .ScreenWSub1 = this .ScreenW - 1 ;
		
		this .ScreenBuffer = new int [ScreenH*ScreenW];
		
		this .RowIndex = new int [ScreenH];
		int in = 0 ; 
		for (int i = 0 ; i < ScreenH ; i ++)
		{
			RowIndex [i] = in;
			in += this .ScreenW;
		}//for 
	}
	/**
	 * 画线带裁减
	 * @param sx
	 * @param sy
	 * @param ex
	 * @param ey
	 */
	public void DrawLine (int sx , int sy , int ex , int ey , int Color)
	{
		///////////////////////////////裁减
		float t1 , t2 , t3 , t4 ;//交点
		int dx = ex - sx  , dy = ey - sy ;
		float ftemp ;
		t1 = (this .clip.ClipLTPoint.M[0] - sx) / dx;
		t2 = (this .clip.ClipLTPoint.M[0] + this.clip.ClipW - 1 - sx) / dx;
		t3 = (this .clip.ClipLTPoint.M[1] - sy) / dy;
		t4 = (this .clip.ClipLTPoint.M[1] + this.clip.ClipH - 1 - sy) / dy;
		//交点分组
		//System .out.println (t1);
		if (t1 > t2)
		{
			ftemp = t1 ;
			t1 = t2 ;
			t2 = ftemp;
		}//if 
		if (t3 > t4)
		{
			ftemp = t3 ;
			t3 = t4 ;
			t4 = ftemp;
		}
		//确定新端点
		float st = 0f, et = 1.0f ;
		if (t1 > st)
			st = t1;
		if (t3 > st)
			st = t3;
		
		if (t2 < et)
			et = t2 ;
		if (t4 < et)
			et = t4;
		//System .out .println ("st:" + st + "			et:" + et);
		if (st > et)
			return ;
		
		sx += dx * st ;
		sy += dy * st ;
		ex += dx * (et - 1);
		ey += dy * (et - 1);
		
		///////////////////////////////////绘制
		dx = ex - sx ;
		dy = ey - sy ; 
		int error = 0; //误差
		int addx = 1 ;//, addy = 1;
		int iny = this.ScreenW;
		
		if (dx < 0)
			addx = -1 ;
		if (dy < 0)
		{
		//	addy = -1 ;
			iny = -this .ScreenW;
		}
		//dx &= 0x7fffffff;
		dx = Math .abs(dx);
		//dy &= 0x7fffffff;
		dy = Math .abs(dy);
		int dy2 = dy << 1;
		int dx2 = dx << 1;
		
		int index = this.RowIndex[sy] + sx ;
		
		if (dx > dy)
		{
			for (int i = 0 ; i <= dx ; i ++)
			{
				this .ScreenBuffer[index] = Color ;
				if (error > 0)
				{
					error -= dx2 ;
					index += iny ;
				}//if 
				error += dy2 ;
				index += addx ;
			}//for 
		}//if 
		else 
		{
			for (int  i = 0 ; i <= dy ; i ++)
			{
				this .ScreenBuffer[index] = Color ;
				if (error > 0)
				{
					error -= dy2 ;
					index += addx ;
				}//if 
				error += dx2 ;
				index += iny ;
			}//for 
		}//else 
	}
	/**
	 * 画象素
	 * @param x
	 * @param y
	 * @param Color
	 */
	public void DrawPixle (int x , int y , int Color)
	{
		if (x >= this .ScreenW || x < 0)
			return ;
		if (y >= this .ScreenH || y < 0)
			return ;
		this .ScreenBuffer [y * this .ScreenW + x] = Color ;
	}
	/**
	 * 渲染缓存
	 * @param g
	 */
	public void EndRander (Graphics g)
	{
		g .drawRGB(this .ScreenBuffer , 0 , this.ScreenW , 0 , 0 , 
				this .ScreenW , this .ScreenH,false);
	}
	
	/**
	 * 设置渲染器裁减区
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public void SetClip (int x , int y , int w , int h)
	{
		this .clip.ClipLTPoint.M[0] = x ;
		this .clip.ClipLTPoint.M[1] = y ;
		this .clip.ClipW = w ;
		this .clip.ClipH = h ;
	}
	/**
	 * 绘制Image对象
	 * @param img
	 * @param x 以左上点为准
	 * @param y
	 */
	public void DrawImage (Image img , int x , int y)
	{
		int igw = img .getWidth() ;
		int igh = img .getHeight() ;
		if (x >= this .ScreenW || y >= this .ScreenH 
				|| x + igw <= 0 || y + igh <= 0)
			return ;
		this .ImgBuf = new int [igw * igh];
		img .getRGB(ImgBuf , 0 , igw ,0 , 0, igw , igh);
		//Image的绘制范围
		int rectx = x, recty = y, rectw = igw, recth = igh;
		
		//Buf上的绘制点
		int Bufsx = rectx , Bufsy = recty;
		if (rectx < 0)
		{
			rectw += rectx ;
			rectx = -x;
			Bufsx = 0 ;
		}//if 
		if (recty < 0)
		{
			recth += recty ;
			recty = -y ;
			Bufsy = 0 ;
		}//if 
		
		rectw = (rectx + rectw > this .ScreenWSub1)?ScreenWSub1 - rectx : rectx;
		recth = (recty + recth > this .ScreenHSub1)?ScreenHSub1 - recty : recty;
		//绘制索引
		
		int BuffIndex = Bufsx + this.RowIndex[Bufsy];
		int ImgIndex = rectx + recty * igw;
		
		//绘制图像
		int i , j;
		for (i = 0 ; i < recth ; i ++)
		{
			for (j = 0 ; j < rectw ; j ++)
			{
				this .ScreenBuffer [BuffIndex] = ImgBuf [ImgIndex];
				BuffIndex ++ ;
				ImgIndex ++ ;
			}//for 
			BuffIndex = BuffIndex - rectw + this .ScreenW;
			ImgIndex = ImgIndex - rectw + igw ;
		}//for 
	}
	/**
	 * 填充水平直线
	 * @param y
	 * @param sx
	 * @param ex
	 */
	public void FillFlatLine (int y , int sx , int ex , int color)
	{
		int nt ;
		
		if (sx > ex)
		{
			nt = sx ;
			sx = ex ;
			ex = nt ;
		}//if
		
		//边界检查
		if (y < 0 || y > this .ScreenHSub1)
			return;
		if (sx > this .ScreenWSub1)
			return ;
		
		if (sx < 0)
			sx = 0;
		if (ex > this .ScreenWSub1)
			ex = this .ScreenWSub1;
		
		nt = this.RowIndex[y] + sx ;
		for (int i = sx ; i <= ex ; i++)
		{
			this .ScreenBuffer [nt] = color ;
			nt ++;
		}//for 
	}
	/**
	 * 颜色插值填充水平直线
	 * @param y
	 * @param sx
	 * @param ex
	 * @param sc
	 * @param ec
	 */
	public void FillFlatLineGOURAUD (int y , int sx , int ex , int sc , int ec)
	{
		int nt ;
		
		if (sx > ex)
		{
			nt = sx ;
			sx = ex ;
			ex = nt ;
		}//if
		
		FIX16 dc = new FIX16 ((float)(ec - sc)/(ex - sx));
		int drawc = sc;
		FIX16 addc = new FIX16();
		addc .Data = 0;
		//边界检查
		if (y > this .ScreenHSub1 || y < 0)
			return;
		if (sx > this .ScreenWSub1)
			return ;
		
		if (sx < 0)
			sx = 0;
		if (ex > this .ScreenWSub1)
			ex = this .ScreenWSub1;
		
		nt = this.RowIndex[y] + sx ;
		for (int i = sx ; i <= ex ; i++)
		{
			this .ScreenBuffer [nt] = drawc + (addc.Data >> FIX16.FIX16_SHIFT);
			nt ++;
			addc.Data += dc.Data ;
		}//for 
	}
	/**
	 * 渲染三角形
	 * @param poly
	 */
	public void DrawPoly (Poly_4D poly)
	{
		int temp ;
		//顶点索引
		int Index1 = poly.VIndex[0] , Index2 = poly.VIndex[1] ,
		Index3 = poly.VIndex[2];
		//顶点排序
		if (poly.Tlist[Index1].Pos.M[1] > poly.Tlist[Index2].Pos.M[1])
		{
			temp = Index1;
			Index1 = Index2;
			Index2 = temp;
		}
		if (poly.Tlist[Index1].Pos.M[1] > poly.Tlist[Index3].Pos.M[1])
		{
			temp = Index1;
			Index1 = Index3;
			Index3 = temp;
		}
		if (poly.Tlist[Index3].Pos.M[0] > poly.Tlist[Index2].Pos.M[0])
		{
			temp = Index2;
			Index2 = Index3;
			Index3 = temp;
		}
		FIX16 x1 = new FIX16 (poly.Tlist[Index1].Pos.M[0]);
		FIX16 y1 = new FIX16 (poly.Tlist[Index1].Pos.M[1]);
		FIX16 x2 = new FIX16 (poly.Tlist[Index2].Pos.M[0]);
		FIX16 y2 = new FIX16 (poly.Tlist[Index2].Pos.M[1]);
		FIX16 x3 = new FIX16 (poly.Tlist[Index3].Pos.M[0]);
		FIX16 y3 = new FIX16 (poly.Tlist[Index3].Pos.M[1]);
		
		FIX16 ldx = new FIX16 ((poly.Tlist[Index3].Pos.M[0] - poly.Tlist[Index1].Pos.M[0]) /
				(poly.Tlist[Index3].Pos.M[1] - poly.Tlist[Index1].Pos.M[1]));
		FIX16 rdx = new FIX16 ((poly.Tlist[Index2].Pos.M[0] - poly.Tlist[Index1].Pos.M[0]) /
				(poly.Tlist[Index2].Pos.M[1] - poly.Tlist[Index1].Pos.M[1]));
		
		int drawy , drawsx = x1.FIX16_TO_INT(), drawex = x1 .FIX16_TO_INT();
		int sy = y1.FIX16_TO_INT() , ey , midy ;
		if (y2 .Data > y3 .Data)
		{
			ey = y2 .FIX16_TO_INT();
			midy = y3 .FIX16_TO_INT();
		}
		else 
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -