开源项目之C++界面库 GLUI

GLUI是一个C++界面库,它提供了buttons, checkboxes, radio buttons, 等常用控件,以及OPENGL支持。GLUI界面系统依赖于GLUT来处理窗口、和鼠标管理等,而绘制部分采用OPENGL绘制。工程如图:

这个库需要用到第三方GLUT工具库(已放到源码包中),给出了六个实例,开源库的原理就引用网上的资料了,接着介绍下实例是如何应用的。

窗体初始化
GLUI包含三个主要的类:
          GLUI_Master_Object
          GLUI
          GLUI_Control
其中有且只有一个全局的GLUI_Master_Object对象GLUI_Master.所有的GLUI窗口的创建都必须通过这个对象.这可以让GLUI通过一个全局对象来追踪所有的窗口,方便窗口的管理.这里简单介绍一下如何使用GLUI创建和控制窗口.这里介绍的函数都属于GLUI_Master_Object和GLUI类.必须注意,任何GLUI_Master_Object类的成员函数都必须通过全局对象GLUI_Master来调用,而任何GLUI类的成员函数都必须通过GLUI指针来调用,并在使用GLUI指针之前,GLUI指针必须获取GLUI_Master.create_glui()的返回值.

如:

         float version = GLUI_Master.get_version();
         GLUI *glui_window = GLUI_Master.create_glui("GLUI");
         glui_window->add_StaticText("Hello World!");


初始化:首先介绍使用GLUI创建和设置窗口的一下函数.get_version:返回当前GLUI的版本.
用法:float GLUI_Master_Object::get_version(void) 返回值:GLUI版本号

create_glui:创建一个新的窗口. 用法:

GLUI *GLUI_Master_Object::create_glui(char *name, int flags = 0, int x = -1, int y = -1)

参数:
name:GLUI窗口的名字.
flags:初始化标记,如果没有给出此参数则默认值为0,被定义为在当前版本中.
x,y:初始化窗口的坐标.此参数可以不给出,因为GLUI可以自动调整窗口大小以适应所有的控件.
返回值:
新的GLUI窗口的指针

create_glui_subwindow:
在已经存在的GLUT窗口中创建一个新的子窗口
用法:GLUI *GLUI_Master_Object::create_glui_subwindow(int window, int position)
参数:
window:新建GLUI窗口的父窗口(一个已经存在的GLUT窗口)的ID号.
position:子窗口相对于父窗口的位置,可以为以下的值:
           GLUI_SUBWINDOW_RIGHT
           GLUI_SUBWINDOW_LEFT
           GLUI_SUBWINDOW_TOP
           GLUI_SUBWINDOW_BOTTOM
           (注:可以在同一个位置创建任意个数的子窗口,多个相同位置的子窗口会简单的相互叠加,如:两个子窗口都使用了GLUI_SUBWINDOW_TOP参数,这两个子窗口都会定位在父窗口之上,同时,第一个子窗口也会覆盖在第二个子窗口之上.)
返回值:
新建的子窗口的指针.

set_glutIdleFunc:
为GLUI注册一个标准的GLUT空闲回调函数.当GLUI处于空闲时,就会调用该注册的函数.GLUI会截获空闲事件用于自身过程处理,然后才把该事件送给GLUT应用程序.需要注意的是,在注册的空闲回调函数中,当前窗口并没有被定义,所以,如果要在空闲回调函数中向GLUT窗口获取或发送redisplay事件,则必须明确的在回调函数中指定当前窗口.
        如:
                 int main_window;
                 void myGlutIdle(void)//被注册的空闲回调函数
                 {
                    if(glutGetWindow() != main_window)
                    {
                       glutSetWindow(main_window);
                    }
                    glutPostRedisplay();
                 }
用法:
void GLUI_Master_Object::set_glutIdleFunc(void (*f)(void))
参数:
f(void):被注册的空闲回调函数.

set_glutReshapeFunc
set_glutKeyboardFunc
set_glutMouseFunc
set_glutSpecialFunc
用法:
void GLUT_Master_Object::set_glutReshapeFunc(void (*f)(int width, int height));
void GLUT_Master_Object::set_glutKeyboardFunc(void (*f)(unsigned char key, int x, int y));
void GLUT_Master_Object::set_glutMouseFunc(void (*f)(int button, int state, int x,int y));
void GLUI_Master_Object::set_glutSpecialFunc(void (*f)(int key, int x, int y));
参数:
详见glut函数详解(9)--回调API
(这里的回调函数与GLUT中对应的回调函数用法相似)

