Depth

Introduction

DepthUp until now, any primitives that you displayed would appear on top of each other depending on the order that they were displayed in. This would occur, even if the primitives had different z values.

This tutorial will discuss how depth can be added to your programs, allowing the zcoordinate to work correctly.

This is accomplished by using what is called a depth buffer. The depth buffer contains a value for every pixel on the screen. This value is in the range [0,1]. This represents the distance between the object and the viewer. Every coordinate has an associated depth value. When two depth values are compared, the lower of the two values will appear on the screen. A coordinate that is in front of another will have a lower depth value. You will see how this works further down in the tutorial.

Contents of main.cpp :


void init()
{

The first step that must be taken is to enable the depth buffer. This is done by passing the GL_DEPTH_TEST flag to the glEnable function.

For the depth buffer to be updated for writing, GL_TRUE needs to be passed to the glDepthMask function. This value is passed by default so we do not need to call the function. If you would like to disable writing to the depth buffer but you would still the buffer enabled, you can pass GL_FALSE onto this function.

	glEnable(GL_DEPTH_TEST);

At the beginning of the tutorial, we said that the lower the depth value, the closer the coordinate is to the viewer. This can be changed by using the glDepthFunc function. This function specifies how values in the depth buffer will be compared. The values that can be passed onto the function are given below :

Flag Description
GL_NEVER Never passes.
GL_LESS Passes if the incoming depth value is less than the stored value.
GL_EQUAL Passes if the incoming depth value is equal to the stored value.
GL_LEQUAL Passes if the incoming depth value is less than or equal to the stored value.
GL_GREATER Passes if the incoming depth value is greater than the stored value.
GL_NOTEQUAL Passes if the incoming depth value is not equal to the stored value.
GL_GEQUAL Passes if the incoming depth value is greater than or equal to the stored value.
GL_ALWAYS Always passes.

When drawing a primitive, the depth test will take place. If the test passes, the incoming color value will replace the current one.

The default value is GL_LESS. We want the test to pass if the values are equal as well. This will cause objects with the same z value. to display depending on the order that they were drawn in. We therefore pass GL_LEQUAL to the function.

	glDepthFunc(GL_LEQUAL);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

For depth testing to be able to compare values, you need to initialize all values in the buffer. This can be achieved by using the glClearDepthf function. This function takes one parameter which specifies what the values in the depth buffer should be initialized with. We pass 1 to the function as this will allow any primitive to appear on the screen. If you had chosen a comparison function of GL_GREATER or GL_GEQUAL, you would have needed to pass0 to this function.

	glClearDepthf(1.0f);

We are going to be displaying some triangles onscreen to show how the depth buffer works.

	glVertexPointer(3, GL_FLOAT, 0, triangle);
	glColorPointer(4, GL_FLOAT, 0, colors);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
}

void display()
{

As with the color buffer, a call to glClear needs to be made to clear the depth buffer with the value passed toglClearDepthGL_DEPTH_BUFFER_BIT must be passed onto the glClear function to achieve this. This can be done at the same time as the color buffer by separating the flags with an | (or) symbol.

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

We draw our first triangle, centered in the screen.

	glDrawArrays(GL_TRIANGLES, 0, 3);

The second triangle behind and slightly to the left of the first triangle.

	glPushMatrix();
	{
		glTranslatef(-0.2f, 0.0f, -1.0f);

		glDrawArrays(GL_TRIANGLES, 0, 3);

The third triangle is rotated 45 degrees and is at the same distance in the z axis as the second triangle.

		glRotatef(45.0f, 0.0f, 0.0f, 1.0f);

		glDrawArrays(GL_TRIANGLES, 0, 3);
	}
	glPopMatrix();

The last triangle is placed in the same z axis position as the first triangle. This is the small triangle that lies to the right.

	glPushMatrix();
	{
		glTranslatef(0.5f, 0.0f, 0.0f);
		glScalef(0.5f, 0.5f, 0.5f);

		glDrawArrays(GL_TRIANGLES, 0, 3);
	}
	glPopMatrix();

	glFlush();
	glutSwapBuffers();
}

Our menu code has changed slightly. A new function, glIsEnabled, is given below. This function takes one flag and determines if it enabled. You can pass any parameter that is usually passed onto the glEnable andglDisable functions. The code below allows you to select the depth menu option to enable and disable depth testing.

