计算机图形学,不可透视投影和可透视投影

#include<stdio.h>
#include<gl/glut.h>
#include<math.h>
#ifndef PI
#define PI 3.14159265
#endif

void DrawPlane()
{
	glColor3f(0.6, 0.6f, 0.6f);
	glBegin(GL_LINES);
	for (float u = -1.0f; u <= 1.01f; u += 0.2f)//1.01非常巧妙
	{
		glVertex3d(-1, 0, u);
		glVertex3d(1, 0, u);

		glVertex3d(u, 0, -1);
		glVertex3d(u, 0, 1);
	}
	glEnd();
}

void onDisplay()
{
	glClearColor(1, 1, 1, 1);//RGBA 
	glClear(GL_COLOR_BUFFER_BIT);//清除缓存 把窗口清屏,颜色由glClearColor语句决定  清屏在后台进行

								 //第二次上课注释部分
								 //glColor3f(1, 0, 0);
								 /*glBegin(GL_LINES);/第一次上课注释部分

								 glVertex2d(0, 0);
								 glVertex2d(1, 1);
								 glEnd();*/
								 //第二次上课注释部分 
								 //glutWireSphere(0.5, 16, 16);

	glLoadIdentity();
	double m[16] = { 0 };
	m[0] = m[5] = m[10] = m[15] = 1;
	/*m[0] = -1;
	m[5] = -1;
	m[15] = 1;
	m[10] = -1;*/

	//m[0] = 1.5;//x方向的缩放比例
	//m[5] = 0.5;//y方向的缩放比例
	//m[11] = 1;
	//m[9] = 1;

	double theta = 20 * PI / 180.0;
	double alpha = 30 * PI / 180.0;
	double beta = 45 * PI / 180.0;
	//绕X轴旋转
	/*m[5] = cos(theta);
	m[6] = sin(theta);
	m[9] = -sin(theta);
	m[10]= cos(theta);*/

	//绕Y轴旋转
	/*m[0] = cos(theta);
	m[2] = -sin(theta);
	m[8] = sin(theta);
	m[10] = cos(theta);*/

	//先绕Y轴再绕X轴旋转(这个是错误的)
	//正确的应该是先变换的写在后面,后变换的写在前面!!!!!!!!!
	/*m[5] = cos(theta);
	m[6] = sin(theta);
	m[9] = -sin(theta);
	m[10] = cos(theta);
	m[0] = cos(theta);
	m[2] = -sin(theta);
	m[8] = sin(theta);
	m[10] = cos(theta);*/

	//先绕Y轴再绕X轴旋转
	/*m[0] = cos(alpha);
	m[1] = sin(alpha) * sin(beta);
	m[2] = -sin(alpha) * cos(beta);
	m[5] = cos(beta);
	m[6] = sin(beta);
	m[8] = sin(alpha);
	m[9] = -cos(alpha) * sin(beta);
	m[10] = cos(alpha) * cos(beta);
	m[14] = -1;
	glMultMatrixd(m);
	*/

	//自己多练习,调用代码进行平移代换。

	m[14] = -1;
	glMultMatrixd(m);

	/*double rx[16] = { 0 };
	rx[0] = rx[5] = rx[10] = rx[15] = 1;
	rx[5] = cos(beta);
	rx[6] = sin(beta);
	rx[9] = -sin(beta);
	rx[10] = cos(beta);
	glMultMatrixd(rx);

	double ry[16] = { 0 };
	ry[0] = ry[5] = ry[10] = ry[15] = 1;
	ry[0] = cos(alpha);
	ry[2] = -sin(alpha);
	ry[8] = sin(alpha);
	ry[10] = cos(alpha);
	glMultMatrixd(ry);*/



	//前面的代码都是在进行变换,记得搜一下查找变换怎么操作!!!


	DrawPlane();//调用drawplane函数
	glPushMatrix();
	glTranslatef(-0.3f, 0, -0.3f);
	glColor3f(1, 0, 0);
	glutWireCube(0.4f);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(0.3f, 0, -0.3f);
	glColor3f(0, 1, 0);
	glutWireSphere(0.2f, 16, 16);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(0, 0, 0.3f);
	glRotatef(-90, 1, 0, 0);
	glColor3f(1, 1, 0);
	glutWireCone(0.2f, 0.6f, 16, 8);
	glPopMatrix();


	glutSwapBuffers();//交换双缓存!前台和后台缓存进行交换

}

void onReshape(int w, int h)
{
	glViewport(0, 0, w, h);
	double aspect = (double)w / h;//宽高比
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	double near, far;
	near = 0;
	far = 2;

	double ms[16] = {0};
	ms[0] = ms[5] = ms[10] = ms[15]=1;
	if (aspect>1)
	{
		ms[0] = 1.0 / aspect;
	}
	else
	{
		ms[5] = aspect;
	}
	ms[10] = 2.0/(far-near);
	glMultMatrixd(ms);

	double mt[16] = { 0 };
	mt[0] = mt[5] = mt[10] = mt[15] = 1;
	mt[14] = (near+far)*0.5;
	glMultMatrixd(mt);

	//绕y轴旋转-90度
	//double mp[16] = {0};
	//mp[0] = mp[5] = mp[10] = mp[15] = 1;
	//mp[0] = 0;
	//mp[2] = 1;
	//mp[8] = -1;
	//mp[10] = 0;
	//glMultMatrixd(mp);

	//绕x轴旋转+90度
	//double mx[16] = {0};
	//mx[0] = mx[5] = mx[10] = mx[15] = 1;
	//mx[5] = mx[10] = 0;
	//mx[6] = 1;
	//mx[9] = -1;
	//glMultMatrixd(mx);

	//先绕x轴旋转-a度再绕y轴旋转b度
	/*double mx[16] = {0};
	mx[0] = mx[5] = mx[10] = mx[15] = 1;
	mx[5] = sqrt(6.0)/3;
	mx[6] = sqrt(3.0) / 3;
	mx[9] = -sqrt(3.0) / 3;
	mx[10] = sqrt(6.0) / 3;
	glMultMatrixd(mx);
	double my[16] = { 0 };
	my[0] = my[5] = my[10] = my[15] = 1;
	my[0] = sqrt(2.0) / 2;
	my[2] = sqrt(2.0) / 2;
	my[8] = -sqrt(2.0) / 2;
	my[10] = sqrt(2.0) / 2;
	glMultMatrixd(my);*/
	//透视投影变换
	double mp[16] = {0};
	mp[0] = mp[5] = mp[10] = mp[15] = 1;
	mp[3] = 1;
	mp[7] = -1;
	mp[11] = -1;
	glMultMatrixd(mp);
	//if (aspect >= 1) {
	//	glFrustum(-aspect, aspect, -1, 1, 1, 2);
	//gluPerspective(-aspect, aspect, -1, 1);
	//}
	//else
	//{
	//	glFrustum(-1, 1, -1 / aspect, 1 / aspect, 1, 2);//换成0 2试试平移
	//}
	glMatrixMode(GL_MODELVIEW);
	glutPostRedisplay();
}
int main(int argc, char *argv[])
{
	glutInit(&argc, argv);//初始化glut
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//设置OpenGL显示模式(双缓存,RGB颜色模式,深度缓存)
	glutInitWindowSize(400, 300);//设置窗口初始尺寸
	glutInitWindowPosition(100, 100);//设置窗口初始位置 windows窗口坐标系在窗口左上角
	glutCreateWindow("Graphic Study");//设置窗口标题
	glutDisplayFunc(onDisplay);//设置 显示回调
	glutReshapeFunc(onReshape);
	glutMainLoop();//进入glut时间循环
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值