예제 #1
0
파일: matrix.c 프로젝트: Marques199/Craft
void set_matrix_item(float *matrix, int width, int height) {
    float a[16];
    float b[16];
    float aspect = (float)width / height;
    float size = 64;
    float box = height / size / 2;
    float xoffset = 1 - size / width * 2;
    float yoffset = 1 - size / height * 2;
    mat_identity(a);
    mat_rotate(b, 0, 1, 0, -PI / 4);
    mat_multiply(a, b, a);
    mat_rotate(b, 1, 0, 0, -PI / 10);
    mat_multiply(a, b, a);
    mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 1);
    mat_multiply(a, b, a);
    mat_translate(b, -xoffset, -yoffset, 0);
    mat_multiply(a, b, a);
    mat_identity(matrix);
    mat_multiply(matrix, a, matrix);
}
예제 #2
0
static void
Redisplay(void)
{
   GLfloat rot[4][4];
   GLfloat trans[16], mvp[16];

   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   /* Build the modelview * projection matrix */
   build_rotmatrix(rot, CurQuat);
   mat_translate(trans, 0, 0, -10);
   mat_multiply(mvp, trans, (GLfloat *) rot);
   mat_multiply(mvp, Projection, mvp);
   /* Set the MVP matrix */
   glUniformMatrix4fv(uModelViewProj, 1, GL_FALSE, (float *) mvp);

   /* Draw */
   glDrawArrays(GL_TRIANGLES, 0, 3);

   glutSwapBuffers();
}
예제 #3
0
파일: cube.c 프로젝트: Aaron1011/Craft
void make_player(
    float *data,
    float x, float y, float z, float rx, float ry)
{
    float ao[6][4] = {0};
    make_cube_faces(
        data, ao,
        1, 1, 1, 1, 1, 1,
        226, 224, 241, 209, 225, 227,
        0, 0, 0, 0.4);
    float ma[16];
    float mb[16];
    mat_identity(ma);
    mat_rotate(mb, 0, 1, 0, rx);
    mat_multiply(ma, mb, ma);
    mat_rotate(mb, cosf(rx), 0, sinf(rx), -ry);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 36, 3, 9);
    mat_translate(mb, x, y, z);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 36, 0, 9);
}
void palImpulseActuator::Apply()
{
	palMatrix4x4 m;
	mat_identity(&m);
	mat_translate(&m,m_fRelativePosX,m_fRelativePosY,m_fRelativePosZ);
	//printf("rel:%f %f %f  ",m._41,m._42,m._43);
	palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();
	

	palMatrix4x4 out;

	mat_multiply(&out,&bodypos,&m);
	palVector3 newpos;
	newpos.x=out._41;
	newpos.y=out._42;
	newpos.z=out._43;
//	printf("output : %f %f %f ",out._41,out._42,out._43);

//	imp_pos=out;
	
	mat_identity(&m);
	mat_translate(&m,m_fAxisX,m_fAxisY,m_fAxisZ);
	mat_multiply(&out,&bodypos,&m);

//	printf("output : %f %f %f\n",out._41,out._42,out._43);

//	imp_axis=out;

	palVector3 newaxis;
	newaxis.x=out._41-bodypos._41;
	newaxis.y=out._42-bodypos._42;
	newaxis.z=out._43-bodypos._43;
	vec_norm(&newaxis);

	m_pBody->ApplyImpulseAtPosition(newpos.x,newpos.y,newpos.z,newaxis.x*m_fImpulse,newaxis.y*m_fImpulse,newaxis.z*m_fImpulse);


	}
