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

📄 render.cpp

📁 3D游戏场景编辑器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		cpos	=Render_XFormVert(Cam, &box[i]);
		if(cpos.X < x0)	x0	=cpos.X;
		if(cpos.Y < y0)	y0	=cpos.Y;
		if(cpos.Z < z0)	z0	=cpos.Z;
		if(cpos.X > x1)	x1	=cpos.X;
		if(cpos.Y > y1)	y1	=cpos.Y;
		if(cpos.Z > z1)	z1	=cpos.Z;
	}
	cpos	=Render_XFormVert(Cam, pos);
	zval	=(uint32)(cpos.Z * FixedScale);
	
	sx	=(int)((x0 < 0.0f)? 0.0f : (x0 > Cam->Width)? Cam->Width : x0);
	ex	=(int)((x1 < 0.0f)? 0.0f : (x1 > Cam->Width)? Cam->Width : x1);
	sy	=(int)((y0 < 0.0f)? 0.0f : (y0 > Cam->Height)? Cam->Height : y0);
	ey	=(int)((y1 < 0.0f)? 0.0f : (y1 > Cam->Height)? Cam->Height : y1);

	if(!(ex - sx) || !(ey - sy))
	{
		return;
	}

	dxi	=Width / ((int)x1 - (int)x0);
	if(dxi)
	{
		dxf	=Width - ((int)x1 - (int)x0) * dxi;
	}
	else
	{
		dxf	=(int)((((float)Width) / ((int)x1 - (int)x0)) * ((float)Width));
	}
	dyi	=Height / ((int)y1 - (int)y0);
	if(dyi)
	{
		dyf	=Height - ((int)y1 - (int)y0) * dyi;
	}
	else
	{
		dyf	=(int)((((float)Height) / ((int)y1 - (int)y0)) * ((float)Height));
	}
	dyi	*=Width;


	geBitmap_LockForReadNative (bmap, &LockedBitmap, 0, 0);

	src		=((uint16 *)geBitmap_GetBits(LockedBitmap)) + (Height -1) * Width;	// OLD GEDIT

	//	bpal = geBitmap_GetPalette ((geBitmap *)bmap);
//	geBitmap_Palette_GetData (bpal, WorkingPalette, GE_PIXELFORMAT_16BIT_555_BGR, 256);

	zbuf	=Cam->pZBuffer;
	if(*(zbuf + (((sy+((ey-sy)/2)))*Cam->Width + sx+((ex-sx)/2))) > zval)
	{
		for(yroll=0, y=sy;y < ey;y++)
		{
			dst		=((uint16 *)Cam->pBits) + (y * Cam->Width) + sx;
			zbuf	=(uint32 *)(Cam->pZBuffer + (y * Cam->Width) + sx);
			for(xroll=0, xacc=0, x=sx;x < ex;x++)
			{
				//uncomment to get per pixel z (and remove the if above)
				if(/*(*zbuf < zval) && */  (src[xacc] != 0x7fff))
			{
					*dst	= src[xacc];
					*zbuf	=zval;
				}
				dst++;
				zbuf++;
				xacc	+=dxi;	//	old gedit
				xroll	+=dxf;
				if(xroll > Width)
				{
					xroll	-=Width;	//	old gedit
					xacc++;
				}
			}
			src		-=dyi;	//	old gedit
			yroll	+=dyf;	//	old gedit
			if(yroll > Height)
			{
				yroll	-=Height;	
				src		-=Width;	//	old gedit
			}
		}
	}
 	geBitmap_UnLock (LockedBitmap);

  }

