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

📄 render.c

📁 著名物理引擎Hawk的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
					int type = surf->textype;

					if(type&SURF_SKY)
					{
						skytexture = TRUE;
						continue;
					}
					if((type&SURF_NODRAW) && !(type&SURF_SKY))
						continue;
					if(type&SURF_WARP)
					{
						warpedtexture = TRUE;
						continue;
					}
					if(type&SURF_TRANS66)
					{
						transtexture = TRUE;
						continue;
					}
					if(type&SURF_TRANS33)
					{
						transtexture = TRUE;
						continue;
					}
					if(texture != lasttexture)
					{
						graphUseTexture(texture);
						lasttexture = surf->texture;
					}
					surfdrawn++;
					graphDraw(surf->mode, &Level.points[0], first, surf->numpoints);
				}
			}
		}
		if(!warpedtexture)
			return;
		graphBlend(FALSE);
		graphTextureFunc(G_REPLACE);
		for(i=0;i<Level.nsurf;i++) /* draw warped normal textures */
		{
			if(Level.surflist[i])
			{
				SURFACE		*surf = &Level.surf[i];
				int			first = surf->firstpoint;
				int			last = surf->firstpoint + surf->numpoints;
				int			texture = surf->texture;
				int			ntextures = surf->ntextures;

				if(ntextures)
					texture = renGetAnimation(texture, ntextures);
				if(texture)
				{
					int type = surf->textype;

					if(type&SURF_SKY)
						continue;
					if(type&SURF_NODRAW)
						continue;
					if((type&(SURF_TRANS66|SURF_TRANS33)))
						continue;
					if(!(type&SURF_WARP))
						continue;
					if(texture != lasttexture)
					{
						graphUseTexture(texture);
						lasttexture = texture;
					}
					surfdrawn++;
					graphDraw(surf->mode, &Level.points[0], first, surf->numpoints);
				}
			}
		}
	}
	else
	/* flat shade with varied colors */
	{
		graphTexture(FALSE);
		for(i=0;i<Level.nsurf;i++)
		{
			if(Level.surflist[i])
			{
				SURFACE		*surf = &Level.surf[i];
				int			first = surf->firstpoint;
				int			last = surf->firstpoint + surf->numpoints;

				if(Engine.light)
					graphUseColor(255, 255, 255, 255);
				else
					graphUseColor((unsigned char)((i) & 0xff)
						, (unsigned char)(((i) * (i)) & 0xff)
						, (unsigned char)(0xff - (((i) + 0x80) & 0xff))
						, 255);

				surfdrawn++;
				graphDraw(surf->mode, &Level.points[0], first, surf->numpoints);
			}
		}
	}
}

void renDrawTransparent(void)
{
	int		i, lasttexture = 0;

	graphBeginObject();
	graphTexture(TRUE);
	graphUseColor(255, 255, 255, 255);
	if(Engine.texture)	/* if texturing is enabled */
	{
		graphBlend(TRUE);
		graphBlendFunc(G_ONE_MINUS_SRC_ALPHA, G_SRC_ALPHA);
		graphTextureFunc(G_REPLACE);
		for(i=0;i<Level.nsurf;i++) /* draw translucent textures */
		{
			if(Level.surflist[i])
			{
				SURFACE		*surf = &Level.surf[i];
				int			first = surf->firstpoint;
				int			last = surf->firstpoint + surf->numpoints;
				int			texture = surf->texture;
				int			ntextures = surf->ntextures;

				if(ntextures)
					texture = renGetAnimation(texture, ntextures);
				if(texture)
				{
					int type = surf->textype;

					if(type&SURF_SKY)
						continue;
					if(type&SURF_NODRAW)
						continue;
					if(!(type&(SURF_TRANS66|SURF_TRANS33)))
						continue;
					if(type&SURF_WARP)
						continue;
					if(texture != lasttexture)
					{
						graphUseTexture(texture);
						lasttexture = surf->texture;
					}
					surfdrawn++;
					graphDraw(surf->mode, &Level.points[0], first, surf->numpoints);
				}
			}
		}

		if(!warpedtexture)
			return;

		for(i=0;i<Level.nsurf;i++) /* draw warped translucent textures */
		{
			if(Level.surflist[i])
			{
				SURFACE		*surf = &Level.surf[i];
				int			first = surf->firstpoint;
				int			last = surf->firstpoint + surf->numpoints;
				int			texture = surf->texture;
				int			ntextures = surf->ntextures;

				if(ntextures)
					texture = renGetAnimation(texture, ntextures);
				if(texture)
				{
					int type = surf->textype;

					if(type&SURF_SKY)
						continue;
					if(type&SURF_NODRAW)
						continue;
					if(!(type&(SURF_TRANS66|SURF_TRANS33)))
						continue;
					if(!(type&SURF_WARP))
						continue;
					if(texture != lasttexture)
					{
						graphUseTexture(texture);
						lasttexture = texture;
					}
					surfdrawn++;
					graphDraw(surf->mode, &Level.points[0], first, surf->numpoints);
				}
			}
		}
	}
}