set_main_gfx_window:
将一个GLUT窗口与一个GLUI窗口捆绑,当这个GLUI窗口中的一个控件的值发生改变,则该GLUT窗口将会被重绘.
用法:
void GLUI::set_main_gfx_window(int window_id);
参数:
window_id:被绑定的GLUT窗口ID,此ID号可在GLUT窗口被创建时获得(即glutCreateWindow()的返回值),或通过glutGetWindow()的返回值获得.


窗体视口管理
视口管理:这里介绍使用GLUI结合OpenGL时,如何管理视口.get_viewport_area:
确定当前窗口可绘区域的位置和尺寸.这个函数一般在使用到GLUI子窗口时使用,因为子窗口必然会占据父窗口的一小块区域,而绘制在父窗口上的图形并不希望被子窗口覆盖,所以可以通过此函数调整视口的大小.此函数应该在GLUT的reshape callback function中调用.
用法:void GLUI_Master_Object::get_viewport_area(int *x, int *y, int *w, int *h);
参数:
x,y,w,h:该函数被调用后,就可获得当前窗口可绘区域的左上角坐标及其宽度和高度,然后可以根据这些数据通过调用glViewport()设置视口区域.
auto_set_viewport:
自动为当前窗口设置尺寸合适的视口
用法:
void GLUI_Master_Object::auto_set_viewport(void);
例子:

int x, y, w, h;
GLUI_Master.get_viewport_area(&x, &y, &w, &h);
glViewport(x, y, w, h);
以上三句的功能与下面一句等价.
GLUI_Master.auto_set_viewport();

窗体管理
窗口管理:当窗体创建之后可以通过下列函数对窗体进行管理
/********************************************************************/

get_glut_window_id:
返回一个GLUI窗口的窗口ID
用法:int GLUI::get_glut_window_id(void);
返回值:GLUI窗口的ID号

enable,disable:
使GLUI窗口可用或不可用,当一个GLUI窗口不可用时,其上的所有控件都不可用.
用法:
void GLUI::enable(void);
void GLUI::disable(void);

hide:
使GLUI窗口或子窗口隐藏.一个被隐藏的窗口或子窗口不能接受任何用户输入.
用法:void GLUI::hide(void);

show:
使一个被隐藏的窗口或子窗口可见.
用法:void GLUI::show(void);

close:
销毁一个GLUI窗口或子窗口.
用法:
void GLUI::close(void);

close_all:
销毁所有的GLUI窗口和子窗口.此函数应该被全局对象所调用如:GLUI_Master.close_all();
用法:void GLUI_Master_Object::close_all(void);

sync_live:
变量可以与控件相关联,该函数可以使一个GLUI窗口上的所有控件和与这些控件相关联的变量保持同步,也即:读取变量的值,然后根据该值设置与其相关联的控件,使该值在控件上反映出来
用法:void GLUI::sync_live(void);

sync_live_all:
使所有GLUI窗口上的所有控件与与其相关联的变量保持同步,这个函数必须通过全局对象调用,如:GLUI_Master.sync_live_all();
用法:void GLUI_Master_Object::sync_live_all(void);

控件
    GLUI中,所有的控件都是源于GLUI_Control类,所以,他们的操作都非常相似.我们有两种方法创建控件:一种是使用add_control()直接将控件放在窗口之上,另一种是使用add_control_to_panel()将控件置于panel之内. panel是一个可以内置其他控件的容器,panel也可以置于另一个panel之内.这里介绍的函数可以被许多控件调用,用来改变其属性,因此这里介绍的函数可以称为公共函数.
set_name:
为button,checkbox等控件设置名字.
用法:
void GLUI_Control::set_name(char *name);
参数:
name:控件的名字(即:在控件上或控件旁显示的文字)
set_w, set_h:
设置控件的最小宽度或高度
用法:
void GLUI_Control::set_w(int new_size);
void GLUI_Control::set_h(int new_size);
参数:
new_size:控件的最小宽度或高度.
get, set:
获取或设置控件的当前值.
用法:
int GLUI_Control::get_int_val(void);
float GLUI_Control::get_float_val(void);
void GLUI_Control::get_float_array_val(float *float_array_ptr);
char *GLUI_Control::get_text(void);
void GLUI_Control::set_int_val(int int_val);
void GLUI_Control::set_float_val(float float_val);
void GLUI_Control::set_float_array_val(float *float_array_val);
void GLUI_Control::set_text(char *text);
(根据控件对输入输出数据值类型的要求,选取相应的函数)
disable, enable:
使控件可用或不可用,radio group不可用时,其中的button也不可用,panel不可用时,其中的所有控件都不可用.
用法:
void GLUI_Control::enable(void);
void GLUI_Control::disable(void);
set_alignment:
设置控件的对齐方式(居左,居中,居右)
用法:
void GLUI_Control::set_alignment(int align);
参数:
align:对齐方式.可选下面之一:
       GLUI_ALIGN_CENTER
       GLUI_ALIGN_RIGHT
       GLUI_ALIGN_LEFT

