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

📄 polygon3d.java

📁 一个java 3D程序的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			for(i = start; i <= end; i++){
				W.set(xLow_transprent[i]-320, -i + 240, Z_length);
				aDotW = A.dot(W);
				bDotW = B.dot(W);
				cDotW = C.dot(W);

				int l = xHigh_transprent[i]-xLow_transprent[i];
				if(l == 0)
					continue;

				A_offset = A.x*l;
				B_offset = B.x*l;
				C_offset = C.x*l;

				cDotWInverse = 1/cDotW;
				X = (int)(aDotW*cDotWInverse*0xffff);
				Y = (int)(bDotW*cDotWInverse*0xffff);
				aDotW+=A_offset;
				bDotW+=B_offset;
				cDotW+=C_offset;
				cDotWInverse = 1/cDotW;
				X1 = (int)(aDotW*cDotWInverse*0xffff);
				Y1 = (int)(bDotW*cDotWInverse*0xffff);
				dx = (X1 - X)/l;
				dy = (Y1 - Y)/l;

				int index = gameData.screenTable[i];
				for(j = xLow_transprent[i], k = 0; j < xHigh_transprent[i]; k++, j++){
					if(gallery.buffer[index+j]){
						textureIndex = (((dx*k + X)>>16)&widthMask) + ((((dy*k + Y)>>16)&heightMask)<<widthBits);
						temp = Texture[textureIndex];
						theIndex = index+j + (temp/scale)*640;
						if(theIndex< 0 || theIndex >= 307200)
							theIndex = index+j;

						texel =  gallery.screen[theIndex];
						r = ((texel & 0xff0000)>>20)*15;
						g = ((texel & 0x00ff00)>>12)*15;
						b = ((texel & 0x0000ff)    );

						gallery.screenBuffer[index+j] = (r<<16) | (g<<8) | b | 0xff000000;
					}
				}
			}
			for(i = start; i <= end; i++){
				int index = gameData.screenTable[i];
				for(j = xLow_transprent[i]; j < xHigh_transprent[i]; j++){
					if(gallery.buffer[index+j]){
						gallery.screen[index+j] = gallery.screenBuffer[index+j];
						gallery.buffer[index+j] = false;
					}
				}
			}

			return;
		}

		double cDotWInverse;

		for(i = start; i <= end; i++){
			W.set(xLow_transprent[i]-320, -i + 240, Z_length);
			aDotW = A.dot(W);
			bDotW = B.dot(W);
			cDotW = C.dot(W);

			int l = xHigh_transprent[i]-xLow_transprent[i];
			if(l == 0)
				continue;

			A_offset = A.x*l;
			B_offset = B.x*l;
			C_offset = C.x*l;

			cDotWInverse = 1/cDotW;
			X = (int)(aDotW*cDotWInverse*0xffff);
			Y = (int)(bDotW*cDotWInverse*0xffff);
			aDotW+=A_offset;
			bDotW+=B_offset;
			cDotW+=C_offset;
			cDotWInverse = 1/cDotW;
			X1 = (int)(aDotW*cDotWInverse*0xffff);
			Y1 = (int)(bDotW*cDotWInverse*0xffff);
			dx = (X1 - X)/l;
			dy = (Y1 - Y)/l;

			int index = gameData.screenTable[i];
			for(j = xLow_transprent[i], k = 0; j < xHigh_transprent[i]; k++, j++){
				if(gallery.buffer[index+j]){
					textureIndex = (((dx*k + X)>>16)&widthMask) + ((((dy*k + Y)>>16)&heightMask)<<widthBits);
					temp = Texture[textureIndex];
					theIndex = index+j + temp*640;
					if(theIndex< 0 || theIndex >= 307200)
						theIndex = index+j;

					texel =  gallery.screen[theIndex];
					r = ((texel & 0xff0000)>>20)*15;
					g = ((texel & 0x00ff00)>>12)*15;
					b = ((texel & 0x0000ff)    );
					gallery.screenBuffer[index+j] = (r<<16) | (g<<8) | b | 0xff000000;;
				}
			}
		}

		for(i = start; i <= end; i++){
			int index = gameData.screenTable[i];
			for(j = xLow_transprent[i]; j < xHigh_transprent[i]; j++){
				if(gallery.buffer[index+j]){
					gallery.screen[index+j] = gallery.screenBuffer[index+j];
					gallery.buffer[index+j] = false;
				}
			}
		}
	}

	public final void draw(){
		findVectorOUV();

		findClipping();

		//apply specular highlight only when it is in gouraud shading mode
		if(gouraudShading){

			findSpecular();
		}

		scanPolygon();

		if(waterSurface){
			if(xLow_transprent == null){
				xLow_transprent = new int[480];
				xHigh_transprent = new int[480];

			}

			for(int i = 0; i < 480; i++){
				xLow_transprent[i] = xLow[i];
				xHigh_transprent[i] = xHigh[i];
			}

			for(int i = start; i <= end; i++){
				int index = gameData.screenTable[i];
				for(int j = xLow_transprent[i]; j < xHigh_transprent[i]; j++){
					if(gallery.screen[index+j] == -134250){
						gallery.buffer[index+j] = true;
					}
				}
			}
			return;
		}

		A_offset = A.x*16;
		B_offset = B.x*16;
		C_offset = C.x*16;

		double Aoffset;
		double Boffset;
		double Coffset;

		double cDotWInverse;

		if(gouraudShading){
			for(int i = start; i <= end; i++){
				W.set(xLow[i]-320, -i + 240, Z_length);
				aDotW = A.dot(W);
				bDotW = B.dot(W);
				cDotW = C.dot(W);

				if(faceVerticalPolygon){
					cDotWInverse = 1/cDotW;
					BigX = (int)(aDotW*cDotWInverse*0xffff);
					BigY = (int)(bDotW*cDotWInverse*0xffff);
					aDotW+=A_offset;
					bDotW+=B_offset;
					cDotW+=C_offset;
					cDotWInverse = 1/cDotW;
					X1 = (int)(aDotW*cDotWInverse*0xffff);
					Y1 = (int)(bDotW*cDotWInverse*0xffff);
					BigDx = X1 - BigX;
					BigDy = Y1 - BigY;
					dx = BigDx>>16;
					dy = BigDy>>16;
				}

				int temp = gameData.screenTable[i];
				int index;
				int ds = 0;
				if(xHigh[i] > xLow[i])
					ds = (s_right[i] - s_left[i])/(xHigh[i] - xLow[i]);
				for(int j = xLow[i], s = s_left[i]; j < xHigh[i]; j+=16, s+=(ds*16)){
					index = j + temp;
					if(xHigh[i] - j < 16){
						if(xHigh[i] - j>0){
							int offset = xHigh[i] - j;
							Aoffset = A.x*offset;
							Boffset = B.x*offset;
							Coffset = C.x*offset;
							if(index + 16 < gallery.screen.length){
								if(gallery.screen[index] != -134250
									&& gallery.screen[index+2] != -134250
									&& gallery.screen[index+4] != -134250
									&& gallery.screen[index+6] != -134250
									&& gallery.screen[index+8] != -134250
									&& gallery.screen[index+10] != -134250
									&& gallery.screen[index+12] != -134250
									&& gallery.screen[index+14] != -134250
									&& gallery.screen[index+15] != -134250
									){
									continue;
								}
							}


							if(faceVerticalPolygon){
								X = BigX >>16;
								Y = BigY >>16;
							}

							if(!faceVerticalPolygon){
								cDotWInverse = 1/cDotW;
								X = (int)(aDotW*cDotWInverse);
								Y = (int)(bDotW*cDotWInverse);
								aDotW+=Aoffset;
								bDotW+=Boffset;
								cDotW+=Coffset;
								cDotWInverse = 1/cDotW;
								X1 = (int)(aDotW*cDotWInverse);
								Y1 = (int)(bDotW*cDotWInverse);
								dx = X1 - X;
								dy = Y1 - Y;
							}


							for(int k = 0, specular = s; k < offset; k++, specular+=ds){
								if(gallery.screen[index] == -134250){
									textureIndex = (((dx*k/offset) + X)&widthMask) + ((((dy*k/offset) + Y)&heightMask)<<widthBits);
									if(textureIndex < 0)
										textureIndex = 0;
									int texel = Texture[textureIndex];
									int r = ((((texel & 0xff0000)>>16) * diffuse_I) >>8) + (specular >> 16);
									int g = ((((texel & 0x00ff00)>>8 ) * diffuse_I) >>8) + (specular >> 16);
									int b = ((((texel & 0x0000ff)    ) * diffuse_I) >>8) + (specular >> 16);
									if(r > 255)
										r = 255;
									if(g > 255)
										g = 255;
									if(b > 255)
										b = 255;

									gallery.screen[index] = (r<<16) | (g<<8) | b | 0xff000000;
								}
								index++;
							}
						}
						break;
					}

					if(gallery.screen[index] != -134250
						&& gallery.screen[index+2] != -134250
						&& gallery.screen[index+4] != -134250
						&& gallery.screen[index+6] != -134250
						&& gallery.screen[index+8] != -134250
						&& gallery.screen[index+10] != -134250
						&& gallery.screen[index+12] != -134250
						&& gallery.screen[index+14] != -134250
						&& gallery.screen[index+15] != -134250
						){
						if(faceVerticalPolygon){
							BigX+=BigDx;
							BigY+=BigDy;
						}else{
							aDotW+=A_offset;
							bDotW+=B_offset;
							cDotW+=C_offset;
						}
						continue;
					}



					if(faceVerticalPolygon){
						X = BigX >>16;
						Y = BigY >>16;
						BigX+=BigDx;
						BigY+=BigDy;
					}

					if(!faceVerticalPolygon){
						cDotWInverse = 1/cDotW;
						X = (int)(aDotW*cDotWInverse);
						Y = (int)(bDotW*cDotWInverse);
						aDotW+=A_offset;
						bDotW+=B_offset;
						cDotW+=C_offset;
						cDotWInverse = 1/cDotW;
						X1 = (int)(aDotW*cDotWInverse);
						Y1 = (int)(bDotW*cDotWInverse);
						dx = X1 - X;
						dy = Y1 - Y;
					}

					for(int k = 0, specular = s; k < 16; k++, specular+=ds){
						if(gallery.screen[index] == -134250){

							textureIndex = (((dx*k>>4) + X)&widthMask) + ((((dy*k>>4) + Y)&heightMask)<<widthBits);

							if(textureIndex < 0)
								textureIndex = 0;
							int texel = Texture[textureIndex];
							int r = ((((texel & 0xff0000)>>16) * diffuse_I) >>8) + (specular >> 16);
							int g = ((((texel & 0x00ff00)>>8 ) * diffuse_I) >>8) + (specular >> 16);
							int b = ((((texel & 0x0000ff)    ) * diffuse_I) >>8) + (specular >> 16);
							if(r > 255)
								r = 255;
							if(g > 255)
								g = 255;
							if(b > 255)
								b = 255;


							gallery.screen[index] = (r<<16) | (g<<8) | b | 0xff000000;
						}
						index++;
					}
				}

			}
		}else{
			for(int i = start; i <= end; i++){
				W.set(xLow[i]-320, -i + 240, Z_length);
				aDotW = A.dot(W);
				bDotW = B.dot(W);
				cDotW = C.dot(W);

				if(faceVerticalPolygon){
					cDotWInverse = 1/cDotW;
					BigX = (int)(aDotW*cDotWInverse*0xffff);
					BigY = (int)(bDotW*cDotWInverse*0xffff);
					aDotW+=A_offset;
					bDotW+=B_offset;
					cDotW+=C_offset;
					cDotWInverse = 1/cDotW;
					X1 = (int)(aDotW*cDotWInverse*0xffff);
					Y1 = (int)(bDotW*cDotWInverse*0xffff);
					BigDx = X1 - BigX;
					BigDy = Y1 - BigY;
					dx = BigDx>>16;
					dy = BigDy>>16;
				}

				int temp = gameData.screenTable[i];
				int index;
				for(int j = xLow[i]; j < xHigh[i]; j+=16){
					index = j + temp;
					if(xHigh[i] - j < 16){
						if(xHigh[i] - j>0){
							int offset = xHigh[i] - j;
							Aoffset = A.x*offset;
							Boffset = B.x*offset;
							Coffset = C.x*offset;
							if(index + 16 < gallery.screen.length){
								if(gallery.screen[index] != -134250
									&& gallery.screen[index+2] != -134250
									&& gallery.screen[index+4] != -134250
									&& gallery.screen[index+6] != -134250
									&& gallery.screen[index+8] != -134250
									&& gallery.screen[index+10] != -134250
									&& gallery.screen[index+12] != -134250
									&& gallery.screen[index+14] != -134250
									&& gallery.screen[index+15] != -134250
									){
									continue;
								}
							}


							if(faceVerticalPolygon){
								X = BigX >>16;
								Y = BigY >>16;
							}

							if(!faceVerticalPolygon){
								cDotWInverse = 1/cDotW;
								X = (int)(aDotW*cDotWInverse);
								Y = (int)(bDotW*cDotWInverse);
								aDotW+=Aoffset;
								bDotW+=Boffset;
								cDotW+=Coffset;
								cDotWInverse = 1/cDotW;
								X1 = (int)(aDotW*cDotWInverse);
								Y1 = (int)(bDotW*cDotWInverse);
								dx = X1 - X;
								dy = Y1 - Y;
							}


							for(int k = 0; k < offset; k++){
								if(gallery.screen[index] == -134250){
									textureIndex = (((dx*k/offset) + X)&widthMask) + ((((dy*k/offset) + Y)&heightMask)<<widthBits);
									if(textureIndex < 0)
										textureIndex = 0;
									int texel = Texture[textureIndex];
									int r = ((((texel & 0xff0000)>>16) * diffuse_I) >>8);
									int g = ((((texel & 0x00ff00)>>8 ) * diffuse_I) >>8);
									int b = ((((texel & 0x0000ff)    ) * diffuse_I) >>8);
									gallery.screen[index] = (r<<16) | (g<<8) | b | 0xff000000;
								}
								index++;
							}
						}
						break;
					}


					if(gallery.screen[index] != -134250
						&& gallery.screen[index+2] != -134250
						&& gallery.screen[index+4] != -134250
						&& gallery.screen[index+6] != -134250
						&& gallery.screen[index+8] != -134250
						&& gallery.screen[index+10] != -134250
						&& gallery.screen[index+12] != -134250
						&& gallery.screen[index+14] != -134250
						&& gallery.screen[index+15] != -134250
						){
						if(faceVerticalPolygon){
							BigX+=BigDx;
							BigY+=BigDy;
						}else{
							aDotW+=A_offset;
							bDotW+=B_offset;
							cDotW+=C_offset;
						}
						continue;
					}



					if(faceVerticalPolygon){
						X = BigX >>16;
						Y = BigY >>16;
						BigX+=BigDx;
						BigY+=BigDy;
					}

					if(!faceVerticalPolygon){
						cDotWInverse = 1/cDotW;
						X = (int)(aDotW*cDotWInverse);
						Y = (int)(bDotW*cDotWInverse);
						aDotW+=A_offset;
						bDotW+=B_offset;
						cDotW+=C_offset;
						cDotWInverse = 1/cDotW;
						X1 = (int)(aDotW*cDotWInverse);
						Y1 = (int)(bDotW*cDotWInverse);
						dx = X1 - X;
						dy = Y1 - Y;
					}

					for(int k = 0; k < 16; k++){
						if(gallery.screen[index] == -134250){

							textureIndex = (((dx*k>>4) + X)&widthMask) + ((((dy*k>>4) + Y)&heightMask)<<widthBits);

							if(textureIndex < 0)
								textureIndex = 0;
							int texel = Texture[textureIndex];
							int r = ((((texel & 0xff0000)>>16) * diffuse_I) >>8);
							int g = ((((texel & 0x00ff00)>>8 ) * diffuse_I) >>8);
							int b = ((((texel & 0x0000ff)    ) * diffuse_I) >>8);
							gallery.screen[index] = (r<<16) | (g<<8) | b | 0xff000000;
						}
						index++;
					}
				}

			}


		}

	}
}

⌨️ 快捷键说明

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