Exemplo n.º 1
0
void CMD2Model::RenderFrame( void )
{
	static vec3_t	vertlist[ MAX_MD2_VERTS ];	// interpolated vertices
	int				*ptricmds = m_glcmds;		// pointer on gl commands


	// reverse the orientation of front-facing
	// polygons because gl command list's triangles
	// have clockwise winding
	glPushAttrib( GL_POLYGON_BIT );
	glFrontFace( GL_CW );

	// enable backface culling
	glEnable( GL_CULL_FACE );
	glCullFace( GL_BACK );


	// process lighting
	ProcessLighting();

	// interpolate
	Interpolate( vertlist );

	// bind model's texture
	glBindTexture( GL_TEXTURE_2D, m_texid );


	// draw each triangle!
	while( int i = *(ptricmds++) )
	{
		if( i < 0 )
		{	
			glBegin( GL_TRIANGLE_FAN );
			i = -i;
		}
		else
		{
			glBegin( GL_TRIANGLE_STRIP );
		}


		for( /* nothing */; i > 0; i--, ptricmds += 3 )
		{
			// ptricmds[0] : texture coordinate s
			// ptricmds[1] : texture coordinate t
			// ptricmds[2] : vertex index to render

			float l = shadedots[ m_lightnormals[ ptricmds[2] ] ];

			// set the lighting color
			glColor3f( l * lcolor[0], l * lcolor[1], l * lcolor[2] );

			// parse texture coordinates
			glTexCoord2f( ((float *)ptricmds)[0], ((float *)ptricmds)[1] );

			// parse triangle's normal (for the lighting)
			// >>> only needed if using OpenGL lighting
			glNormal3fv( anorms[ m_lightnormals[ ptricmds[2] ] ] );

			// draw the vertex
			glVertex3fv( vertlist[ ptricmds[2] ] );
		}

		glEnd();
	}

	glDisable( GL_CULL_FACE );
	glPopAttrib();
}
Exemplo n.º 2
0
//if usezbuffer = false. we use 1/z buffer
void CRenderObjectsManager:: Render(bool bLighting,bool bbackfaceremove,bool usezbuffer)
{
	//convert to world coordinate system
	Model2World();
	

	int i = 0;
	int j = 0;
	////back face removal
	{
		for( i = 0; i < m_RenderList.size();++i)
		{
			CRenderObject *pObj = m_RenderList[i];
			POLYGONLIST &polygonlist = pObj->m_PolyGonList;
			int polygonsize = polygonlist.size();
			for( j = 0; j < polygonsize;++j)
			{
				POLYGON &polygon = polygonlist[j];
				if(bbackfaceremove)
				{
					VERTICESLIST &pointlist = pObj->m_translateverticesList;
					Vector4D p0p1(pointlist[polygon.v[1]].vertex-pointlist[polygon.v[0]].vertex);
					Vector4D p0p2(pointlist[polygon.v[2]].vertex-pointlist[polygon.v[0]].vertex);
					Vector4D normal = p0p1.CrossProduct(p0p2);
					Vector4D viewvec(m_pCam->m_pos-pointlist[polygon.v[0]].vertex);
					float dp = viewvec.DotProduct(normal);
					if(dp <0)
					{
						polygon.state |= POLYGON_BACKFACEREMOVE;
					}
				}
				else
				{
					polygon.state &= ~POLYGON_BACKFACEREMOVE;
				}
				
			}
		}

	}
	
	
	//Lighting handle
	for( i = 0; i < m_RenderList.size();++i)
	{
			CRenderObject *pObj = m_RenderList[i];
			POLYGONLIST &polygonlist = pObj->m_PolyGonList;
			int polygonsize = polygonlist.size();
			for( j = 0; j < polygonsize;++j)
			{
				POLYGON &polygon = polygonlist[j];
				if(polygon.state & POLYGON_BACKFACEREMOVE) continue;
				if(bLighting)
				 polygon.state |= POLYGON_LIGHTING_PROCESSED;
				else
				{
					polygon.state &= ~POLYGON_LIGHTING_PROCESSED;
					continue;
				}
				VERTICESLIST &pointlist = pObj->m_translateverticesList;
				ProcessLighting(pObj,polygon);
			}
	}
	//convert them to the view,screen
	Matrix mat = m_pCam->GetWorldToCameraMatrix();
	mat *= m_pCam->GetCameraToProjectionMatrix();
	mat *=m_pCam->GetProjectionToScreenMatrix();
	//4d   coordinate .need to convert it to 3d
	
	for( i = 0; i < m_RenderList.size();++i)
	{
		CRenderObject *pObj = m_RenderList[i];
		VERTICESLIST & Translatedvertices = pObj->m_translateverticesList;
		for( j = 0; j < Translatedvertices.size();++j)
		{
			float z  = Translatedvertices[j].vertex.z;
			Translatedvertices[j].vertex *= mat;
			Translatedvertices[j].vertex *= 1/Translatedvertices[j].vertex.w ;
			Translatedvertices[j].vertex.z =z;
		}
	}
	//sort them


	for( i = 0; i < m_RenderList.size();++i)
	{
		CRenderObject *pObj = m_RenderList[i];
		POLYGONLIST &polygonlist = pObj->m_PolyGonList;
		int polygonsize = polygonlist.size();
		for( j = 0; j < polygonsize;++j)
		{
			POLYGON &polygon = polygonlist[j];
			if(polygon.state & POLYGON_BACKFACEREMOVE) continue;
			VERTICESLIST &pointlist = pObj->m_translateverticesList;
			




			/************************************************************************/
			/* Draw wireframe polygon                                                                    */
			/************************************************************************/ 
			/*line(pointlist[polygon.v[0]].x,pointlist[polygon.v[0]].y,
				pointlist[polygon.v[1]].x,pointlist[polygon.v[1]].y,
				RGB(0,255,0));
			line(pointlist[polygon.v[1]].x,pointlist[polygon.v[1]].y,
				pointlist[polygon.v[2]].x,pointlist[polygon.v[2]].y,
				RGB(0,255,0));
			line(pointlist[polygon.v[2]].x,pointlist[polygon.v[2]].y,
				pointlist[polygon.v[0]].x,pointlist[polygon.v[0]].y,
				RGB(0,255,0));*/
			//or
			//DrawWireFramePolygon(pointlist[polygon.v[0]],pointlist[polygon.v[1]],
		    //                pointlist[polygon.v[2]],RGB(0,0,0));
			/************************************************************************/
			/* End wireframe polygon                                                                    */
			/************************************************************************/


			/************************************************************************/
			/* Solid polygon                                                                    */
			/************************************************************************/
			//DrawSolidPolygon(pointlist[polygon.v[0]],pointlist[polygon.v[1]],
			       //       pointlist[polygon.v[2]],RGB(0,255,0));
			/************************************************************************/
			/* End Solid Polygon                                                                    */
			/************************************************************************/

			/************************************************************************/
			/* Lighting processing                                                                    */
			/************************************************************************/
			   if((polygon.state & SHADE_MODEL_CONSTANT) && !(polygon.state & OBJECT_HAS_TEXTURE))
				DrawSolidPolygon(pointlist[polygon.v[0]].vertex,
				                 pointlist[polygon.v[1]].vertex,
					             pointlist[polygon.v[2]].vertex,
						         polygon.state & POLYGON_LIGHTING_PROCESSED ?
								 polygon.ret_color.rgba:polygon.color.rgba);
			   else if( polygon.state & OBJECT_HAS_TEXTURE)
			   {
				   if(usezbuffer)
				     RenderTextureTriangle(pObj,polygon);
				   else
					 DrawTextureTriangle_invz(polygon,pObj);
			   }
			   else
			   {
				   DrawWireFramePolygon(pointlist[polygon.v[0]].vertex,pointlist[polygon.v[1]].vertex,
					   pointlist[polygon.v[2]].vertex,polygon.color.rgba);
			   }
		}
	}


}