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

📄 common.cpp

📁 file code.zip are used to implement ray tracing technology.
💻 CPP
字号:
#include "common.h"IntPoint::IntPoint()			{ x = y = 0; }IntPoint::IntPoint(int xx, int yy)	{ x = xx; y = yy; }void IntPoint::set(int dx, int dy)	{ x = dx; y = dy; }void IntPoint::set(IntPoint& p)		{ x = p.x; y = p.y; }Point2::Point2() 			{x = y = 0.0f;}Point2::Point2(float xx, float yy)	{x=xx; y=yy;}void Point2::set(float xx, float yy)	{x=xx; y=yy;}float Point2::getX()			{return x;}float Point2::getY()			{return y;}void Point2::draw(){    glBegin(GL_POINTS);    glVertex2f((GLfloat)x, (GLfloat)y);    glEnd();}Point3::Point3()				{ x = y = z = 0; }Point3::Point3(float xx, float yy, float zz)	{ x = xx; y = yy; z = zz; }void Point3::set(float dx, float dy, float dz)	{ x = dx; y = dy; z = dz; }void Point3::set(Point3& p)			{ x = p.x; y = p.y; z = p.z; h = p.h; }void Point3::build4tuple(float v[])		{ v[0] = x; v[1] = y; v[2] = z; v[3] = 1.0f; }IntRect::IntRect() { l = 0; r=100; b=0; t=100; }IntRect::IntRect(int left, int right, int bottom, int top) { l=left; r=right; b=bottom; t=top; }void IntRect::set(int left, int right, int bottom, int top) { l=left; r=right; b=bottom; t=top; }int IntRect::getL() { return l; }int IntRect::getR() { return r; }int IntRect::getT() { return t; }int IntRect::getB() { return b; }RealRect::RealRect() {l = 0; r=100; b=0; t=100; } //constructorsRealRect::RealRect(float left, float right, float bottom, float top) {l = left; r=right; b=bottom; t=top;}void RealRect::set(float left, float right, float bottom, float top) { l=left; r=right; b=bottom; t=top; }float RealRect::getL() { return l; }float RealRect::getR() { return r; }float RealRect::getT() { return t; }float RealRect::getB() { return b; }Color3::Color3() { red = green = blue = 0; }Color3::Color3(float r, float g, float b) { red = r; green = g; blue = b; /*clamp();*/ }//Color3::Color3(Color3& c) { red = c.red; green = c.green; blue = c.blue; /* clamp();*/ }void Color3::make(Color3 c) { red = c.red; green = c.green; blue = c.blue; /*clamp();*/ }void Color3::set(Color3& c) { red = c.red; green = c.green; blue = c.blue; /*clamp();*/ }void Color3::set(float r, float g, float b) { red = r; green = g; blue = b; /*clamp();*/ }void Color3::add(float r, float g, float b) { red += r; green += g; blue += b; /*clamp();*/ }void Color3::add(Color3& colr) { red += colr.red ; green += colr.green; blue += colr.blue; /*clamp();*/ }void Color3::add(Color3& src, Color3& refl){    red   += src.red   * refl.red;    green += src.green * refl.green;    blue  += src.blue  * refl.blue;//    clamp();}void Color3::scale(float l) { red *= l; green *= l; blue *= l; /*clamp();*/ }void Color3::build4tuple(float v[]) { v[0] = red; v[1] = green; v[2] = blue; v[3] = 1.0f; }/*	Clamming removed in favor of tone mappingvoid Color3::clamp(){    if(red > 1.0f) red = 1.0f;    if(green > 1.0f) green = 1.0f;    if(blue > 1.0f) blue = 1.0f;}*/bool Color3::isZero() { return (red==0 && green==0 && blue==0); }Vector3::Vector3()				{x = y = z = 0;}//Vector3::Vector3(Vector3& v)			{x = v.x; y = v.y; z = v.z;}Vector3::Vector3(float xx, float yy, float zz)	{x = xx; y = yy; z = zz;}void Vector3::set(Vector3 v)			{ x = v.x; y = v.y; z = v.z;}void Vector3::set(float dx, float dy, float dz) { x = dx; y = dy; z = dz;}void Vector3::setDiff(Point3& a, Point3& b)	{ x = a.x - b.x; y = a.y - b.y; z = a.z - b.z;}void Vector3::flip()				{x = -x; y = -y; z = -z;}void Vector3::scale(float a)			{ x *= a; y *= a; z *=a; }void Vector3::build4tuple(float v[])		{ v[0] = x; v[1] = y; v[2] = z; v[3] = 0;}void Vector3::add(Vector3 a)			{ x += a.x; y += a.y; z += a.z;}float Vector3::dot(Vector3 b)			{return x * b.x + y * b.y + z * b.z;}Vector3 Vector3::cross(Vector3 b){    Vector3 c(y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x);    return c;}void Vector3::normalize(){    double sizeSq = x * x + y * y + z * z;    if(sizeSq < 0.0000001) return; // does nothing to zero vectors;    float scaleFactor = 1.0/(float)sqrt(sizeSq);    x *= scaleFactor; y *= scaleFactor; z *= scaleFactor;}double Vector3::length() { return sqrt(x*x + y*y + z*z); }void Vector3::rotate(float angle, Vector3 axis){    glMatrixMode(GL_MODELVIEW);    glPushMatrix(); glLoadIdentity();    glRotatef(angle, axis.x, axis.y, axis.z);    float *m = new float[16];    glGetFloatv(GL_MODELVIEW_MATRIX, m);    glPopMatrix();    float values[4] = {x, y, z, 1}, newVals[4] = { 0, 0, 0, 0 };    for(int i=0; i<4; i++)        for(int j=0; j<4; j++)            newVals[i] += m[i*4+j]*values[j];    x = newVals[0]; y = newVals[1]; z = newVals[2];}PointCluster::PointCluster() {num = 0; pt = NULL;}PointCluster::PointCluster(int n){    pt = new Point3[n]; assert(pt);    num = n;}

⌨️ 快捷键说明

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