Panels:一个容器,可以内置其他控件,也可以内置另一个panel.
add_panel:
在GLUI窗口上新建一个panel控件.
用法:GLUI_Panel *GLUI::add_panel(char *name, int tyep = GLUI_PANEL_EMBOSSED);

add_panel_to_panel:
在另一个panel之内新建一个panel控件.
用法:GLUI_Panel *GLUI::add_panel_to_panel(GLUI_Panel *panel, char *name, int type = GLUI_PANEL_EMBOSSED);

参数:

name:panel控件的名字(可以为空,如若指定了名字,会在panel的左上角显示).

type:panel的样式.
       GLUI_PANEL_EMBOSSED:用内嵌的线条画一个矩形框(默认值).
       GLUI_PANEL_RAISED:用外凸的线条画一个矩形框,不显示名字.
       GLUI_PANEL_NONE:不绘制矩形框,只用来将控件组织成一个控件组.
panel:指向另一个panel控件的指针.新建的panel控件将会置于该panel之中.

返回值:新建的panel控件的指针.

Rollouts:类似于panel也是一个容器,功能上可以与panel互相替代,不同之处在于该控件可以被折叠起来,此时其内置的控件不可见,只有当其展开后,内置控件才可见.

add_rollout:
在GLUI窗口中新建rollout控件.
用法:GLUI_Rollout *GLUI::add_rollout(char *name, int open = true);

add_rollout_to_panel:
在另一个已经存在的rollout或panel中新建一个rollout控件.
用法:GLUI_Rollout *GLUI::add_rollout_to_panel(GLUI_Panel *panel, char *name, int open = true);

参数:
name:控件的名字.
open:如果为true,则rollout初始设置为打开;如果为false,则初始设置为闭合.
panel:指向另一个panel或rollout控件的指针.新建的rollout控件将会置于该panel或rollout之中.

返回值:
新建rollout控件的指针.

Columns:控件在GLUI窗口中的布局是按照控件定义的顺序自上而下放置的,在竖直方向上形成一个控件列,而column则会开辟一个新的控件列(即在旧的控件列的右侧新建一个新的控件列),其后所定义的控件将置于该新建的控件列中(即在新的控件列中自上而下布局),直至新的控件列被创建.

add_column:
在GLUI窗口上新建column.
用法:void GLUI::add_column(int draw_bar = true);

add_column_to_panel:
在panel中新建column.
用法:void GLUI::add_column_to_panel(GLUI_Panel *panel, int draw_bar = true);

参数:
draw_bar:如果为true,则在新建控件列时,会绘制一条竖线将其与原先的控件列区分开;如为false,则只创建控件列,不绘制竖线.
panel:指向一个panel控件的指针.新建的column控件将会置于该panel之中.


Buttons:按钮

add_button:
在GLUI窗口上直接新建按钮.
用法:GLUI_Button *GLUI::add_button(char *name, int id = -1, GLUI_Update_CB callback = NULL);

add_button_to_panel:在一个已经存在的panel中创建按钮.
用法:GLUI_Button *GLUI::add_button_to_panel(GLUI_Panel *panel,char *name,int id = -1,GLUI_Update_CB callback = NULL);

参数:
name:按钮的名字,即在按钮上显示的文字.
id:按钮的ID值.如果callback被定义了,则当callback被调用时,id值会作为参数传递给callback.
callback:接受一个整形参数的callback函数.当按钮被触发时,它会被调用.
panel:指向一个panel控件的指针.新建的button控件将会置于该panel之中.

返回值:
新建的按钮控件的指针.

Checkboxes:复选框

add_checkbox:
在GLUI窗口上直接创建新的checkbox.
用法:GLUI_Checkbox *GLUI::add_checkbox(char *name, int *live_var = NULL, int id = -1,GLUI_Update_CB callback = NULL);

