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

📄 primitiveshapes.cpp

📁 file code.zip are used to implement ray tracing technology.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "PrimitiveShapes.h"//@$@$@$@$@$@$@$@$@$@ Plane class @$@$@$@$@$@$@$@$@$Plane::Plane() {};void Plane::makeExtentPoints(PointCluster& clust) {}bool Plane::hit(Ray &r, Intersection &inter){    Ray genRay;    inter.numHits = 0;    xfrmRay(genRay, r);    double denom = genRay.getDir().z;    if(fabs(denom) < 0.001) return false;    double time = -genRay.getStart().z/denom;    if(time<=0.0) return false;    double hx = genRay.getStart().x + genRay.getDir().x *time;    double hy = genRay.getStart().y + genRay.getDir().y *time;    inter.numHits=1;    inter.hit[0].hitObject = this;    inter.hit[0].hitTime = time;    inter.hit[0].isEntering = true;    inter.hit[0].surface=0;    inter.hit[0].hitPoint.set(hx,hy,0);    inter.hit[0].hitNormal.set(0,0,1);    return true;}void Plane::drawOpenGL() {}void Plane::print(){    cout << "Object: Plane\n";    transf.print();}//@$@$@$@$@$@$@$@$@$@ Checkerboard class @$@$@$@$@$@$@$@$@$Checkerboard::Checkerboard(){	checkObj1 = checkObj2 = NULL;	dx = dy = 1;}Checkerboard::Checkerboard(double newDX, double newDY){	checkObj1 = checkObj2 = NULL;	dx = newDX; dy = newDY;}void Checkerboard::makeExtentPoints(PointCluster& clust) {}bool Checkerboard::hit(Ray &r, Intersection &inter){	// Create the checkerboard materials	if(checkObj1 == NULL)	{		// Create the new objects		checkObj1 = new Plane();		checkObj2 = new Plane();		// Copy the transformation matrices		checkObj1->transf.set(this->transf);		checkObj2->transf.set(this->transf);		checkObj1->invTransf.set(this->invTransf);		checkObj2->invTransf.set(this->invTransf);		// Copy the material		checkObj1->mtrl.set(this->mtrl);		checkObj2->mtrl.set(this->mtrl);		// Change the material properties		checkObj1->mtrl.ambient.set(0.15, 0.15, 0.15);		checkObj1->mtrl.diffuse.set(0.15, 0.15, 0.15);		checkObj1->mtrl.specular.set(0.15, 0.15, 0.15);		checkObj2->mtrl.ambient.set(0.8, 0.8, 0.8);		checkObj2->mtrl.diffuse.set(0.8, 0.8, 0.8);		checkObj2->mtrl.specular.set(0.8, 0.8, 0.8);	}    Ray genRay;    inter.numHits = 0;    xfrmRay(genRay, r);    double denom = genRay.getDir().z;    if(fabs(denom) < 0.001) return false;    double time = -genRay.getStart().z/denom;    if(time<=0.0) return false;    double hx = genRay.getStart().x + genRay.getDir().x *time;    double hy = genRay.getStart().y + genRay.getDir().y *time;	unsigned int checkState = (int)(floor(hx/dx) + floor(hy/dy));	if(checkState%2 == 0) inter.hit[0].hitObject = checkObj1;	else inter.hit[0].hitObject = checkObj2;    inter.numHits=1;    inter.hit[0].hitTime = time;    inter.hit[0].isEntering = true;    inter.hit[0].surface=0;    inter.hit[0].hitPoint.set(hx,hy,0);    inter.hit[0].hitNormal.set(0,0,1);    return true;}void Checkerboard::drawOpenGL() {}void Checkerboard::print(){    cout << "Object: Checkerboard\n";    transf.print();}//@$@$@$@$@$@$@$@$@$@ Square class @$@$@$@$@$@$@$@$@$Square::Square() {}void Square::makeExtentPoints(PointCluster& clust) {}bool Square::hit(Ray &r, Intersection &inter){    Ray genRay;    inter.numHits=0;    xfrmRay(genRay, r);    double denom = genRay.getDir().z;    if(fabs(denom) < 0.001) return false;    double time = -genRay.getStart().z/denom;    if(time<=0.0) return false;    double hx=genRay.getStart().x + genRay.getDir().x *time;    double hy=genRay.getStart().y + genRay.getDir().y *time;    if((hx>1.0)||(hx < -1.0)) return false;    if((hy>1.0)||(hy < -1.0)) return false;    inter.numHits=1;    inter.hit[0].hitObject=this;    inter.hit[0].hitTime=time;    inter.hit[0].isEntering=false;    inter.hit[0].surface=0;    inter.hit[0].hitPoint.set(hx,hy,0);    inter.hit[0].hitNormal.set(0,0,1);    return true;}void Square::drawOpenGL() {}void Square::print(){    cout << "Object: Square\n";    transf.print();}//@$@$@$@$@$@$@$@$@ Cube class $@$@$@$@$@$@$@$@$@$@Cube::Cube() {}void Cube::makeExtentPoints(PointCluster& clust) {}bool Cube::hit(Ray &r, Intersection &inter){    Ray genRay;    xfrmRay(genRay, r);    double tHit, numer, denom;    double tIn = -100000.0, tOut = 100000.0;    int inSurf, outSurf;    float rayDirX = genRay.getDir().x,          rayDirY = genRay.getDir().y,          rayDirZ = genRay.getDir().z,          rayStrX = genRay.getStart().x,          rayStrY = genRay.getStart().y,          rayStrZ = genRay.getStart().z;                // Loop through the six faces of the cube    for(int i=0; i < 6; i++)    {        // Set-up the plane fraction for this face of the cube        switch(i)        {            case 0: // Top of cube (XZ-plane up 1.0)                numer = 1.0 - rayStrY;                denom = rayDirY;                break;            case 1: // Bottom of cube (XZ-plane down 1.0)                numer = 1.0 + rayStrY;                denom = - rayDirY;                break;            case 2: // Right of cube (YZ-plane right 1.0)                numer = 1.0 - rayStrX;                denom = rayDirX;                break;            case 3: // Left of cube (YZ-plane left 1.0)                numer = 1.0 + rayStrX;                denom = - rayDirX;                break;            case 4: // Front of cube (XY-plane forward 1.0)                numer = 1.0 - rayStrZ;                denom = rayDirZ;                break;            case 5: // Back of cube (XY-plane back 1.0)                numer = 1.0 + rayStrZ;                denom = - rayDirZ;                break;        }        if(fabs(denom) < 0.00001) // Ray is parallel to this face        {            if(numer < 0) return false;	// Ray is outside        }        else	// Ray is not parallel        {            tHit = numer/denom;            if(denom > 0) // We are exiting the cube            {                if(tHit < tOut) // A new earlier Exit time                    { tOut = tHit; outSurf = i; }            }            else // We are entering the cube            {                if(tHit > tIn) // A new later Enter time                    { tIn = tHit; inSurf = i; }            }                    }        if(tIn >= tOut) return false;	// Miss (early out)    }    // Now create the hits for the intersection    int num = 0;    if(tIn > 0.00001)	// Is first hit in front of the eye    {        inter.hit[num].hitTime = tIn;        inter.hit[num].surface = inSurf;        inter.hit[num].isEntering = true;        inter.hit[num].hitObject = this;        Point3 PO(genRay.calculatePointOnRay(tIn));        Point3 N(cubeNormal(inSurf));        inter.hit[num].hitPoint.set(PO.x, PO.y, PO.z);        inter.hit[num].hitNormal.set(N.x, N.y, N.z);        num++;    }    if(tOut > 0.00001)	// Is second hit in front of the eye    {        inter.hit[num].hitTime = tOut;        inter.hit[num].surface = outSurf;        inter.hit[num].isEntering = false;        inter.hit[num].hitObject = this;        Point3 PO(genRay.calculatePointOnRay(tOut));        Point3 N(cubeNormal(outSurf));        inter.hit[num].hitPoint.set(PO.x, PO.y, PO.z);        inter.hit[num].hitNormal.set(N.x, N.y, N.z);        num++;    }    inter.numHits = num;    return (num > 0);}Point3 Cube::cubeNormal(int surface){    Point3 norm;    switch(surface)    {        case 0: norm.set( 0, 1, 0); break;        case 1: norm.set( 0,-1, 0); break;        case 2: norm.set( 1, 0, 0); break;        case 3: norm.set(-1, 0, 0); break;        case 4: norm.set( 0, 0, 1); break;        case 5: norm.set( 0, 0,-1); break;    }    return norm;}void Cube::drawOpenGL() {RGBpixmap pix[6];	glEnable(GL_TEXTURE_2D);	pix[0].readBMPFile("floor.bmp");   //t?o pixmap b?ng gi?i thu?t
	printf("HELLO\n");
	pix[0].setTexture(2001);   
	printf("hellp\n");
	pix[1].readBMPFile("forest.bmp"); // t?o pixmap t? t?p tin h靚h ?nh
	pix[1].setTexture(2002);
	pix[2].readBMPFile("money.bmp"); // t?o pixmap t? t?p tin h靚h ?nh
	pix[2].setTexture(2003);
	pix[3].readBMPFile("lemon.bmp"); // t?o pixmap t? t?p tin h靚h ?nh
	pix[3].setTexture(2004);
	pix[4].readBMPFile("wall.bmp"); // t?o pixmap t? t?p tin h靚h ?nh
	pix[4].setTexture(2005);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	glPushMatrix();	glBindTexture(GL_TEXTURE_2D, 2001);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0, 0.0); 
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glTexCoord2f(1.0,  0.0); 
		glVertex3f(-1.0f, 1.0f,  1.0f);
		glTexCoord2f( 1.0,  1.0); 

⌨️ 快捷键说明

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