예제 #5
0
파일: main.c 프로젝트: kanitaoru/Craft
void update_matrix_item(float *matrix) {
    float a[16];
    float b[16];
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);
    float aspect = (float)width / height;
    float size = 64;
    float box = height / size / 2;
    float xoffset = 1 - size / width * 2;
    float yoffset = 1 - size / height * 2;
    mat_identity(a);
    mat_rotate(b, 0, 1, 0, PI / 4);
    mat_multiply(a, b, a);
    mat_rotate(b, 1, 0, 0, -PI / 10);
    mat_multiply(a, b, a);
    mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 1);
    mat_multiply(a, b, a);
    mat_translate(b, -xoffset, -yoffset, 0);
    mat_multiply(a, b, a);
    mat_identity(matrix);
    mat_multiply(matrix, a, matrix);
}
void mat_translate(matrix4 m, vector3 v)
{
    matrix4 m2;
    m2[0][0] =
    m2[1][1] =
    m2[2][2] =
    m2[3][3] = 1.0f;
    m2[0][1] =
    m2[0][2] =
    m2[0][3] =
    m2[1][0] =
    m2[1][2] =
    m2[1][3] =
    m2[2][0] =
    m2[2][1] =
    m2[2][3] = 0.0f;
    m2[3][0] = v[0];
    m2[3][1] = v[1];
    m2[3][2] = v[2];
    mat_multiply(m, m2);
}
void palPropeller::Apply() {

	palMatrix4x4 m;
	mat_identity(&m);
	mat_translate(&m,m_fRelativePosX,m_fRelativePosY,m_fRelativePosZ);
	//printf("rel:%f %f %f  ",m._41,m._42,m._43);
	palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();


	palMatrix4x4 out;

	mat_multiply(&out,&bodypos,&m);
	palVector3 newpos;
	newpos.x=out._41;
	newpos.y=out._42;
	newpos.z=out._43;
	if (newpos.y > 0.1f ) return;

	Float thrust = m_Voltage*m_a_l;
	SetImpulse(thrust);
	palImpulseActuator::Apply();
}
예제 #8
0
void make_cube(
    float *data, float ao[6][4], float light[6][4],
    int left, int right, int top, int bottom, int front, int back,
    float x, float y, float z, float n, int w, const int blocks[256][6])
{
    int wleft = blocks[w][0];
    int wright = blocks[w][1];
    int wtop = blocks[w][2];
    int wbottom = blocks[w][3];
    int wfront = blocks[w][4];
    int wback = blocks[w][5];
    make_cube_faces(
        data, ao, light,
        left, right, top, bottom, front, back,
        wleft, wright, wtop, wbottom, wfront, wback,
        n);
    float ma[16];
    float mb[16];
    mat_identity(ma);
    mat_translate(mb, x, y, z);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, (left + right + top + bottom + front + back)*6, 0, 10);
}
void palForceActuator::Apply() {
	palMatrix4x4 m,resp;
	mat_identity(&m);
	mat_translate(&m,m_fRelativePosX,m_fRelativePosY,m_fRelativePosZ);
	palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();
	mat_multiply(&resp,&m,&bodypos); //the acting force points resulting position.
	palVector3 tpos,respos;
	tpos.x=resp._41;	tpos.y=resp._42;	tpos.z=resp._43;
	vec_mat_mul(&respos,&bodypos,&tpos);
	//printf("body is at      : %f %f %f\n",bodypos._41,bodypos._42,bodypos._43);
	//printf("forcepoint is at: %f %f %f\n",respos.x,respos.y,respos.z);
	palVector3 axis,resaxis;
	axis.x=m_fAxisX;	axis.y=m_fAxisY;	axis.z=m_fAxisZ;
	//printf("axis was: %f %f %f\n",axis.x,axis.y,axis.z);
	vec_mat_mul(&resaxis,&bodypos,&axis);
	//printf("axis is:  %f %f %f\n",resaxis.x,resaxis.y,resaxis.z);
	//printf("forcepoint is at: %f %f %f\n",resp._41,resp._42,resp._43);
	//m_pBody->AddForceAtPosition(respos.x,respos.y,respos.z,resaxis.x*m_fForce,resaxis.y*m_fForce,resaxis.z*m_fForce);
	//m_pBody->AddForceAtPosition(respos.x,respos.y,respos.z,axis.x*m_fForce,axis.y*m_fForce,axis.z*m_fForce);
	m_pBody->ApplyForceAtPosition(tpos.x,tpos.y,tpos.z,axis.x*m_fForce,axis.y*m_fForce,axis.z*m_fForce);
	//m_pBody->AddForceAtPosition(1,1,0,axis.x*m_fForce,axis.y*m_fForce,axis.z*m_fForce);
	
}
예제 #10
0
파일: matrix.cpp 프로젝트: jlirochon/client
void set_matrix_item_r(float *matrix, int width, int height, float scale,
        float xoffset, float yoffset, float rx, float ry, float rz) {
    float a[16];
    float b[16];
    float aspect = (float)width / height;
    float size = 64 * scale;
    float box = height / size / 2;
    mat_identity(a);
    mat_rotate(b, 0, 1, 0, rx);
    mat_multiply(a, b, a);
    mat_rotate(b, 1, 0, 0, ry);
    mat_multiply(a, b, a);
    mat_rotate(b, 0, 0, 1, rz);
    mat_multiply(a, b, a);
    mat_ortho(b, -box * aspect, box * aspect, -box, box, -3, 2);
    mat_multiply(a, b, a);
    mat_translate(b, -xoffset, -yoffset, 0);
    mat_multiply(a, b, a);
    mat_identity(matrix);
    mat_multiply(matrix, a, matrix);
}
예제 #11
0
파일: summa.c 프로젝트: dougblack/parallel
void
summa_mult (int m, int n, int k, int s_max,
	    const double* A_local, const double* B_local,
	    double* C_local, MPI_Comm comm2d,
	    double* p_t_comp, double* p_t_comm)
{
  int rank = mpih_getRank (comm2d);
  int P_row, P_col;
  summa_getProcSize (comm2d, &P_row, &P_col);
  int rank_row, rank_col;
  summa_getProcCoords (comm2d, &rank_row, &rank_col);
  MPI_Comm comm_row = getCommRow__ (comm2d);
  MPI_Comm comm_col = getCommCol__ (comm2d);

  int m_local, n_local;
  summa_getMatDims (m, n, comm2d, &m_local, &n_local);

  double* A_strip = (double *)malloc (m_local * s_max * sizeof (double));
  double* B_strip = (double *)malloc (s_max * n_local * sizeof (double));
  mpih_assert (A_strip && B_strip);

  double t_comp = 0, t_comm = 0; /* Time in computation vs. communication */
  int iter_k = 0; /* iterates over strips in k dimension */
  int owner_A = 0; /* owner of current A strip */
  int owner_B = 0; /* owner of current B strip */
  while (iter_k < k) {
    int k_start_A = mm1d_getBlockStart (k, P_col, owner_A);
    int k_local_A = mm1d_getBlockLength (k, P_col, owner_A);

    int k_start_B = mm1d_getBlockStart (k, P_row, owner_B);
    int k_local_B = mm1d_getBlockLength (k, P_row, owner_B);

    /* Determine a strip width, s, that still resides in both the
       current A and B blocks. */
    int s = min_int (s_max, min_int (k_start_A + k_local_A - iter_k,
				     k_start_B + k_local_B - iter_k));

    double t_start = MPI_Wtime (); /* For timing communication */
    /* Step 1: Broadcast m_local x s strip of A */
    /* === @@ YOUR CODE GOES HERE @@ === */
    /* Step 2: Broadcast s x n_local strip of B */
    /* === @@ YOUR CODE GOES HERE @@ === */
    t_comm += MPI_Wtime () - t_start;

    /* Step 3: Local multiply */
    t_start = MPI_Wtime (); /* For timing computation */
    mat_multiply (m_local, n_local, s,
		  A_strip, m_local, B_strip, s,
		  C_local, m_local);
    t_comp += MPI_Wtime () - t_start;

    iter_k += s;
    if (iter_k >= k_start_A + k_local_A) ++owner_A;
    if (iter_k >= k_start_B + k_local_B) ++owner_B;
  }

  /* Clean-up and return */
  MPI_Comm_free (&comm_row);
  MPI_Comm_free (&comm_col);
  free (A_strip);
  free (B_strip);
  if (p_t_comp) *p_t_comp = t_comp;
  if (p_t_comm) *p_t_comm = t_comm;
}
예제 #12
0
static
void
verify__ (int m, int n, int k)
{
  MPI_Comm comm = MPI_COMM_WORLD;
  if (!isEnvEnabled_mpi__ (comm, "VERIFY", 1)) return;

  double* A = NULL;
  double* B = NULL;
  double* C_soln = NULL;
  double* C_bound = NULL;

  /* First, run the trusted sequential version */
  int rank = mpih_getRank (comm);
  if (rank == 0) {
    setupSeqProblem__ (m, n, k, &A, &B, &C_soln, &C_bound);

    /* Measure time for the sequential problem. */
    mat_setZero (m, n, C_soln);
    double t_start = MPI_Wtime ();
    mat_multiply (m, n, k, A, m, B, k, C_soln, m);
    double dt_seq = MPI_Wtime () - t_start;
    mpih_debugmsg (comm, "t_seq = %g s\n", dt_seq);

    /* Recompute, to get the error bound this time */
    mpih_debugmsg (MPI_COMM_WORLD, "Estimating error bound...\n");
    mat_multiplyErrorbound (m, n, k, A, m, B, k, C_soln, m, C_bound, m);
  }

  /* Next, run the untrusted 1D algorithm */
  if (rank == 0) mpih_debugmsg (comm, "Distributing A, B, and C...\n");
  double* A_local = mm1d_distribute (m, k, A, comm);
  double* B_local = mm1d_distribute (k, n, B, comm);
  double* C_local = mm1d_alloc (m, n, comm);
  mm1d_setZero (m, n, C_local, comm);

  /* Do multiply */
  if (rank == 0) mpih_debugmsg (comm, "Computing C <- C + A*B...\n");
  mm1d_mult (m, n, k, A_local, B_local, C_local, comm, NULL, NULL);

  /* Compare the two answers (in parallel) */
  if (rank == 0) mpih_debugmsg (comm, "Verifying...\n");
  int P = mpih_getSize (comm);
  double* C_soln_local = mm1d_distribute (m, n, C_soln, comm);
  double* C_bound_local = mm1d_distribute (m, n, C_bound, comm);
  for (int i = 0; i < m; ++i) {
    int n_local = mm1d_getBlockLength (n, P, rank);
    for (int j = 0; j < n_local; ++j) {
      const double errbound = C_bound_local[i + j*m] * 3.0 * k * DBL_EPSILON;
      const double c_trusted = C_soln_local[i + j*m]; 
      const double c_untrusted = C_local[i + j*m];
      double delta = fabs (c_untrusted - c_trusted);
      if (delta > errbound)
	mpih_debugmsg (comm,
		       "*** Entry (%d, %d) --- Error bound violated ***\n    ==> |%g - %g| == %g > %g\n",
		       c_untrusted, c_trusted, delta, errbound, i, j);
      mpih_assert (delta <= errbound);
    }
  }
  if (rank == 0) mpih_debugmsg (comm, "Passed!\n");

  /* Cleanup */
  if (rank == 0) {
    free (A);
    free (B);
    free (C_soln);
    free (C_bound);
  }
  mm1d_free (A_local, comm);
  mm1d_free (B_local, comm);
  mm1d_free (C_local, comm);
}
void palFakeBuoyancy::Apply() {
		palMatrix4x4 m;
		m=m_pBody->GetLocationMatrix();
		palBoxGeometry *pbg;
		pbg = dynamic_cast<palBoxGeometry *>(m_pBody->m_Geometries[0]);
		if (pbg ){
			/*
			float box_height = pbg->m_fHeight;
			if (m._42 < box_height) {
				float hunder = box_height - m._42; //how much of the box's height is under water?
				if (hunder > box_height) hunder = box_height; //maximum is box height

				float volume = pbg->m_fWidth*pbg->m_fDepth * hunder;

				float gravity = 9.8f;

				float fb = volume * gravity * m_density;

				m_pBody->AddForce(0,fb,0);
			}
			*/
			Float volume = pbg->m_fWidth*pbg->m_fDepth * pbg->m_fHeight;
			Float quatervolume = volume/4;
			Float quatersphereradius = (Float) pow((3/4.0)*quatervolume / M_PI,1/3.0);

			Float r = quatersphereradius;
			//r = pbg->m_fHeight

			palMatrix4x4 m;
			palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();
			palMatrix4x4 out;

			mat_identity(&m);
			mat_translate(&m,pbg->m_fWidth*0.5f,0,0);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
				CalcSphereForce(r,out._42,m_density),0);
//					CalcSphereForce(pbg->m_fHeight,out._42,m_density,volume*0.5f),0);

			mat_identity(&m);
			mat_translate(&m,-pbg->m_fWidth*0.5f,0,0);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
					CalcSphereForce(r,out._42,m_density),0);