void	Render_3DTextureZBufferOutline(ViewVars *Cam, const geVec3d *pos, const geBitmap *bmap, uint32 OutlineColor)
{
	int		x, y, i, Width, Height;
	int		xroll, yroll;
	int		sx, sy, ex, ey;
	geVec3d	cpos;
	uint16	*src;
	uint16	*dst;
	uint32	*zbuf, zval;
	geVec3d box[8];
	geFloat	x0, x1, y0, y1, z0, z1;
	int		dxi, dxf, dyi, dyf, xacc;

//	Bitmap_Palette	bpal;
//	geBoolean		Outlining	=GE_FALSE;

	//get the center position on the screen
	float xPlus8  = (float)(pos->X + 8.0);
	float xMinus8 = (float)(pos->X - 8.0);
	float yPlus8  = (float)(pos->Y + 8.0);
	float yMinus8 = (float)(pos->Y - 8.0);
	float zPlus8  = (float)(pos->Z + 8.0);
	float zMinus8 = (float)(pos->Z - 8.0);
	geBitmap *LockedBitmap;

	x0=y0=z0	=99999.0f;
	x1=y1=z1	=-99999.0f;

	{
		geBitmap_Info info, info2;

		geBitmap_GetInfo (bmap, &info, &info2);
		Width = info.Width;
		Height = info.Height;
	}
//	Width	=Bitmap_GetWidth(bmap);
//	Height	=Bitmap_GetHeight(bmap);

	geVec3d_Set (&box[0], xPlus8,  yPlus8,  zPlus8);
	geVec3d_Set (&box[1], xMinus8, yPlus8,  zPlus8);
	geVec3d_Set (&box[2], xMinus8, yMinus8, zPlus8);
	geVec3d_Set (&box[3], xPlus8,  yMinus8, zPlus8);
	geVec3d_Set (&box[4], xPlus8,  yPlus8,  zMinus8);
	geVec3d_Set (&box[5], xMinus8, yPlus8,  zMinus8);
	geVec3d_Set (&box[6], xMinus8, yMinus8, zMinus8);
	geVec3d_Set (&box[7], xPlus8,  yMinus8, zMinus8);
	for(i=0;i < 8;i++)
	{
		cpos	=Render_XFormVert(Cam, &box[i]);
		if(cpos.X < x0)	x0	=cpos.X;
		if(cpos.Y < y0)	y0	=cpos.Y;
		if(cpos.Z < z0)	z0	=cpos.Z;
		if(cpos.X > x1)	x1	=cpos.X;
		if(cpos.Y > y1)	y1	=cpos.Y;
		if(cpos.Z > z1)	z1	=cpos.Z;
	}
	cpos	=Render_XFormVert(Cam, pos);
	zval	=(uint32)(cpos.Z * FixedScale);
	
	sx	=(int)((x0 < 0.0f)? 0.0f : (x0 > Cam->Width)? Cam->Width : x0);
	ex	=(int)((x1 < 0.0f)? 0.0f : (x1 > Cam->Width)? Cam->Width : x1);
	sy	=(int)((y0 < 0.0f)? 0.0f : (y0 > Cam->Height)? Cam->Height : y0);
	ey	=(int)((y1 < 0.0f)? 0.0f : (y1 > Cam->Height)? Cam->Height : y1);

	if(!(ex - sx) || !(ey - sy))
	{
		return;
	}

	dxi	=Width / ((int)x1 - (int)x0);
	if(dxi)
	{
		dxf	=Width - ((int)x1 - (int)x0) * dxi;
	}
	else
	{
		dxf	=(int)((((float)Width) / ((int)x1 - (int)x0)) * ((float)Width));
	}
	dyi	=Height / ((int)y1 - (int)y0);
	if(dyi)
	{
		dyf	=Height - ((int)y1 - (int)y0) * dyi;
	}
	else
	{
		dyf	=(int)((((float)Height) / ((int)y1 - (int)y0)) * ((float)Height));
	}
	dyi	*=Width;

	geBitmap_LockForReadNative (bmap, &LockedBitmap, 0, 0);

	src		=((uint16 *)geBitmap_GetBits(LockedBitmap)) + (Height -1) * Width;
//	Bitmap_GetPalette(bmap,  &bpal);
	zbuf	=Cam->pZBuffer;
	if(*(zbuf + (((sy+((ey-sy)/2)))*Cam->Width + sx+((ex-sx)/2))) > zval)
	{
		for(yroll=0, y=sy;y < ey;y++)
		{
			dst		=((uint16 *)Cam->pBits) + (y * Cam->Width) + sx;
			zbuf	=(uint32 *)(Cam->pZBuffer + (y * Cam->Width) + sx);
			for(xroll=0, xacc=0, x=sx;x < ex;x++)
			{
				//uncomment to get per pixel z (and remove the if above)
				if(/*(*zbuf < zval) && */(src[xacc] != 0x7fff))
				{
/*					if(Outlining)
					{
						*dst	=((uint16)(((bpal.bpEntries[*(src + xacc)].bpeB)>>3)
									| ((bpal.bpEntries[*(src + xacc)].bpeG & 0xf8)<<2)
									| ((bpal.bpEntries[*(src + xacc)].bpeR & 0xf8)<<7)));
						*zbuf	=zval;
					}
					else
					{
						*(dst-1)=(((OutlineColor & 0x1f) + (*(dst-1) & 0x1f))>>1)
								|((((OutlineColor & 0x1f00)>>7) + (*(dst-1) & 0x3e0))>>1)
								|((((OutlineColor & 0x1f0000)>>14) + (*(dst-1) & 0x7c00))>>1);
						*dst	=(((OutlineColor & 0xff) + bpal.bpEntries[*(src + xacc)].bpeB)>>4)
								|(((((OutlineColor & 0xff00)>>8) + bpal.bpEntries[*(src + xacc)].bpeG)>>4)<<5)
								|(((((OutlineColor & 0xff0000)>>16) + bpal.bpEntries[*(src + xacc)].bpeR)>>4)<<10);
*/						*dst	=((uint16)(((OutlineColor & 0xf8)<<7) | ((OutlineColor & 0xf800)>>6) | ((OutlineColor & 0xf80000)>>19)));
						*zbuf	=zval;

//						Outlining	=GE_TRUE;
//					}
				}
/*				else if(Outlining)
				{
					Outlining	=GE_FALSE;
					*(dst-1)=(((OutlineColor & 0x1f) + (*(dst-1) & 0x1f))>>1)
							|((((OutlineColor & 0x1f00)>>7) + (*(dst-1) & 0x3e0))>>1)
							|((((OutlineColor & 0x1f0000)>>14) + (*(dst-1) & 0x7c00))>>1);
					*(dst)	=(((OutlineColor & 0x1f) + (*(dst) & 0x1f))>>1)
							|((((OutlineColor & 0x1f00)>>7) + (*(dst) & 0x3e0))>>1)
							|((((OutlineColor & 0x1f0000)>>14) + (*(dst) & 0x7c00))>>1);
				}
*/				dst++;
				zbuf++;
				xacc	+=dxi;
				xroll	+=dxf;
				if(xroll > Width)
				{
					xroll	-=Width;
					xacc++;
				}
			}
			src		-=dyi;
			yroll	+=dyf;
			if(yroll > Height)
			{
				yroll	-=Height;
				src		-=Width;
			}
		}
	}
	geBitmap_UnLock (LockedBitmap);
}