BOOL renClipObject(OBJECT *obj, VIEW *view)
{
	vec3_t	    v, mins, maxs;
	float	    d;

	/* check the PVS */
	if(!bspInPVS(obj->origin, view->object->origin))
		return TRUE;

	/* translate bbox vectors */
	mins[X] = obj->mins[X] + obj->origin[X];
	mins[Y] = obj->mins[Y] + obj->origin[Y];
	mins[Z] = obj->mins[Z] + obj->origin[Z];
	maxs[X] = obj->maxs[X] + obj->origin[X];
	maxs[Y] = obj->maxs[Y] + obj->origin[Y];
	maxs[Z] = obj->maxs[Z] + obj->origin[Z];

	/* clip to the right view clipping plane */
	v[X] = (float)(view->clipr[X] > 0) ? maxs[X] : mins[X];
	v[Y] = (float)(view->clipr[Y] > 0) ? maxs[Y] : mins[Y];
	v[Z] = (float)(view->clipr[Z] > 0) ? maxs[Z] : mins[Z];
	VectorSubtract(v, view->cam, v);
	d = DotProduct(v, view->clipr);

	if(d < 0)
		return TRUE;

	/* clip to the left view clipping plane */
	v[X] = (float)(view->clipl[X] > 0) ? maxs[X] : mins[X];
	v[Y] = (float)(view->clipl[Y] > 0) ? maxs[Y] : mins[Y];
	v[Z] = (float)(view->clipl[Z] > 0) ? maxs[Z] : mins[Z];
	VectorSubtract(v, view->cam, v);
	d = DotProduct(v, view->clipl);

	if(d < 0)
		return TRUE;

	/* clip to the top view clipping plane */
	v[X] = (float)(view->clipt[X] > 0) ? maxs[X] : mins[X];
	v[Y] = (float)(view->clipt[Y] > 0) ? maxs[Y] : mins[Y];
	v[Z] = (float)(view->clipt[Z] > 0) ? maxs[Z] : mins[Z];
	VectorSubtract(v, view->cam, v);
	d = DotProduct(v, view->clipt);

	if(d < 0)
		return TRUE;

	/* clip to the bottom view clipping plane */
	v[X] = (float)(view->clipb[X] > 0) ? maxs[X] : mins[X];
	v[Y] = (float)(view->clipb[Y] > 0) ? maxs[Y] : mins[Y];
	v[Z] = (float)(view->clipb[Z] > 0) ? maxs[Z] : mins[Z];
	VectorSubtract(v, view->cam, v);
	d = DotProduct(v, view->clipb);

	return(d < 0);
}

