OpenGL 多邊形偏移

2021-07-01 18:45:40 字數 3266 閱讀 4536

#include "gltools.h"

#include "glshadermanager.h"

#include

#else

#define freeglut_static

#include

#endif

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, 1.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_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學習十三 多邊形偏移

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

OpenGL 多邊形偏移 Frame顯示方式

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

OpenGL 多邊形的鏤空

1.凸多邊形的定義是多邊形的內角均小於180 2.多邊形可以設定鏤空效果,使用glenable gl polygon stipple 開啟鏤空模式,之後使用glpolygonstipple 設定鏤空基本圖案。void glpolygonstipple const glubyte mask mask是...