青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 閱讀(2510) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲视频在线免费观看| 亚洲欧洲免费视频| 国产一级精品aaaaa看| 欧美午夜精品电影| 欧美日韩一区二区在线视频 | 久久久久中文| 欧美中文字幕视频| 久久视频国产精品免费视频在线| 美女精品视频一区| 亚洲电影网站| 亚洲午夜激情免费视频| 午夜精品区一区二区三| 久久综合九色综合欧美狠狠| 久久精品一二三区| 欧美精品福利视频| 国产精品福利在线观看网址| 国产一区二区看久久| 亚洲第一久久影院| 日韩午夜精品| 久久一区免费| 国产性色一区二区| 国产女主播一区| 国产视频精品va久久久久久| 亚洲大胆人体视频| 亚洲一区二三| 麻豆久久婷婷| 一本色道久久88综合日韩精品 | 狠狠色丁香久久综合频道| 亚洲国产精品一区| 亚洲一品av免费观看| 久久久之久亚州精品露出| 亚洲人成小说网站色在线| 亚洲国产婷婷| 一本色道久久综合亚洲精品高清 | 亚洲影院在线观看| 欧美 日韩 国产 一区| 一区二区日韩欧美| 美女网站在线免费欧美精品| 亚洲专区一区| 麻豆精品国产91久久久久久| 国产精品视频精品视频| 久久成人av少妇免费| 久久频这里精品99香蕉| 欧美日韩伦理在线免费| 国产婷婷色综合av蜜臀av| 另类国产ts人妖高潮视频| 一本色道久久综合亚洲精品不| 亚洲视频在线观看网站| 久久久久久成人| 欧美国产视频一区二区| 亚洲自拍偷拍网址| 久久综合色婷婷| 久久综合色婷婷| 亚洲二区在线视频| 亚洲福利视频一区| 性高湖久久久久久久久| 麻豆九一精品爱看视频在线观看免费 | 欧美午夜国产| 国产欧美一区二区三区在线老狼| 欧美日韩在线一区二区三区| 国产伊人精品| 欧美在线三区| 中文在线资源观看视频网站免费不卡| 欧美福利电影网| 国产精品久久久| 一区二区三区欧美亚洲| 欧美激情亚洲精品| 巨乳诱惑日韩免费av| 国产亚洲欧美色| 久久亚洲午夜电影| 久久都是精品| 国内精品视频666| 久久久久久久999精品视频| 欧美在线不卡视频| 亚洲国产精品悠悠久久琪琪| 欧美激情综合色| 欧美日韩一区二区三区四区在线观看| 中国成人在线视频| 亚洲一区二区成人| 国产一区二区电影在线观看| 免费久久99精品国产自| 欧美激情乱人伦| 国产精品欧美风情| 亚洲在线视频观看| 一区二区三区.www| 国产日韩在线亚洲字幕中文| 久久午夜电影| 久久综合激情| 亚洲香蕉视频| 久久久7777| 亚洲精品国产精品乱码不99| 日韩视频专区| 黄色欧美成人| 老色鬼久久亚洲一区二区| 老牛嫩草一区二区三区日本| 亚洲天堂av图片| 欧美亚洲视频| 99国产成+人+综合+亚洲欧美| 日韩亚洲成人av在线| 欧美视频在线一区| 99国产成+人+综合+亚洲欧美| 性欧美超级视频| 亚洲视频一区| 久久精品123| 亚洲福利视频二区| 亚洲欧美国产高清va在线播| 亚洲日韩欧美一区二区在线| 午夜在线一区二区| 制服诱惑一区二区| 免费日韩av片| 欧美在线观看你懂的| 欧美黑人多人双交| 亚洲伊人久久综合| 久久狠狠亚洲综合| 日韩视频永久免费| 午夜欧美精品| 亚洲一区二区三区四区五区午夜| 久久久99精品免费观看不卡| 夜久久久久久| 免费观看成人www动漫视频| 欧美中文字幕视频在线观看| 欧美日韩精品免费 | 欧美r片在线| 久久伊人一区二区| 欧美尤物巨大精品爽| 欧美黄色免费网站| 欧美夫妇交换俱乐部在线观看| 国产亚洲一区二区三区在线观看| 一本色道久久| 亚洲理伦电影| 亚洲视频一区二区在线观看| 日韩一级免费| 妖精成人www高清在线观看| 欧美无乱码久久久免费午夜一区 | 久久青青草原一区二区| 午夜精品久久久久久久99水蜜桃| 欧美人成网站| 欧美激情免费观看| 亚洲国产婷婷香蕉久久久久久| 久久久青草青青国产亚洲免观| 羞羞答答国产精品www一本| 91久久极品少妇xxxxⅹ软件| 欧美一区二区视频在线观看2020 | 亚洲欧美综合一区| 欧美精品系列| 亚洲毛片一区二区| 一二三区精品| 国产精品高潮呻吟久久av无限| 日韩一二三区视频| 一区二区三区 在线观看视| 欧美日韩国产一级| 亚洲理论在线观看| 亚洲欧美日韩在线观看a三区| 国产精品久久久久永久免费观看| 亚洲一级黄色av| 久久精品一二三区| 国模一区二区三区| 久久综合久久综合这里只有精品| 欧美成人日本| 一区二区三区四区蜜桃| 国产精品99一区二区| 欧美一级午夜免费电影| 欧美成人xxx| 亚洲国产一区二区三区青草影视 | 久久久国产精品亚洲一区| 在线播放豆国产99亚洲| 欧美大片免费观看在线观看网站推荐| 亚洲精品字幕| 欧美伊人久久大香线蕉综合69| 国产伦精品一区二区三区视频孕妇| 亚洲已满18点击进入久久| 久久蜜桃av一区精品变态类天堂| 亚洲精品在线观看免费| 国产精品毛片va一区二区三区| 亚洲欧美在线免费观看| 欧美1区免费| 亚洲一区免费网站| 影音先锋久久久| 国产精品久久久久9999吃药| 久久激情视频| 一区二区不卡在线视频 午夜欧美不卡' | 欧美精品一二三| 亚洲自拍电影| 亚洲国产精选| 香蕉成人伊视频在线观看| 国内精品久久久久久久影视麻豆| 欧美jizz19性欧美| 亚洲欧美视频一区| 91久久夜色精品国产网站| 免费成人性网站| 午夜精品福利一区二区三区av| 亚洲观看高清完整版在线观看| 午夜国产精品影院在线观看| 亚洲啪啪91| 国产亚洲一区二区三区在线播放| 欧美日韩直播| 欧美激情一区二区三区四区| 久久久久久尹人网香蕉| 亚洲欧美色一区| 宅男66日本亚洲欧美视频|