void RenderEntities(void)
{
	int i;
	entity_t* entity;
	matrix_t inv_modelMatrix;
	vec4_t modelSpaceLightPos;
	vec4_t modelSpaceCameraPos;
	matrix_t tmp;
	
	
	renderer.isRenderingShadow = ((renderer.props & PROP_SHADOW) == PROP_SHADOW) ;
	if (renderer.isRenderingShadow)
	{
		glCullFace(GL_FRONT);
		glBindFramebuffer(GL_FRAMEBUFFER,shadowFBOId);
		glClearColor(1.0,1.0,1.0,1.0);
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glClearColor(0.0,0.0,0.0,1.0);
		SRC_UseShader(&shaders[SHADOW_GENERATOR_SHADER]);
		glEnableVertexAttribArray(currentShader->vars[SHADER_ATT_VERTEX] );
		glViewport(0, 0, renderWidth*shadowMapRation, renderHeight*shadowMapRation);
		
		//Setup perspective and camera
		gluPerspective(light.fov, camera.aspect,camera.zNear, camera.zFar, projectionMatrix);
		gluLookAt(light.position, light.lookAt, light.upVector, modelViewMatrix);
	
		entity = map;
		for(i=0; i < num_map_entities; i++,entity++)
		{		
			matrix_multiply(modelViewMatrix,entity->matrix, tmp);
			matrix_multiply(projectionMatrix,tmp, modelViewProjectionMatrix);
			glUniformMatrix4fv(currentShader->vars[SHADER_MVT_MATRIX]   ,1,GL_FALSE, modelViewProjectionMatrix);
			
			if (entity->type == ENTITY_OBJ)
				RenderEntitiesOBJ(entity->model);
			else
				RenderEntitiesMD5(entity->model);
			
			//Also need to cache the PVM matrix in entity
			matrixCopy(modelViewProjectionMatrix,entity->cachePVMShadow);
		}
		
		glViewport(0, 0, renderWidth, renderHeight);
		glCullFace(GL_BACK);
		
		renderer.isRenderingShadow = 0;
		
		glBindFramebuffer(GL_FRAMEBUFFER,renderer.mainFramebufferId);
	}
	
	
	
	//Setup perspective and camera
	SetupCamera();
	
	entity = map;
	
	for(i=0; i < num_map_entities; i++,entity++)
	{
		//if (entity->type != ENTITY_OBJ)
		//	continue;
		
		SRC_BindUberShader(entity->material->prop);
		
		matrix_multiply(modelViewMatrix,entity->matrix, tmp);
		matrix_multiply(projectionMatrix,tmp, modelViewProjectionMatrix);
		glUniformMatrix4fv(currentShader->vars[SHADER_MVT_MATRIX]   ,1,GL_FALSE,modelViewProjectionMatrix);
		
		ComputeInvModelMatrix(entity->matrix, inv_modelMatrix);
		
		matrix_transform_vec4t(inv_modelMatrix, light.position, modelSpaceLightPos);
		glUniform3fv(currentShader->vars[SHADER_UNI_LIGHT_POS],1,modelSpaceLightPos);
		
		matrix_transform_vec4t(inv_modelMatrix, camera.position, modelSpaceCameraPos);
		glUniform3fv(currentShader->vars[SHADER_UNI_CAMERA_POS],1,modelSpaceCameraPos);

		
		
		if (renderer.props & PROP_SHADOW == PROP_SHADOW)
			glUniformMatrix4fv(currentShader->vars[SHADER_LIGHTPOV_MVT_MATRIX], 1, GL_FALSE,entity->cachePVMShadow);
		
		
		SetTextures(entity->material);
		
		if (entity->material->hasAlpha )
		{
			if (!renderer.isBlending)
			{
				renderer.isBlending = 1;
				glEnable(GL_BLEND);
				glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
				STATS_AddBlendingSwitch();
			}
		}
		else
		{
			if (renderer.isBlending)
			{
				renderer.isBlending = 0;
				glDisable(GL_BLEND);
				STATS_AddBlendingSwitch();
			}
		}
		
		glUniform1f(currentShader->vars[SHADER_UNI_MATERIAL_SHININESS], entity->material->shininess);
		glUniform3fv(currentShader->vars[SHADER_UNI_MAT_COL_SPECULAR],1,entity->material->specularColor);
		
		
		if (entity->type == ENTITY_OBJ)
			RenderEntitiesOBJ(entity->model);
		else
			RenderEntitiesMD5(entity->model);
		
		
	}
	
}
Beispiel #2
0
void ENT_GenerateWorldSpaceBBox(entity_t* entity)
{
	//Transform the bbox from modelSpace to WorldSpace.
	
	vec4_t modelSpaceMinPoint ;
	vec4_t modelSpaceMaxPoint;
	vec4_t worldSpaceMinPoint ;
	vec4_t worldSpaceMaxPoint;
	
	
	vectorCopy(entity->model->modelSpacebbox.min,modelSpaceMinPoint);
	modelSpaceMinPoint[3] = 1;
	
	vectorCopy(entity->model->modelSpacebbox.max,modelSpaceMaxPoint);
	modelSpaceMaxPoint[3] = 1;
	
	matrix_transform_vec4t(entity->matrix, modelSpaceMinPoint , worldSpaceMinPoint);
	matrix_transform_vec4t(entity->matrix, modelSpaceMaxPoint , worldSpaceMaxPoint);
	
	
//	printf("bbox min: [%.2f,%.2f,%.2f]\n", worldSpaceMinPoint[0], worldSpaceMinPoint[1], worldSpaceMinPoint[2]);
//	printf("bbox max: [%.2f,%.2f,%.2f]\n", worldSpaceMaxPoint[0], worldSpaceMaxPoint[1], worldSpaceMaxPoint[2]);
	
	//Generate 6 points defining the box limits.
	
	
	
	//Unit cube volume
	//x  y  z
	//0  0  0
	//1  0  0
	//0  0  1
	//1  0  1
	//0  1  0
	//1  1  0
	//0  1  1
	//1  1  1
	
	// 0 0 0
	entity->worldSpacebbox[0][0] = worldSpaceMinPoint[0] ;
	entity->worldSpacebbox[0][1] = worldSpaceMinPoint[1] ;
	entity->worldSpacebbox[0][2] = worldSpaceMinPoint[2] ;
	
	// 1 0 0
	entity->worldSpacebbox[1][0] = worldSpaceMaxPoint[0] ;
	entity->worldSpacebbox[1][1] = worldSpaceMinPoint[1] ;
	entity->worldSpacebbox[1][2] = worldSpaceMinPoint[2] ;

	// 0 0 1
	entity->worldSpacebbox[2][0] = worldSpaceMinPoint[0] ;
	entity->worldSpacebbox[2][1] = worldSpaceMinPoint[1] ;
	entity->worldSpacebbox[2][2] = worldSpaceMaxPoint[2] ;

	//1  0  1
	entity->worldSpacebbox[3][0] = worldSpaceMaxPoint[0] ;
	entity->worldSpacebbox[3][1] = worldSpaceMinPoint[1] ;
	entity->worldSpacebbox[3][2] = worldSpaceMaxPoint[2] ;
		
	//0  1  0
	entity->worldSpacebbox[4][0] = worldSpaceMinPoint[0] ;
	entity->worldSpacebbox[4][1] = worldSpaceMaxPoint[1] ;
	entity->worldSpacebbox[4][2] = worldSpaceMinPoint[2] ;
	
	//1  1  0
	entity->worldSpacebbox[5][0] = worldSpaceMaxPoint[0] ;
	entity->worldSpacebbox[5][1] = worldSpaceMaxPoint[1] ;
	entity->worldSpacebbox[5][2] = worldSpaceMinPoint[2] ;
	
	//0  1  1
	entity->worldSpacebbox[6][0] = worldSpaceMinPoint[0] ;
	entity->worldSpacebbox[6][1] = worldSpaceMaxPoint[1] ;
	entity->worldSpacebbox[6][2] = worldSpaceMaxPoint[2] ;
	
	//1  1  1
	entity->worldSpacebbox[7][0] = worldSpaceMaxPoint[0] ;
	entity->worldSpacebbox[7][1] = worldSpaceMaxPoint[1] ;
	entity->worldSpacebbox[7][2] = worldSpaceMaxPoint[2] ;
}