示例#1
0
Cylinder :: Cylinder(float r, float h, int vs, int rs)
{
	// set radius
	radius = r;
	// set height
	height = h/2.0;
	
	// set stack interval
	float stack = h / vs;
	// set slice interval
	float slice = (2 * M_PI) / rs;
	
	// define the number of vertices
	verts_size = (vs + 1) * (rs) + 2;
	verts = new GLfloat*[verts_size];
	
	// define the number of faces
	faces_size = (vs + 1) * rs * 2;
	faces = new int*[faces_size];
	
	// define arrays for normals
	v_norms = new GLfloat*[verts_size];
	f_norms = new GLfloat*[faces_size];
	
	// define to keep track of faces 
	// for vertex normal calculation
	int vert_faces[verts_size];
	
	// VERTICES
	/////////////////////////////////////////////
	
	GLfloat* v;
	
	// current position of the stack on the y-axis
	float curr_stack;
	// current angle of rotation for slice
	float curr_slice;
	// current positions in the array
	int pos, pos2, pos3;
	
	// loop to set "body" vertices
	for( int i = 0; i < vs + 1; i ++ )
	{
		// calculate stack angle
		curr_stack = -i * stack;
		
		for( int j = 0; j < rs; j++ )
		{
			// calculate slice angle
			curr_slice = j * slice;
			
			// calculate position in the array
			pos = i * rs + j;
			
			// allocate a new vertex
			v = new GLfloat[4];
			// set vertex at north pole
			v[0] = radius;
			v[1] = height;
			v[2] = 0;
			v[3] = 1;
			
			// rotate by stack angle
			v_translate(v, 0.0, curr_stack, 0.0);
			// rotate by slice angle
			v_rotate_y(v, curr_slice);
			
			// store in verts array
			verts[pos] = v;
			// initialize vertex normal
			v_norms[pos] = new GLfloat[4];
			init_vector(v_norms[pos]);
			// initalize vertex face count
			vert_faces[pos] = 0;
		}
	}
	
	pos = verts_size - 2;
	// allocate a new vertex
	v = new GLfloat[4];
	// set vertex at north pole
	v[0] = 0;
	v[1] = height;
	v[2] = 0;
	v[3] = 1;
	// store in verts array
	verts[pos] = v;
	// initialize vertex normal
	v_norms[pos] = new GLfloat[4];
	init_vector(v_norms[pos]);
	// initalize vertex face count
	vert_faces[pos] = 0;
	
	pos = verts_size - 1;
	// allocate a new vertex
	v = new GLfloat[4];
	// set vertex at south pole
	v[0] = 0;
	v[1] = -height;
	v[2] = 0;
	v[3] = 1;
	// store in verts array
	verts[pos] = v;
	// initialize vertex normal
	v_norms[pos] = new GLfloat[4];
	init_vector(v_norms[pos]);
	// initalize vertex face count
	vert_faces[pos] = 0;
	
	
	// FACES + NORMALS
	/////////////////////////////////////////////
	
	// position of face in array
	int f_pos;
	
	// first loop sets the body
	for( int i = 0; i < vs; i++ )
	{
		for( int j = 0; j < 2*(rs-1); j+= 2 )
		{
			// calculate position in the array
			pos = i * rs + j/2;
			pos2 = (i + 1) * rs +j/2;
			pos3 = pos2 + 1;
			f_pos = i * 2 * rs + j;
			
			set_face(f_pos, pos, pos2, pos3, vert_faces);
			
			f_pos++;
			pos2++;
			pos3 = pos + 1;
			
			set_face(f_pos, pos, pos2, pos3, vert_faces);
			
		}
		
		// calculate position in the array
		pos = (i * rs)+ rs - 1;
		pos2 = (i + 1) * rs + rs - 1;
		pos3 = (i + 1) * rs;
		f_pos++;
		
		set_face(f_pos, pos, pos2, pos3, vert_faces);
		
		f_pos++;
		pos2 = (i+1) * rs;
		pos3 = i * rs;
		
		set_face(f_pos, pos, pos2, pos3, vert_faces);
	}
	
	// second loop sets the top endcap
	////////////////////////////////////////
	for( int i = 0; i < rs - 1; i++ )
	{
		// set positions in arrays
		pos = verts_size-2;
		pos2 = i;
		pos3 = i + 1;
		f_pos++;
		
		set_face(f_pos, pos, pos2, pos3, vert_faces);
	}
	
	// set positions in arrays
	pos = verts_size-2;
	pos2 = pos3;
	pos3 = 0;
	f_pos++;
	
	set_face(f_pos, pos, pos2, pos3, vert_faces);
	
	// third loop sets the bottom endcap
	////////////////////////////////////////
	for( int i = 0; i < rs - 1; i++ )
	{
		// set positions in arrays
		pos = verts_size-1;
		pos3 = (vs) * rs + i;
		pos2 = pos3 + 1;
		f_pos++;
		
		set_face(f_pos, pos, pos2, pos3, vert_faces);
	}
	
	// set positions in arrays
	pos = verts_size-1;
	pos2 = pos3;
	pos3 = (vs) * rs;
	f_pos++;
	
	set_face(f_pos, pos, pos2, pos3, vert_faces);
	
	// VERTEX NORMALS
	/////////////////////////////////////////////
	
	for( int i = 0; i < verts_size; i++ )
	{
		normalize(v_norms[i]);
	}
	
	v_norms[verts_size-2][0] = 0.0;
	v_norms[verts_size-2][1] = 1.0;
	v_norms[verts_size-2][2] = 0.0;
	v_norms[verts_size-2][3] = 1.0;
	
	v_norms[verts_size-1][0] = 0.0;
	v_norms[verts_size-1][1] = -1.0;
	v_norms[verts_size-1][2] = 0.0;
	v_norms[verts_size-1][3] = 1.0;
}
示例#2
0
kernel void compute_z_by_ry_local(global int *cur_y, global int *cur_z, global int *cur_r,
				  global int *z_by_ry, local int *orig_y, local int *new_y,
				  uint N, uint D, uint K, uint f_img_width) {

  const uint V_SCALE = 0, H_SCALE = 1, V_TRANS = 2, H_TRANS = 3, NUM_TRANS = 4;
  uint nth = get_global_id(0); // nth is the index of images
  uint kth = get_global_id(1); // kth is the index of features
  uint f_img_height = D / f_img_width;

  // copy the original feature image to local memory
  for (int dth = 0; dth < D; dth++) {
    orig_y[kth * D + dth] = cur_y[kth * D + dth];
  }
  // wait until copying is done
  barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); 
    
  // vertically scale the feature image
  uint v_scale = cur_r[nth * (K * NUM_TRANS) + kth * NUM_TRANS + V_SCALE];
  scale(orig_y, new_y, kth, f_img_height, f_img_width, 0, v_scale, D);

  // copy new_y back to orig_y so that new_y can be used again
  for (int dth = 0; dth < D; dth++) {
    orig_y[kth * D + dth] = new_y[kth * D + dth];
  }
  // wait until copying is done
  barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); 

  // horizontal scale the feature image
  uint h_scale = cur_r[nth * (K * NUM_TRANS) + kth * NUM_TRANS + H_SCALE];
  scale(orig_y, new_y, kth, f_img_height, f_img_width, h_scale, 0, D);
  
  // copy new_y back to orig_y so that new_y can be used again
  for (int dth = 0; dth < D; dth++) {
    orig_y[kth * D + dth] = new_y[kth * D + dth];
  }

  // wait until copying is done
  barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); 

  // vertically translate the feature image
  uint v_dist = cur_r[nth * (K * NUM_TRANS) + kth * NUM_TRANS + V_TRANS];
  v_translate(orig_y, new_y, kth, f_img_height, f_img_width, v_dist, D);
  
  // copy new_y back to orig_y so that new_y can be used again
  for (int dth = 0; dth < D; dth++) {
    orig_y[kth * D + dth] = new_y[kth * D + dth];
  }

  // wait until copying is done
  barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); 

  // horizontally translate the feature image
  uint h_dist = cur_r[nth * (K * NUM_TRANS) + kth * NUM_TRANS + H_TRANS];
  h_translate(orig_y, new_y, kth, f_img_height, f_img_width, h_dist, D);

  // wait until all transformation is done
  barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); 

  /* at this point, for each object, a transformed y (new_y) has been generated */
  if (kth == 0) {
    for (int dth = 0; dth < D; dth++) {
      z_by_ry[nth * D + dth] = 0;
      for (int k = 0; k < K; k++) {
	z_by_ry[nth * D + dth] += new_y[k * D + dth] * cur_z[nth * K + k];
      }
    }
  }
}
示例#3
0
Torus :: Torus(float r, float r2, int vs, int rs)
{
	// set radii
	radius = r;
	radius2 = r2;
	
	// set stack interval
	float stack = (2 * M_PI) / vs;
	// set slice interval
	float slice = (2 * M_PI) / rs;
	
	// define the number of vertices
	verts_size = vs * rs;
	verts = new GLfloat*[verts_size];
	
	// define the number of faces
	faces_size = verts_size * 2;
	faces = new int*[faces_size];
	
	// define arrays for normals
	v_norms = new GLfloat*[verts_size];
	f_norms = new GLfloat*[faces_size];
	
	// define to keep track of faces 
	// for vertex normal calculation
	int vert_faces[verts_size];
	
	// VERTICES
	/////////////////////////////////////////////
	
	GLfloat* v;
	
	// current angle of rotation for stack
	float curr_stack;
	// current angle of rotation for slice
	float curr_slice;
	// current positions in the array
	int pos, pos2, pos3;
	
	// loop to set vertices
	for( int i = 0; i < vs; i++ )
	{
		// calculate stack angle
		curr_stack = -i * stack;
		
		for( int j = 0; j < rs; j++ )
		{
			// calculate slice angle
			curr_slice = j * slice;
			
			// calculate position in the array
			pos = i * rs + j;
			
			// allocate new vertex
			v = new GLfloat[4];
			v[0] = 0;
			v[1] = r2;
			v[2] = 0;
			v[3] = 1;
			
			// rotate by stack angle
			v_rotate_z(v, curr_stack);
			// translate to edge of the ring
			v_translate(v, r, 0.0, 0.0);
			// rotate by slice angle
			v_rotate_y(v, curr_slice);
			
			// store in verts array
			verts[pos] = v;
			// initialize vertex normal
			v_norms[pos] = new GLfloat[4];
			init_vector(v_norms[pos]);
			// initalize vertex face count
			vert_faces[pos] = 0;
		}
	}
	
	// FACES + NORMALS
	/////////////////////////////////////////////
	
	// position of face in array
	int f_pos;
	
	// first loop sets the body
	for( int i = 0; i < vs; i++ )
	{
		for( int j = 0; j < 2*(rs-1); j+= 2 )
		{
			// calculate position in the array
			pos = i * rs + j/2;
			if(i == vs-1)
			{
				pos2 = j/2;
			}
			else
			{
				pos2 = (i + 1) * rs +j/2;
			}
			pos3 = pos2 + 1;
			f_pos = i * 2 * rs + j;
			
			set_face(f_pos, pos, pos2, pos3, vert_faces);
			
			f_pos++;
			pos2++;
			pos3 = pos + 1;
			
			set_face(f_pos, pos, pos2, pos3, vert_faces);
			
		}
		
		// calculate position in the array
		pos = (i * rs)+ rs - 1;
		if(i == vs-1)
		{
			pos2 = rs - 1;
			pos3 = 0;
		}
		else
		{
			pos2 = (i + 1) * rs + rs - 1;
			pos3 = (i + 1) * rs;
		}
		f_pos++;
		
		set_face(f_pos, pos, pos2, pos3, vert_faces);
		
		f_pos++;
		if(i == vs-1)
		{
			pos2 = 0;
		}
		else
		{
			pos2 = (i+1) * rs;
		}
		pos3 = i * rs;
		
		set_face(f_pos, pos, pos2, pos3, vert_faces);
	}
	
	// VERTEX NORMALS
	/////////////////////////////////////////////
	
	// loop through vertices
	for( int i = 0; i < verts_size; i++ )
	{
		v_norms[i][0] = v_norms[i][0] / vert_faces[i];
		v_norms[i][1] = v_norms[i][1] / vert_faces[i];
		v_norms[i][2] = v_norms[i][2] / vert_faces[i];
		v_norms[i][3] = 1.0;
		normalize(v_norms[i]);
	}
	
}