Пример#10 Создание GUI используя GLUI, часть 2
#include <iostream>
#include <string>
#include "glut.h"
#include "glui.h"
using namespace std;
float length = 5;
float scale =1;
int Lighting=1;
std::string text = "User interface collection";
#define max 10
#define min -max
#define drawOneLine(x1,y1,z1,x2,y2,z2) glBegin(GL_LINES); \
glVertex3f(x1,y1,z1); glVertex3f (x2,y2,z2); glEnd();
int obj = 2;
GLUI *glui;
GLUI_Rotation *sph_rot;
GLUI_RadioGroup *radio;
float cube_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
float view_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
float obj_pos[] = { 0.0, 0.0, 0.0 };
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glDepthFunc(GL_LESS);
GLfloat light0_ambient[] = {0.1f, 0.1f, 0.3f, 1.0f};
GLfloat light0_diffuse[] = {.6f, .6f, 1.0f, 1.0f};
GLfloat light0_position[] = {1.0f, 1.0f, 1.0f, 0.0f};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
//glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);//объекты находятся в одном слое
glEnable(GL_COLOR_MATERIAL);//цветной материал, иначе все ЧБ
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(min, max, min, max, min, max);
gluLookAt(3, 1, 0, 0,0,0, 0,1,0);//from, to, vector
glMatrixMode(GL_MODELVIEW);
}
void SysCooor(float length)
{
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(length, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, length, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, length);
glEnd();
glEnable (GL_LINE_STIPPLE);
glLineStipple (1.5, 0x00FF);
glColor3f(1.0f, 0.0f, 0.0f);
drawOneLine (0.0, 0.0, 0.0, -length, 0.0, 0.0);
glColor3f(0.0f, 1.0f, 0.0f);
drawOneLine (0.0, 0.0, 0.0, 0.0, -length, 0.0);
glColor3f(0.0f, 0.0f, 1.0f);
drawOneLine (0.0, 0.0, 0.0, 0.0, 0.0, -length);
glLineWidth (1.0);
glDisable (GL_LINE_STIPPLE);
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (Lighting==0) glDisable(GL_LIGHTING);
else glEnable(GL_LIGHTING);
glPushMatrix();
glScalef(scale,scale,scale);
glRotatef(-90,1,0,0);//+Z сверху c наклоном
glRotatef(-45,0,0,1);//XY к пользователю
glTranslatef( obj_pos[0], obj_pos[1], obj_pos[2] );
glMultMatrixf( cube_rotate );
SysCooor(length);
glColor3ub( 255, 0, 0 );
glRasterPos3f( 1, 1 ,8);
for (unsigned int i=0; i<text.length(); ++i)
glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, text[i] );
if ( obj == 0 ) glutSolidSphere(2,16,16);
else if ( obj == 1 ) glutSolidIcosahedron();
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
glFlush();
}
void control_cb( int control )
{
for(int i=0;i<16;i++)
cube_rotate[i]=0;//{ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
for(int i=0;i<16;i+=5)
cube_rotate[i]=1;
}
void GUI()
{
glui = GLUI_Master.create_glui_subwindow( 1, GLUI_SUBWINDOW_LEFT );
(new GLUI_Spinner( glui, "Length:", &length ))->set_int_limits( 0, 10 );
new GLUI_Separator( glui );
(new GLUI_Spinner( glui, "Scale:", &scale ))->set_int_limits( 0, 5 );
new GLUI_Separator( glui );
GLUI_Panel *panelLight = new GLUI_Panel( glui, "", GLUI_PANEL_NONE );
new GLUI_Checkbox( panelLight, "Light", &Lighting );
GLUI_EditText *edittext= new GLUI_EditText( glui, "Text:", text, -1);
new GLUI_Button( glui, "Orientation=0", 0, control_cb);
GLUI_Panel *obj_panel = new GLUI_Panel( glui, "Panel" );
radio = new GLUI_RadioGroup( obj_panel,&obj,-1 );
new GLUI_RadioButton( radio, "Sphere" );
new GLUI_RadioButton( radio, "Icosahedron" );
new GLUI_RadioButton( radio, "EMPTY" );
GLUI_Rollout *Move = new GLUI_Rollout(glui, "Move", false );
GLUI_Translation *trans_xy =
new GLUI_Translation(Move, "Objects XY", GLUI_TRANSLATION_XY, obj_pos );
trans_xy->set_speed( .1 );
for(int i=0;i<5;i++)
new GLUI_StaticText( glui, "" );
sph_rot = new GLUI_Rotation(glui, "Rotate", cube_rotate );
sph_rot->set_spin(1);
sph_rot->set_h(50);
sph_rot->set_w(50);
for(int i=0;i<4;i++)
new GLUI_StaticText( glui, "" );
new GLUI_Button( glui, "QUIT", 0,(GLUI_Update_CB)exit );
}
void mouse(int button,int state,int x,int y)
{
switch(button)
{
case GLUT_MIDDLE_BUTTON:
exit(0);
}
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA |GLUT_DEPTH);
glutInitWindowPosition(150, 50);
glutInitWindowSize(800,600);
glutCreateWindow("user interface");
GLUI_Master.set_glutSpecialFunc( NULL );
init();
glutDisplayFunc(draw);
glutMouseFunc(mouse);
GUI();
glutMainLoop();
}
Для самостоятельной работы:
1. Добавьте возможность изменять цвет объекта с помощью инструмента Spinner
2. Добавьте возможность вращения объекта без вращения глобальных(мировых) осей координат