Perspective

Introduction

PerspectiveIn the real world, if you have many objects of the same size at different distances from you, you will notice that the further away the objects are, the smaller they appear.

In the previous tutorial, you would have noticed that the triangles that were placed further back were actually the same size as the first triangle in the front.

This tutorial will explain how a perspective view can be created to cause objects further away to appear smaller. We will also discuss how standard shapes can be easily created using the UG library.

Contents of main.cpp :


We first create two variables to hold the width and the height of the window. You will see how these will be used later.

int w = 0;
int h = 0;

A variable is kept to indicate whether we are looking at an orthographic or perspective view. This allows us to change between the two to see the difference.

bool perspective = true;

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

If you are wanting to move the position of the camera (your view), you could modify the projection matrix. This can get quite complicated. To make your life easier, you can use the gluLookAtf function which is part of theGLU|ES library. The equivalent function in the UG library is the ugluLookAtf function.

This function takes 9 parameters. This consists of 3 coordinates or vectors. The first coordinate specifies where you would like the camera to be placed within your world. The second coordinate specifies the point that you would like the camera to be pointing to. The last coordinate specifies the normalized up vector. You will usually use(0,1,0) for this vector.

The code below places the camera 2 units away from the origin and looks towards the origin.

	gluLookAtf(
		0.0f, 0.0f, 2.0f,
		0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f);

The next segment of code draws 3 squares, each one appearing behind and to the left of the previous one. Instead of creating a vertex array for a square, we use the UG library function ugSolidCubef. This function draws a cube for us at (0,0,0). Some other similar functions are given below :

ugSolidBox(GLfloat Width, GLfloat Depth, GLfloat Height);
ugSolidConef(GLfloat base, GLfloat height, GLint slices, GLint stacks);
ugSolidCubef(GLfloat size);
ugSolidDisk(GLfloat inner_radius, GLfloat outer_radius, GLshort rings, GLshort slices);
ugSolidSpheref(GLfloat radius, GLint slices, GLint stacks);
ugSolidTorusf(GLfloat ir, GLfloat or, GLint sides, GLint rings);
ugSolidTube(GLfloat radius, GLfloat height, GLshort stacks, GLshort slices);

	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	glTranslatef(0.25f, 0.0f, 0.0f);
	ugSolidCubef(0.5f);

	glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
	glTranslatef(-0.25f, 0.0f, -1.0f);
	ugSolidCubef(0.5f);

	glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
	glTranslatef(-0.25f, 0.0f, -1.0f);
	ugSolidCubef(0.5f);
   
	glFlush();
	glutSwapBuffers();
}

Our menu code changes slightly. When you select the perspective menu option, the view must change betweenperspective and orthographic. The reshape function which will be explained soon must be called with the current width and the height to update the view. We must then redraw the screen to show the update.

	case 2 :
		perspective = !perspective;
		reshape(w, h);
		glutPostRedisplay();
		break;

The beginning of our reshape function remains the same.

void reshape(int width, int height)
{
	w = width;
	h = height;

	if (!height)
		height = 1;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glViewport(0, 0, width, height);

Like using glOrthof to create an orthographic view, glFrustumf can be used to create a perspective view. The parameters are the same as the glOrthof function ie. left, right, bottom, top, near, far.

This creates a view similar to the image below. The larger the plane, the smaller the object will appear.

Perspective Frustum

As you can see, this function is not very intuitive. Another function, gluPerspectivef, has been created to overcome this. Like the gluLookAtf function, the UG library equivalent is the ugluPerspectivef function. The parameters follow below :

GLfloat fovy - This specifies the field of view. A 90 degree angle means that you can see everything directly to the left right around to the right of you. This is not how humans see things. I have passed a value of 45 degrees which is a more accurate value.

GLfloat aspect - This specifies that aspect ratio that you desire. This is usually specified as the width divided by the height of the window.

GLfloat n & GLfloat f - This specifies the near and far clipping planes as normal.

The code below sets up a perspective view or an orthographic view depending on the perspective boolean variable.

	if (perspective)
		gluPerspectivef(45.0f, 1.0f * width / height, 1.0f, 100.0f);
	else
		glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 20.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

Well done. You should now be able to set up a perspective view. This allows you to add proper depth to your OpenGL scenes. When running the program, you can select the perspective menu option to change betweenperspective and orthographic view.

OrthographicPerspective
Orthographic ViewPerspective View

Please let me know of any comments you may have : Contact Me

内容概要:本文档详细介绍了一个基于MATLAB实现的跨尺度注意力机制(CSA)结合Transformer编码器的多变量时间序列预测项目。项目旨在精准捕捉多尺度时间序列特征,提升多变量时间序列的预测性能,降低模型计算复杂度与训练时间,增强模型的解释性和可视化能力。通过跨尺度注意力机制,模型可以同时捕获局部细节和全局趋势,显著提升预测精度和泛化能力。文档还探讨了项目面临的挑战,如多尺度特征融合、多变量复杂依赖关系、计算资源瓶颈等问题,并提出了相应的解决方案。此外,项目模型架构包括跨尺度注意力机制模块、Transformer编码器层和输出预测层,文档最后提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,尤其是熟悉MATLAB和深度学习的科研人员、工程师和研究生。 使用场景及目标:①需要处理多变量、多尺度时间序列数据的研究和应用场景,如金融市场分析、气象预测、工业设备监控、交通流量预测等;②希望深入了解跨尺度注意力机制和Transformer编码器在时间序列预测中的应用;③希望通过MATLAB实现高效的多变量时间序列预测模型,提升预测精度和模型解释性。 其他说明:此项目不仅提供了一种新的技术路径来处理复杂的时间序列数据,还推动了多领域多变量时间序列应用的创新。文档中的代码示例和详细的模型描述有助于读者快速理解和复现该项目,促进学术和技术交流。建议读者在实践中结合自己的数据集进行调试和优化,以达到最佳的预测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值