BOOL Render_PointInFrustum(ViewVars *Cam, geVec3d *v)
{
	int		i;

	for(i=0;i < 4;i++)
		if((geVec3d_DotProduct(v, &Cam->FrustPlanes[i].Normal)-Cam->FrustPlanes[i].Dist) < 0.0f)
			return	FALSE;

	return	TRUE;
}

geFloat Render_ViewDeltaToRadians
(
	const ViewVars *v, 
	const float dx
)
{
	return (dx)*(ONE_OVER_2PI/Render_GetXScreenScale (v));
}

void Render_ViewDeltaToRotation
	(
	  const ViewVars *v, 
	  const float dx, 
	  geVec3d *VecRotate
	)
{
	float RotationRads;

	assert (v != NULL);
	assert (VecRotate != NULL);

	RotationRads = (dx)*(ONE_OVER_2PI/Render_GetXScreenScale (v));
	switch (v->ViewType)
	{
		case VIEWTOP :	// +dx = negative rotation about Y
			geVec3d_Set (VecRotate, 0.0f, -RotationRads, 0.0f);
			break;
		case VIEWFRONT :  // +dx = negative rotation about Z
			//disable roll 
			geVec3d_Set (VecRotate, 0.0f, 0.0f, -RotationRads);
			break;
		case VIEWSIDE :	// +dx = positive rotation about X
			geVec3d_Set (VecRotate, RotationRads, 0.0f, 0.0f);
			break;
		default :
			assert (0);  // can't happen!
	}
}

// Return world position at center of view
geVec3d		Render_GetViewCenter (const ViewVars *v)
{
	geVec3d TopLeft;
	geVec3d BottomRight;
	geVec3d Center;

	Render_ViewToWorld (v, 0, 0, &TopLeft);
	Render_ViewToWorld (v, v->Width-1, v->Height-1, &BottomRight);
	geVec3d_Add (&TopLeft, &BottomRight, &Center);
	geVec3d_Scale (&Center, 0.5f, &Center);
	return Center;
}