void renDrawObjects(VIEW *view)
{
	OBJECT		*o = &GLevel.objects[0];
	int			frame;
	vec3_t		color;

	graphTexture(TRUE);
	graphBlend(FALSE);
	graphTextureFunc(G_MODULATE);
	do
	{
		if(o->model == NO_MODEL) /* skip if no model info */
		{
			o = o->next;
			continue;
		}
		if(o->animation != NO_ANIMATION)/* update animation */
		{
			animUpdate(o->animation);
			frame = animGetCurrentFrame(o->animation);
		}
		else
			frame = 0;
		if((!(view->type&V_EXTERNAL))&&(o == GLevel.view[0]->object)) /* skip if player and not external view */
		{
			o = o->next;
			continue;
		}
		if(!renClipObject(o, view))
		{
			MODEL	*model = &Level.models[o->model];
			int		comnumber = 0;

			lightGetObject(o, color);
			glColor3fv(color);
			graphBeginObject();
			graphTranslate(o->origin[X], o->origin[Y], o->origin[Z]);

			/* testing */
/*			graphTexture(FALSE);
			graphUseColor(255, 255, 255, 255);
			glBegin(GL_LINE_LOOP);
				glVertex3f(o->mins[X], o->mins[Y], o->mins[Z]);
				glVertex3f(o->maxs[X], o->maxs[Y], o->mins[Z]);
				glVertex3f(o->maxs[X], o->maxs[Y], o->maxs[Z]);
				glVertex3f(o->mins[X], o->mins[Y], o->maxs[Z]);
			glEnd();
			glBegin(GL_LINE_LOOP);
				glVertex3f(o->maxs[X], o->mins[Y], o->mins[Z]);
				glVertex3f(o->mins[X], o->maxs[Y], o->mins[Z]);
				glVertex3f(o->mins[X], o->maxs[Y], o->maxs[Z]);
				glVertex3f(o->maxs[X], o->mins[Y], o->maxs[Z]);
			glEnd();
*/
			graphTexture(TRUE);

			graphRotate(o->angles[X], 1, 0, 0 );
			graphRotate(o->angles[Y], 0, 1, 0 );
			graphRotate(o->angles[Z], 0, 0, 1 );

			/* draw the BSP model */
			if(o->type&O_BSPMODEL)
			{
				int firstsurf = BSP.dmodels[o->model].firstface;
				int lastsurf = firstsurf + BSP.dmodels[o->model].numfaces;
				int i;

				for(i=firstsurf;i<lastsurf;i++)
				{
					SURFACE		*surf = &Level.surf[i];
					int			first = surf->firstpoint;
					int			last = surf->firstpoint + surf->numpoints;
					int			texture = surf->texture;
					int			ntextures = surf->ntextures;

					if(ntextures)
						texture = renGetAnimation(texture, ntextures);
					if(texture)
					{
						graphUseTexture(texture);
						surfdrawn++;
						graphDraw(surf->mode, &Level.points[0], first, surf->numpoints);
					}
				}
				o = o->next;
				continue;
			}
			/* else draw the md2 model */
			graphUseTexture(model->texture[0] + o->skin);
			while(model->glcmds[comnumber])
			{
				int			count = model->glcmds[comnumber++];
				vec3_t		scale, translate;
				daliasframe_t	*f = (daliasframe_t	*)&model->frames[frame * model->framesize];
				dtrivertx_t	*verts = f->verts;
		
				VectorCopy(f->scale, scale);
				VectorCopy(f->translate, translate);

				if(count>0)
					glBegin(GL_TRIANGLE_STRIP);
				else
				{
					glBegin(GL_TRIANGLE_FAN);
					count = -(count);
				}
				while(count--)
				{
					vec3_t	v;
					vec_t	s = *(float *)&model->glcmds[comnumber++];
					vec_t	t = *(float *)&model->glcmds[comnumber++];
					int		vindex = model->glcmds[comnumber++];
					byte	bX = verts[vindex].v[X];

					glTexCoord2f(s, t);
					v[X] = (float)verts[vindex].v[X] * scale[X] + translate[X];
					v[Y] = (float)verts[vindex].v[Y] * scale[Y] + translate[Y];
					v[Z] = (float)verts[vindex].v[Z] * scale[Z] + translate[Z];
					glVertex3fv(v);
				}
				glEnd();
			}
			if(o->weapon == NO_MODEL)
			{
				o = o->next;
				continue;
			}
			model = &Level.models[o->weapon];
			comnumber = 0;
			graphUseTexture(model->texture[0]);
			while(model->glcmds[comnumber])
			{
				int			count = model->glcmds[comnumber++];
				vec3_t		scale, translate;
				daliasframe_t	*f = (daliasframe_t	*)&model->frames[frame * model->framesize];
				dtrivertx_t	*verts = f->verts;
		
				VectorCopy(f->scale, scale);
				VectorCopy(f->translate, translate);

				if(count>0)
					glBegin(GL_TRIANGLE_STRIP);
				else
				{
					glBegin(GL_TRIANGLE_FAN);
					count = -(count);
				}
				while(count--)
				{
					vec3_t	v;
					vec_t	s = *(float *)&model->glcmds[comnumber++];
					vec_t	t = *(float *)&model->glcmds[comnumber++];
					int		vindex = model->glcmds[comnumber++];
					byte	bX = verts[vindex].v[X];

					glTexCoord2f(s, t);
					v[X] = (float)verts[vindex].v[X] * scale[X] + translate[X];
					v[Y] = (float)verts[vindex].v[Y] * scale[Y] + translate[Y];
					v[Z] = (float)verts[vindex].v[Z] * scale[Z] + translate[Z];
					glVertex3fv(v);
				}
				glEnd();
			}
		}
		o = o->next;
	}while(o);
}