add_checkbox_to_panel:
在已经存在的panel中创建新的checkbox.
用法:GLUI_Checkbox *GLUI::add_check_to_panel(GLUI_Panel *panel, char *name, int *live_var = NULL, int id = -1, GLUI_Update_CB callback = NULL);

参数:
name:checkbox的名字.
live_var:与控件checkbox相关联的整形指针,当checkbox控件状态发生变化时,该整形值会自动更新.
id:复选框的ID值.如果callback被定义了,则当callback被调用时,id值会作为参数传递给callback.
callback:接受一个整形参数的callback函数.当按复选框触发时,它会被调用.
panel:指向一个panel控件的指针.新建的checkbox控件将会置于该panel之中.

返回值:新建的checkbox控件的指针.


example1 效果如图:


主要的源码:

[cpp]  view plain copy
  1. int main(int argc, char* argv[])  
  2. {  
  3.   /****************************************/  
  4.   /*   Initialize GLUT and create window  */  
  5.   /****************************************/  
  6.   //初始化操作  
  7.   glutInit(&argc, argv);  
  8.   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );  
  9.   glutInitWindowPosition( 50, 50 );  
  10.   glutInitWindowSize( 300, 300 );  
  11.    
  12.   main_window = glutCreateWindow( "GLUI Example 1" );  
  13.   glutDisplayFunc( myGlutDisplay );  
  14.   glutReshapeFunc( myGlutReshape );    
  15.   
  16.   /****************************************/  
  17.   /*       Set up OpenGL lights           */  
  18.   /****************************************/  
  19.   
  20.   GLfloat light0_ambient[] =  {0.1f, 0.1f, 0.3f, 1.0f};  
  21.   GLfloat light0_diffuse[] =  {.6f, .6f, 1.0f, 1.0f};  
  22.   GLfloat light0_position[] = {1.0f, 1.0f, 1.0f, 0.0f};  
  23.   
  24.   glEnable(GL_LIGHTING);  
  25.   glEnable(GL_LIGHT0);  
  26.   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);  
  27.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);  
  28.   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);  
  29.   
  30.   /****************************************/  
  31.   /*          Enable z-buferring          */  
  32.   /****************************************/  
  33.   
  34.   glEnable(GL_DEPTH_TEST);  
  35.   
  36.   
  37.   /****************************************/  
  38.   /*         Here's the GLUI code         */  
  39.   /****************************************/  
  40.     
  41.   GLUI *glui = GLUI_Master.create_glui( "GLUI" );  
  42.   new GLUI_Checkbox( glui, "Wireframe", &wireframe );  
  43.   (new GLUI_Spinner( glui, "Segments:", &segments ))  
  44.     ->set_int_limits( 3, 60 );   
  45.      
  46.   glui->set_main_gfx_window( main_window );  
  47.   
  48.   /* We register the idle callback with GLUI, *not* with GLUT */  
  49.   GLUI_Master.set_glutIdleFunc( myGlutIdle );   
  50.   
  51.   glutMainLoop();  
  52.   
  53.   return EXIT_SUCCESS;  
  54. }  

example2 效果如图:


主要代码:

