• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            OpenGL Polygon Offset

            OpenGL

            Polygon Offset



            Tutorials > OpenGL > Polygon?Offset

            LINK:http://www.zeuscmd.com/tutorials/opengl/15-PolygonOffset.php
            ull Source

            Introduction

            Polygon Offset It is often quite useful to accentuate the edges of your objects by rendering your object once in fill mode and once in line mode. This often produces unsatisfactory results as the line may move into and out of the polygon. This effect is commonly known as stitching.

            You may have also noticed in the past that when rendering two polygons that overlap each other, Z-fighting occurs and parts of each polygon are rendered.

            The results of stitching and z-fighting can be seen in the figures below.

            Stitching Effect Stitching Effect
            Stitching Effect Z-Fighting Effect

            This tutorial expands on tutorial 13 and shows how you can overcome these effects by making use of polygon offsets.

            Contents of main.cpp :


            One variable is kept to indicate whether polygon offsets are currently being used. This will allow you to toggle polygon offsets on and off to see exactly how this technique improves the program.

            				bool offset = true;
            

            The drawBox function remains exactly the same except that the calls to change the current color have been removed.

            				void drawBox()
            {
            	glBegin(GL_QUADS);
            		// FRONTglVertex3f(-0.5f, -0.5f,  0.5f);
            		glVertex3f( 0.5f, -0.5f,  0.5f);
            		glVertex3f( 0.5f,  0.5f,  0.5f);
            		glVertex3f(-0.5f,  0.5f,  0.5f);
            		// BACKglVertex3f(-0.5f, -0.5f, -0.5f);
            		glVertex3f(-0.5f,  0.5f, -0.5f);
            		glVertex3f( 0.5f,  0.5f, -0.5f);
            		glVertex3f( 0.5f, -0.5f, -0.5f);
            		// LEFTglVertex3f(-0.5f, -0.5f,  0.5f);
            		glVertex3f(-0.5f,  0.5f,  0.5f);
            		glVertex3f(-0.5f,  0.5f, -0.5f);
            		glVertex3f(-0.5f, -0.5f, -0.5f);
            		// RIGHTglVertex3f( 0.5f, -0.5f, -0.5f);
            		glVertex3f( 0.5f,  0.5f, -0.5f);
            		glVertex3f( 0.5f,  0.5f,  0.5f);
            		glVertex3f( 0.5f, -0.5f,  0.5f);
            		// TOPglVertex3f(-0.5f,  0.5f,  0.5f);
            		glVertex3f( 0.5f,  0.5f,  0.5f);
            		glVertex3f( 0.5f,  0.5f, -0.5f);
            		glVertex3f(-0.5f,  0.5f, -0.5f);
            		// BOTTOMglVertex3f(-0.5f, -0.5f,  0.5f);
            		glVertex3f(-0.5f, -0.5f, -0.5f);
            		glVertex3f( 0.5f, -0.5f, -0.5f);
            		glVertex3f( 0.5f, -0.5f,  0.5f);
            	glEnd();
            }
            

            To show the effects of z-fighting, we will be displaying some polygons on top of a cube. A drawPolygon function has therefore been created to simplify creation of multiple quads.

            				void drawPolygon()
            {
            	glBegin(GL_QUADS);
            		glVertex3f(-0.5f, -0.5f,  0.0f);
            		glVertex3f( 0.5f, -0.5f,  0.0f);
            		glVertex3f( 0.5f,  0.5f,  0.0f);
            		glVertex3f(-0.5f,  0.5f,  0.0f);
            	glEnd();
            }
            

            The beginning of our display function remains the same.

            				void display()
            {
            	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            	glLoadIdentity();
            
            	gluLookAt(
            		0.0f, 0.0f, 3.0f,
            		0.0f, 0.0f, 0.0f,
            		0.0f, 1.0f, 0.0f);
            
            	glRotatef(xrot, 1.0f, 0.0f, 0.0f);
            	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
            

            The object we will be creating can be seen at the beginning of this tutorial. It is a red cube, outlined by a black line. The one side of the red cube has 3 overlapping polygons on top of it.

            Polygon offsets can be used in a number of ways to reduce the errors caused by these overlapping primitives. One possible solution is to offset the polygons of the cube to make it slightly smaller. This will allow the lines and additional polygons from being rendered without interfering with the cube.

            There are three different ways to enable polygon offset, one for each rasterization mode (GL_POINT, GL_LINE and GL_FILL). This is specified by a single call to glEnable with one of the parameters, GL_POLYGON_OFFSET_POINT, GL_POLYGON_OFFSET_LINE and GL_POLYGON_OFFSET_FILL respectively.

            The cube will first be rendered, so we pass the GL_POLYGON_OFFSET_FILL flag onto the glEnable function.

            				if (offset)
            	{
            		glEnable(GL_POLYGON_OFFSET_FILL);
            

            It is necessary to specify how much a polygon must be offset. This is achieved with a call to the glPolygonOffset function. This function accepts 2 parameters, GLfloatfactor and GLfloatunits.

            The depth value of each fragment is added to an offset value which is calculate in the following way?:

            offset = (m * factor) + (r * units)

            where m is the maximum depth slope of the polygon and r is the smallest value guaranteed to produce a resolvable difference in window coordinate depth values. The value r is an implementation-specific constant.

            A positive offset will push the object away from you whereas a negative offset will pull the object towards you.

            The depth slope is the change in z (depth) value divided by the change in either x or y coordinates as you traverse the polygon. Therefore, polygons that are parallel to the near and far clipping planes will have a depth slope of zero. The closer the depth slope is to 0, the smaller the offset needs to be.

            There is quite a bit of maths involved but luckily it is often good enough to simply use values such as 1.0 or 0.0 for the factor and units parameters, which is what we will be doing in this tutorial. As we want the polygons of the cube to be pushed away from us, we pass positive 1 as both parameters to glPolygonOffset.

            				glPolygonOffset(1.0f, 1.0f);
            	}
            

            The cube is then rendered as per normal in the GL_FILL rasterization mode. After this has been done, polygon offset is disabled.

            				glColor3f(1.0f, 0.0f, 0.0f);
            	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
            	drawBox();
            
            	if (offset)
            		glDisable(GL_POLYGON_OFFSET_FILL);
            

            The green and yellow polygons on the front of the cube can now be rendered without worrying about z-fighting.

            				glColor3f(0.0f, 1.0f, 0.0f);
            	glPushMatrix();
            		glTranslatef(-0.25f, -0.25f, 0.5f);
            		glScalef(0.5f, 0.5f, 0.5f);
            		drawPolygon();
            	glPopMatrix();
            
            	glColor3f(1.0f, 1.0f, 0.0f);
            	glPushMatrix();
            		glTranslatef(0.25f, 0.25f, 0.5f);
            		glScalef(0.5f, 0.5f, 0.5f);
            		drawPolygon();
            	glPopMatrix();
            

            If we now try to render the blue polygon, it will cause z-fighting with the green and yellow polygons on the front of the cube.

            A way around this is to pull the blue polygon towards us to prevent z-fighting with both the cube and the front polygons. This is done by passing -1 as both parameters to the glPolygonOffset function.

            				if (offset)
            	{
            		glEnable(GL_POLYGON_OFFSET_FILL);
            		glPolygonOffset(-1.0f, -1.0f);
            	}
            
            	glColor3f(0.0f, 0.0f, 1.0f);
            	glPushMatrix();
            		glTranslatef(0.0f, 0.0f, 0.5f);
            		glScalef(0.5f, 0.5f, 0.5f);
            		drawPolygon();
            	glPopMatrix();
            
            	if (offset)
            		glDisable(GL_POLYGON_OFFSET_FILL);
            

            If we were to now render the outline of the cube, stitching would occur with the front polygons. The lines can therefore be pulled towards us in the same way that the blue polygon was. The only difference is that now we need to pass GL_POLYGON_OFFSET_LINE onto the glEnable function instead.

            				if (offset)
            	{
            		glEnable(GL_POLYGON_OFFSET_LINE);
            		glPolygonOffset(-1.0f, -1.0f);
            	}
            
            	glColor3f(0.0f, 0.0f, 0.0f);
            	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
            	drawBox();
            
            	if (offset)
            		glDisable(GL_POLYGON_OFFSET_LINE);
            		
            	glFlush();
            }
            

            Our idle function remains the same except that now polygon offset can be toggled on and off by pressing the O key.

            				void idle()
            {
            	.
            	.
            
            	if (opengl->isKeyDown('O'))
            	{
            		opengl->keyUp('O');
            		offset = !offset;
            	}
            }
            

            By enabling and disabling polygon offset, you can clearly see how it affects your program. The result is shown below?:

            Orthographic View Perspective View
            Polygon Offset Enabled Polygon Offset Disabled

            You should now be able to effectively combat z-buffering and stitching. This is a great feature of OpenGL and can be used to greatly enhance the appearance of your application.

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

            Win32?Source?Files?: Visual Studio Dev-C++
            GLUT?Source?Files?: Visual Studio Dev-C++ Unix / Linux

            posted on 2006-11-16 11:48 zmj 閱讀(2497) 評論(0)  編輯 收藏 引用

            狠狠色伊人久久精品综合网| 2019久久久高清456| 99热成人精品热久久669| 久久精品午夜一区二区福利| 久久国产精品久久精品国产| 色婷婷噜噜久久国产精品12p| 97视频久久久| 国产午夜精品理论片久久| 中文字幕无码久久人妻| AV色综合久久天堂AV色综合在| 亚洲成人精品久久| 亚洲国产精品无码久久98| 99久久国产综合精品五月天喷水| 亚洲欧洲久久av| 久久精品免费观看| 久久久久亚洲精品日久生情| 久久99精品国产99久久| 久久婷婷色综合一区二区| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 久久精品中文字幕无码绿巨人| 亚洲一本综合久久| 亚洲AV无码久久精品成人 | 亚洲伊人久久综合中文成人网| 日日噜噜夜夜狠狠久久丁香五月| 久久人妻少妇嫩草AV蜜桃| 久久国产亚洲精品麻豆| 精品久久久久久中文字幕人妻最新| 亚洲乱码日产精品a级毛片久久| 亚洲国产精品久久久久婷婷软件| 久久综合综合久久综合| 国内精品久久久久影院薰衣草| 狠狠色综合网站久久久久久久| 九九久久99综合一区二区| AV色综合久久天堂AV色综合在| 99精品国产综合久久久久五月天 | 久久毛片免费看一区二区三区| 国产精品免费看久久久| 东京热TOKYO综合久久精品| 97精品国产97久久久久久免费 | 久久久久国产精品熟女影院| 无码人妻少妇久久中文字幕蜜桃|