//					CalcSphereForce(pbg->m_fHeight,out._42,m_density,volume*0.5f),0);

			//the following are the z-axis floats:
			mat_identity(&m);
			mat_translate(&m,0,0,pbg->m_fDepth*0.5f);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
				CalcSphereForce(r,out._42,m_density),0);

			mat_identity(&m);
			mat_translate(&m,0,0,-pbg->m_fDepth*0.5f);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
				CalcSphereForce(r,out._42,m_density),0);

		} 
		palSphereGeometry *psg;
		psg = dynamic_cast<palSphereGeometry *>(m_pBody->m_Geometries[0]);
		if (psg) {
			m_pBody->ApplyForce(0,
				CalcSphereForce(psg->m_fRadius,m._42,m_density),0);
			/*
			float sphere_radius = psg->m_fRadius;
			if (m._42 < sphere_radius*2) {
				float hunder = sphere_radius*2 - m._42; //how much of the box's height is under water?
				if (hunder > sphere_radius*2) hunder = sphere_radius*2; //maximum is box height
				//float volume = sphere_radius*sphere_radius * hunder; //wrong wrong wrong.

				float volume =  M_PI * hunder * (3 * sphere_radius * hunder - hunder*hunder) / 3.0f;

				float gravity = 9.8f;
				float fb = volume * gravity * m_density;
				m_pBody->AddForce(0,fb,0);
			}
			*/
		}
	}