[cpp]  view plain copy
  1. void myGlutKeyboard(unsigned char Key, int x, int y)  
  2. {  
  3.   switch(Key)  
  4.   {  
  5.     // A few keys here to test the sync_live capability.  
  6.   case 'o':  
  7.     // Cycle through object types  
  8.     ++obj %= 3;  
  9.     GLUI_Master.sync_live_all();  
  10.     break;  
  11.   case 'w':  
  12.     // Toggle wireframe mode  
  13.     wireframe = !wireframe;  
  14.     GLUI_Master.sync_live_all();  
  15.     break;  
  16.   case 27:   
  17.   case 'q':  
  18.     exit(0);  
  19.     break;  
  20.   };  
  21.   glutPostRedisplay();  
  22. }  
  23.   
  24.   
  25. /***************************************** myGlutMenu() ***********/  
  26.   
  27. void myGlutMenu( int value )  
  28. {  
  29.   myGlutKeyboard( value, 0, 0 );  
  30. }  
  31.   
  32. /***************************************** myGlutMouse() **********/  
  33.   
  34. void myGlutMouse(int button, int button_state, int x, int y )  
  35. {  
  36.   if ( button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN ) {  
  37.     last_x = x;  
  38.     last_y = y;  
  39.   }  
  40. }  
  41.   
  42.   
  43. /***************************************** myGlutMotion() **********/  
  44.   
  45. void myGlutMotion(int x, int y )  
  46. {  
  47.   rotationX += (float) (y - last_y);  
  48.   rotationY += (float) (x - last_x);  
  49.   
  50.   last_x = x;  
  51.   last_y = y;  
  52.   
  53.   glutPostRedisplay();   
  54. }  
  55.   
  56. /**************************************** myGlutReshape() *************/  
  57.   
  58. void myGlutReshape( int x, int y )  
  59. {  
  60.   xy_aspect = (float)x / (float)y;  
  61.   glViewport( 0, 0, x, y );  
  62.   
  63.   glutPostRedisplay();  
  64. }  
  65.   
  66. /***************************************** myGlutDisplay() *****************/  
  67.   
  68. void myGlutDisplay( void )  
  69. {  
  70.   glClearColor( .9f, .9f, .9f, 1.0f );  
  71.   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  72.   
  73.   glMatrixMode( GL_PROJECTION );  
  74.   glLoadIdentity();  
  75.   glFrustum( -xy_aspect*.08, xy_aspect*.08, -.08, .08, .1, 15.0 );  
  76.   
  77.   glMatrixMode( GL_MODELVIEW );  
  78.   glLoadIdentity();  
  79.   glTranslatef( 0.0f, 0.0f, -1.6f );  
  80.   glRotatef( rotationY, 0.0, 1.0, 0.0 );  
  81.   glRotatef( rotationX, 1.0, 0.0, 0.0 );  
  82.   
  83.   /*** Now we render object, using the variables 'obj', 'segments', and 
  84.     'wireframe'.  These are _live_ variables, which are transparently  
  85.     updated by GLUI ***/  
  86.     
  87.   if ( obj == 0 ) {  
  88.     if ( wireframe )        
  89.       glutWireSphere( .6, segments, segments );  
  90.     else                    
  91.       glutSolidSphere( .6, segments, segments );  
  92.   }  
  93.   else if ( obj == 1 ) {  
  94.     if ( wireframe )  
  95.       glutWireTorus( .2,.5,16,segments );  
  96.     else  
  97.       glutSolidTorus( .2,.5,16,segments );  
  98.   }  
  99.   else if ( obj == 2 ) {  
  100.     if ( wireframe )  
  101.       glutWireTeapot( .5 );  
  102.     else  
  103.       glutSolidTeapot( .5 );  
  104.   }  
  105.   
  106.   glDisable( GL_LIGHTING );  /* Disable lighting while we render text */  
  107.   glMatrixMode( GL_PROJECTION );  
  108.   glLoadIdentity();  
  109.   gluOrtho2D( 0.0, 100.0, 0.0, 100.0  );  
  110.   glMatrixMode( GL_MODELVIEW );  
  111.   glLoadIdentity();  
  112.   glColor3ub( 0, 0, 0 );  
  113.   glRasterPos2i( 10, 10 );  
  114.   
  115.   //  printf( "text: %s\n", text );  
  116.   
  117.   /*** Render the live character array 'text' ***/  
  118.   for (unsigned int i=0; i<text.length(); ++i)  
  119.     glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, text[i] );  
  120.   
  121.   glEnable( GL_LIGHTING );  
  122.   
  123.   glutSwapBuffers();   
  124. }  
  125.   
  126.   
  127. /**************************************** main() ********************/  
  128.   
  129. int main(int argc, char* argv[])  
  130. {  
  131.   /****************************************/  
  132.   /*   Initialize GLUT and create window  */  
  133.   /****************************************/  
  134.   
  135.   glutInit(&argc, argv);  
  136.   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );  
  137.   glutInitWindowPosition( 50, 50 );  
  138.   glutInitWindowSize( 300, 300 );  
  139.    
  140.   main_window = glutCreateWindow( "GLUI Example 2" );  
  141.   glutDisplayFunc( myGlutDisplay );  
  142.   glutReshapeFunc( myGlutReshape );    
  143.   glutKeyboardFunc( myGlutKeyboard );  
  144.   glutMotionFunc( myGlutMotion );  
  145.   glutMouseFunc( myGlutMouse );  
  146.   
  147.   /****************************************/  
  148.   /*       Set up OpenGL lights           */  
  149.   /****************************************/  
  150.   
  151.   GLfloat light0_ambient[] =  {0.1f, 0.1f, 0.3f, 1.0f};  
  152.   GLfloat light0_diffuse[] =  {.6f, .6f, 1.0f, 1.0f};  
  153.   GLfloat light0_position[] = {1.0f, 1.0f, 1.0f, 0.0f};  
  154.   
  155.   glEnable(GL_LIGHTING);  
  156.   glEnable(GL_LIGHT0);  
  157.   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);  
  158.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);  
  159.   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);  
  160.   
  161.   /****************************************/  
  162.   /*          Enable z-buferring          */  
  163.   /****************************************/  
  164.   
  165.   glEnable(GL_DEPTH_TEST);  
  166.   
  167.   /****************************************/  
  168.   /*         Here's the GLUI code         */  
  169.   /****************************************/  
  170.   
  171.   GLUI *glui = GLUI_Master.create_glui( "GLUI", 0, 400, 50 ); /* name, flags, 
  172.                                  x, and y */  
  173.   new GLUI_StaticText( glui, "GLUI Example 2" );  
  174.   new GLUI_Separator( glui );  
  175.   checkbox = new GLUI_Checkbox( glui, "Wireframe", &wireframe, 1, control_cb );  
  176.   spinner  = new GLUI_Spinner( glui, "Segments:", &segments, 2, control_cb );  
  177.   spinner->set_int_limits( 3, 60 );  
  178.   edittext = new GLUI_EditText( glui, "Text:", text, 3, control_cb );  
  179.   GLUI_Panel *obj_panel = new GLUI_Panel( glui, "Object Type" );  
  180.   radio = new GLUI_RadioGroup( obj_panel,&obj,4,control_cb );  
  181.   new GLUI_RadioButton( radio, "Sphere" );  
  182.   new GLUI_RadioButton( radio, "Torus" );  
  183.   new GLUI_RadioButton( radio, "Teapot" );  
  184.   new GLUI_Button( glui, "Quit", 0,(GLUI_Update_CB)exit );  
  185.    
  186.   glui->set_main_gfx_window( main_window );  
  187.   
  188.   /* We register the idle callback with GLUI, *not* with GLUT */  
  189.   //GLUI_Master.set_glutIdleFunc( myGlutIdle );  
  190.   GLUI_Master.set_glutIdleFunc( NULL );  
  191.   
  192.   glutMainLoop();  
  193.   
  194.   return EXIT_SUCCESS;  
  195. }  

