Re: [clutter] Is there a ClutterReflectTexture to use in Clutter 1.0?

2010-07-16 Thread Emmanuele Bassi
[re-directing to the correct mailing list]

hi;

On Fri, 2010-07-16 at 11:34 +0800, Jianchun Zhou wrote:

 In Clutter 0.5, I had ever got a class named ClutterReflectTexture,
 and now I want use this reflect function in Clutter 1.0
 
 I wonder if there is a similar one or not?

that code was never in Clutter: it was in one of the toys.

nowadays, two years and a half after that code was written, we have
better ways of accomplishing the same result - even in languages that
are not C. :-)

first of all, ClutterCloneTexture has been replaced by ClutterClone,
which can clone all actors, not just textures.

then you don't need to drop into GL to paint: Cogl, the Clutter OpenGL
abstraction library, can do that for you, and more efficiently.

the whole paint() virtual function implementation can be replaced with
something like this:

static void
clutter_texture_reflection_paint (ClutterActor *actor)
{
  ClutterClone *super = CLUTTER_CLONE (actor);
  ClutterTextureReflectionPrivate *priv;
  ClutterActor *source;
  ClutterActorBox *box;
  CoglHandle material;
  gfloat width, height, r_height;
  gfloat rty;
  CoglColor color_1, color_2;
  CoglTextureVertex vertices[4];

  priv = CLUTTER_TEXTURE_REFLECTION (actor)-priv;

  /* if we don't have a source actor, don't paint */
  source = clutter_clone_get_source (super);
  if (source == NULL)
return;

  /* if the source texture does not have any content, don't paint */
  material = clutter_texture_get_cogl_material (CLUTTER_TEXTURE (source));
  if (material == NULL)
return;

  /* get the size of the reflection */
  clutter_actor_get_allocation_box (actor, box);
  clutter_actor_box_get_size (box, width, height);

  /* get the composite opacity of the actor */
  opacity = clutter_actor_get_paint_opacity (actor);

  /* clamp the size of the reflection */
  r_height = priv-reflection_height;
  if (r_height  0 || r_height  height)
r_height = height;

  /* figure out the texel for the reflection */
  rty = r_height / height;

  /* figure out the two colors for the reflection: the first is
   * full color and the second is the same, but at 0 opacity
   */
  cogl_color_set_from_4f (color_1, 1.0, 1.0, 1.0, opacity / 255.);
  cogl_color_premultiply (color_1);
  cogl_color_set_from_4f (color_2, 1.0, 1.0, 1.0, 0.0);
  cogl_color_premultiply (color_2);

  /* now describe the four vertices of the quad; since it has
   * to be a reflection, we need to invert it as well
   */
  vertices[0].x = 0; vertices[0].y = 0; vertices[0].z = 0;
  vertices[0].tx = 0.0; vertices[0].ty = 1.0;
  vertices[0].color = color_1;

  vertices[1].x = width; vertices[1].y = 0; vertices[1].z = 0;
  vertices[1].tx = 1.0; vertices[1].ty = 1.0;
  vertices[1].color = color_1;

  vertices[2].x = width; vertices[2].y = r_height; vertices[2].z = 0;
  vertices[2].tz = 1.0; vertices[2].ty = 1.0 - rty;
  vertices[2].color = color_2;

  vertices[3].x = 0; vertices[3].y = r_height; vertices[3].z = 0;
  vertices[3].tx = 0.0; vertices[3].ty = 1.0 - rty;
  vertices[3].color = color_2;

  /* paint the same texture but with a different geometry */
  cogl_set_source (material);
  cogl_polygon (vertices, 4, TRUE);
}

[the code above is untested and uncompiled, but translated from the
python example available in Git[0] so it should just work]

ciao,
 Emmanuele.

[0] 
http://git.clutter-project.org/bindings/pyclutter/plain/examples/reflection.py


-- 
Emmanuele Bassi, Open Source Software Engineer
Intel Open Source Technology Center

-- 
To unsubscribe send a mail to clutter+unsubscr...@o-hand.com



[clutter] Is there a ClutterReflectTexture to use in Clutter 1.0?

2010-07-15 Thread Jianchun Zhou
Hi, all:

In Clutter 0.5, I had ever got a class named ClutterReflectTexture, and now
I want use this reflect function in Clutter 1.0

I wonder if there is a similar one or not?

Thanks.

-- 
Best Regards
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Authored By Matthew Allum  mal...@openedhand.com
 *
 * Copyright (C) 2006 OpenedHand
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#define CLUTTER_PARAM_READWRITE \
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |G_PARAM_STATIC_BLURB


/**
 * SECTION:clutter-reflect-texture
 * @short_description: Actor for cloning existing textures in an 
 * efficient way.
 *
 * #ClutterReflectTexture allows the cloning of existing #ClutterTexture with 
 * a refelction like effect.
 */

