Exemplo n.º 1
0
// LP change: make it better able to do long-distance views
uint16 RenderVisTreeClass::next_polygon_along_line(int16 * polygon_index,
	world_point2d *origin, /* not necessairly in polygon_index */
	long_vector2d *_vector, // world_vector2d *vector,
	int16 *clipping_endpoint_index, /* if non-NONE on entry this is the solid endpoint weÕre shooting for */
	int16 *clipping_line_index, /* NONE on exit if this polygon transition wasnÕt accross an elevation line */
	int16 bias)
{
	polygon_data *polygon	= get_polygon_data(*polygon_index);
	int16 next_polygon_index, crossed_line_index, crossed_side_index;
	bool passed_through_solid_vertex = false;
	uint16 clip_flags = 0;


	ADD_POLYGON_TO_AUTOMAP(*polygon_index);
	PUSH_POLYGON_INDEX(*polygon_index);

	int16 state = _looking_for_first_nonzero_vertex;
	int16 vertex_index = 0;
	int16 vertex_delta = 1; /* start searching clockwise from vertex zero */
	
	// LP change: added test for looping around:
	// will remember the first vertex examined when the state has changed
	auto initial_vertex_index = vertex_index;
	
	bool changed_state = true;
	
	do
	{
		// Jump out of loop?
		if (changed_state)
			changed_state = false;
		else if (vertex_index == initial_vertex_index)
		{
			// Attempt to idiot-proof it by returning nothing
			next_polygon_index = NONE;
			crossed_line_index = NONE;
			crossed_side_index = NONE;
			break;
		}
			
		auto endpoint_index = polygon->endpoint_indexes[vertex_index];
		world_point2d *vertex = &get_endpoint_data(endpoint_index)->vertex;
		// LP change to make it more long-distance-friendly
		
		//urghhhhhhhhhhh
		CROSSPROD_TYPE cross_product	= 
			CROSSPROD_TYPE(int32(vertex->x)-int32(origin->x))*_vector->j 
			- 
			CROSSPROD_TYPE(int32(vertex->y)-int32(origin->y))*_vector->i;
		
		if (cross_product < 0)
		{
		    switch (state)
		    {
			case _looking_for_first_nonzero_vertex:
			    /* search counterclockwise for transition (right to left) */
			    state = _looking_counterclockwise_for_left_vertex;
			    vertex_delta = -1;
			    // LP change: resetting loop test
			    initial_vertex_index = vertex_index;
			    changed_state = true;
			    break;

			case _looking_clockwise_for_right_vertex: /* found the transition we were looking for */
			{
			    ix i = WRAP_LOW(vertex_index, polygon->vertex_count-1);
			    next_polygon_index = polygon->adjacent_polygon_indexes[i];
			    crossed_line_index = polygon->line_indexes[i];
			    crossed_side_index = polygon->side_indexes[i];
			}
			case _looking_for_next_nonzero_vertex: /* next_polygon_index already set */
			    state = NONE;
			    break;
		    }
		} 
		else if (cross_product > 0)
		{
		    switch (state)
		    {
			case _looking_for_first_nonzero_vertex:
			    /* search clockwise for transition (left to right) */
			    state= _looking_clockwise_for_right_vertex;
			    // LP change: resetting loop test
			    initial_vertex_index = vertex_index;
			    changed_state = true;
			    break;

			case _looking_counterclockwise_for_left_vertex: /* found the transition we were looking for */
			    next_polygon_index = polygon->adjacent_polygon_indexes[vertex_index];
			    crossed_line_index = polygon->line_indexes[vertex_index];
			    crossed_side_index = polygon->side_indexes[vertex_index];
			case _looking_for_next_nonzero_vertex: /* next_polygon_index already set */
			    state = NONE;
			    break;
		    }
		} 
		else
		{
		    if (state!=_looking_for_first_nonzero_vertex)
		    {
			if (endpoint_index==*clipping_endpoint_index) 
				passed_through_solid_vertex = true;

			/* 
				if we think we know what's on the other side of this zero (these zeros)
				change the state: if we don't find what we're looking for then the polygon
				is entirely on one side of the line or the other (except for this vertex),
				in any case we need to call decide_where_vertex_leads() to find out what's
				on the other side of this vertex 
			*/
			switch (state)
			{
			    case _looking_clockwise_for_right_vertex:
			    case _looking_counterclockwise_for_left_vertex:
				next_polygon_index= *polygon_index;
				clip_flags|= decide_where_vertex_leads(&next_polygon_index, &crossed_line_index, &crossed_side_index,
					   vertex_index, origin, _vector, clip_flags, bias);
				state = _looking_for_next_nonzero_vertex;
				// LP change: resetting loop test
				initial_vertex_index = vertex_index;
				changed_state = true;
				break;
			}
		    }
		}
		/* adjust vertex_index (clockwise or counterclockwise, depending on vertex_delta) */
		vertex_index = vertex_delta < 0 ? WRAP_LOW(vertex_index, polygon->vertex_count - 1) :
			WRAP_HIGH(vertex_index, polygon->vertex_count - 1);
	}
	while (state!=NONE);


	/* 
		if we didn't pass through the solid vertex we were aiming for, set clipping_endpoint_index to NONE,
		we assume the line we passed through doesn't clip, and set clipping_line_index to NONE
		(this will be corrected in a few moments if we chose poorly) 
	*/
	if (!passed_through_solid_vertex) 
		*clipping_endpoint_index = NONE;
		
	*clipping_line_index = NONE;
	
	if (crossed_line_index==NONE)
	{
		*polygon_index = next_polygon_index;
		return clip_flags;
	}

	const line_data *restrict line = get_line_data(crossed_line_index);

	/* add the line we crossed to the automap */
	ADD_LINE_TO_AUTOMAP(crossed_line_index);

	/* if the line has a side facing this polygon, mark the side as visible */
	if (crossed_side_index!=NONE) 
		SET_RENDER_FLAG(crossed_side_index, _side_is_visible);

	/* if this line is transparent we need to check for a change in elevation for clipping,
		if itÕs not transparent then we canÕt pass through it */
	// LP change: added test for there being a polygon on the other side
	if (LINE_IS_TRANSPARENT(line) && next_polygon_index != NONE)
	{
		const polygon_data *restrict next_polygon = get_polygon_data(next_polygon_index);
		
		if (line->highest_adjacent_floor > next_polygon->floor_height ||
			line->highest_adjacent_floor > polygon->floor_height) 
				clip_flags |= _clip_down; /* next polygon floor is lower */
				
		if (line->lowest_adjacent_ceiling < next_polygon->ceiling_height ||
			line->lowest_adjacent_ceiling < polygon->ceiling_height) 
				clip_flags |= _clip_up; /* next polygon ceiling is higher */
				
		if ( clip_flags&(_clip_up|_clip_down) ) 
			*clipping_line_index = crossed_line_index;
	}
	else
Exemplo n.º 2
0
static void update_view_data(
	struct view_data *view)
{
	angle theta;

	// LP change: doing all the FOV changes here:
	View_AdjustFOV(view->field_of_view,view->target_field_of_view);
	
	if (view->effect==NONE)
	{
		view->world_to_screen_x= view->real_world_to_screen_x;
		view->world_to_screen_y= view->real_world_to_screen_y;
	}
	else
	{
		update_render_effect(view);
	}
	
	view->untransformed_left_edge.i= view->world_to_screen_x;
	view->untransformed_right_edge.i= - view->world_to_screen_x;
	
	/* calculate world_to_screen_y*tan(pitch) */
	view->dtanpitch= (view->world_to_screen_y*sine_table[view->pitch])/cosine_table[view->pitch];

	/* calculate left cone vector */
	theta= NORMALIZE_ANGLE(view->yaw-view->half_cone);
	view->left_edge.i= cosine_table[theta], view->left_edge.j= sine_table[theta];
	
	/* calculate right cone vector */
	theta= NORMALIZE_ANGLE(view->yaw+view->half_cone);
	view->right_edge.i= cosine_table[theta], view->right_edge.j= sine_table[theta];
	
	/* calculate top cone vector (negative to clip the right direction) */
	view->top_edge.i= - view->world_to_screen_y;
	view->top_edge.j= - (view->half_screen_height + view->dtanpitch); /* ==k */

	/* calculate bottom cone vector */
	view->bottom_edge.i= view->world_to_screen_y;
	view->bottom_edge.j= - view->half_screen_height + view->dtanpitch; /* ==k */

	/* if weÕre sitting on one of the endpoints in our origin polygon, move us back slightly (±1) into
		that polygon.  when we split rays weÕre assuming that weÕll never pass through a given
		vertex in different directions (because if we do the tree becomes a graph) but when
		we start on a vertex this can happen.  this is a destructive modification of the origin. */
	{
		short i;
		struct polygon_data *polygon= get_polygon_data(view->origin_polygon_index);
		
		for (i= 0;i<polygon->vertex_count;++i)
		{
			struct world_point2d *vertex= &get_endpoint_data(polygon->endpoint_indexes[i])->vertex;
			
			if (vertex->x==view->origin.x && vertex->y==view->origin.y)
			{
				world_point2d *ccw_vertex= &get_endpoint_data(polygon->endpoint_indexes[WRAP_LOW(i, polygon->vertex_count-1)])->vertex;
				world_point2d *cw_vertex= &get_endpoint_data(polygon->endpoint_indexes[WRAP_HIGH(i, polygon->vertex_count-1)])->vertex;
				world_vector2d inset_vector;
				
				inset_vector.i= (ccw_vertex->x-vertex->x) + (cw_vertex->x-vertex->x);
				inset_vector.j= (ccw_vertex->y-vertex->y) + (cw_vertex->y-vertex->y);
				view->origin.x+= SGN(inset_vector.i);
				view->origin.y+= SGN(inset_vector.j);
				
				break;
			}
		}
		
		/* determine whether we are under or over the media boundary of our polygon; we will see all
			other media boundaries from this orientation (above or below) or fail to draw them. */
		if (polygon->media_index==NONE)
		{
			view->under_media_boundary= false;
		}
		else
		{
			struct media_data *media= get_media_data(polygon->media_index);
			
			// LP change: idiot-proofing
			if (media)
			{
				view->under_media_boundary= UNDER_MEDIA(media, view->origin.z);
				view->under_media_index= polygon->media_index;
			} else {
				view->under_media_boundary= false;
			}
		}
	}
}
// LP change: make it better able to do long-distance views
uint16 RenderVisTreeClass::decide_where_vertex_leads(
	short *polygon_index,
	short *line_index,
	short *side_index,
	short endpoint_index_in_polygon_list,
	world_point2d *origin,
	long_vector2d *_vector, // world_vector2d *vector,
	uint16 clip_flags,
	short bias)
{
	polygon_data *polygon= get_polygon_data(*polygon_index);
	short endpoint_index= polygon->endpoint_indexes[endpoint_index_in_polygon_list];
	short index;
	
	switch (bias)
	{
		case _no_bias:
//			dprintf("splitting at endpoint #%d", endpoint_index);
			clip_flags|= _split_render_ray;
			*polygon_index= *line_index= *side_index= NONE;
			index= NONE;
			break;
		
		case _clockwise_bias:
			index= endpoint_index_in_polygon_list;
			break;
		
		case _counterclockwise_bias:
			index= WRAP_LOW(endpoint_index_in_polygon_list, polygon->vertex_count-1);
			break;
		
		default:
			// LP change:
			assert(false);
			// halt();
	}
	
	if (index!=NONE)
	{
		line_data *line;
		world_point2d *vertex;
		CROSSPROD_TYPE cross_product;

		*line_index= polygon->line_indexes[index];
		*side_index= polygon->side_indexes[index];
		*polygon_index= polygon->adjacent_polygon_indexes[index];
		
		line= get_line_data(*line_index);
		if (*polygon_index!=NONE && LINE_IS_TRANSPARENT(line))
		{
			polygon= get_polygon_data(*polygon_index);
			
			/* locate our endpoint in this polygon */
			for (index=0;
					index<polygon->vertex_count && polygon->endpoint_indexes[index]!=endpoint_index;
					++index)
				;
			vassert(index!=polygon->vertex_count, csprintf(temporary, "endpoint #%d not in polygon #%d", endpoint_index, *polygon_index));
	
			switch (bias)
			{
				case _clockwise_bias: index= WRAP_HIGH(index, polygon->vertex_count-1); break;
				case _counterclockwise_bias: index= WRAP_LOW(index, polygon->vertex_count-1); break;
				default:
					assert(false);
					break;
			}
			
			vertex= &get_endpoint_data(polygon->endpoint_indexes[index])->vertex;
			// LP change: made more long-distance-friendly
			cross_product= CROSSPROD_TYPE(long(vertex->x)-long(origin->x))*_vector->j - CROSSPROD_TYPE(long(vertex->y)-long(origin->y))*_vector->i;
			
			if ((bias==_clockwise_bias&&cross_product>=0) || (bias==_counterclockwise_bias&&cross_product<=0))
			{
				/* weÕre leaving this endpoint, set clip flag in case itÕs solid */
				clip_flags|= (bias==_clockwise_bias) ? _clip_left : _clip_right;
			}
		}

//		dprintf("left endpoint #%d via line #%d to polygon #%d (bias==#%d)", endpoint_index, *line_index, *polygon_index, bias);
	}

	return clip_flags;
}