void renDrawParticles(VIEW *view)
{
	graphBeginObject();
	if(nparticles) /* draw particles */
	{
		graphDrawParticles();
	}
}

void renBlendScreen(unsigned char r, unsigned char g,
					unsigned char b, unsigned char a)
{
	int				x = Engine.w;
	int				y = Engine.h;
	TPOINT			p[4];

	memset(&p, 0, sizeof(p));

	graphTexture(FALSE);
	graphUseColor(r, g, b, a);
	graphBlend(TRUE);
	graphBlendFunc(G_SRC_ALPHA, G_ONE_MINUS_SRC_ALPHA);

	p[0].v[X] = 0;
	p[0].v[Y] = 0;

	p[1].v[X] = 0;
	p[1].v[Y] = y ;

	p[2].v[X] = x;
	p[2].v[Y] = y ;

	p[3].v[X] = x;
	p[3].v[Y] = 0;

	graphDraw(G_TRIANGLE_FAN, &p[0], 0, 4);
}

void HAWK_Render(void)
{
	int		i = 0;
	int		nviews = GLevel.nview;

	GLevel.skyangle += GLevel.skyrotate * Engine.curframes / GAME_TICKS;
	if(GLevel.skyangle > 360.0f)
		GLevel.skyangle -= 360.0f;
	if(GLevel.skyangle < -360.0f)
		GLevel.skyangle += 360.0f;

	graphDepthTest(TRUE);
	graphClearBuffers(G_COLOR_BUFFER_BIT);
	do{
		VIEW	*view = GLevel.view[i];
		OBJECT	*obj = view->object;
		int		w = (int)(view->width * view->scaling);
		int		h = (int)(view->height * view->scaling);
		int		x = (view->width - w)/2 + view->scrX;
		int		y = (view->height - h)/2 + view->scrY;


		graphDepthFunc(G_LEQUAL);
		graphClearMatrix(MODEL_MATRIX|PROJECTION_MATRIX);
		graphSetViewport(view->angle, x, y, w, h);
		graphClearBuffers(G_DEPTH_BUFFER_BIT);

		graphLookAt( view->cam[X], view->cam[Y], view->cam[Z],
					view->cam[X] + view->look[X],
					view->cam[Y] + view->look[Y],
					view->cam[Z] + view->look[Z],
					0, 0, 1);

		renDrawBackground(view);

		renDrawSurfaces(view);
		
		renDrawLightmaps(view);
		
		renDrawObjects(view);
		
		renDrawParticles(view);
		
		renDrawTransparent();
	
	}while(++i != nviews);

	graphClearMatrix(MODEL_MATRIX|PROJECTION_MATRIX);
	graphBegin2D();
	if(nviews >1) /* draw the box around any views */
	{
		graphTexture(FALSE);
		for(i=1;i<nviews;i++)
		{
			VIEW	*view = GLevel.view[i];
			int		j;
			int		w = (int)(view->width * view->scaling);
			int		h = (int)(view->height * view->scaling);
			int		x = (view->width - w)/2 + view->scrX;
			int		y = (view->height - h)/2 + view->scrY;
			TPOINT	p[4];

			memset(&p, 0, sizeof(p));
			for(j=0;j<2;j++)
			{
				int		o = j;
				unsigned char	c = (j==0?(0):(255));

				graphUseColor(c, c, c, 255);

				p[0].v[X] = x + o;
				p[0].v[Y] = y + o;

				p[1].v[X] = x + w - o;
				p[1].v[Y] = y + o ;

				p[2].v[X] = x + w - o;
				p[2].v[Y] = y + h - o ;

				p[3].v[X] = x + o;
				p[3].v[Y] = y + h - o;

				graphDraw(G_LINE_LOOP, &p[0], 0, 4);
			}
		}
	}
	if(GLevel.view[0]->needblend) /* draw the colored quad */
	{
		float			*color = GLevel.view[0]->blendcolor;
		unsigned char	r, g, b, a;

		r = (unsigned char)(color[R]*255);
		g = (unsigned char)(color[G]*255);
		b = (unsigned char)(color[B]*255);
		a = (unsigned char)(color[A]*255);
		renBlendScreen(r, g, b, a);
	}

	renDrawMessage(8, 10, 5, "Running on %s OpenGL", Engine.GLrenderer);
	renDrawMessage(8, 10, 20, "fps: %4.2f on HAWK DLL version %2.2f", Engine.fps, Engine.version);
if(0)
{
	renDrawMessage(8, 10, 5, "leafs checked: %d", Engine.leafschecked);
	renDrawMessage(8, 10, 20, "surfs checked: %d", Engine.surfschecked);
	renDrawMessage(8, 10, 35, "angle: %4.2f", GLevel.objects[0].angles[Z]);
	renDrawMessage(8, 10, 35, "animation frame %d", renGetAnimation(0, 32));

	renDrawMessage(8, 10, 5, "Running on %s OpenGL by %s", Engine.GLrenderer, Engine.GLvendor );
	renDrawMessage(8, 10, 20, "fps: %4.2f", Engine.fps);
	renDrawMessage(8, 10, 5, "#Particles: %d", nparticles);
	renDrawMessage(8, 10, 5, "%d polygons drawn", surfdrawn);

	renDrawMessage(8, 10, 5, "Z: %4.2f", GLevel.objects[0].origin[Z]);
	renDrawMessage(8, 10, 20, "Y: %4.2f", GLevel.objects[0].origin[Y]);
	renDrawMessage(8, 10, 35, "X: %4.2f", GLevel.objects[0].origin[X]);
	renDrawMessage(8, 10, 35, "Time : %4.2f", (float)(getTime())/1000);
}
	renLogo();
	if(Engine.mouse && !Engine.paused)
		renDrawCursor();
	if(Engine.sight)
		renDrawSight();
}

⌨️ 快捷键说明

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