⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 primitiveshapes.cpp

📁 file code.zip are used to implement ray tracing technology.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		glVertex3f(  1.0f, 1.0f,  1.0f);
		glTexCoord2f( 0.0, 1.0); 
		glVertex3f( 1.0f,  1.0f, -1.0f);
	glEnd();
	
	glBindTexture(GL_TEXTURE_2D, 2002);
	glBegin(GL_QUADS);
		glTexCoord2f(-1.0, -1.0); 
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(-1.0,  2.0); 
		glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f( 2.0,  2.0); 
		glVertex3f(  1.0f, -1.0f,  1.0f);
		glTexCoord2f( 2.0, -1.0); 
		glVertex3f( 1.0f,  -1.0f, -1.0f);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 2003);
	glBegin(GL_QUADS);
		glTexCoord2f( 0.0,  0.0); 
		glVertex3f(1.0f, -1.0f,  1.0f);
		glTexCoord2f( 0.0,  2.0); 
		glVertex3f(1.0f, -1.0f, -1.0f);
		glTexCoord2f( 2.0,  2.0); 
		glVertex3f(1.0f,  1.0f, -1.0f);
		glTexCoord2f( 2.0,  0.0); 
		glVertex3f(1.0f,  1.0f,  1.0f);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 2004);
	glBegin(GL_QUADS);
		glTexCoord2f( 0.0,  0.0); 
		glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f( 0.0,  2.0); 
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f( 2.0,  2.0); 
		glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f( 2.0,  0.0); 
		glVertex3f(-1.0f,  1.0f,  1.0f);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 2005);
	glBegin(GL_QUADS);
		glTexCoord2f( 0.0,  0.0); 
		glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f( 0.0,  2.0); 
		glVertex3f(1.0f, -1.0f, 1.0f);
		glTexCoord2f( 2.0,  2.0); 
		glVertex3f(1.0f,  1.0f, 1.0f);
		glTexCoord2f( 2.0,  0.0); 
		glVertex3f(-1.0f,  1.0f,  1.0f);
	glEnd();
	
	glBindTexture(GL_TEXTURE_2D, 2002);
	glBegin(GL_QUADS);
		glTexCoord2f( 0.0,  0.0); 
		glVertex3f(-1.0f, -1.0f,  -1.0f);
		glTexCoord2f( 0.0,  2.0); 
		glVertex3f(1.0f, -1.0f, -1.0f);
		glTexCoord2f( 2.0,  2.0); 
		glVertex3f(1.0f,  1.0f, -1.0f);
		glTexCoord2f( 2.0,  0.0); 
		glVertex3f(-1.0f,  1.0f,  -1.0f);
	glEnd();

	glFlush();
	glPopMatrix();
	}void Cube::print(){    cout << "Object: Cube\n";    transf.print();}//@$@$@$@$@$@$@$@$ Sphere class @$@$@$@$@$@$@$@$@$@$@$@Sphere::Sphere() {}void Sphere::makeExtentPoints(PointCluster& clust) {}bool Sphere::hit(Ray& r, Intersection& inter){    Ray genRay;    xfrmRay(genRay, r);    double A, B, C;    Vector3 dir = genRay.getDir();    Point3 start = genRay.getStart();    //Prepare the coefficients of the quadratic    A = dir.dot(dir);    B = start.x*dir.x + start.y*dir.y + start.z*dir.z;    C = start.x*start.x + start.y*start.y + start.z*start.z - 1;    //Test discriminant to see if there is a hit    double discrim = B*B - A*C;    if(discrim <= 0) return false;    int num = 0;    double discRoot = sqrt(discrim);    //Find the first root of the quadratic (1st hit)    double t1 = (-B - discRoot)/A;    if(t1 > 0.00001) // a negative root will be behind the camera    {        inter.hit[num].hitTime = t1;        inter.hit[num].hitObject = this;        inter.hit[num].isEntering = true;        inter.hit[num].surface = 0;        Point3 P(genRay.calculatePointOnRay(t1));        inter.hit[num].hitPoint.set(P.x, P.y, P.z);        inter.hit[num].hitNormal.set(P.x, P.y, P.z);        num = 1;    }    //Find the second root of the quadratic (2nd hit)    double t2 = (-B + discRoot)/A;    if(t2 > 0.00001)    {        inter.hit[num].hitTime = t2;        inter.hit[num].hitObject = this;        inter.hit[num].isEntering = false;        inter.hit[num].surface = 0;        Point3 P(genRay.calculatePointOnRay(t2));        inter.hit[num].hitPoint.set(P.x, P.y, P.z);        inter.hit[num].hitNormal.set(P.x, P.y, P.z);        inter.hit[num].hitNormal.flip();        num++;    }    inter.numHits = num;    return (num > 0);}void Sphere::print(){    cout << "Object Sphere\n";    transf.print();}void Sphere::drawOpenGL() {}//@$@$@$@$@$@$@$@$@$@ TaperedCylinder class @$@$@$@$@$@$@$@$@$TaperedCylinder::TaperedCylinder() {}TaperedCylinder::TaperedCylinder(float radius) { smallRadius = radius; }void TaperedCylinder::makeExtentPoints(PointCluster& clust) {}bool TaperedCylinder::hit(Ray &r, Intersection &inter){    Ray genRay;		// Will hold the generic ray for generic intersection    xfrmRay(genRay, r);	// Transform the ray to the generic intersection space    int num = 0;	// A counter for the number of hits that occur        double A, B, C;	// Quadratic Coefficients    double t1, t2;	// The Quadratic Roots    Vector3 dir = genRay.getDir();    Point3 start = genRay.getStart();        // Give some variables short names for neater equations    double sm = smallRadius - 1;    double fDir = sm * dir.z;    double fStart = sm * start.z + 1;    // Calculate the Quadratic Coefficients    A = dir.x*dir.x + dir.y*dir.y - fDir*fDir;    B = start.x*dir.x + start.y*dir.y - fStart*fDir;    C = start.x*start.x + start.y*start.y - fStart*fStart;    double discrim = B*B - A*C;        if(discrim > 0.0)    {        double disc_root = (double)sqrt(double(discrim));        //Earlier Hit        t1 = (-B - disc_root)/A;        float zHit = start.z + dir.z * t1; // Is hit within the base and cap        if(t1 > 0.00001 && zHit <= 1.0 && zHit >= 0)        {            inter.hit[num].hitTime = t1;            inter.hit[num].surface = 0;            num++;        }		//later Hit        t2 = (-B + disc_root)/A;        zHit = start.z + dir.z * t2; // Is hit within the base and cap        if(t2 > 0.00001 && zHit <= 1.0 && zHit >= 0)        {            inter.hit[num].hitTime = t2;            inter.hit[num].surface = 0;            num++;        }    }    // Test to see if it hits the base    double tb = -start.z/dir.z;	//Hit time at z = 0 plane    if(tb > 0.00001 &&       pow(start.x + dir.x * tb, 2) +       pow(start.y + dir.y * tb, 2) < 1)    {        inter.hit[num].hitTime = tb;        inter.hit[num].surface = 1;	// 1 for the base        num++;    }    // Test to see if it hits the cap    double tc = (1 - start.z)/dir.z;	//Hit time at z = 1 plane    if(tc > 0.00001 &&       pow(start.x + dir.x * tc, 2) +       pow(start.y + dir.y * tc, 2) <       pow(smallRadius, 2))    {        inter.hit[num].hitTime = tc;        inter.hit[num].surface = 2;	// 2 for the cap        num++;    }    if(num == 0) return false;    inter.numHits = num;    if(num == 1)	// Eye must be inside the cylinder    {        // Assign the rest of the info        inter.hit[0].isEntering = false;        inter.hit[0].hitObject = this;    }    else	// We have two hits    {        //Sort the two hits        if(inter.hit[0].hitTime > inter.hit[1].hitTime)	//If They are swapped        {            double tempT = inter.hit[0].hitTime;            int tempS = inter.hit[0].surface;            inter.hit[0].hitTime = inter.hit[1].hitTime;            inter.hit[1].hitTime = tempT;            inter.hit[0].surface = inter.hit[1].surface;            inter.hit[1].surface = tempS;        }        //Assign the rest of the info        inter.hit[0].isEntering = true;        inter.hit[1].isEntering = false;        inter.hit[0].hitObject = inter.hit[1].hitObject = this;    }        //Finally, calculate the point of intersection and the surface normal    for(int i=0; i<num; i++)    {        Point3 PO(genRay.calculatePointOnRay(inter.hit[i].hitTime));        inter.hit[i].hitPoint.set(PO);        switch(inter.hit[i].surface)        {            case 0: // Hit wall of Cylinder                inter.hit[i].hitNormal.set(PO.x, PO.y, -sm*(1 + sm * PO.z));                break;            case 1: // Hit Base                inter.hit[i].hitNormal.set(0, 0, -1);                break;            case 2: // Hit Cap                inter.hit[i].hitNormal.set(0, 0, 1);                break;        }    }    return true;}void TaperedCylinder::drawOpenGL() { };void TaperedCylinder::print(){    cout << "Object: Tapered Cylinder with small radius " << smallRadius << endl;    transf.print();}//@$@$@$@$@$@$@$@$@$@ Cone class @$@$@$@$@$@$@$@$@$Cone::Cone() : TaperedCylinder(0.0) {}void Cone::print(){    cout << "Object: Cone" << endl;    transf.print();}void Cone::drawOpenGL() {}//@$@$@$@$@$@$@$@$@$@ Cylinder class @$@$@$@$@$@$@$@$@$Cylinder::Cylinder() : TaperedCylinder(1.0) {}void Cylinder::print(){    cout << "Object: Cylinder" << endl;    transf.print();}void Cylinder::drawOpenGL() { }//@$@$@$@$@$@$@$@$@$@ Torus class @$@$@$@$@$@$@$@$@$Torus::Torus() {}void Torus::makeExtentPoints(PointCluster& clust) {};bool Torus::hit(Ray &r, Intersection &inter) { return false; }void Torus::drawOpenGL() {}void Torus::print(){    cout << "Object: Torus" << endl;    transf.print();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -