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

📄 scene.h

📁 file code.zip are used to implement ray tracing technology.
💻 H
字号:
#ifndef _SCENE__
#define _SCENE__

#include <string>
#include <iostream>
#include <fstream>
#include <strstream>
using namespace std;
#include <windows.h>
#include "shapes.h"
#include "common.h"
#include "affine.h"
#include "RGBpixmap.h"
//@@@@@@@@@@@@@@@@@ DefUnit & DefUnitStack classes @@@@@@@@@@@@@@ 
//used in Scene to read SDL files 
class DefUnit { 

public: 
	string    name, stuff; 
	DefUnit(string n, string s) {stuff = s;name = n;} 
}; 

class DefUnitStack { 
public: 
	DefUnitStack() { 
		stack = NULL; 
	} 
	void push(string n, string s) {  
		D4S *temp_d4s = new D4S; 
		temp_d4s->current = new DefUnit(n, s); 
		temp_d4s->next = stack; 
		stack = temp_d4s; 
	} 
	void print() { 
		D4S *temp = stack; 
		string t; 
		while (temp) { 
			cout << temp->current->name << ":" ; 
			cout << temp->current->stuff << endl; 
			temp = temp->next; 
		} 
	} 
	int search(string s) { 
		D4S *temp = stack; 
		while (temp) { 
			if ( temp->current->name == s ) { 
				return(1); 
			} 
			temp = temp->next; 
		} 
		return(0); 
	} 
	string contents(string s) { 
		D4S *temp = stack; 
		while (temp) { 
			if (temp->current->name == s ) { 
				return(temp->current->stuff); 
			} 
			temp = temp->next; 
		} 
		return(NULL); 
	} 
	void release() 
	{ 
		while(stack) 
		{ 
			D4S* tmp = stack; // grab it 
			//cerr << "releasing def_stack item: "<< tmp->current->name<< endl; 
			stack = stack->next; // advance p 
			delete tmp->current; // release 2 strings 
			delete tmp; // release node 
		} 
		stack = NULL; 
	} 
private: 
	struct D4S { 
		DefUnit *current; 
		struct D4S *next; 
	} d4s; 
	D4S *stack; 
}; // end of DefUnitStack class 

class Boolean: public GeomObj{
public:
	GeomObj *left, *right;
	Boolean():left(NULL),right(NULL){}
	virtual bool hit(Ray &r, Intersection &inter){return false;}
	virtual void drawOpenGL()
	{ // just draw its children
		if(left)left->drawOpenGL();
		if(right)right->drawOpenGL();
	}
};
//@@@@@@@@@@@@@@@@@@@@ UnionBool @@@@@@@@@@@@@@@@
class UnionBool : public Boolean{
public:
	UnionBool(){Boolean();} //constructor
	bool hit(Ray &r, Intersection &inter){return false;}
};
//@@@@@@@@@@@@@@@@@@@@ IntersectionBool @@@@@@@@@@@@@@@@
class IntersectionBool : public Boolean{
public:
	IntersectionBool(){Boolean();}
	bool hit(Ray &r, Intersection &inter){return false;}
};
//@@@@@@@@@@@@@@@@@@@@ DifferenceBool @@@@@@@@@@@@@@@@
class DifferenceBool : public Boolean{
public:
	DifferenceBool(){Boolean();}
	bool hit(Ray &r, Intersection &inter){return false;}
};
//@@@@@@@@@@@@@  Scene class @@@@@@@@@@@@@@@@@@@@ 
class Scene{ 
public: 
	Light *light;  // attach linked list of lights here 
	GeomObj * obj;  // attach the object list here 

	Vector3 up;
    Point3 eye, lookAt;
    float nearPlane, farPlane, angle, aspect;
	size_t xRes, yRes;
	RGBpixmap pixmap[8]; //list of attached pixmaps
	size_t reflRays, refrRays, shadowRays;
	
	Color3 background, ambient; 
	int maxRecursionDepth; 
	float minReflectivity, minTransparency; 
	bool isInShadow(Ray& f);
	void bindTexture(int TextureType);
	Scene():light(NULL),obj(NULL),tail(NULL) //default constructor 
	{ 
		currMtrl.setDefault();
		background.set(0,0,0.6f);
		ambient.set(0.1f,0.1f,0.1f);
		minReflectivity = 0.5;
		minTransparency = 0.5;
		maxRecursionDepth = 3;

		// Some default viewing parameters
		up.set(0, 1, 0);
		eye.set(4, 4, 4);
		lookAt.set(0, 0, 0);
		angle = 50;
		aspect = 640.0/480.0;
		xRes = 640;
		yRes = 480;
		nearPlane = 1;
		farPlane = 7;
		reflRays = refrRays = shadowRays = 0;
	}  

	Scene(string fname);   
 	void freeScene();  
	bool read(string fname); 
	Color3 shade(Ray &ray); // implementation left to the reader
	void getFirstHit(Ray &ray, Intersection &best); 
	GeomObj* getObject();
	void makeLightsOpenGL(void);
	void drawSceneOpenGL(void);
	void drawSceneCanvas(void);
	void printScene(void); 

	void xfrmNormal(Vector3 &N2, Affine4 transf, Vector3 &N1);
	Color3 phongLighting(Material mtrl, Point3 p, Vector3 N, Ray ray);
	void spawnReflectedRay(Material mtrl, Point3 p, Vector3 N, Ray ray, Color3 &result);

private: 
	// private stuff used only for reading a scene 
	int line; 
	int nextline;   
	ifstream  *file_in; 
	strstream *f_in; 
	strstream *temp_fin; 
	DefUnitStack *def_stack; 
	GeomObj * tail; // tail of object list 
	AffineStack affStk; // affine stack 
	Material currMtrl;
	//<<<<<<<< methods >>>>>>>>>>> 
	string nexttoken(void); 
	float getFloat(); 
	bool isidentifier(string keyword); 
	void cleanUp();
	//+++++++++++++ TokenType +++++++++++++ 
	enum TokenType {IDENT, LIGHT, ROTATE, TRANSLATE,  
		SCALE, PUSH, POP, IDENTITYAFFINE,  
		EYE, LOOKAT, UP, NEARPLANE, FARPLANE, VIEWANGLE,
		GLOBALAMBIENT, BACKGROUND, MINREFLECTIVITY, 
		MINTRANSPARENCY, MAXRECURSIONDEPTH, CUBE, SPHERE, TORUS, PLANE, 
		SQUARE, CYLINDER, CONE, TAPEREDCYLINDER, TETRAHEDRON, OCTAHEDRON,  
		DODECAHEDRON, ICOSAHEDRON,BUCKYBALL, TEAPOT, CHECKERBOARD,
		DIAMOND,UNION,INTERSECTION, DIFFERENCEa, MAKEPIXMAP, 
		MESH, DEFAULTMATERIALS, AMBIENT, DIFFUSE,SPECULAR, 
		SPECULARFRACTION, SURFACEROUGHNESS,EMISSIVE, SPECULAREXPONENT, 
		SPEEDOFLIGHT, TRANSPARENCY,REFLECTIVITY, PARAMETERS, TEXTURE, 
		LFTCURLY, RGHTCURLY, DEF, USE, T_NULL, F_EOF, UNKNOWN }; 
	//<<<<<<<<<<<<<whichtoken >>>>>>>>>>>>>> 
	TokenType whichtoken(string keyword);
	}; // end of Scene.h 



#endif

⌨️ 快捷键说明

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