#include GL/gl.h

#include clutter-reflect-texture.h

enum
{
  PROP_0,
  PROP_REFLECTION_HEIGHT
};

G_DEFINE_TYPE (ClutterReflectTexture,
	   clutter_reflect_texture,
	   CLUTTER_TYPE_CLONE_TEXTURE);

#define CLUTTER_REFLECT_TEXTURE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_REFLECT_TEXTURE, ClutterReflectTexturePrivate))

struct _ClutterReflectTexturePrivate
{
  gint reflection_height;
};

static void
reflect_texture_render_to_gl_quad (ClutterReflectTexture *ctexture, 
 int x1, 
 int y1, 
 int x2, 
 int y2)
{
  gint   qx1 = 0, qx2 = 0, qy1 = 0, qy2 = 0;
  gint   qwidth = 0, qheight = 0;
  gint   x, y, i =0, lastx = 0, lasty = 0;
  gint   n_x_tiles, n_y_tiles; 
  gint   pwidth, pheight, rheight;
  float tx, ty, ty2;

  ClutterReflectTexturePrivate *priv = ctexture-priv;
  ClutterActor *parent_texture = CLUTTER_ACTOR(clutter_clone_texture_get_parent_texture(CLUTTER_CLONE_TEXTURE(ctexture)));

  priv = ctexture-priv;

  qwidth  = x2 - x1;
  qheight = y2 - y1;

  rheight = priv-reflection_height;

  if (rheight  qheight)
rheight = qheight;

  if (!CLUTTER_ACTOR_IS_REALIZED (parent_texture))
  clutter_actor_realize (parent_texture);

  /* Only paint if parent is in a state to do so */
  if (!clutter_texture_has_generated_tiles (CLUTTER_TEXTURE(parent_texture)))
return;
  
  clutter_texture_get_base_size (CLUTTER_TEXTURE(parent_texture), 
 pwidth, pheight); 

  if (!clutter_texture_is_tiled (CLUTTER_TEXTURE(parent_texture)))
{
  clutter_texture_bind_tile (CLUTTER_TEXTURE(parent_texture), 0);

  /* NPOTS textures *always* used if extension available
   */
  if (clutter_feature_available (CLUTTER_FEATURE_TEXTURE_RECTANGLE))
	{
	  tx = (float) pwidth;
	  ty = (float) pheight;
	  ty2 = (float)(clutter_actor_get_height (CLUTTER_ACTOR(ctexture)) * rheight) 
 / pheight;
	  ty2 = pheight - ty2;

	}
  else
	{
	  tx = (float) pwidth / clutter_util_next_p2 (pwidth);  
	  ty = (float) pheight / clutter_util_next_p2 (pheight);
	}

  qx1 = x1; qx2 = x2;
  qy1 = y1; qy2 = y1 + rheight;

  glBegin (GL_QUADS);

  glColor4ub (255*0.45, 255*0.45, 255*0.45, 
		  clutter_actor_get_opacity (CLUTTER_ACTOR(ctexture)));

  glTexCoord2f (0, ty);   
  glVertex2i   (qx1, qy1);

  glTexCoord2f (tx,  ty);   
  glVertex2i   (qx2, qy1);

  glColor4ub (0, 0, 0, clutter_actor_get_opacity (CLUTTER_ACTOR(ctexture)));

  glTexCoord2f (tx,  ty2);
  glVertex2i   (qx2, qy2);
  
  glTexCoord2f (0, ty2);
  glVertex2i   (qx1, qy2);

  glEnd ();	
  
  return;
}

  clutter_texture_get_n_tiles (CLUTTER_TEXTURE(parent_texture), 
			   n_x_tiles, n_y_tiles); 

  for (x = 0; x  n_x_tiles; x++)
{
  lasty = 0;

  for (y = 0; y  n_y_tiles; y++)
	{
	  gint actual_w, actual_h;
	  gint xpos, ypos, xsize, ysize, ywaste, xwaste;
	  
	  clutter_texture_bind_tile (CLUTTER_TEXTURE(parent_texture), i);
	 
	  clutter_texture_get_x_tile_detail (CLUTTER_TEXTURE(parent_texture), 
	 x, xpos, xsize, xwaste);

	  clutter_texture_get_y_tile_detail (CLUTTER_TEXTURE(parent_texture), 
	 y, ypos, ysize, ywaste);

	  actual_w = xsize - xwaste;
	  actual_h = ysize - ywaste;

	  tx = (float) actual_w / xsize;
	  ty = (float) actual_h / ysize;

	  qx1 = x1 + lastx;
	  qx2 = qx1 + ((qwidth * actual_w ) / pwidth );