藍寶書 第三章
單緩沖與雙緩沖
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);單緩沖 相關代碼見例3.12
直接將圖像改變顯示在畫布上,使用glFlush()提交緩沖
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);雙緩沖
一般圖像改變在后臺緩沖區,使用glutSwapBuffers()將后臺緩沖提交至前臺緩沖區
void glDrawBuffer(Glenummode) 該函數可設置當前改變前臺緩沖區還是后臺緩沖區
glDrawBuffer(GL_FRONT) 前臺
glDrawBuffer(GL_BACK) 后臺
顏色緩沖區與深度緩沖區
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
上述語句初始化了顏色緩沖區、雙緩沖區以及深度緩沖區
若未啟用深度緩沖區(glDisable(GL_DEPTH_TEST)),則深度數據也會寫入顏色緩沖區,使用glDepthMask(GL_FALSE)禁止寫入,第六章詳細講解
剪刀盒(類似于opencv的感興趣區域)
只對剪刀盒范圍內進行處理
glEnable(GL_SCISSOR_TEST);啟用剪刀盒
void glScissor(GLintx, GLint y, GLsizeiwidth, GLsizei height); 設置剪刀盒范圍
相關代碼見例3.13
模板緩沖區
類似于噴油漆,在紙上鏤空一個模板,再噴上油漆,下面的紙上只有鏤空部分才有顏色。
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL);初始化模板緩沖區glEnable(GL_STENCIL_TEST);啟用模板緩沖區
void glStencilFunc(GLenumfunc, GLint ref, GLuint mask);設置模板參考:
點擊打開鏈接
點擊打開鏈接
相關代碼見例3.14
例3.12 單緩沖區示例(單擊窗口進行新的緩沖,增大了圖形半徑)
#include <windows.h> #include <math.h> #include <GL/GL.h> #include <GL/GLU.h> #include <GL/glut.h> #define GL_PI 3.1415f // This function does any needed initialization on the rendering void RenderScene(void){ static GLdouble dRadius = 15; static GLdouble dAngle = 0.0; // Clear blue window glClearColor(0.0f, 0.0f, 1.0f, 0.0f); if (dAngle == 0.0) glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); glVertex2d(dRadius * cos(dAngle), dRadius * sin(dAngle)); glEnd(); dRadius *= 1.01; dAngle += 0.2; if (dAngle > 30.0) { dRadius = 15; dAngle = 0.0; } glFlush();}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_SINGLE | GLUT_RGBA); glutInitWindowSize(800, 600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene);//顯示回調函數 glutReshapeFunc(ChangeSize);//窗口大小變形回調函數 SetupRC(); glutMainLoop(); return 0;}例3.13 剪刀盒示例#include <windows.h> #include <math.h> #include <GL/GL.h> #include <GL/GLU.h> #include <GL/glut.h> #define GL_PI 3.1415f // This function does any needed initialization on the rendering void RenderScene(void){ glClearColor(0.0f, 0.0f, 1.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); // Now set scissor to smaller red sub region glClearColor(1.0f, 0.0f, 0.0f, 0.0f); glScissor(100, 100, 600, 400); glEnable(GL_SCISSOR_TEST); glClear(GL_COLOR_BUFFER_BIT); // Finally, an even smaller green rectangle glClearColor(0.0f, 1.0f, 0.0f, 0.0f); glScissor(200, 200, 400, 200); glClear(GL_COLOR_BUFFER_BIT); // Turn scissor back off for next render glDisable(GL_SCISSOR_TEST); 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;}例3.14 模板示例#include <windows.h> #include <math.h> #include <GL/GL.h> #include <GL/GLU.h> #include <GL/glut.h> #define GL_PI 3.1415f // This function does any needed initialization on the rendering void RenderScene(void){ GLdouble dRadius = 0.1; // Initial radius of spiral GLdouble dAngle; // Looping variable // Clear blue window glClearColor(0.0f, 0.0f, 1.0f, 0.0f); // Use 0 for clear stencil, enable stencil test glClearStencil(0.0f); glEnable(GL_STENCIL_TEST); // Clear color and stencil buffer glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // All drawing commands fail the stencil test, and are not // drawn, but increment the value in the stencil buffer. glStencilFunc(GL_NEVER, 0x0, 0x0); glStencilOp(GL_INCR, GL_INCR, GL_INCR); // Spiral pattern will create stencil pattern // Draw the spiral pattern with white lines. We // make the lines white to demonstrate that the // stencil function prevents them from being drawn glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_LINE_STRIP); for (dAngle = 0; dAngle < 400.0; dAngle += 0.1) { glVertex2d(dRadius * cos(dAngle), dRadius * sin(dAngle)); dRadius *= 1.002; } glEnd(); // Now, allow drawing, except where the stencil pattern is 0x1 // and do not make any further changes to the stencil buffer glStencilFunc(GL_NOTEQUAL, 0x1, 0x1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // Now draw red bouncing square // (x and y) are modified by a timer function glColor3f(1.0f, 0.0f, 0.0f); glRectf(0, 0, 100, 100); // All done, do the buffer swap 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;}
新聞熱點
疑難解答