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

📄 psgl.h

📁 关于stl模型的加载
💻 H
字号:
#ifndef _PSGL_H_
#define _PSGL_H_


#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <cmath>


#define _ToRad(degree) \
	(degree) * PsGL::PI / 180
#define _ToDegree(rad) \
	(rad) * 180 / PsGL::PI

class PsGL
{
public:
	PsGL(void);
public:
	~PsGL(void);

/*结构定义*/
	typedef struct PsPoint3Df {
		GLfloat x;
		GLfloat y;
		GLfloat z;
	} PsVector3Df;
	typedef struct PsPoint3Dd {
		GLdouble x;
		GLdouble y;
		GLdouble z;
	} PsVector3Dd;
	typedef struct PsPoint3Dld {
		long double x;
		long double y;
		long double z;
	} PsVector3Dld;
	struct PsColor3f {
		GLfloat red;
		GLfloat green;
		GLfloat blue;
	};
	struct PsColor4f {
		GLfloat red;
		GLfloat green;
		GLfloat blue;
		GLfloat alpha;
	};

/*静态成员*/
public:
	static const double PI;

public:
	//计算叉乘
	static void mathVectorProductd(PsGL::PsVector3Dd& vector_1, PsGL::PsVector3Dd& vector_2, PsGL::PsPoint3Dd& result);
	//旋转一个点
	static void mathRotated(GLdouble axis_x, GLdouble axis_y, GLdouble axis_z, GLfloat angle, PsGL::PsPoint3Dd& point);
	//归一化向量,返回向量的原长度
	static GLdouble mathNormalizeVector(GLdouble& x, GLdouble& y, GLdouble& z);
	//反投影一个点
	static void unProjectPoint(int screen_x, int screen_y, GLdouble& x, GLdouble& y, GLdouble& z);
	//反投影一组点
	static void unProjectPoints(int screen_pos[][2], PsGL::PsPoint3Dd points[], int n);

protected:
};