void Render_ViewToWorld(const ViewVars *v, const int x, const int y, geVec3d *wp)
/*
  XY view coordinate transformed to world coordinate, depending on view.

    Mouse Coordinates
    -----------------

		   |
		   |
 		   |
	-------+-------> +X
		   |
		   |
		   |
		   +Y


        Top View				  Front View				   Side View
  	    --------				  ----------				   ---------
									   +Y						   +Y
		   |						   |						   |
		   |						   |						   |
		   |						   |						   |
	-------+-------> +X			-------+-------> +X			-------+-------> +Z
		   |						   |						   |
		   |						   |						   |
		   |						   |						   |
  		   +Z
*/
{
	geFloat	ZoomInv=1.0f / v->ZoomFactor;

	switch (v->ViewType)
	{
		case VIEWTOP :
		{
			geVec3d_Set (wp, (x - v->XCenter), 0.0f, (y - v->YCenter));
			geVec3d_Scale (wp, ZoomInv, wp);
			geVec3d_Add (wp, &v->CamPos, wp);
			break;
		}
		case VIEWFRONT :
		{

			geVec3d_Set (wp, (x - v->XCenter), -(y - v->YCenter), 0.0f);
			geVec3d_Scale (wp, ZoomInv, wp);
			geVec3d_Add (wp, &v->CamPos, wp);
			break;
		}
		case VIEWSIDE :
		{
			geVec3d_Set (wp, 0.0f, -(y - v->YCenter), (x - v->XCenter));
			geVec3d_Scale (wp, ZoomInv, wp);
			geVec3d_Add (wp, &v->CamPos, wp);
			break;
		}
		default :
		{
			geVec3d_Set 
			(
				wp,
				-(x -v->XCenter)*(v->MaxScreenScaleInv), 
				-(y -v->YCenter)*(v->MaxScreenScaleInv), 
				1.0f
			);
			geVec3d_Normalize(wp);
			break;
		}
	}
}

POINT Render_OrthoWorldToView(const ViewVars *v, geVec3d const *wp)
{
	POINT	sc = {0, 0};
	geVec3d ptView;
	switch (v->ViewType)
	{
		case VIEWTOP :
		{
			geVec3d_Subtract (wp, &v->CamPos, &ptView);
			geVec3d_Scale (&ptView, v->ZoomFactor, &ptView);
			sc.x = (int)(v->XCenter + ptView.X);
			sc.y = (int)(v->YCenter + ptView.Z);
			break;
		}
		case VIEWFRONT :
		{
			geVec3d_Subtract (wp, &v->CamPos, &ptView);
			geVec3d_Scale (&ptView, v->ZoomFactor, &ptView);
			sc.x = (int)(v->XCenter + ptView.X);
			sc.y = (int)(v->YCenter - ptView.Y);
			break;
		}
		case VIEWSIDE :
		{
			geVec3d_Subtract (wp, &v->CamPos, &ptView);
			geVec3d_Scale (&ptView, v->ZoomFactor, &ptView);
			sc.x = (int)(v->XCenter + ptView.Z);
			sc.y = (int)(v->YCenter - ptView.Y);
			break;
		}
		default :
//			assert (0);	// bad view type
			// I should be able to assert here, but I can't.
			// somebody is calling this function for the rendered
			// view.  Bad stuff, really, but I don't have time to change it.
			break;
	}
	return sc;
}

