Blame c++/perspective/src/perspectivesandboxview.cpp

0662a2
0662a2
#include "perspectivesandboxview.h"
0662a2
0662a2
0662a2
View::PointPtr
0662a2
	persp_p0,
0662a2
	persp_px,
0662a2
	persp_py,
0662a2
	persp_p1,
0662a2
	bounds_p0,
0662a2
	bounds_p1;
0662a2
0662a2
PerspectiveSandBoxView::PerspectiveSandBoxView():
0662a2
	persp_p0  (new View::Point( Vector2(-1, -1) )),
0662a2
	persp_px  (new View::Point( Vector2( 1, -1) )),
0662a2
	persp_py  (new View::Point( Vector2(-1,  1) )),
0662a2
	persp_p1  (new View::Point( Vector2( 1,  1) )),
0662a2
	bounds_p0 (new View::Point( Vector2(-2, -2) )),
0662a2
	bounds_p1 (new View::Point( Vector2( 2,  2) ))
0662a2
{
0662a2
	transform.scale(50, 50);
0662a2
	points.push_back(persp_p0);
0662a2
	points.push_back(persp_px);
0662a2
	points.push_back(persp_py);
0662a2
	points.push_back(persp_p1);
0662a2
	points.push_back(bounds_p0);
0662a2
	points.push_back(bounds_p1);
0662a2
}
0662a2
0662a2
void
0662a2
PerspectiveSandBoxView::draw_grid(
0662a2
	const Cairo::RefPtr<cairo::context> &context,</cairo::context>
0662a2
	const Matrix &matrix,
0662a2
	const Color &color )
0662a2
{
0662a2
	const int count = 100;
0662a2
	const int subcount = 10;
0662a2
0662a2
	const Real ps = get_pixel_size();
0662a2
0662a2
	for(int i = -count; i < count; ++i) {
0662a2
		for(int j = -count; j < count; ++j) {
0662a2
			Vector4 src(i/(Real)subcount, j/(Real)subcount, 0, 1);
0662a2
			Vector4 dst = matrix*src;
0662a2
			Real w = dst.w;
0662a2
			if (w > real_precision) {
0662a2
				Vector2 pos(dst.x/w, dst.y/w);
0662a2
				
0662a2
				Real ka = clamp(1/w, 0, 1);
0662a2
				
0662a2
				Real a = color.a*ka;
0662a2
				Real r = 4*ps;
0662a2
				if (i) r *= 0.5;
0662a2
				if (j) r *= 0.5;
0662a2
				if (i % 10) r *= 0.75;
0662a2
				if (j % 10) r *= 0.75;
0662a2
				context->set_source_rgba(color.r, color.g, color.b, a);
0662a2
				context->arc(pos.x, pos.y, r, 0, 2.0*M_PI);
0662a2
				context->fill();
0662a2
			}
0662a2
		}
0662a2
	}
0662a2
}
0662a2
0662a2
void
0662a2
PerspectiveSandBoxView::draw_line(
0662a2
	const Cairo::RefPtr<cairo::context> &context,</cairo::context>
0662a2
	const Pair2 &bounds,
0662a2
	Real a,
0662a2
	Real b,
0662a2
	Real c,
0662a2
	const Color &color )
0662a2
{
0662a2
	
0662a2
}
0662a2
0662a2
void
0662a2
PerspectiveSandBoxView::on_draw_view(const Cairo::RefPtr<cairo::context> &context) {</cairo::context>
0662a2
	context->save();
0662a2
0662a2
	const Real ps = get_pixel_size();
0662a2
	context->set_line_width(ps);
0662a2
0662a2
	Vector2 p0 = persp_p0->position;
0662a2
	Vector2 px = persp_px->position;
0662a2
	Vector2 py = persp_py->position;
0662a2
	Vector2 p1 = persp_p1->position;
0662a2
	
0662a2
	Pair2 bounds(bounds_p0->position, bounds_p1->position);
0662a2
	
0662a2
	
0662a2
	// perspective matrix
0662a2
	Matrix matrix;
0662a2
	{
0662a2
		Vector2 A = px - p1;
0662a2
		Vector2 B = py - p1;
0662a2
		Vector2 C = p0 + p1 - px - py;
0662a2
		
0662a2
		Real cw = A.y*B.x - A.x*B.y;
0662a2
		Real aw = B.x*C.y - B.y*C.x;
0662a2
		Real bw = A.y*C.x - A.x*C.y;
0662a2
		
0662a2
		Vector2 c = p0*cw;
0662a2
		Vector2 a = px*(cw + aw) - c;
0662a2
		Vector2 b = py*(cw + bw) - c;
0662a2
0662a2
		matrix.row_x() = Vector4(a, 0, aw);
0662a2
		matrix.row_y() = Vector4(b, 0, bw);
0662a2
		matrix.row_w() = Vector4(c, 0, cw);
0662a2
	}
0662a2
0662a2
	draw_grid(context, matrix, Color(0, 1, 0, 1));
0662a2
0662a2
	// draw frames
0662a2
	context->set_source_rgba(0, 0, 1, 0.75);
0662a2
	context->move_to(p0.x, p0.y);
0662a2
	context->line_to(px.x, px.y);
0662a2
	context->line_to(p1.x, p1.y);
0662a2
	context->line_to(py.x, py.y);
0662a2
	context->close_path();
0662a2
	context->stroke();
0662a2
0662a2
	context->move_to(bounds.p0.x, bounds.p0.y);
0662a2
	context->line_to(bounds.p1.x, bounds.p0.y);
0662a2
	context->line_to(bounds.p1.x, bounds.p1.y);
0662a2
	context->line_to(bounds.p0.x, bounds.p1.y);
0662a2
	context->close_path();
0662a2
	context->stroke();
0662a2
	
0662a2
	context->restore();
0662a2
}
0662a2