代码改变世界

OpenGL 学习笔记(1)初始化窗体

2010-10-16 10:57  Clingingboy  阅读(3019)  评论(1编辑  收藏  举报

  前言

      学习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;
}


显示效果是显示一个蓝色背景的一个窗体

image

以下对上述代码进行介绍

窗体管理(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环境,并建立一个初始窗体