/*光照类*/
/*光源属性使用GLfloat,Get方法除色彩外使用GLdouble*/
class PsGLLight :
	public PsGL
{//只能通过createLight()获得光源对象
private:
	PsGLLight(void);//私有化构造函数
protected:
	~PsGLLight(void);//保护析构函数

/*静态成员*/
protected:
	static GLenum IDs[];
	static GLfloat light_model_ambient[];//光照模型环境光
	static PsGLLight* light_pool[];//光源池
	static int MAX_LIGHT_NUMBER;//最大光源数目
	static int light_number;//光源数目
	static bool bLighting;//光照状态

	/*Extension*/
	static bool bTwoSide;//双面光照状态
	static bool bLocalView;//局部观察点状态
	static bool bSeparateSpecularColor;//高光分离状态


public:
	//只能通过createLight()获得光源对象
	static PsGLLight* createLight();
	static bool deleteLight(int index);
	static void deleteAllLight();
	static int getMaxLightNumber() { return MAX_LIGHT_NUMBER; }

	static void enableLighting(bool enable);//开启光照

	/*Extension*/
	static void enableLMLocalViewer(bool enable);//开启局部观察点
	static void enableLMTwoSide(bool enable);//开启双面光照
	static void enableLMSeparateSpecularColor(bool enable);//开启分离处理高光色


	//设置光照模型环境光色
	static void setLMAmbientColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
	static void getLMAmbientColor(PsGL::PsColor4f& color);

	static bool isLighting() { return bLighting; }

	/*Extension*/
	static bool isLMLocalViewer() { return bLocalView; }
	static bool isLMTwoSide() { return bTwoSide; }
	static bool isLMSeparateSpecularColor() { return bSeparateSpecularColor; }


/*基本属性*/
protected:
	int nIndex;
	bool bEnable;//光源状态
	GLenum nID;
	GLfloat light_position[4];//光源位置(默认原点)
	GLfloat light_orientation[3];//光线方向(默认z)
	GLfloat light_cutoff_angle;//光锥截止角(默认180度)
	GLfloat light_attenuation[3];//光线衰减因子(0,常数;1,线性;2,平方)
	GLfloat light_exponent;//聚光度(默认0.0,光锥内光强一致)
	GLfloat light_color[4];//光源色(散射色)
	GLfloat light_ambient[4];//环境光色
	GLfloat light_specular[4];//高光色

/*基本方法*/
public:
	int getIndex() { return nIndex; }
	GLenum getID() { return nID; }
	void enable(bool enable);//开启此光源
	bool isEnable() { return bEnable; }

	//设置光源位置
	void setPosition(GLfloat x, GLfloat y, GLfloat z)
	{
		light_position[0] = x;
		light_position[1] = y;
		light_position[2] = z;
		light_position[3] = 0.0;
		glLightfv(nID, GL_POSITION, light_position);
	}
	void getPosition(PsGL::PsPoint3Dd& point)
	{
		point.x = light_position[0];
		point.y = light_position[1];
		point.z = light_position[2];
	}

	//设置光线方向
	void setOrientation(GLfloat x, GLfloat y, GLfloat z)
	{
		light_orientation[0] = x;
		light_orientation[1] = y;
		light_orientation[2] = z;
		glLightfv(nID, GL_SPOT_DIRECTION, light_orientation);
	}
	void getOrientation(PsGL::PsVector3Dd& vector)
	{
		vector.x = light_orientation[0];
		vector.y = light_orientation[1];
		vector.z = light_orientation[2];
	}

	//设置光锥截止角
	void setCutoffAngle(GLfloat angle = 180.0)
	{
		light_cutoff_angle = angle;
		glLightf(nID, GL_SPOT_CUTOFF, light_cutoff_angle);
	}

	//设置常量衰减因子
	void setConstantAttenuation(GLfloat factor)
	{
		light_attenuation[0] = factor;
		glLightf(nID, GL_CONSTANT_ATTENUATION, light_attenuation[0]);
	}
	GLfloat getConstantAttenuation() { return light_attenuation[0]; }
	//设置线性衰减因子
	void setLinearAttenuation(GLfloat factor)
	{
		light_attenuation[1] = factor;
		glLightf(nID, GL_LINEAR_ATTENUATION, light_attenuation[1]);
	}
	GLfloat getLinearAttenuation() { return light_attenuation[1]; }
	//设置平方衰减因子
	void setQuadraticAttenuation(GLfloat factor)
	{
		light_attenuation[2] = factor;
		glLightf(nID, GL_QUADRATIC_ATTENUATION, light_attenuation[2]);
	}
	GLfloat getQuadraticAttenuation() { return light_attenuation[2]; }
	//设置衰减因子
	void setAttenuation(GLfloat constant, GLfloat linear, GLfloat quadratic)
	{
		setConstantAttenuation(constant);
		setLinearAttenuation(linear);
		setQuadraticAttenuation(quadratic);
	}

	//设置聚光指数
	void setExponent(GLfloat exponent)
	{
		light_exponent = exponent;
		glLightf(nID, GL_SPOT_EXPONENT, light_exponent);
	}
	GLfloat getExponent() { return light_exponent; }

	//设置光源色
	void setLightColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
	{
		light_color[0] = red;
		light_color[1] = green;
		light_color[2] = blue;
		light_color[3] = alpha;
		glLightfv(nID, GL_DIFFUSE, light_color);
	}
	void getLightColor(PsGL::PsColor4f& color)
	{
		color.red = light_color[0];
		color.green = light_color[1];
		color.blue = light_color[2];
		color.alpha = light_color[3];
	}

	//设置环境光色
	void setLightAmbientColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
	{
		light_ambient[0] = red;
		light_ambient[1] = green;
		light_ambient[2] = blue;
		light_ambient[3] = alpha;
		glLightfv(nID, GL_AMBIENT, light_ambient);
	}
	void getLightAmbientColor(PsGL::PsColor4f& color)
	{
		color.red = light_ambient[0];
		color.green = light_ambient[1];
		color.blue = light_ambient[2];
		color.alpha = light_ambient[3];
	}

	//设置高光色
	void setLightSpecularColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
	{
		light_specular[0] = red;
		light_specular[1] = green;
		light_specular[2] = blue;
		light_specular[3] = alpha;
		glLightfv(nID, GL_SPECULAR, light_specular);
	}
	void getLightSpecularColor(PsGL::PsColor4f& color)
	{
		color.red = light_specular[0];
		color.green = light_specular[1];
		color.blue = light_specular[2];
		color.alpha = light_specular[3];
	}

};