example3 效果如图:


主要源码:

[cpp]  view plain copy
  1. int main(int argc, char* argv[])  
  2. {  
  3.   /****************************************/  
  4.   /*   Initialize GLUT and create window  */  
  5.   /****************************************/  
  6.   
  7.   glutInit(&argc, argv);  
  8.   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );  
  9.   glutInitWindowPosition( 50, 50 );  
  10.   glutInitWindowSize( 300, 300 );  
  11.    
  12.   main_window = glutCreateWindow( "GLUI Example 3" );  
  13.   glutDisplayFunc( myGlutDisplay );  
  14.   glutReshapeFunc( myGlutReshape );    
  15.   glutKeyboardFunc( myGlutKeyboard );  
  16.   glutMotionFunc( myGlutMotion );  
  17.   glutMouseFunc( myGlutMouse );  
  18.   
  19.   /****************************************/  
  20.   /*       Set up OpenGL lights           */  
  21.   /****************************************/  
  22.   
  23.   glEnable(GL_LIGHTING);  
  24.   glEnable( GL_NORMALIZE );  
  25.   
  26.   glEnable(GL_LIGHT0);  
  27.   glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);  
  28.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);  
  29.   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);  
  30.   
  31.   glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);  
  32.   glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);  
  33.   glLightfv(GL_LIGHT1, GL_POSITION, light1_position);  
  34.   
  35.   /****************************************/  
  36.   /*          Enable z-buferring          */  
  37.   /****************************************/  
  38.   
  39.   glEnable(GL_DEPTH_TEST);  
  40.   
  41.   /****************************************/  
  42.   /*         Here's the GLUI code         */  
  43.   /****************************************/  
  44.   
  45.   printf( "GLUI version: %3.2f\n", GLUI_Master.get_version() );  
  46.   
  47.   glui = GLUI_Master.create_glui( "GLUI", 0, 400, 50 ); /* name, flags, 
  48.                                x, and y */  
  49.   new GLUI_StaticText( glui, "GLUI Example 3" );   
  50.   obj_panel = new GLUI_Panel(glui, "Object" );  
  51.   
  52.   /***** Control for the object type *****/  
  53.   
  54.   GLUI_Panel *type_panel = new GLUI_Panel( obj_panel, "Type" );  
  55.   radio = new GLUI_RadioGroup(type_panel,&obj_type,4,control_cb);  
  56.   new GLUI_RadioButton( radio, "Sphere" );  
  57.   new GLUI_RadioButton( radio, "Torus" );  
  58.   new GLUI_RadioButton( radio, "Teapot" );  
  59.   
  60.   checkbox =   
  61.     new GLUI_Checkbox(obj_panel, "Wireframe", &wireframe, 1, control_cb );  
  62.   spinner =  
  63.     new GLUI_Spinner( obj_panel, "Segments:", &segments);  
  64.   spinner->set_int_limits( 3, 60 );  
  65.   spinner->set_alignment( GLUI_ALIGN_RIGHT );  
  66.   
  67.   scale_spinner =   
  68.     new GLUI_Spinner( obj_panel, "Scale:", &scale);  
  69.   scale_spinner->set_float_limits( .2f, 4.0 );  
  70.   scale_spinner->set_alignment( GLUI_ALIGN_RIGHT );  
  71.   
  72.   new GLUI_Separator( obj_panel );  
  73.   edittext = new GLUI_EditText( obj_panel, "Text:", text );  
  74.   edittext->set_w( 150 );  
  75.   
  76.   /******** Add some controls for lights ********/  
  77.   
  78.   GLUI_Panel *light0 = new GLUI_Panel( glui, "Light 1" );  
  79.   GLUI_Panel *light1 = new GLUI_Panel( glui, "Light 2" );  
  80.   
  81.   new GLUI_Checkbox( light0, "Enabled", &light0_enabled,   
  82.                      LIGHT0_ENABLED_ID, control_cb );  
  83.   light0_spinner =   
  84.     new GLUI_Spinner( light0, "Intensity:",  
  85.                       &light0_intensity, LIGHT0_INTENSITY_ID,  
  86.                       control_cb );  
  87.   light0_spinner->set_float_limits( 0.0, 1.0 );  
  88.   
  89.   new GLUI_Checkbox( light1, "Enabled", &light1_enabled,  
  90.                      LIGHT1_ENABLED_ID, control_cb );  
  91.   light1_spinner =   
  92.     new GLUI_Spinner( light1, "Intensity:",   
  93.                       &light1_intensity, LIGHT1_INTENSITY_ID,  
  94.                       control_cb );  
  95.   light1_spinner->set_float_limits( 0.0, 1.0 );  
  96.   light1_spinner->disable();   /* Disable this light initially */  
  97.   
  98.   /****** Add a grayed-out counter *****/  
  99.     
  100.   GLUI_EditText *counter_edittext =   
  101.     new GLUI_EditText( glui, "Count:", &counter );  
  102.   counter_edittext->disable();  
  103.   
  104.   /****** Button to Open Command Line Window ******/  
  105.   open_console_btn =   
  106.     new GLUI_Button(glui, "Open Console", OPEN_CONSOLE_ID, pointer_cb);  
  107.   
  108.   /****** A 'quit' button *****/  
  109.   
  110.   new GLUI_Button(glui, "Quit", 0,(GLUI_Update_CB)exit );  
  111.   
  112.   /**** Link windows to GLUI, and register idle callback ******/  
  113.     
  114.   glui->set_main_gfx_window( main_window );  
  115.   
  116.   /* We register the idle callback with GLUI, not with GLUT */  
  117.   GLUI_Master.set_glutIdleFunc( myGlutIdle );  
  118.   
  119.   /**** Regular GLUT main loop ****/    
  120.   glutMainLoop();  
  121.   
  122.   return EXIT_SUCCESS;  
  123. }  

