Пример #1
0
static tb_void_t tb_float_test_ceil(tb_float_t x)
{
    __tb_volatile__ tb_long_t       n = 10000000;
    __tb_volatile__ tb_long_t       r = 0;
    tb_hong_t t = tb_mclock();
    while (n--)
    {
        r = tb_ceil(x);
    }
    t = tb_mclock() - t;
    tb_printf("[float]: ceil(%f): %ld, %lld ms\n", (x), r, t);
}
Пример #2
0
Файл: draw.c Проект: cosim/gbox2
static __tb_inline__ tb_void_t g2_gl_draw_style_fill_shader_radial(g2_gl_draw_t* draw, g2_gl_rect_t const* bounds)
{
	// enter texture state
	g2_gl_draw_enter_texture_state(draw);

	// enter texture matrix
	g2_gl_draw_enter_texture_matrix(draw);

	// init texcoords
	draw->texcoords[0] = 0.0f;
	draw->texcoords[1] = 0.0f;
	draw->texcoords[2] = 1.0f;
	draw->texcoords[3] = 0.0f;
	draw->texcoords[4] = 0.0f;
	draw->texcoords[5] = 1.0f;
	draw->texcoords[6] = 1.0f;
	draw->texcoords[7] = 1.0f;

	// apply texcoords
	g2_gl_draw_apply_texcoords(draw);

	// init radial variables
	tb_float_t smatrix[16];
	g2_gl_matrix_from(smatrix, &draw->shader->matrix);
	tb_float_t cx = g2_float_to_tb(draw->shader->u.radial.cp.c.x);
	tb_float_t cy = g2_float_to_tb(draw->shader->u.radial.cp.c.y);
	tb_float_t x0 = g2_gl_matrix_apply_x(smatrix, cx, cy);
	tb_float_t y0 = g2_gl_matrix_apply_y(smatrix, cx, cy);

	// init scale factor
	tb_float_t sx = tb_fabs(smatrix[0]);
	tb_float_t sy = tb_fabs(smatrix[5]);
	tb_float_t fs = tb_min(sx, sy);
	if (fs < 1e-9) fs = 1e-9;

	// init maximum radius 
	tb_float_t n1 = (x0 - bounds->x1) * (x0 - bounds->x1) + (y0 - bounds->y1) * (y0 - bounds->y1);
	tb_float_t n2 = (x0 - bounds->x2) * (x0 - bounds->x2) + (y0 - bounds->y1) * (y0 - bounds->y1);
	tb_float_t n3 = (x0 - bounds->x1) * (x0 - bounds->x1) + (y0 - bounds->y2) * (y0 - bounds->y2);
	tb_float_t n4 = (x0 - bounds->x2) * (x0 - bounds->x2) + (y0 - bounds->y2) * (y0 - bounds->y2);
	if (n2 > n1) n1 = n2;
	if (n3 > n1) n1 = n3; 
	if (n4 > n1) n1 = n4; 
	tb_float_t rm = (tb_float_t)(tb_isqrti(tb_ceil(n1)) + 1) / fs;

	// the radial factor
	static g2_gl_draw_radial_factor_t factors[] =
	{
		{0.105396307f, 	12.0f, 	30} // rm * sin(6.05)
	,	{0.070626986f, 	8.0f, 	45} // rm * sin(4.05)
	,	{0.035771616f, 	4.0f, 	90} // rm * sin(2.05)
	};
	tb_assert(g2_quality() < tb_arrayn(factors));
	g2_gl_draw_radial_factor_t const* factor = &factors[g2_quality()];

	/* init fragment vertices
	 *        
	 *        fn
	 * *****|*****
	 *  *   |   *
	 *   *rm|  *
	 *    * | *
	 *     *|*
	 *      *
	 */
	tb_float_t fn = rm * factor->factor; // rm * sin(x.05)
	draw->vertices[0] = cx - fn;
	draw->vertices[1] = cy - rm;
	draw->vertices[2] = cx + fn;
	draw->vertices[3] = cy - rm;
	draw->vertices[4] = cx;
	draw->vertices[5] = cy;

	// init fragment bounds
	g2_gl_rect_t fbounds;
	g2_gl_bounds_init(&fbounds, draw->vertices[0], draw->vertices[1]);
	g2_gl_bounds_done(&fbounds, draw->vertices[2], draw->vertices[3]);
	g2_gl_bounds_done(&fbounds, draw->vertices[4], draw->vertices[5]);
	
	// apply vertices
	g2_gl_draw_apply_vertices(draw);

	// apply texture matrix
	g2_gl_draw_apply_texture_matrix(draw, &fbounds);

	// save vetex matrix
	tb_float_t matrix0[16];
	g2_gl_matrix_copy(matrix0, draw->vmatrix);

	// apply shader matrix
	g2_gl_matrix_multiply(draw->vmatrix, smatrix);

	// init rotate matrix: rotate one degress
	tb_float_t matrix1[16];
	g2_gl_matrix_init_rotatep(matrix1, factor->rotation, cx, cy);

	// rotate for drawing all fragments
	tb_size_t n = factor->count;
	while (n--)
	{
		// rotate one degress
		g2_gl_matrix_multiply(draw->vmatrix, matrix1);

		// apply vetex matrix
		g2_gl_draw_apply_vertex_matrix(draw);

		// draw fragment
		g2_glDrawArrays(G2_GL_TRIANGLE_STRIP, 0, 3);

	}

	// restore vetex matrix
	g2_gl_matrix_copy(draw->vmatrix, matrix0);

	// apply vetex matrix
	g2_gl_draw_apply_vertex_matrix(draw);

	// leave texture matrix
	g2_gl_draw_leave_texture_matrix(draw);

	// leave texture state
	g2_gl_draw_leave_texture_state(draw);
}