/*相机类*/
class PsGLCamera :
	public PsGL
{
public:
	PsGLCamera(void);
public:
	~PsGLCamera(void);

/*基本属性*/
protected:
	GLdouble camera_ScaleFactor;//缩放因子
	GLfloat camera_RotateSpeed;//旋转速度(角度制)
	PsGL::PsPoint3Dd camera_Position;//位置
	PsGL::PsPoint3Dd camera_ViewPoint;//视点
	PsGL::PsVector3Dd camera_UpVector;//上向量
	PsGL::PsVector3Dd camera_DirectionVector;//方向向量

/*Set and Get*/
public:
	void setScaleFactor(GLdouble factor) { camera_ScaleFactor = factor; }
	GLdouble getScaleFactor() { return camera_ScaleFactor; }

	void setRotateSpeed(GLfloat angle) { camera_RotateSpeed = angle; }
	GLfloat getRotateSpeed() { return camera_RotateSpeed; }

	void setUpVector(GLdouble x, GLdouble y, GLdouble z)
	{
		camera_UpVector.x = x;
		camera_UpVector.y = y;
		camera_UpVector.z = z;
	}
	const PsGL::PsVector3Dd& getUpVector() { return camera_UpVector; }

	void setPosition(GLdouble x, GLdouble y, GLdouble z)
	{
		camera_Position.x = x;
		camera_Position.y = y;
		camera_Position.z = z;
		//update direction vector
		camera_DirectionVector.x = camera_ViewPoint.x - x;
		camera_DirectionVector.y = camera_ViewPoint.y - y;
		camera_DirectionVector.z = camera_ViewPoint.z - z;
	}
	const PsGL::PsPoint3Dd& getPosition() { return camera_Position; }

	void setViewPoint(GLdouble x, GLdouble y, GLdouble z)
	{
		camera_ViewPoint.x = x;
		camera_ViewPoint.y = y;
		camera_ViewPoint.z = z;
		//update direction vector
		camera_DirectionVector.x = x - camera_Position.x;
		camera_DirectionVector.y = y - camera_Position.y;
		camera_DirectionVector.z = z - camera_Position.z;
	}
	const PsGL::PsPoint3Dd& getViewPoint() { return camera_ViewPoint; }

	void refreshDirectionVector()
	{
		camera_DirectionVector.x = camera_ViewPoint.x - camera_Position.x;
		camera_DirectionVector.y = camera_ViewPoint.x - camera_Position.y;
		camera_DirectionVector.z = camera_ViewPoint.x - camera_Position.z;
	}
	const PsGL::PsVector3Dd& getDirectionVector() { return camera_DirectionVector; }

/*高级操作*/
public:
	//放置相机
	void viewFromCamera();
	//环绕视点
	void rotatePosition(GLdouble axis_x, GLdouble axis_y, GLdouble axis_z, GLfloat angle);
	void rotatePosition(int pre_x, int pre_y, int new_x, int new_y);
	//球面移动视点
	void rotateViewPoint(GLdouble axis_x, GLdouble axis_y, GLdouble axis_z, GLfloat angle);
	void rotateViewPoint(int pre_x, int pre_y, int new_x, int new_y);
	//平移相机
	void translateCamera(GLdouble vector_x, GLdouble vector_y, GLdouble vector_z);
	void translateCamera(int pre_x, int pre_y, int new_x, int new_y);
	//旋转相机
	void rotateCamera(GLfloat degree);
};



/*场景管理类*/
class PsGLSceneManager :
	public PsGL
{
public:
	PsGLSceneManager(void);
public:
	~PsGLSceneManager(void);

/*结构定义*/
	struct _LightInfo {
		char* name;
	};

	struct _CameraInfo {
		char* name;
	};
};






#endif

⌨️ 快捷键说明

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