藍寶書 第三章
畫多邊形
構造原則:1 多邊形所有點在同一個平面內(建議將一個多邊形拆為多個三角形)2 多邊形必須是凸的,邊的延長線和其他邊不能相交
1)繪制四邊形
glBegin(GL_QUADS);glVertex2f(……);
glVertex2f(……);
glVertex2f(……);
glVertex2f(……);
glEnd();
四個點必須在同一個平面
2)繪制連續四邊形
glBegin(GL_QUAD_STRip);
glVertex2f(……);點a
glVertex2f(……);點b
glVertex2f(……);點c
glVertex2f(……);點d
glVertex2f(……);點e
glVertex2f(……);點f
glEnd();繪制四邊形abcd和cdef,四邊形cdef四個點連接順序不定,點連接方向與四邊形abcd相同3)繪制多邊形
glBegin(GL_POLYGON);
glVertex2f(……);點a
glVertex2f(……);點b
glVertex2f(……);點c
glVertex2f(……);點d
glVertex2f(……);點e
glVertex2f(……);點f
glEnd();繪制多邊形abcdef
4)多邊形點畫模式
glEnable(GL_POLYGON_STIPPLE);啟用自定義圖形
(GLubyte *) pBitmap;
glPolygonStipple(pBitmap);設置自定義圖形,
模板每一行用十六進制表示
相關代碼見例3.10
5)三角形拼接多邊形時設置邊線是否可見
glEdgeFlag (FALSE);glVertex2f(……);……后續頂點開始的線段屬于內部線段,不可見
glEdgeFlag (TRUE);glVertex2f(……);……后續頂點開始的線段屬于外部線段,可見
參考:點擊打開鏈接
相關代碼見例3.11
例3.10多邊形點畫模式
#include <windows.h> #include <math.h> #include <GL/GL.h> #include <GL/GLU.h> #include <GL/glut.h> // Bitmap of campfireGLubyte fire[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf0,0x00, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x1f, 0xe0,0x1f, 0x80, 0x1f, 0xc0, 0x0f, 0xc0, 0x3f, 0x80,0x07, 0xe0, 0x7e, 0x00, 0x03, 0xf0, 0xff, 0x80,0x03, 0xf5, 0xff, 0xe0, 0x07, 0xfd, 0xff, 0xf8,0x1f, 0xfc, 0xff, 0xe8, 0xff, 0xe3, 0xbf, 0x70,0xde, 0x80, 0xb7, 0x00, 0x71, 0x10, 0x4a, 0x80,0x03, 0x10, 0x4e, 0x40, 0x02, 0x88, 0x8c, 0x20,0x05, 0x05, 0x04, 0x40, 0x02, 0x82, 0x14, 0x40,0x02, 0x40, 0x10, 0x80, 0x02, 0x64, 0x1a, 0x80,0x00, 0x92, 0x29, 0x00, 0x00, 0xb0, 0x48, 0x00,0x00, 0xc8, 0x90, 0x00, 0x00, 0x85, 0x10, 0x00,0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 };GLboolean bCull = true;GLboolean bDepth = true;GLboolean bOutline = true;GLfloat xRot = 30.0f;GLfloat yRot = 30.0f;// Define a constant for the value of PI #define GL_PI 3.1415f // This function does any needed initialization on the rendering void RenderScene(void){ // Clear the window glClear(GL_COLOR_BUFFER_BIT); // Turn culling on if flag is set glPushMatrix(); glBegin(GL_POLYGON); glVertex2f(-120.0f, 50.0f); glVertex2f(20.0f, 50.0f); glVertex2f(50.0f, 20.0f); glVertex2f(50.0f, -20.0f); glVertex2f(20.0f, -50.0f); glVertex2f(-20.0f, -50.0f); glVertex2f(-50.0f, -20.0f); glVertex2f(-50.0f, 20.0f); glEnd(); // Restore transformations glPopMatrix(); glutSwapBuffers();}void SetuPRC(){ // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set drawing color to red glColor3f(1.0f, 0.0f, 0.0f); // Enable polygon stippling glEnable(GL_POLYGON_STIPPLE); // Specify a specific stipple pattern glPolygonStipple(fire);}void ChangeSize(GLsizei w, GLsizei h){ GLfloat nRange = 100.0f; // Prevent a divide by zero if (h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset projection matrix stack glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange); else glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity();}int main(int argc, char* argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(800, 600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene);//顯示回調函數 glutReshapeFunc(ChangeSize);//窗口大小變形回調函數 SetupRC(); glutMainLoop(); return 0;}例3.11三角形拼接多邊形時設置邊線是否可見#include <windows.h> #include <math.h> #include <GL/GL.h> #include <GL/GLU.h> #include <GL/glut.h> // Define a constant for the value of PI #define GL_PI 3.1415f // This function does any needed initialization on the rendering void RenderScene(void){ // Clear the window glClear(GL_COLOR_BUFFER_BIT); // Turn culling on if flag is set GLboolean bEdgeFlag = FALSE; glPushMatrix(); glBegin(GL_TRIANGLES); glEdgeFlag(bEdgeFlag); glVertex2f(-20.0f, 0.0f); glEdgeFlag(TRUE); glVertex2f(20.0f, 0.0f); glVertex2f(0.0f, 40.0f); glVertex2f(-20.0f, 0.0f); glVertex2f(-60.0f, -20.0f); glEdgeFlag(bEdgeFlag); glVertex2f(-20.0f, -40.0f); glEdgeFlag(TRUE); glVertex2f(-20.0f, -40.0f); glVertex2f(0.0f, -80.0f); glEdgeFlag(bEdgeFlag); glVertex2f(20.0f, -40.0f); glEdgeFlag(TRUE); glVertex2f(20.0f, -40.0f); glVertex2f(60.0f, -20.0f); glEdgeFlag(bEdgeFlag); glVertex2f(20.0f, 0.0f); glEdgeFlag(TRUE); // Center square as two triangles glEdgeFlag(bEdgeFlag); glVertex2f(-20.0f, 0.0f); glVertex2f(-20.0f, -40.0f); glVertex2f(20.0f, 0.0f); glVertex2f(-20.0f, -40.0f); glVertex2f(20.0f, -40.0f); glVertex2f(20.0f, 0.0f); glEdgeFlag(TRUE); // Done drawing triangles glEnd(); // Restore transformations glPopMatrix(); glutSwapBuffers();}void SetupRC(){ // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set drawing color to red glColor3f(1.0f, 0.0f, 0.0f); //glPolygonMode(GL_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Enable polygon stippling //glEnable(GL_POLYGON_STIPPLE); // Specify a specific stipple pattern //glPolygonStipple(fire);}void ChangeSize(GLsizei w, GLsizei h){ GLfloat nRange = 100.0f; // Prevent a divide by zero if (h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset projection matrix stack glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange); else glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity();}int main(int argc, char* argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(800, 600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene);//顯示回調函數 glutReshapeFunc(ChangeSize);//窗口大小變形回調函數 SetupRC(); glutMainLoop(); return 0;}
新聞熱點
疑難解答