如果想著重顯示實心物體的邊緣,可以先用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 #include 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學習十三 多邊形偏移
如果想著重顯示實心物體的邊緣,可以先用gl fill模式繪製這個物體,然後再gl line下再次用另外一種不同的顏色再次繪製 如右圖 但是由於光柵化的方式不完全相同,因此直線和多邊形經過計算後的z值也可能不同 也可能向後,這就導致了線和實心忽濃忽暗的效果 右圖2 glpolygonmode gl f...
OpenGL 多邊形的鏤空
1.凸多邊形的定義是多邊形的內角均小於180 2.多邊形可以設定鏤空效果,使用glenable gl polygon stipple 開啟鏤空模式,之後使用glpolygonstipple 設定鏤空基本圖案。void glpolygonstipple const glubyte mask mask是...