void Render_RenderOrthoGridFromSize(ViewVars *v, geFloat Interval, HDC ViewDC)
{
	geVec3d		ystep, xstep, Delt, Delt2;
	//, Test={1.0f, 1.0f, 1.0f};
	int			i, cnt, xaxis, yaxis, inidx;
	static int axidx[3][2]={ 2, 1, 0, 2, 0, 1 };
	geFloat	gsinv;//, sy;
	Box3d ViewBox;
	POINT		sp;

	inidx	=(v->ViewType>>3)&0x3;

	xaxis	=axidx[inidx][0];
	yaxis	=axidx[inidx][1];


	Render_ViewToWorld(v, Units_Round(-Interval), Units_Round(-Interval), &Delt);
	Render_ViewToWorld(v, Units_Round(v->Width+Interval), Units_Round(v->Height+Interval), &Delt2);

	Box3d_Set 
		(
		  &ViewBox,
		  Delt.X, Delt.Y, Delt.Z,
		  Delt2.X, Delt2.Y, Delt2.Z
		);
	VectorToSUB(ViewBox.Min, inidx)	=-FLT_MAX;
	VectorToSUB(ViewBox.Max, inidx)	=FLT_MAX;

	//snap viewmin and viewmax to grid
	gsinv	=1.0f/(geFloat)Interval;
	for(i=0;i<3;i++)
	{
		VectorToSUB(ViewBox.Min, i)	=(geFloat) ((int)(VectorToSUB(ViewBox.Min, i)*gsinv))*Interval;
		VectorToSUB(ViewBox.Max, i)	=(geFloat) ((int)(VectorToSUB(ViewBox.Max, i)*gsinv))*Interval;
	}

	geVec3d_Copy(&VecOrigin, &xstep);
	geVec3d_Copy(&VecOrigin, &ystep);
	VectorToSUB(ystep, yaxis)	=(geFloat)Interval;
	VectorToSUB(xstep, xaxis)	=(geFloat)Interval;

	// horizontal lines
	geVec3d_Copy(&ViewBox.Min, &Delt);
	geVec3d_Copy(&ViewBox.Min, &Delt2);
	VectorToSUB(Delt2, xaxis)	=VectorToSUB(ViewBox.Max, xaxis);
	cnt	=Units_Round((VectorToSUB(ViewBox.Max, yaxis) - VectorToSUB(ViewBox.Min, yaxis))*gsinv);
	for(i=0;i <= cnt;i++)
	{
		sp	=Render_OrthoWorldToView(v, &Delt);
		MoveToEx(ViewDC, 0, sp.y, NULL);
		sp	=Render_OrthoWorldToView(v, &Delt2);
		LineTo(ViewDC, v->Width, sp.y);
		geVec3d_Add(&Delt, &ystep, &Delt);
		geVec3d_Add(&Delt2, &ystep, &Delt2);
	}

	// vertical lines
	geVec3d_Copy(&ViewBox.Min, &Delt);
	geVec3d_Copy(&ViewBox.Min, &Delt2);
	VectorToSUB(Delt2, yaxis)	=VectorToSUB(ViewBox.Max, yaxis);
	cnt	=Units_Round((VectorToSUB(ViewBox.Max, xaxis) - VectorToSUB(ViewBox.Min, xaxis))*gsinv);
	for(i=0;i <= cnt;i++)
	{
		sp	=Render_OrthoWorldToView(v, &Delt);
		MoveToEx(ViewDC, sp.x, 0, NULL);
		sp	=Render_OrthoWorldToView(v, &Delt2);
		LineTo(ViewDC, sp.x, v->Height);
		geVec3d_Add(&Delt, &xstep, &Delt);
		geVec3d_Add(&Delt2, &xstep, &Delt2);
	}
}

void Render_RenderBrushSelFacesOrtho(ViewVars *Cam, Brush *b, HDC ViewDC)
{
	int	i, j;

	if(!b)
		return;

	for(i=0;i < Brush_GetNumFaces(b);i++)
	{
		Face			*f		=Brush_GetFace(b, i);
		const geVec3d	*pnts	=Face_GetPoints(f);

		if(!Face_IsSelected(f))
			continue;

		for(j=0;j < Face_GetNumPoints(f);j++)
		{
			plist[j]	=Render_OrthoWorldToView(Cam, &pnts[j]);
		}
		plist[j]	=Render_OrthoWorldToView(Cam, &pnts[0]);
		Polyline(ViewDC, plist, j+1);
	}
}

void Render_RenderBrushSheetFacesOrtho(ViewVars *Cam, Brush *b, HDC ViewDC)
{
	int				j;
	Face			*f;
	const geVec3d	*pnts;

	if(!b)
	{
		return;
	}

	f		=Brush_GetFace(b, 0);
	pnts	=Face_GetPoints(f);

	for(j=0;j < Face_GetNumPoints(f);j++)
	{
		plist[j]	=Render_OrthoWorldToView(Cam, &pnts[j]);
	}
	plist[j]	=Render_OrthoWorldToView(Cam, &pnts[0]);
	Polyline(ViewDC, plist, j+1);
}
/*
void Render_RenderBrushHintFacesOrtho(ViewVars *Cam, Brush *b, HDC ViewDC)
{
	int	i, j;

	if(!b)
		return;

	for(i=0;i < b->bd.NumFaces;i++)
	{
		if(!(b->Faces[i].Flags & HINT))
			continue;

		for(j=0;j < b->Faces[i].NumPoints;j++)
		{
			plist[j]	=Render_OrthoWorldToView(Cam, &b->Faces[i].Points[j]);

⌨️ 快捷键说明

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