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

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


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   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>
            国产农村妇女毛片精品久久麻豆| 欧美一区久久| 久久都是精品| 中文国产亚洲喷潮| 久久免费视频观看| 久久成人久久爱| 国产精品国内视频| 亚洲国产日本| 在线观看成人一级片| 亚洲欧美日韩国产综合在线 | 久久久久成人精品| 亚洲黄一区二区三区| 国产主播一区| 午夜精品久久久| 先锋影音国产一区| 国产精品久久久一本精品| 99热精品在线观看| 亚洲麻豆视频| 欧美日韩大片一区二区三区| 欧美激情一区二区三区在线| 在线视频国内自拍亚洲视频| 欧美一区二区三区免费大片| 午夜精品久久久| 国产精品自在欧美一区| av成人天堂| 亚洲一区www| 国产精品美女久久福利网站| 亚洲视频一二三| 欧美一区二视频| 国产欧美日韩在线| 欧美在线免费视频| 免费日韩成人| 99国产精品久久久久老师| 欧美精品1区2区| 一区二区三区产品免费精品久久75 | 亚洲高清一二三区| 亚洲经典在线| 欧美日本乱大交xxxxx| 亚洲精品五月天| 亚洲资源av| 国产无一区二区| 另类亚洲自拍| 亚洲靠逼com| 欧美一区影院| 亚洲国产成人在线视频| 欧美精品导航| 亚洲一区国产一区| 久久影院午夜论| 亚洲三级影片| 国产精品色婷婷| 久久久国产一区二区| 亚洲国产另类精品专区| 亚洲女性喷水在线观看一区| 国产日韩欧美制服另类| 久久久国产91| 日韩亚洲欧美高清| 久久久久九九九| 日韩写真在线| 国产日韩在线看| 欧美精品九九| 香蕉精品999视频一区二区 | 一区二区三区 在线观看视频| 久久av二区| 99re6这里只有精品视频在线观看| 国产精品久久久爽爽爽麻豆色哟哟| 欧美一区二区三区成人| 亚洲黄色影院| 久久理论片午夜琪琪电影网| 亚洲激情六月丁香| 国产精品一级二级三级| 欧美99久久| 先锋a资源在线看亚洲| 91久久午夜| 狼人天天伊人久久| 亚洲欧美国产精品va在线观看| 一区在线免费| 国产日韩精品在线播放| 女女同性精品视频| 午夜亚洲性色视频| 一本久道久久综合婷婷鲸鱼| 久久亚洲综合色| 香蕉久久精品日日躁夜夜躁| 亚洲裸体俱乐部裸体舞表演av| 国产日韩成人精品| 国产精品第十页| 欧美激情国产精品| 亚洲精品视频免费观看| 久久午夜精品一区二区| 亚洲欧美在线免费观看| 99这里只有精品| 亚洲人成人77777线观看| 国内精品久久久久伊人av| 国产精品久久网站| 国产精品成人一区| 欧美日韩亚洲视频| 欧美了一区在线观看| 欧美aⅴ一区二区三区视频| 久久精品最新地址| 欧美一级专区免费大片| 亚洲欧美国产制服动漫| 一区二区三区黄色| 洋洋av久久久久久久一区| 91久久精品国产91久久| 亚洲国产天堂久久国产91| 欧美成ee人免费视频| 你懂的国产精品永久在线| 久久免费偷拍视频| 老牛国产精品一区的观看方式| 久久国产精品毛片| 久久久久久久综合色一本| 欧美中在线观看| 久久精品99国产精品| 久久久精品五月天| 久久精品国产久精国产思思| 欧美一区亚洲| 久久亚洲春色中文字幕久久久| 久久久久久久999| 久久综合久久久久88| 米奇777在线欧美播放| 男女激情久久| 亚洲人成小说网站色在线| 亚洲精品在线一区二区| 在线综合亚洲| 欧美一级淫片aaaaaaa视频| 欧美在线免费观看| 欧美777四色影视在线| 欧美日韩美女| 国产欧美日韩综合精品二区| 国语自产精品视频在线看抢先版结局| 今天的高清视频免费播放成人| 永久域名在线精品| 日韩一区二区免费高清| 亚洲欧美久久久| 久久久久久久国产| 欧美激情偷拍| 在线一区二区三区四区五区| 校园春色综合网| 欧美不卡在线| 国产精品日韩久久久久| 激情一区二区三区| 99国产精品自拍| 欧美在线网址| 亚洲精华国产欧美| 亚洲欧美一区二区三区在线| 久久在线免费视频| 国产精品久久久久天堂| 韩日成人av| 亚洲天堂成人在线观看| 久久久精品五月天| 99精品视频免费观看视频| 久久国产88| 欧美午夜电影在线| 亚洲成色最大综合在线| 亚洲欧美国产77777| 欧美二区视频| 午夜国产精品影院在线观看 | 欧美激情一区二区久久久| 国产精品网站在线观看| 亚洲精品在线观| 老牛国产精品一区的观看方式| 99视频精品在线| 蜜臀av一级做a爰片久久 | 欧美午夜在线一二页| 亚洲国产精品一区二区尤物区| 亚洲综合大片69999| 亚洲福利视频免费观看| 欧美伊人久久| 国产精品久久久久久久久免费樱桃 | 黄色小说综合网站| 亚洲一区在线看| 亚洲精品1区2区| 老司机午夜精品视频| 国产真实久久| 久久激情网站| 亚洲图片在区色| 欧美性大战xxxxx久久久| 亚洲伦理在线| 亚洲国产老妈| 欧美高清hd18日本| 亚洲黄一区二区| 欧美成人午夜| 久久天天躁狠狠躁夜夜av| 国产亚洲精品aa| 欧美一区永久视频免费观看| 中日韩男男gay无套| 欧美三级日韩三级国产三级| 亚洲精品久久久一区二区三区| 麻豆精品91| 久久人人爽人人爽| 激情久久综合| 麻豆亚洲精品| 免费人成精品欧美精品| 一区二区在线视频| 欧美1区免费| 欧美va天堂在线| 宅男噜噜噜66一区二区66| 91久久极品少妇xxxxⅹ软件| 欧美精品一区二区精品网| 亚洲美女在线看| 日韩一级黄色大片|