Esempio n. 1
0
// Returns NONE of it does not have valid clip info
short RenderVisTreeClass::calculate_endpoint_clipping_information(
  short endpoint_index,
  uint16 clip_flags)
{
  // If this endpoint was not transformed, then don't do anything with it,
  // and indicate that it's not a valid endpoint
  if (!TEST_RENDER_FLAG(endpoint_index, _endpoint_has_been_transformed)) {
    return NONE;
  }

  // LP addition: extend the endpoint-clip list
  endpoint_clip_data Dummy;
  Dummy.flags = 0;                              // Fake initialization to shut up CW
  EndpointClips.push_back(Dummy);
  size_t Length = EndpointClips.size();
  assert(Length <= 32767);
  assert(Length >= 1);
  size_t LastIndex = Length-1;

  endpoint_data *endpoint= get_endpoint_data(endpoint_index);
  endpoint_clip_data *data= &EndpointClips[LastIndex];
  int32 x;

  assert((clip_flags&(_clip_left|_clip_right)));       /* must have a clip flag */
  assert((clip_flags&(_clip_left|_clip_right))!=(_clip_left|_clip_right));       /* but canÕt have both */
  assert(!TEST_RENDER_FLAG(endpoint_index, _endpoint_has_clip_data));

  // LP change: compose a true transformed point to replace endpoint->transformed,
  // and use it in the upcoming code
  long_vector2d transformed_endpoint;
  overflow_short_to_long_2d(endpoint->transformed,endpoint->flags,
                            transformed_endpoint);

  data->flags= clip_flags&(_clip_left|_clip_right);
  switch (data->flags)
  {
  case _clip_left:
    data->vector.i= transformed_endpoint.i;
    data->vector.j= transformed_endpoint.j;
    break;
  case _clip_right:               /* negatives so we clip to the correct side */
    data->vector.i= -transformed_endpoint.i;
    data->vector.j= -transformed_endpoint.j;
    break;
  }
  // warn(data->vector.i);

  // assert(TEST_RENDER_FLAG(endpoint_index, _endpoint_has_been_transformed));
  x= endpoint_x_coordinates[endpoint_index];

  data->x= (short)PIN(x, 0, view->screen_width);

  return (short)LastIndex;
}
Esempio 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;
			}
		}
	}
}
Esempio n. 3
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
Esempio n. 4
0
// Main routine
void RenderVisTreeClass::build_render_tree()
{
	assert(view);	// Idiot-proofing

	/* initialize the queue where we remember polygons we need to fire at */
	initialize_polygon_queue();

	/* initialize our node list to contain the root, etc. */
	initialize_render_tree();
	
	/* reset clipping buffers */
	initialize_clip_data();
	
	// LP change:
	// Adjusted for long-vector handling
	// Using start index of list of nodes: 0
	long_vector2d view_edge;
	
	short_to_long_2d( view->left_edge, view_edge );
	cast_render_ray( &view_edge, NONE, &Nodes.front(), _counterclockwise_bias );
	
	short_to_long_2d( view->right_edge, view_edge );
	cast_render_ray( &view_edge, NONE, &Nodes.front(), _clockwise_bias );
	
	/* 
		pull polygons off the queue, fire at all their new endpoints, building the tree as we go 
	*/
	while (polygon_queue_size)
	{
		auto polygon_index 	= PolygonQueue[ --polygon_queue_size ];
		polygon_data *polygon 	= get_polygon_data(polygon_index);
		
		assert( !POLYGON_IS_DETACHED(polygon) );
		
		const ix vertex_count = polygon->vertex_count;
		for( ix vertex_index = 0; vertex_index < vertex_count; ++vertex_index )
		{
			const auto endpoint_index	= polygon->endpoint_indexes[vertex_index];
			endpoint_data *endpoint		= get_endpoint_data(endpoint_index);
			
			if (TEST_RENDER_FLAG(endpoint_index, _endpoint_has_been_visited))
				continue;
			// LP change: move toward correct handling of long distances
			long_vector2d _vector;
			
			/* transform all visited endpoints */
			endpoint->transformed = endpoint->vertex;
			transform_overflow_point2d( &endpoint->transformed, 
						(world_point2d *) &view->origin, 
						view->yaw, 
						&endpoint->flags );
						
			/* calculate an outbound vector to this endpoint */
			// LP: changed to do long distance correctly.	
			_vector.i 	= int32( endpoint->vertex.x ) - int32( view->origin.x );
			_vector.j	= int32( endpoint->vertex.y ) - int32( view->origin.y );
			
			// LP change: compose a true transformed point to replace endpoint->transformed,
			// and use it in the upcoming code
			long_vector2d transformed_endpoint;
			overflow_short_to_long_2d( 	endpoint->transformed, 
							endpoint->flags, 
							transformed_endpoint );
			if (transformed_endpoint.i > 0)
			{
				const int32 x = view->half_screen_width + 
				( transformed_endpoint.j * view->world_to_screen_x ) / transformed_endpoint.i;
				
				endpoint_x_coordinates[ endpoint_index ] = 
					static_cast< int16 >( PIN(x, INT16_MIN, INT16_MAX) );
					
				SET_RENDER_FLAG(endpoint_index, _endpoint_has_been_transformed);
			}
			
			/* 
				do two cross products to determine whether this endpoint is in our view cone or not
				(we don't have to cast at points outside the cone) 
			*/
			const auto ri = view->right_edge.i;
			const auto rj = view->right_edge.j;
			const int32 crossprod_right = ( ri * _vector.j ) - ( rj * _vector.i );
			
			const auto li = view->left_edge.i;
			const auto lj = view->left_edge.j;
			const int32 crossprod_left = ( li * _vector.j ) - ( lj * _vector.i );
			
			if( crossprod_right <= 0 && crossprod_left >= 0 )
			{
				//it's in our view, cast at it
				int16 endpoint_;
				
				if( ENDPOINT_IS_TRANSPARENT(endpoint) )
					endpoint_ = NONE;
				else
					endpoint_ = endpoint_index;
					
				cast_render_ray(&_vector, endpoint_, &Nodes.front(), _no_bias);
			}
			SET_RENDER_FLAG(endpoint_index, _endpoint_has_been_visited);
		
		}
	}
}
void RenderVisTreeClass::calculate_line_clipping_information(
	short line_index,
	uint16 clip_flags)
{
	// LP addition: extend the line-clip list
	line_clip_data Dummy;
	Dummy.flags = 0;			// Fake initialization to shut up CW
	LineClips.push_back(Dummy);
	size_t Length = LineClips.size();
	assert(Length <= 32767);
	assert(Length >= 1);
	size_t LastIndex = Length-1;
	
	line_data *line= get_line_data(line_index);
	// LP change: relabeling p0 and p1 so as not to conflict with later use
	world_point2d p0_orig= get_endpoint_data(line->endpoint_indexes[0])->vertex;
	world_point2d p1_orig= get_endpoint_data(line->endpoint_indexes[1])->vertex;
	// LP addition: place for new line data
	line_clip_data *data= &LineClips[LastIndex];

	/* itÕs possible (in fact, likely) that this lineÕs endpoints have not been transformed yet,
		so we have to do it ourselves */
	// LP change: making the operation long-distance friendly
	uint16 p0_flags = 0, p1_flags = 0;
	transform_overflow_point2d(&p0_orig, (world_point2d *) &view->origin, view->yaw, &p0_flags);
	transform_overflow_point2d(&p1_orig, (world_point2d *) &view->origin, view->yaw, &p1_flags);
	
	// Defining long versions here and copying over
	long_point2d p0, p1;
	long_vector2d *pv0ptr = (long_vector2d*)(&p0), *pv1ptr = (long_vector2d*)(&p1);
	overflow_short_to_long_2d(p0_orig,p0_flags,*pv0ptr);
	overflow_short_to_long_2d(p1_orig,p1_flags,*pv1ptr);
	
	clip_flags&= _clip_up|_clip_down;	
	assert(clip_flags&(_clip_up|_clip_down));
	assert(!TEST_RENDER_FLAG(line_index, _line_has_clip_data));

	SET_RENDER_FLAG(line_index, _line_has_clip_data);
	line_clip_indexes[line_index]= static_cast<vector<size_t>::value_type>(LastIndex);
	
	data->flags= 0;

	if (p0.x>0 && p1.x>0)
	{
		// LP change:
		long_point2d *p;
		world_distance z;
		long transformed_z;
		long y, y0, y1;
		long x0= view->half_screen_width + (p0.y*view->world_to_screen_x)/p0.x;
		long x1= view->half_screen_width + (p1.y*view->world_to_screen_x)/p1.x;
	
		data->x0= (short)PIN(x0, 0, view->screen_width);
		data->x1= (short)PIN(x1, 0, view->screen_width);
		if (data->x1<data->x0) SWAP(data->x0, data->x1);
		if (data->x1>data->x0)
		{
			if (clip_flags&_clip_up)
			{
				/* precalculate z and transformed_z */
				z= line->lowest_adjacent_ceiling-view->origin.z;
				transformed_z= z*view->world_to_screen_y;
				
				/* calculate and clip y0 and y1 (screen y-coordinates of each side of the line) */
				y0= (p0.x>0) ? (view->half_screen_height - transformed_z/p0.x + view->dtanpitch) : 0;
				y1= (p1.x>0) ? (view->half_screen_height - transformed_z/p1.x + view->dtanpitch) : 0;
		
				/* pick the highest (closest to zero) and pin it to the screen */
				if (y0<y1) y= y0, p= &p0; else y= y1, p= &p1;
				y= PIN(y, 0, view->screen_height);
				
				/* if weÕre not useless (clipping up off the top of the screen) set up top-clip information) */
				if (y<=0)
				{
					clip_flags&= ~_clip_up;
				}
				else
				{
					data->top_vector.i= - p->x, data->top_vector.j= - z;
					data->top_y= y;
				}
			}
			
			if (clip_flags&_clip_down)
			{
				z= line->highest_adjacent_floor - view->origin.z;
				transformed_z= z*view->world_to_screen_y;
				
				/* calculate and clip y0 and y1 (screen y-coordinates of each side of the line) */
				y0= (p0.x>0) ? (view->half_screen_height - transformed_z/p0.x + view->dtanpitch) : view->screen_height;
				y1= (p1.x>0) ? (view->half_screen_height - transformed_z/p1.x + view->dtanpitch) : view->screen_height;
				
				/* pick the highest (closest to zero screen_height) and pin it to the screen */
				if (y0>y1) y= y0, p= &p0; else y= y1, p= &p1;
				y= PIN(y, 0, view->screen_height);
				
				/* if weÕre not useless (clipping up off the bottom of the screen) set up top-clip information) */
				if (y>=view->screen_height)
				{
					clip_flags&= ~_clip_down;
				}
				else
				{
					data->bottom_vector.i= p->x,  data->bottom_vector.j= z;
					data->bottom_y= y;
				}
			}
	
			data->flags= clip_flags;
//			dprintf("line #%d clips %x @ %p", line_index, clip_flags, data);
		}
	}
}
// 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;
}