예제 #14
0
void sc_light_updateSpotMtx(ENTITY* inLight)
{
	SC_OBJECT* ObjData = (SC_OBJECT*)(inLight.SC_SKILL);
	
	
	float fTexAdj[16] =
	{ 0.5, 0.0, 0.0, 0.0,
		0.0,-0.5,	0.0, 0.0,
		0.0, 0.0, 1.0, 0.0,
	0.0, 0.0, 0.0, 1.0 };
	
	fTexAdj[12] = 0.5 + ((float)0.5/(float)256);
	fTexAdj[13] = 0.5 + ((float)0.5/(float)256);
	
	
	VECTOR lightDir;

	//lightDir = malloc(sizeof(VECTOR));
	if(inLight.tilt > 89.9 && inLight.tilt < 90.1 || inLight.tilt < -89.9 && inLight.tilt > -90.1)
	{
		vec_set(lightDir, inLight.pan);
		lightDir.y += 0.2;
		vec_for_angle(lightDir,lightDir);
	}
	else vec_for_angle(lightDir, inLight.pan);
	
	sc_skill(inLight, SC_OBJECT_LIGHT_DIR, lightDir);
	//if(lightDir.y == -90) lightDir.y = 80; //might have to comment this in again (?)
	
	/*
	//if spotlight has a shadowview attached
	if(ObjData.light.view != NULL)
	{
		D3DXMATRIX matView_, matProj_;
		view_to_matrix(ObjData.light.view, &matView_, &matProj_);
		mat_multiply(&matView_, &matProj_);
		mat_multiply(&matView_, fTexAdj);
		sc_skill(inLight, SC_OBJECT_LIGHT_MATRIX, &matView_);
		
		ptr_remove(matView_);
		ptr_remove(matProj_);
		return;
	}
	*/
	
	
	//create lightViewMatrix
	D3DXVECTOR3 vEyePt;
	D3DXVECTOR3 vLookatPt;
	D3DXVECTOR3 vUpVec;
	//vEyePt = sys_malloc(sizeof(D3DXVECTOR3));
	//vLookatPt = sys_malloc(sizeof(D3DXVECTOR3));
	//vUpVec = sys_malloc(sizeof(D3DXVECTOR3));
	
	vEyePt.x = inLight.x;
	vEyePt.y = inLight.z;
	vEyePt.z = inLight.y;
	vLookatPt.x = inLight.x + lightDir.x;	
	vLookatPt.y = inLight.z + lightDir.z;
	vLookatPt.z = inLight.y + lightDir.y;
	vUpVec.x = 0;
	vUpVec.y = 1;
	vUpVec.z = 0;

	D3DXMATRIX mtxLightView;
	//mtxLightView = sys_malloc(sizeof(D3DXMATRIX));
	D3DXMatrixLookAtLH( &mtxLightView, vEyePt, vLookatPt, vUpVec);
	
	//create lightProjectionMatrix
	D3DXMATRIX mtxLightProj;
	D3DXMatrixPerspectiveFovLH(&mtxLightProj, 	(((float)ObjData.light.arc/(float)180))*SC_PI, (float)1, (float)1, (float)ObjData.light.range );
	
	
	//pass projection matrix to light	
	D3DXMATRIX mtxLightWorldViewProj;
	//mtxLightWorldViewProj = sys_malloc(sizeof(D3DXMATRIX));
	mat_set(&mtxLightWorldViewProj, &mtxLightView);
	mat_multiply(&mtxLightWorldViewProj, &mtxLightProj);
	mat_multiply(&mtxLightWorldViewProj, fTexAdj);
	
	sc_skill(inLight, SC_OBJECT_LIGHT_MATRIX, &mtxLightWorldViewProj);
	
	/*
	//this lags like hell
	sys_free(vEyePt);
	sys_free(vLookatPt);
	sys_free(vUpVec);
	sys_free(mtxLightView);
	sys_free(mtxLightProj);
	sys_free(mtxLightWorldViewProj);
	*/
	
	//better
	ptr_remove(vEyePt);
	ptr_remove(vLookatPt);
	ptr_remove(vUpVec);
	ptr_remove(mtxLightView);
	ptr_remove(mtxLightProj);
	ptr_remove(mtxLightWorldViewProj);
}
예제 #15
0
  void example_matrix_utilities() {
    // Run this and look at stdout to see examples using the multi-dimensional matrix.
    // Note: MatrixF is a typedef for Matrix<float>
    cout << "Matrix utility examples." << endl;

    cout << "// 3 x 4 matrix, initialized to 0." << endl;
    cout << "MatrixF X(3,4);" << endl;
    MatrixF X(3,4);

    cout << "cout << X << endl;" << endl;
    cout << X << endl;

    cout << "// Create range of values in X." << endl;

    cout << "float i = 0;" << endl;
    float i = 0;
    cout << "// Fill up X." << endl;
    cout << R"(apply_sequential(X, [&] (float a) {
        // Ignore the value of a.
        i += 0.1f;
        return i;
      });)" << endl;
    apply_sequential(X, [&] (float a) {
        // Ignore the value of a.
        i += 0.1f;
        return i;
      });

    cout << "cout << X << endl;" << endl;
    cout << X << endl;

    cout << "// Apply sqrt() to all elements in X." << endl;
    cout << R"(apply(X, [] (float a) {
        return std::sqrt(a);
      });)" << endl;

    apply(X, [] (float a) {
        return std::sqrt(a);
      });

    cout << "cout << X << endl;" << endl;
    cout << X << endl;

    cout << "MatrixF Y(4,5);" << endl;
    MatrixF Y(4,5);

    cout << "cout << Y << endl;" << endl;
    cout << Y << endl;

    cout << "i = 0;" << endl;
    i = 0;

    cout << R"(apply_sequential(Y, [&] (float a) {
        // Ignore the value of a.
        i += 1.0f;
        return i;
      });)" << endl;
    apply_sequential(Y, [&] (float a) {
        // Ignore the value of a.
        i += 1.0f;
        return i;
      });

    cout << endl << "// map2() example:" << endl;

    cout << "cout << Y << endl;" << endl;
    cout << Y << endl;

    cout << "MatrixF A = Y;" << endl;
    MatrixF A = Y;


    cout << "cout << A << endl;" << endl;
    cout << A << endl;

    cout << "MatrixF B = Y;" << endl;
    MatrixF B = Y;

    cout << "cout << B << endl;" << endl;
    cout << B << endl;

    cout << "// Apply map2(): " << endl;
    cout << R"(map2(Y, A, B, [] (float a, float b) {
        return a + 2*b;
      });)" << endl << endl;

    map2(Y, A, B, [] (float a, float b) {
        return a + 2*b;
      });

    cout << "cout << Y << endl;" << endl;
    cout << Y << endl;

    cout << "// narrow() example:" << endl;

    i = 0;

    cout << R"(apply_sequential(Y, [&] (float a) {
        // Ignore the value of a.
        i += 1.0f;
        return i;
      });)" << endl;
    apply_sequential(Y, [&] (float a) {
        // Ignore the value of a.
        i += 1.0f;
        return i;
      });

    cout << "cout << Y << endl;" << endl;
    cout << Y << endl;

    cout << "MatrixF D = narrow(Y, 1, 1, 2);" << endl;
    MatrixF D = narrow(Y, 1, 1, 2);

    cout << "cout << D << endl;" << endl;
    cout << D << endl;

    cout << "// Now randomize D:" << endl;
    cout << "randomize_normal(D, 1.0f, 1.0f);" << endl;
    randomize_normal(D, 1.0f, 1.0f);

    cout << "cout << D << endl;" << endl;
    cout << D << endl;

    cout << "// Now copy data from D back into same locations in Y:" << endl;
    cout << "reverse_narrow(D, Y, 1, 1, 2);" << endl;
    reverse_narrow(D, Y, 1, 1, 2);

    cout << "cout << Y << endl;" << endl;
    cout << Y << endl;

    cout << "// Matrix multilication example:" << endl;
    cout << "MatrixF U(3,4);" << endl;
    MatrixF U(3,4);
    cout << "cout << U << endl;" << endl;
    cout << U << endl;
    cout << "randomize_uniform(U, -1.0f, 1.0f);" << endl;
    randomize_uniform(U, -1.0f, 1.0f);
    cout << "cout << U << endl;" << endl;
    cout << U << endl;
    cout << "MatrixF R(4,5);" << endl;
    MatrixF R(4,5);
    cout << "cout << R << endl;" << endl;
    cout << R << endl;
    cout << "set_value(R, 1.0f);" << endl;
    set_value(R, 1.0f);
    cout << "cout << R << endl;" << endl;
    cout << R << endl;    
    cout << "// Compute C = U*R:" << endl;
    cout << "MatrixF C;" << endl;
    MatrixF C;
    cout << "// Note: C has not been initialized to the required output dimensions but will be " << endl;
    cout << "// resized to the correct dimensions inside the matrix multiplication function." << endl;
    cout << "// Many of the matrix utility functions work like this (auto re-sizing of result)." << endl;
    cout << "mat_multiply(C, U, R);" << endl;
    mat_multiply(C, U, R);
    cout << "cout << C << endl;" << endl;
    cout << C << endl;

  }