……

example6 效果如图:


主要的代码:

[cpp]  view plain copy
  1. int main(int argc, char* argv[])  
  2. {  
  3.   glutInit(&argc, argv);  
  4.   
  5.   GLUI *edit = GLUI_Master.create_glui("Help on GLUI Widgets", 0);  
  6.   main_window = edit->get_glut_window_id();  
  7.   GLUI_Panel *ep = new GLUI_Panel(edit,"",true);  
  8.   new GLUI_StaticText(ep,"Widget Information:");  
  9.   hah = new GLUI_List(ep,true,1,control_cb);  
  10.   hah->add_item(0,"GLUI 2.3");  
  11.   hah->add_item(1,"TextBox");  
  12.   hah->add_item(2,"Scrollbar");  
  13.   hah->add_item(3,"GLUI_String");  
  14.   hah->add_item(4,"CommandLine");  
  15.   hah->add_item(5,"Tree");  
  16.   hah->add_item(6,"List");  
  17.   hah->add_item(7,"FileBrowser");  
  18.   new GLUI_StaticText(ep,"Open Text File:");  
  19.   fb = new GLUI_FileBrowser(ep, ""false, 7, control_cb);  
  20.   fb->set_h(180);  
  21.   hah->set_h(180);  
  22.   new GLUI_Column(ep,false);   
  23.   
  24.   moo = new GLUI_TextBox(ep,true);  
  25.   moo->set_text(general);  
  26.   moo->set_h(400);  
  27.   moo->set_w(410);  
  28.   moo->disable();  
  29.   enable_textbox=0;  
  30.   new GLUI_Checkbox(ep, "Enable text box:",&enable_textbox,12,control_cb);  
  31.   
  32.   tree = GLUI_Master.create_glui("Tree Test", 0);  
  33.   ep = new GLUI_Panel(tree, "Tree Controls");  
  34.   bedit = new GLUI_EditText(ep, "New Branch Name:");  
  35.   new GLUI_Checkbox(ep, "Display Numbers", &num_display);  
  36.   new GLUI_StaticText(ep, "Number format:");  
  37.   GLUI_RadioGroup *rg = new GLUI_RadioGroup(ep, &num_format);  
  38.   new GLUI_RadioButton(rg, "Level Only");  
  39.   new GLUI_RadioButton(rg, "Hierarchal");  
  40.   new GLUI_Button(ep, "Update Format", 11, control_cb);   
  41.   new GLUI_Column(ep);  
  42.   new GLUI_Button(ep, "Add Branch", 2, control_cb);   
  43.   new GLUI_Button(ep, "Del Branch", 3, control_cb);  
  44.   new GLUI_Button(ep, "Up Branch", 4, control_cb);   
  45.   new GLUI_Button(ep, "Goto Root", 5, control_cb);  
  46.   new GLUI_Column(ep);  
  47.   new GLUI_Button(ep, "Descend to Leaf", 6, control_cb);   
  48.   new GLUI_Button(ep, "Next Branch", 8, control_cb);   
  49.   new GLUI_Button(ep, "Expand All", 9, control_cb);   
  50.   new GLUI_Button(ep, "Collapse All", 10, control_cb);   
  51.   tp = new GLUI_TreePanel(tree,"Tree Test");  
  52.   tp->set_format(GLUI_TREEPANEL_ALTERNATE_COLOR |   
  53.                  GLUI_TREEPANEL_CONNECT_CHILDREN_ONLY |  
  54.                  GLUI_TREEPANEL_DISPLAY_HIERARCHY |   
  55.                  GLUI_TREEPANEL_HIERARCHY_NUMERICDOT);  
  56.   tp->set_level_color(1,1,1);  
  57.   tp->ab("foo you");  
  58.   tree->hide();  
  59.    
  60.   edit->set_main_gfx_window(main_window);   
  61.   tree->set_main_gfx_window(main_window);   
  62.   
  63.   glutMainLoop();  
  64.   return 0;  
  65. }  

