OpenGL學習十三 多邊形偏移

2021-06-20 06:49:44 字數 3572 閱讀 4194

如果想著重顯示實心物體的邊緣,可以先用gl_fill模式繪製這個物體,然後再gl_line下再次用另外一種不同的顏色再次繪製(如右圖),但是由於光柵化的方式不完全相同,因此直線和多邊形經過計算後的z值也可能不同 ,也可能向後,這就導致了線和實心忽濃忽暗的效果(右圖2)

glpolygonmode(gl_front_and_back, gl_line);

glutsolidsphere(1.0, 20, 12);

glpolygonmode(gl_front_and_back, gl_fill);

glutsolidsphere(1.0, 20, 12);

為了解決這個問題可以是重合的z值適當的偏移下

做法:1.啟用偏移模式

glenable(gl_polygon_offset_line|gl_polygon_offset_point|gl_polygon_offset_fill);

分別對應gl_fill,gl_point,gl_line

2.設定偏移量

glpolygonoffset(polyfactor, polyunits);

偏移量演算法很複雜建議都設定為1.0

#include "header.h"

gluint list;

glint spinx = 0;

glint spiny = 0;

glfloat tdist = 0.0;

glfloat polyfactor = 1.0;

glfloat polyunits = 1.0;

void display (void)

; glfloat black = ;

glclear (gl_color_buffer_bit | gl_depth_buffer_bit);

glpushmatrix ();

gltranslatef (0.0, 0.0, tdist);

glrotatef ((glfloat) spinx, 1.0, 0.0, 0.0);

glrotatef ((glfloat) spiny, 0.0, 1.0, 0.0);

glmaterialfv(gl_front, gl_ambient_and_diffuse, gray);

glmaterialfv(gl_front, gl_specular, black);

glmaterialf(gl_front, gl_shininess, 0.0);

glenable(gl_lighting);

glenable(gl_light0);

glenable(gl_polygon_offset_fill);

glpolygonoffset(polyfactor, polyunits);

glcalllist (list);

gldisable(gl_polygon_offset_fill);

gldisable(gl_lighting);

gldisable(gl_light0);

glcolor3f (1.0, 1.0, 0.0);

glpolygonmode(gl_front_and_back, gl_line);

glcalllist (list);

glpolygonmode(gl_front_and_back, gl_fill);

glpopmatrix ();

glflush ();

}void gfxinit (void)

; glfloat light_diffuse = ;

glfloat light_specular = ;

glfloat light_position = ;

glfloat global_ambient = ;

glclearcolor (0.0, 0.0, 0.0, 1.0);

list = glgenlists(1);

glnewlist (list, gl_compile);

glutsolidsphere(1.0, 20, 12);

glendlist ();

glenable(gl_depth_test);

gllightfv (gl_light0, gl_ambient, light_ambient);

gllightfv (gl_light0, gl_diffuse, light_diffuse);

gllightfv (gl_light0, gl_specular, light_specular);

gllightfv (gl_light0, gl_position, light_position);

gllightmodelfv (gl_light_model_ambient, global_ambient);

}void reshape(int width, int height)

void mouse(int button, int state, int x, int y)

break;

case glut_middle_button:

switch (state)

break;

case glut_right_button:

switch (state)

break;

default:

break;

}}void keyboard (unsigned char key, int x, int y)

break;

case 't':

if (tdist > -5.0)

break;

case 'f':

polyfactor = polyfactor + 0.1;

printf ("polyfactor is %f\n", polyfactor);

glutpostredisplay();

break;

case 'f':

polyfactor = polyfactor - 0.1;

printf ("polyfactor is %f\n", polyfactor);

glutpostredisplay();

break;

case 'u':

polyunits = polyunits + 1.0;

printf ("polyunits is %f\n", polyunits);

glutpostredisplay();

break;

case 'u':

polyunits = polyunits - 1.0;

printf ("polyunits is %f\n", polyunits);

glutpostredisplay();

break;

default:

break;

}}int main(int argc, char** argv)

OpenGL 多邊形偏移

include gltools.h include glshadermanager.h include else define freeglut static include endif gluint list glint spinx 0 glint spiny 0 glfloat tdist 0....

OpenGL 多邊形偏移 Frame顯示方式

如果想著重顯示實心物體的邊緣,可以先用gl fill模式繪製這個物體,然後再gl line下再次用另外一種不同的顏色再次繪製 如右圖 但是由於光柵化的方式不完全相同,因此直線和多邊形經過計算後的z值也可能不同 也可能向後,這就導致了線和實心忽濃忽暗的效果 右圖2 glpolygonmode gl f...

OpenGL學習筆記四(多邊形)

在qt中的opengl模組中,使用opengl是比較簡單的,只需要在你建立的專案中新增 qopenglwidget 和qopenglfunctions就可以使用opengl基本上所有的功能。include include 在qt中使用opengl繪製點線也非常簡單。在開始繪製點線之前有幾個函式是必須...