void menu(int entry)
{
	switch(entry)
	{
	case 1 : exit(0); break;
	case 2 :
		if (glIsEnabled(GL_DEPTH_TEST))
			glDisable(GL_DEPTH_TEST);
		else
			glEnable(GL_DEPTH_TEST);
		
		glutPostRedisplay();
		break;
	}
}

The glutInitDisplayMode / ugCreateWindow can be used to enable certain buffers. For the depth buffer to work with the UG library, the UG_DEPTH flag needs to passed as the second parameter of the ugCreateWindowfunction. With GLUT|ES, you need to simply pass the GLUT_DEPTH flag onto the glutInitDisplayMode function as shown below. This allocates memory for the depth buffer.

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

Running the program, you will notice that the second and third triangles both appear behind the first triangle. This is because they are at a more negative z axis position.

The fourth triangle is in front of the first triangle as it was drawn afterwards. The third triangle appears in front of the second triangle for the same reason.

Turning off depth testing by selecting the depth menu option will result in the triangles overlapping depending on the order that they were drawn in. The first triangle is at the back and the last triangle is shown in the front.

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

03-14
<think>好的,我需要解决用户关于技术领域中“depth”一词的疑问。用户提到可能涉及深度学习、深度优先搜索或其他技术概念,我需要综合这些领域来解释“depth”的不同含义。 首先,查阅用户提供的引用内容,特别是引用[1]中提到的移动设备上的深度估计网络,这里“depth”指的是三维空间中的深度信息,用于计算机视觉中的场景理解,比如立体视觉或单目深度估计。引用[4]中的对象检测可能不直接涉及“depth”,但结合其他资料,可以确定在深度学习中,深度通常指神经网络的层数,即深度神经网络(DNN)。 然后,数据结构中的深度优先搜索(DFS)中的“depth”指的是沿着树或图的路径深入探索到底再回溯的策略,这里的深度是路径层次的概念。 接下来,需要区分不同上下文中“depth”的具体含义。在计算机视觉中,它指三维深度;在深度学习中,指网络层数;在算法中,指搜索的层次深度。可能还需要提到其他领域,如数据挖掘中的深度数据或网络深度在系统架构中的应用。 用户提供的引用中,引用[1]详细说明了移动设备上的深度估计应用,引用[3][2]似乎不太相关,但引用[4]中的对象检测可能涉及深度学习模型,其中深度网络的使用是关键。需要确保每个解释都有对应的引用支持,例如在深度估计部分引用[1],深度学习部分引用[4]中的相关技术。 最后,生成相关问题时要覆盖各个解释领域,帮助用户进一步探索。例如,深度学习的优化、深度优先搜索的应用、深度估计的技术挑战等。</think>在技术领域中,“**depth**”一词根据上下文有多种含义,以下是常见解释: --- ### 1. **深度学习中的“深度”(Deep Learning)** 指神经网络中**隐藏层的数量**,层数越多则网络越“深”。例如: $$ \text{深度神经网络}(DNN) = f_{n}(...f_{2}(f_{1}(输入))) $$ 深度模型通过多层非线性变换提取复杂特征,常用于图像识别[^4]、自然语言处理等场景。例如,卷积神经网络(CNN)的深度直接影响特征抽象能力[^1]。 --- ### 2. **计算机视觉中的“深度”(Depth Estimation)** 指三维空间中物体到相机的**距离信息**,常用于: - 立体视觉(Stereo Vision):通过双目摄像头计算深度[^1] - 单目深度估计:从单张图像预测深度图(如MobileDepth等移动端优化模型)[^1] - AR/VR场景重建:结合深度传感器(如LiDAR或双像素传感器)[^1] --- ### 3. **算法中的“深度优先搜索”(Depth-First Search, DFS)** 一种**遍历树或图的策略**,沿着分支深入到底再回溯,常用于路径搜索、拓扑排序等。例如: ```python def dfs(node): if node not in visited: visited.add(node) for neighbor in node.neighbors: dfs(neighbor) ``` --- ### 4. **其他技术场景** - **数据深度(Data Depth)**:统计学中描述数据点在分布中的位置 - **网络深度(Network Depth)**:系统架构中层次结构的复杂度(如OSI模型) - **代码深度(Code Depth)**:程序嵌套层级(如循环/条件语句嵌套) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值