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

📄 main.cpp

📁 关于stl模型的加载
💻 CPP
字号:
#include "STLReader.h"
#include "PsGL.h"
#include <gl\glut.h>


bool bIsFullscreen = false;

CModel3D			model;
PsGLCamera		camera;
PsGLLight*		pLight = NULL;

int pre_x;
int pre_y;
int win_w;
int win_h;

void init();
void openFile();
void setCamera();
void display();
void reshape(int w, int h);
void mouseButtonEvent(int button, int state, int x, int y);
void mouseMoveEvent(int x, int y);
void keyEvent(unsigned char key, int x, int y);

int main(int argc, char* argv[])
{//这里的内容可以不需要修改

	glutInit(&argc, argv);//处理命令行参数
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);//显示模式:双缓冲,RGBA色彩深度
	glutInitWindowSize(800, 600);//初始窗口大小
	glutInitWindowPosition(100, 100);//初始窗口位置
	glutCreateWindow(argv[0]);//创建窗口
	init();
	glutDisplayFunc(display);//设定显示回调函数
	glutReshapeFunc(reshape);//设定窗口大小改变事件回调函数
	glutMouseFunc(mouseButtonEvent);//鼠标按键事件回调函数
	glutMotionFunc(mouseMoveEvent);//鼠标移动事件回调函数
	glutKeyboardFunc(keyEvent);//键盘按键事件

	/************************************************************************/
	/* 其它事件必须在主消息循环之前设定                                             */
	/************************************************************************/

	glutMainLoop();//进入消息循环

	return 0;
}

void init()
{//在这里可以进行一些初始化工作
	openFile();
	glutSetWindowTitle(model.m_strModelName);
	
	glClearColor(0.8, 0.8, 0.8, 1.0);//清屏颜色
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);
	
	setCamera();

}

void openFile()
{
	char		filename[512];
	printf_s("Open STLA File >");
	scanf_s("%s", filename);
	model.readFromSTL(filename);
	model.showInfo();
}

void setCamera()
{
	camera.setPosition(model.m_stCenter.x,
		model.m_stCenter.y,
		model.m_stCenter.z + model.m_dbDiagonal*2.25);
	camera.setUpVector(0.0, 1.0, 0.0);
	camera.setViewPoint(model.m_stCenter.x,
		model.m_stCenter.y,
		model.m_stCenter.z);
	camera.setRotateSpeed(0.8);
}

void display()
{//所有的绘图部分放在这里
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除缓冲区
	glMatrixMode(GL_MODELVIEW);//模型视点矩阵模式
	glLoadIdentity();//设置单位矩阵

	/************************************************************************/
	/* 在这里进行绘图                                                               */
	/************************************************************************/

	//绘图例子,使用了glut的绘图函数
	camera.viewFromCamera();//设置视点

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	model.renderModel();

	glutSwapBuffers();
}

void reshape(int w, int h)
{//窗口大小改变时会调用,需要在这里重新进行视口设置和投影矩阵设置
	float N, F;
	float ratio = (float) w/(float) h;
	float display_w, display_h, display_z;

	win_w = w;
	win_h = h;

	//Near = (1/(q-1))*Z
	//Far  = (q/(q-1))*Z
	//q为远景面对近景面的放大倍数
	//Z为场景的纵深

	//近景面的宽度为场景最大长度(对角线)的1.1倍
	//视景体的深度为场景最大长度(对角线)的1.1倍
	display_w = model.m_dbDiagonal*1.2;
	display_h = display_w/ratio;
	display_z = model.m_dbDiagonal*1.5;
	//q = 2
	N = display_z;
	F = 2*display_z;

	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-display_w*0.5, display_w*0.5,
		-display_h*0.5, display_h*0.5,
		N, F);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void mouseButtonEvent(int button, int state, int x, int y)
{//在这里处理鼠标按键按下的事件
	//需要注意屏幕坐标从左上角开始,opengl的视口坐标从左下角开始
	if(button == GLUT_LEFT_BUTTON)
	{
		if (state == GLUT_DOWN)
		{
			pre_x = x;
			pre_y = y;
			glutSetCursor(GLUT_CURSOR_SPRAY);
		}
		else
			glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
	}
}

void mouseMoveEvent(int x, int y)
{//在这里处理鼠标移动事件
	if (pre_x != x || pre_y != y)
	{
		camera.rotatePosition(pre_x, pre_y, x, y);//移动镜头
	}

	pre_x = x;
	pre_y = y;
	glutPostRedisplay();
}

void keyEvent(unsigned char key, int x, int y)
{//在这里处理键盘事件

	switch (key)
	{
	case 'f'://一个按下f全屏的例子
		if (!bIsFullscreen)
		{
			glutFullScreen();
		} 
		else
		{
			glutReshapeWindow(800, 600);
			glutPositionWindow(100, 100);
		}
		bIsFullscreen = !bIsFullscreen;
		break;

	case 'o':
		openFile();
		glutSetWindowTitle(model.m_strModelName);
		reshape(win_w, win_h);
		setCamera();
		glutPostRedisplay();
		break;

	case '1':
		model.m_bIsShaded = false;
		model.m_bIsWired = true;
		glutPostRedisplay();
		break;

	case '2':
		model.m_bIsShaded = true;
		model.m_bIsWired = false;
		glutPostRedisplay();
		break;

	case '3':
		model.m_bIsShaded = true;
		model.m_bIsWired = true;
		glutPostRedisplay();
		break;

	default:
		break;
	}

}

⌨️ 快捷键说明

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