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

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

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产日韩欧美日韩| 久久久噜噜噜久久中文字免| 91久久久久久| 亚洲自拍偷拍福利| 在线一区观看| 国产精品一区二区三区久久久| 午夜一级久久| 久久精品人人| 亚洲精品国产拍免费91在线| 亚洲乱码国产乱码精品精天堂| 欧美午夜精品一区| 久久精品91| 麻豆精品视频在线观看视频| 日韩视频免费| 亚洲影院污污.| 一区二区在线观看视频在线观看| 亚洲承认在线| 快射av在线播放一区| 久久久久免费观看| 99视频精品在线| 亚洲一区精品在线| 在线观看91久久久久久| 99xxxx成人网| 经典三级久久| 正在播放欧美一区| 一区二区在线看| 一区二区欧美亚洲| 在线欧美电影| 亚洲一级特黄| 最新国产精品拍自在线播放| 亚洲午夜激情网站| 亚洲激情在线观看视频免费| 亚洲专区在线| 亚洲免费观看在线视频| 欧美一区高清| 亚洲午夜国产成人av电影男同| 久久久www| 亚洲欧美另类综合偷拍| 牛牛精品成人免费视频| 欧美一区二区在线免费播放| 欧美精品在线观看播放| 久久免费99精品久久久久久| 欧美私人网站| 亚洲精品国产精品国自产观看浪潮 | 欧美日产一区二区三区在线观看| 久久国产福利| 国产精品久久九九| 亚洲人www| 最新中文字幕一区二区三区| 亚洲欧美日韩在线综合| 亚洲视频在线观看网站| 欧美ed2k| 欧美福利视频网站| 国语自产在线不卡| 亚洲欧美成人一区二区在线电影 | 国产一区二区三区四区在线观看| 日韩一级二级三级| av成人免费观看| 欧美/亚洲一区| 欧美国产日韩xxxxx| 国产一区二区三区在线观看免费视频| 在线视频精品一| 这里只有精品在线播放| 欧美日本中文字幕| 91久久久久久| 在线视频你懂得一区| 欧美理论电影在线播放| 亚洲人成网站影音先锋播放| 亚洲高清视频在线观看| 开元免费观看欧美电视剧网站| 蜜臀久久99精品久久久画质超高清| 国内一区二区三区| 久久久久综合一区二区三区| 久久综合网hezyo| 影视先锋久久| 欧美风情在线观看| 亚洲精一区二区三区| 野花国产精品入口| 欧美日本韩国一区| 亚洲免费观看在线观看| 99国产欧美久久久精品| 欧美三级电影大全| 黄色成人在线| 欧美国产日韩在线观看| 久久综合久久久久88| 在线激情影院一区| 欧美一区二视频在线免费观看| 国产精品国产自产拍高清av王其 | 影音先锋日韩精品| 亚洲一区二区三区影院| 亚洲午夜精品久久久久久app| 欧美成人高清| 一区二区不卡在线视频 午夜欧美不卡在| 一区二区不卡在线视频 午夜欧美不卡在 | 一本大道久久a久久精二百| 亚洲一二三四区| 国产日韩在线亚洲字幕中文| 久久久久久久久久久一区 | 麻豆av一区二区三区| 亚洲精品社区| 国产精品久久久久久久久果冻传媒| 亚洲欧美另类久久久精品2019| 久久综合伊人77777麻豆| 亚洲三级色网| 国产欧美一区二区三区在线老狼| 久久久五月天| 一区二区欧美国产| 久久综合狠狠综合久久激情| 亚洲色图综合久久| 韩国一区二区三区在线观看| 欧美激情视频在线播放| 亚洲欧美日韩国产一区二区| 亚洲大片一区二区三区| 9l视频自拍蝌蚪9l视频成人| 国产亚洲午夜高清国产拍精品| 欧美成人午夜| 久久国内精品视频| 99热精品在线观看| 免费不卡亚洲欧美| 亚洲女性喷水在线观看一区| 18成人免费观看视频| 国产精品久久久久免费a∨大胸 | 亚洲影视中文字幕| 亚洲国产精彩中文乱码av在线播放| 午夜欧美大片免费观看| 亚洲美女免费视频| 精品粉嫩aⅴ一区二区三区四区| 欧美三级资源在线| 久久久久久久一区二区三区| 午夜精品一区二区三区在线| 亚洲三级免费| 欧美激情视频在线免费观看 欧美视频免费一 | 亚洲国产婷婷香蕉久久久久久| 国产精品视频大全| 欧美精选一区| 久久免费视频网站| 欧美亚洲日本网站| 中文精品一区二区三区| 亚洲日本中文字幕| 91久久亚洲| 欧美激情国产日韩精品一区18| 久久精品中文字幕一区二区三区 | 一色屋精品亚洲香蕉网站| 国产一区二区三区在线免费观看 | 欧美不卡在线视频| 蜜臀av性久久久久蜜臀aⅴ| 久久久久国内| 久久久久九九九| 久久久中精品2020中文| 久久精品盗摄| 久久香蕉国产线看观看av| 久久精品一区二区三区不卡牛牛| 久久福利视频导航| 性欧美大战久久久久久久久| 欧美一区二区三区免费在线看| 西瓜成人精品人成网站| 欧美在线观看一区二区| 欧美影视一区| 久久日韩精品| 欧美精品情趣视频| 欧美日韩视频免费播放| 国产精品久久久久久久久久免费 | 欧美激情一区二区三区四区| 欧美精品aa| 欧美性猛交99久久久久99按摩| 国产精品久久久一区麻豆最新章节| 国产精品视频免费观看| 国产亚洲精品一区二555| 伊人久久大香线| 久久琪琪电影院| 亚洲欧美日韩天堂| 亚洲欧美国产精品桃花| 欧美一区亚洲| 欧美www视频| 亚洲日韩成人| 亚洲免费一区二区| 久久视频一区| 欧美肉体xxxx裸体137大胆| 国产精品视频免费一区| 一区二区视频在线观看| 日韩一级黄色片| 久久高清免费观看| 欧美黄色aaaa| 亚洲免费影院| 免费不卡视频| 国产精品一区二区久久国产| 精久久久久久久久久久| 亚洲伦理自拍| 久久久久.com| 一区二区三区你懂的| 久久亚洲国产成人| 国产精品一级久久久| 91久久久久久| 老司机精品视频网站| 99精品国产福利在线观看免费| 久久精品视频在线看| 欧美日韩伦理在线| 亚洲高清一区二区三区| 亚洲综合首页| 亚洲黄页视频免费观看|