? 前言
????? 學習OpenGL只是興趣愛好,因為對圖形比較感興趣.將以OpenGl的紅寶書(7)和藍寶石書(4)為基礎,雖然手頭有紅寶書書,但感覺沒藍寶石書寫的好
準備工作
首先要下載一個工具庫(GLUT)
http://www.opengl.org/resources/libraries/glut/
只要把相應文件放在system32和lib目錄下就可以了
第一個OpenGL程序
#include <GL/glut.h> #include <stdlib.h>void display(void) {/* clear all pixels */glClear (GL_COLOR_BUFFER_BIT);glFlush (); }int main(int argc, char** argv) {glutInit(&argc, argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100);glutCreateWindow ("hello");glClearColor (0.0, 0.0, 1.0, 0.0);glutDisplayFunc(display); glutMainLoop();return 0; }
顯示效果是顯示一個藍色背景的一個窗體
以下對上述代碼進行介紹
窗體管理(Window Management)
Several routines perform tasks necessary for initializing a window:
? glutInit(int *argc, char **argv) initializes GLUT and processes any command
line arguments (for X, this would be options such as -display and
-geometry). glutInit() should be called before any other GLUT routine.
? glutInitDisplayMode(unsigned int mode) specifies whether to use an
RGBA or color-index color model. You can also specify whether you
want a single- or double-buffered window. (If you’re working in colorindex
mode, you’ll want to load certain colors into the color map; use
glutSetColor() to do this.) Finally, you can use this routine to indicate
that you want the window to have an associated depth, stencil,
multisampling, and/or accumulation buffer. For example, if you want
a window with double buffering, the RGBA color model, and a depth
buffer, you might call glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGBA | GLUT_DEPTH).
? glutInitWindowPosition(int x, int y) specifies the screen location for
the upper-left corner of your window.
? glutInitWindowSize(int width, int height) specifies the size, in pixels,
of your window.
? glutInitContextVersion(int majorVersion, int minorVersion) specifies
which version of OpenGL you want to use. (This is a new addition
available only when using Freeglut, and was introduced with OpenGL
Version 3.0. See “OpenGL Contexts” on page 27 for more details on
OpenGL contexts and versions.)
? glutInitContextFlags(int flags) specifes the type of OpenGL context
you want to use. For normal OpenGL operation, you can omit this call
from your program. However, if you want to use a forward-compatible
OpenGL context, you will need to call this routine. (This is also a new
addition available only in Freeglut, and was introduced with OpenGL
Version 3.0. See “OpenGL Contexts” on page 27 for more details on
the types of OpenGL contexts.)
? int glutCreateWindow(char *string) creates a window with an OpenGL
context. It returns a unique identifier for the new window. Be warned:
until glutMainLoop() is called, the window is not yet displayed.
我們知道低級win32 api初始化一個窗體是比較復雜的,glut庫簡化了這個過程
回調函數
即win32的消息循環系統,
glutDisplayFunc調用一個函數,該函數會不斷的被調用
glutMainLoop用于啟動程序,并使程序不斷在運行不退出,即進入消息循環
glClearColor 用于清除緩沖區顏色,并重新設置新的顏色
guFlush 刷新OpenGl命令隊列,如果不調用此方法,那么重新繪制的圖形將無法顯示
?
本節目的在于建立OpenGL環境,并建立一個初始窗體