예제 #16
0
파일: cube.c 프로젝트: Princever/MiniWorld
void make_plant(
    float *data, float ao, float light,
    float px, float py, float pz, float n, int w, float rotation)
{
    //四个面的顶点
    static const float positions[4][4][3] = {
        {{ 0, -1, -1}, { 0, -1, +1}, { 0, +1, -1}, { 0, +1, +1}},
        {{ 0, -1, -1}, { 0, -1, +1}, { 0, +1, -1}, { 0, +1, +1}},
        {{-1, -1,  0}, {-1, +1,  0}, {+1, -1,  0}, {+1, +1,  0}},
        {{-1, -1,  0}, {-1, +1,  0}, {+1, -1,  0}, {+1, +1,  0}}
    };
    //x,z方向
    static const float normals[4][3] = {
        {-1, 0, 0},
        {+1, 0, 0},
        {0, 0, -1},
        {0, 0, +1}
    };
    //贴图坐标
    static const float uvs[4][4][2] = {
        {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
        {{1, 0}, {0, 0}, {1, 1}, {0, 1}},
        {{0, 0}, {0, 1}, {1, 0}, {1, 1}},
        {{1, 0}, {1, 1}, {0, 0}, {0, 1}}
    };
    //索引
    static const float indices[4][6] = {
        {0, 3, 2, 0, 1, 3},
        {0, 3, 1, 0, 2, 3},
        {0, 3, 2, 0, 1, 3},
        {0, 3, 1, 0, 2, 3}
    };
    float *d = data;
    float s = 0.0625;
    float a = 0;
    float b = s;
    float du = (plants[w] % 16) * s;
    float dv = (plants[w] / 16) * s;
    for (int i = 0; i < 4; i++) {
        for (int v = 0; v < 6; v++) {
            int j = indices[i][v];
            *(d++) = n * positions[i][j][0];
            *(d++) = n * positions[i][j][1];
            *(d++) = n * positions[i][j][2];
            *(d++) = normals[i][0];
            *(d++) = normals[i][1];
            *(d++) = normals[i][2];
            *(d++) = du + (uvs[i][j][0] ? b : a);
            *(d++) = dv + (uvs[i][j][1] ? b : a);
            *(d++) = ao;
            *(d++) = light;
        }
    }
    float ma[16];
    float mb[16];
    mat_identity(ma);
    mat_rotate(mb, 0, 1, 0, RADIANS(rotation));
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 24, 3, 10);
    mat_translate(mb, px, py, pz);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 24, 0, 10);
}
예제 #17
0
static
void
verify__ (int m, int n, int k, int P_row, int P_col, int s)
{
  if (!checkEnvEnabled__ ("VERIFY", 1)) return;

  MPI_Comm comm2d = summa_createTopology (MPI_COMM_WORLD, P_row, P_col);
  int rank = mpih_getRank (comm2d);

  double* A = NULL;
  double* B = NULL;
  double* C_soln = NULL;
  double* C_bound = NULL;

  /* Whoever has rank == 0 will create the test problem. */
  if (rank == 0) {
    setupSeqProblem__ (m, n, k, &A, &B, &C_soln, &C_bound);

    /* Measure time for the sequential problem. */
    mat_setZero (m, n, C_soln);
    double t_start = MPI_Wtime ();
    mat_multiply (m, n, k, A, m, B, k, C_soln, m);
    double dt_seq = MPI_Wtime () - t_start;
    mpih_debugmsg (MPI_COMM_WORLD, "t_seq = %g s\n", dt_seq);

    /* Recompute, to get the error bound this time */
    mpih_debugmsg (MPI_COMM_WORLD, "Estimating error bound...\n");
    mat_multiplyErrorbound (m, n, k, A, m, B, k, C_soln, m, C_bound, m);
  }

  /* Next, run the (untrusted) SUMMA algorithm */
  if (rank == 0) mpih_debugmsg (comm2d, "Distributing A, B, and C...\n");
  double* A_local = summa_distribute (m, k, A, 0, comm2d);
  double* B_local = summa_distribute (k, n, B, 0, comm2d);
  double* C_local = summa_alloc (m, n, comm2d);
  summa_setZero (m, n, C_local, comm2d);

  /* Do multiply */
  if (rank == 0) mpih_debugmsg (comm2d, "Computing C <- C + A*B...\n");
  summa_mult (m, n, k, s, A_local, B_local, C_local, comm2d, NULL, NULL);

  /* Compare the two answers (in parallel) */
  if (rank == 0) mpih_debugmsg (comm2d, "Verifying...\n");
  int rank_row, rank_col;
  summa_getProcCoords (comm2d, &rank_row, &rank_col);
  double* C_soln_local = summa_distribute (m, n, C_soln, 0, comm2d);
  double* C_bound_local = summa_distribute (m, n, C_bound, 0, comm2d);
  int m_local = mm1d_getBlockLength (m, P_row, rank_row);
  int n_local = mm1d_getBlockLength (n, P_col, rank_col);
  for (int i = 0; i < m_local; ++i) {
    for (int j = 0; j < n_local; ++j) {
      const double errbound = C_bound_local[i + j*m_local] * 3.0 * k * DBL_EPSILON;
      const double c_trusted = C_soln_local[i + j*m_local]; 
      const double c_untrusted = C_local[i + j*m_local];
      double delta = fabs (c_untrusted - c_trusted);
      if (delta > errbound)
	mpih_debugmsg (comm2d,
		       "*** Entry (%d, %d) --- Error bound violated ***\n    ==> |%g - %g| == %g > %g\n",
		       c_untrusted, c_trusted, delta, errbound, i, j);
      mpih_assert (delta <= errbound);
    }
  }
  if (rank == 0) mpih_debugmsg (comm2d, "Passed!\n");

  /* Clean-up */
  summa_free (A_local, comm2d);
  summa_free (B_local, comm2d);
  summa_free (C_local, comm2d);
  summa_free (C_soln_local, comm2d);
  summa_free (C_bound_local, comm2d);
  if (rank == 0) {
    free (A);
    free (B);
    free (C_soln);
    free (C_bound);
  }
  summa_freeTopology (comm2d);
}