예제 #1
0
파일: Picture.cpp 프로젝트: alwalker/TTU
InstanceObject* Picture::buildInstanceObject(char* fileName, BasicObject* obj)
{
   InstanceObject* io = new InstanceObject(obj);
   FileIO* inputFile = new FileIO(fileName, 1);  //for reading

   //scale transformation
   string line = inputFile->readLine();  //skip this line
   line = inputFile->readLine();
   double sx = atof(line.c_str());
   line = inputFile->readLine();
   double sy = atof(line.c_str());
   line = inputFile->readLine();
   double sz = atof(line.c_str());
   Matrix* scale = AffineTransforms::scale(sx, sy, sz);

   //rotation transformations
   line = inputFile->readLine();  //skip this line
   line = inputFile->readLine();
   double rx = atof(line.c_str());
   Matrix* rotX = AffineTransforms::rotateX(rx);
   line = inputFile->readLine();
   double ry = atof(line.c_str());
   Matrix* rotY = AffineTransforms::rotateY(ry);
   line = inputFile->readLine();
   double rz = atof(line.c_str());
   Matrix* rotZ = AffineTransforms::rotateZ(rz);

   //translation transformation
   line = inputFile->readLine();  //skip this line
   line = inputFile->readLine();
   double tx = atof(line.c_str());
   line = inputFile->readLine();
   double ty = atof(line.c_str());
   line = inputFile->readLine();
   double tz = atof(line.c_str());
   Matrix* translate = AffineTransforms::translate(tx, ty, tz);

   //material
   line = inputFile->readLine();  //skip this line
   line = inputFile->readLine();
   double mr = atof(line.c_str());
   line = inputFile->readLine();
   double mg = atof(line.c_str());
   line = inputFile->readLine();
   double mb = atof(line.c_str());
   Color* mat = new Color(mr, mg, mb);
   io->setDiffuseMaterial(mat);

   //standard TRS form
   io->buildTransform(scale);  //deletes the matrix when done
   io->buildTransform(rotX);  
   io->buildTransform(rotY);  
   io->buildTransform(rotZ); 
   io->buildTransform(translate);

   //Get shininess
   FileIO* shaderFile = new FileIO("shade.txt", 1);
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();
   line = shaderFile->readLine();

   io->setShininess(atof(line.c_str()));

   delete shaderFile;
   delete inputFile;
   return io;
}
예제 #2
0
파일: Picture.cpp 프로젝트: alwalker/TTU
void Picture::render(Pixel* px)
{
	Color *ambient = new Color(1, 1, 1);
	Vertex *eye = new Vertex(0,0,0);
	Light *light = new Light();
	double attenuation = 0;
//while(true)
//{

   //char* fileName = "sphere.txt";
   //BasicObject* sphere = readObject(fileName);
   ////delete[] fileName;  //mingw appears to delete this automatically

   //fileName = "trs.txt";
   //InstanceObject* sphereInstance = buildInstanceObject(fileName, sphere);
   ////delete[] fileName;

   //obtaining the window transform
   int widthPixels = px->getWidth();  //the dimensions of the panel on which the drawing will occur
   int heightPixels = px->getHeight();

   getShaderInfo(eye, ambient, light, &attenuation);
   Scene* scene = new Scene(light, ambient);
   scene->buildTransform(getCameraTransform("camera.txt"));
   scene->buildTransform(getPerspectiveTransform("fov.txt", widthPixels, heightPixels));
   scene->buildTransform(AffineTransforms::window(widthPixels, heightPixels));

	BasicObject* cube = readObject("cube.txt");
	BasicObject* cylinder = readObject("cylinder.txt");

	InstanceObject* upperArm = new InstanceObject(cube);
	upperArm->setDiffuseMaterial(new Color(1,0,0));
	upperArm->buildTransform(AffineTransforms::scale(.2, 1, .2));
	InstanceObject* lowerArm = new InstanceObject(cube);
	lowerArm->setDiffuseMaterial(new Color(0,1,0));
	lowerArm->buildTransform(AffineTransforms::scale(.2, 2, .2));
	InstanceObject* base = new InstanceObject(cylinder); //no instance transform
	base->setDiffuseMaterial(new Color(0,0,1));

	Matrix *temp;
	TransformNode *upperArmTransform, *lowerArmTransform, *baseTransform, *robotArm1, *robotArm2;

	//upper arm transform
	temp = AffineTransforms::translate(0, 1, 0);
	temp = AffineTransforms::rotateZ(90)->multiply(temp);
	temp = AffineTransforms::translate(0, -1, 0)->multiply(temp);
	temp = AffineTransforms::translate(0, 3, 0)->multiply(temp);
	upperArmTransform = new TransformNode();
	upperArmTransform->buildTransform(temp);

	//lower arm transform
	temp = AffineTransforms::translate(0, 2, 0);
	temp = AffineTransforms::rotateZ(-20)->multiply(temp);
	temp = AffineTransforms::translate(0, -2, 0)->multiply(temp);
	temp = AffineTransforms::translate(0, 3, 0)->multiply(temp);
	lowerArmTransform = new TransformNode();
	lowerArmTransform->buildTransform(temp);

	//base transform
	temp = AffineTransforms::rotateY(30);
	temp = AffineTransforms::translate(0, -2, 0)->multiply(temp);
	baseTransform = new TransformNode();
	baseTransform->buildTransform(temp);

	//robot arm #1
	temp = AffineTransforms::scale(.2, .2, .2);
	temp = AffineTransforms::translate(0, 0, -3)->multiply(temp);
	robotArm1 = new TransformNode();
	robotArm1->buildTransform(temp);

	//robot arm #2
	temp = AffineTransforms::scale(.2, .2, .2);
	temp = AffineTransforms::rotateX(45)->multiply(temp);
	temp = AffineTransforms::translate(-3, .5, 0)->multiply(temp);
	temp = AffineTransforms::scale(.2, .2, .2)->multiply(temp);
	robotArm2 = new TransformNode();
	robotArm2->buildTransform(temp);

	//build scene
	upperArmTransform->addChild(upperArm);
	lowerArmTransform->addChild(upperArmTransform);
	lowerArmTransform->addChild(lowerArm);
	baseTransform->addChild(lowerArmTransform);
	baseTransform->addChild(base);
	robotArm1->addChild(baseTransform);
	robotArm2->addChild(baseTransform);
	scene->addTransformNode(robotArm1);
	scene->addTransformNode(robotArm2);

	scene->render(px, eye, attenuation);
	delete scene;

   /*TransformNode* tn = new TransformNode();
   tn->addChild(sphereInstance);
   scene->addTransformNode(tn);

   scene->render(px, eye, attenuation);
   delete scene;*/
//}
}