原文出处:http://blog.csdn.net/banketree/article/details/7970952

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程解决的问题: 作为游戏行业或者图形学从业者,你是否面临以下问题: 到底openGL底层如何实现的? 到底矩阵操作变换是怎么做到的? 到底光栅化的算法以及原理是什么? 到底如何才能从3D世界投射到2D屏幕呢? 图形学有这么多的矩阵操作,到底如何推导如何应用呢? 学完这门课程,你应该就可以从底层了解一个初级的openGL图形接口如何实现,图形学最底层的封装到底面临哪些挑战;跟随我们一行一行写完代码,你就会得到一个迷你版本的openGL图形,你可以深度体会图形从模型变换,观察矩阵变换,投影矩阵变换一直到光栅化纹理操作的全套模拟流程。 课程介绍: 本课程将带领学员不使用任何图形,实现从0到1的图形学接口封装以及算法讲解,并且带领大家手敲代码,一行一行进行实现。 涵盖了(环境搭建,绘制点,Bresenham算法绘制完美直线,三角形拆分绘制算法,颜色插值算法,图片操作,图片二次插值放缩算法,纹理系统接口搭建及封装,矩阵操作理论以及实践,openGL类似接口封装,3D世界的图形学理论及接口封装等) 最终将带领大家通过C++实现一个3D世界的图形接口,方便所有人入门图形学,进行接下来的openGL接口以及GPU编程的学习   本课程为系列课程的第一步入门,且带领所有人进行实现,更加实用,可以让大家打牢图形学的基础知识及编程技能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值