void TransformationMatrix::recompose(const DecomposedType& decomp)
{
    makeIdentity();
    
    // first apply perspective
    m_matrix[0][3] = (float) decomp.perspectiveX;
    m_matrix[1][3] = (float) decomp.perspectiveY;
    m_matrix[2][3] = (float) decomp.perspectiveZ;
    m_matrix[3][3] = (float) decomp.perspectiveW;
    
    // now translate
    translate3d((float) decomp.translateX, (float) decomp.translateY, (float) decomp.translateZ);
    
    // apply rotation
    double xx = decomp.quaternionX * decomp.quaternionX;
    double xy = decomp.quaternionX * decomp.quaternionY;
    double xz = decomp.quaternionX * decomp.quaternionZ;
    double xw = decomp.quaternionX * decomp.quaternionW;
    double yy = decomp.quaternionY * decomp.quaternionY;
    double yz = decomp.quaternionY * decomp.quaternionZ;
    double yw = decomp.quaternionY * decomp.quaternionW;
    double zz = decomp.quaternionZ * decomp.quaternionZ;
    double zw = decomp.quaternionZ * decomp.quaternionW;
    
    // Construct a composite rotation matrix from the quaternion values
    TransformationMatrix rotationMatrix(1 - 2 * (yy + zz), 2 * (xy - zw), 2 * (xz + yw), 0, 
                           2 * (xy + zw), 1 - 2 * (xx + zz), 2 * (yz - xw), 0,
                           2 * (xz - yw), 2 * (yz + xw), 1 - 2 * (xx + yy), 0,
                           0, 0, 0, 1);
    
    multLeft(rotationMatrix);
    
    // now apply skew
    if (decomp.skewYZ) {
        TransformationMatrix tmp;
        tmp.setM32((float) decomp.skewYZ);
        multLeft(tmp);
    }
    
    if (decomp.skewXZ) {
        TransformationMatrix tmp;
        tmp.setM31((float) decomp.skewXZ);
        multLeft(tmp);
    }
    
    if (decomp.skewXY) {
        TransformationMatrix tmp;
        tmp.setM21((float) decomp.skewXY);
        multLeft(tmp);
    }
    
    // finally, apply scale
    scale3d((float) decomp.scaleX, (float) decomp.scaleY, (float) decomp.scaleZ);
}
Exemplo n.º 2
0
//WARNING: This function probably doesn't work AT ALL!
float * CalculateTangentSpace( int Triangles, int VertexCount, int * Indices, float * verts, float * normals, float * texs )
{
	//Here is the place to calculate the Tangent values.
	//It is a vector pointing in the direction of increasing u.
	int i;
	float * tans = 0;

	if( normals && texs && verts )
	{
		//If we have both Texture coords and normals, we can calculate a tangent matrix.
		tans = malloc( VertexCount * sizeof( float ) * 4 );
		memset( tans, 0, VertexCount * sizeof( float ) * 4 );

		//Process modeled after: http://www.terathon.com/code/tangent.html

		float * tan1 = malloc( VertexCount * sizeof( float ) * 3 );
		float * tan2 = malloc( VertexCount * sizeof( float ) * 3 );

		memset( tan1, 0, VertexCount * sizeof( float ) * 3 );
		memset( tan2, 0, VertexCount * sizeof( float ) * 3 );

		for( i = 0; i < Triangles; i++ )
		{
			int v1 = Indices[i*3+0];
			int v2 = Indices[i*3+1];
			int v3 = Indices[i*3+2];


			float * t1 = &texs[v1*3];
			float * t2 = &texs[v2*3];
			float * t3 = &texs[v3*3];
			float * p1 = &verts[v1*3];
			float * p2 = &verts[v2*3];
			float * p3 = &verts[v3*3];

			float vec1[3];
			float vec2[3];
			float tex1[3];
			float tex2[3];

			sub3d( vec1, p2, p1 ); //(x,y,z)
			sub3d( vec2, p3, p1 );
			sub3d( tex1, t2, t1 ); //(s,t,u)
			sub3d( tex2, t3, t1 );

	        float r = 1.0f / ( tex1[0] * tex2[1] - tex2[0] * tex1[1] );
			float sdir[3] = {
				(tex2[1] * vec1[0] - tex1[1] * vec2[0]) * r,
				(tex2[1] * vec1[1] - tex1[1] * vec2[1]) * r,
                (tex2[1] * vec1[2] - tex1[1] * vec2[2]) * r };
			float tdir[3] = {
				(tex1[0] * vec2[0] - tex2[0] * vec1[0]) * r,
				(tex1[0] * vec2[1] - tex2[0] * vec1[1]) * r,
                (tex1[0] * vec2[2] - tex2[0] * vec1[2]) * r };

			add3d( &tan1[v1*3], &tan1[v1*3], sdir );
			add3d( &tan1[v2*3], &tan1[v2*3], sdir );
			add3d( &tan1[v3*3], &tan1[v3*3], sdir );

			add3d( &tan2[v1*3], &tan2[v1*3], sdir );
			add3d( &tan2[v2*3], &tan2[v2*3], sdir );
			add3d( &tan2[v3*3], &tan2[v3*3], sdir );
		}

		//Normalize and orthoganlize.
		for (i = 0; i < VertexCount; i++)
		{
		    const float * n = &normals[i*3];
		    const float * t = &tan1[i*3];
		    
		    // Gram-Schmidt orthogonalize
			float tmp[3];
			float tdn[3];
			float thisdot = dot3d( n, t );
			scale3d( tdn, n, thisdot );
			sub3d( tmp, t, tdn );
			normalize3d( &tans[i*4], tmp );

			cross3d( tmp, n, t );
			tans[i*4+3] = (dot3d( tmp, &tan2[i*3] ) < 0)?-1:1; //set handedness
		}
	}

	return tans;
}
Exemplo n.º 3
0
int main(int argc, char** argv)
{
  // parse command
  unsigned int pos = 1;
  unsigned int mask = (argc >= 2) ? 0 : 0xFFFF;
  while(pos < argc)
  {
    unsigned int sceneNumber = atoi(argv[pos++]);
    mask |= 1 << sceneNumber;
  }


  // Initialize
  phong p32(32.0f);
  phong p64(64.0f);
  phong p16(16.0f);
  diffuse d;
  constantAlbedo red(color(1.0f, 0.0f, 0.0f));
  constantAlbedo green(color(0.0f, 1.0f, 0.0f));
  constantAlbedo blue(color(0.0f, 0.0f, 1.0f));
  constantAlbedo white(color(1.0f, 1.0f, 1.0f));

  std::vector<triangle> sphere = createSphere( vec3d(0.0f, 0.0f, 0.0f), 1.0f, 16, 16 );
  std::vector<triangle> plane = createPlane( vec3d(-1.0f, -1.0f, 0.0f), vec3d(0.0f, 2.0f, 0.0f), vec3d(2.0f, 0.0f, 0.0f) );

  material redMaterial;
  redMaterial.addComponent( reflectanceComponent(&red, &d) );
  redMaterial.addComponent( reflectanceComponent(&white, &p32) );

  material greenMaterial;
  greenMaterial.addComponent( reflectanceComponent(&green, &d) );
  greenMaterial.addComponent( reflectanceComponent(&white, &p64) );

  material blueMaterial;
  blueMaterial.addComponent( reflectanceComponent(&blue, &d) );
  blueMaterial.addComponent( reflectanceComponent(&white, &p16) );

  material whiteMaterial;
  whiteMaterial.addComponent( reflectanceComponent(&white, &d) );

  triangleMesh redSphere(sphere, redMaterial);
  triangleMesh greenSphere(sphere, greenMaterial);
  triangleMesh blueSphere(sphere, blueMaterial);
  triangleMesh whitePlane(plane, whiteMaterial);

  sceneGraphObject redSphereObject(redSphere);
  sceneGraphObject greenSphereObject(greenSphere);
  sceneGraphObject blueSphereObject(blueSphere);
  sceneGraphObject whitePlaneObject(whitePlane);

  camera cam( vec3d(0.0f, 0.0f, 5.0f),
	      vec3d(0.0f, 0.0f, -1.0f),
	      vec3d(0.0f, 1.0f, 0.0f),
	      35.0f * M_PI / 180.0f,
	      512, 512 );

  directionalLightsource ls( color(1.0f, 1.0f, 1.0f), vec3d(0.0f, -1.0f, -1.0f) );
  directionalLightsource frontal( color(1.0f, 1.0f, 1.0f), vec3d(0.0f, 0.0f, -1.0f) );


  /////////////////////////////////////////////////////

  if((mask & 2) != 0)
  {
    sceneGraphNode sg1;
    sg1.addChildNode(redSphereObject);
  
    std::cout << "Generating Image 1." << std::endl;
    image result1 = generateImage(cam, sg1, ls);
    result1.save("hw4-result1.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 4) != 0)
  {
    sceneGraphNode sg2node1(translation3d(vec3d(+1.0f, 0.0f, 0.0f)));
    sg2node1.addChildNode(redSphereObject);

    sceneGraphNode sg2node2(translation3d(vec3d(-1.0f, 0.0f, 0.0f)));
    sg2node2.addChildNode(blueSphereObject);

    sceneGraphNode sg2;
    sg2.addChildNode(sg2node1);
    sg2.addChildNode(sg2node2);

    std::cout << "Generating Image 2." << std::endl;
    image result2 = generateImage(cam, sg2, ls);
    result2.save("hw4-result2.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 8) != 0)
  {
    sceneGraphNode sg3node1(scale3d(2.0f, 0.5f, 0.5f));
    sg3node1.addChildNode(greenSphereObject);

    sceneGraphNode sg3;
    sg3.addChildNode(sg3node1);
  
    std::cout << "Generating Image 3." << std::endl;
    image result3 = generateImage(cam, sg3, ls);
    result3.save("hw4-result3.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 16) != 0)
  {
    sceneGraphNode sg2node1(translation3d(vec3d(+1.0f, 0.0f, 0.0f)));
    sg2node1.addChildNode(redSphereObject);

    sceneGraphNode sg2node2(translation3d(vec3d(-1.0f, 0.0f, 0.0f)));
    sg2node2.addChildNode(blueSphereObject);

    sceneGraphNode sg2;
    sg2.addChildNode(sg2node1);
    sg2.addChildNode(sg2node2);

    sceneGraphNode sg4node1(rotationY3d(M_PI));
    sg4node1.addChildNode(sg2);

    sceneGraphNode sg4;
    sg4.addChildNode(sg4node1);
  
    std::cout << "Generating Image 4." << std::endl;
    image result4 = generateImage(cam, sg4, ls);
    result4.save("hw4-result4.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 32) != 0)
  {
    sceneGraphNode sg5node0(scale3d(0.5f, 0.5f, 0.5f));
    sceneGraphNode sg5node1(rotationY3d(0.5 * M_PI));
    sceneGraphNode sg5node2(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node3(translation3d(vec3d(0.0f, 2.0f, 0.0f)));
    sceneGraphNode sg5node4(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node5(translation3d(vec3d(0.0f, 2.0f, 0.0f)));
    sceneGraphNode sg5node6(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node7(translation3d(vec3d(0.0f, 2.0f, 0.0f)));
    sceneGraphNode sg5node8(rotationX3d(0.5 * M_PI));
    sceneGraphNode sg5node9(translation3d(vec3d(0.0f, 2.0f, 0.0f)));

    sceneGraphNode sg5;
    sg5.addChildNode(sg5node0);
    sg5node0.addChildNode(sg5node1);
    sg5node1.addChildNode(sg5node2);
    sg5node2.addChildNode(sg5node3);
    sg5node3.addChildNode(redSphereObject);
    sg5node3.addChildNode(sg5node4);
    sg5node4.addChildNode(sg5node5);
    sg5node5.addChildNode(greenSphereObject);
    sg5node5.addChildNode(sg5node6);
    sg5node6.addChildNode(sg5node7);
    sg5node7.addChildNode(blueSphereObject);
    sg5node7.addChildNode(sg5node8);
    sg5node8.addChildNode(sg5node9);
    sg5node9.addChildNode(blueSphereObject);
    
    std::cout << "Generating Image 5." << std::endl;
    image result5 = generateImage(cam, sg5, ls);
    result5.save("hw4-result5.ppm");
  }


  /////////////////////////////////////////////////////

  if((mask & 64) != 0)
  {
    sceneGraphNode cubeside( translation3d(vec3d(0.0f, 0.0f, 1.0f)) );
    cubeside.addChildNode(whitePlaneObject);

    sceneGraphNode cubeNode1( rotationY3d(0.0f / 2.0f * M_PI) );
    cubeNode1.addChildNode(cubeside);
    sceneGraphNode cubeNode2( rotationY3d(1.0f / 2.0f * M_PI) );
    cubeNode2.addChildNode(cubeside);
    sceneGraphNode cubeNode3( rotationY3d(2.0f / 2.0f * M_PI) );
    cubeNode3.addChildNode(cubeside);
    sceneGraphNode cubeNode4( rotationY3d(3.0f / 2.0f * M_PI) );
    cubeNode4.addChildNode(cubeside);
    sceneGraphNode cubeNode5( rotationX3d(0.5f * M_PI) );
    cubeNode5.addChildNode(cubeside);
    sceneGraphNode cubeNode6( rotationX3d(-0.5f * M_PI) );
    cubeNode6.addChildNode(cubeside);
    
    sceneGraphNode cube;
    cube.addChildNode(cubeNode1);
    cube.addChildNode(cubeNode2);
    cube.addChildNode(cubeNode3);
    cube.addChildNode(cubeNode4);
    cube.addChildNode(cubeNode5);
    cube.addChildNode(cubeNode6);
    
    sceneGraphNode sg6( rotation3d(0.25*M_PI, vec3d(1.0f, 0.75f, 0.5f)) );
    sg6.addChildNode(cube);
  
    std::cout << "Generating Image 6." << std::endl;
    image result6 = generateImage(cam, sg6, frontal);
    result6.save("hw4-result6.ppm");
  }

  // Done.
  return 0;
}