2012年7月22日 星期日

GLUI


CB的global compiler settings -> compiler settings -> Other options 填-fexceptions
#defines填FREEGLUT_STATIC

link libraries加入libFreeGLUT.a libGLUI.a GlU32(GlU32.Lib) Gdi32(Gdi32.Lib) OpenGL32(OpenGL32.Lib) User32(User32.Lib) WinMM(WinMM.Lib)

main.cpp
//#define GLUT_DISABLE_ATEXIT_HACK //出ATEXIT_HACK錯要加
#include "iostream"
#include "math.h"
#include "glui/GL/glui.h"
#define PI 3.141592653
const float deg2Rad = PI / 180.;
const float rad2Deg = 180. / PI;

using namespace std;
//------------------------------------------------------------------------
struct neoColor
{
    float r, g, b;
 neoColor() {}
 neoColor(float r, float g, float b) : r(r), g(g), b(b) {}
 void Set(float ri, float gi, float bi) { r = ri; g = gi; b = bi; }
};
struct neoVec2
{
 float x, y;
 /// Default constructor does nothing (for performance).
 neoVec2() {}
 /// Construct using coordinates.
 neoVec2(float x, float y) : x(x), y(y) {}
 /// Set this vector to all zeros.
 void SetZero() { x = 0.0f; y = 0.0f; }
 /// Set this vector to some specified coordinates.
 void Set(float x_, float y_) { x = x_; y = y_; }
 /// Negate this vector.
 neoVec2 operator -() const { neoVec2 v; v.Set(-x, -y); return v; }
 /// Read from and indexed element.
 float operator () (int i) const
 {
  return (&x)[i];
 }

 /// Write to an indexed element.
 float& operator () (int i)
 {
  return (&x)[i];
 }
 /// Add a vector to this vector.
 void operator += (const neoVec2& v)
 {
  x += v.x; y += v.y;
 }
 /// Subtract a vector from this vector.
 void operator -= (const neoVec2& v)
 {
  x -= v.x; y -= v.y;
 }
 /// Multiply this vector by a scalar.
 void operator *= (float a)
 {
  x *= a; y *= a;
 }
 /*
 neoVec2 operator * (float a)
 {
  return neoVec2(x *= a, y *= a);
 }
 */
 /// Get the length of this vector (the norm).
 float Length() const
 {
  return sqrt(x * x + y * y);
 }
 /// Get the length squared. For performance, use this instead of
 /// b2Vec2::Length (if possible).
 float LengthSquared() const
 {
  return x * x + y * y;
 }
 /// Convert this vector into a unit vector. Returns the length.
 float Normalize()
 {
  float length = Length();
  float invLength = 1.0f / length;
  x *= invLength;
  y *= invLength;
  return length;
 }
 /// Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
 neoVec2 Skew() const
 {
  return neoVec2(-y, x);
 }
};
    inline neoVec2 operator + (const neoVec2& a, const neoVec2& b)
    {
        return neoVec2(a.x + b.x, a.y + b.y);
    }
    inline neoVec2 operator - (const neoVec2& a, const neoVec2& b)
    {
        return neoVec2(a.x - b.x, a.y - b.y);
    }
    inline neoVec2 operator * (float s, const neoVec2& a)
    {
        return neoVec2(s * a.x, s * a.y);
    }
    inline bool operator == (const neoVec2& a, const neoVec2& b)
    {
        return a.x == b.x && a.y == b.y;
    }




struct neoVec3
{
    float x, y, z;
 /// Default constructor does nothing (for performance).
 neoVec3() {}
 /// Construct using coordinates.
 neoVec3(float x, float y, float z) : x(x), y(y), z(z) {}

 /// Set this vector to all zeros.
 void SetZero() { x = 0.0f; y = 0.0f; z = 0.0f; }

 /// Set this vector to some specified coordinates.
 void Set(float x_, float y_, float z_) { x = x_; y = y_; z = z_; }

 /// Negate this vector.
 neoVec3 operator -() const {neoVec3 v; v.Set(-x, -y, -z); return v; }

 /// Add a vector to this vector.
 void operator += (const neoVec3& v)
 {
  x += v.x; y += v.y; z += v.z;
 }

 /// Subtract a vector from this vector.
 void operator -= (const neoVec3& v)
 {
  x -= v.x; y -= v.y; z -= v.z;
 }

 /// Multiply this vector by a scalar.
 void operator *= (float s)
 {
  x *= s; y *= s; z *= s;
 }
};
//--------------------------------------------------------------------------------
namespace
{
int mainWindow;
GLint winWidth = 640;
GLint winHeight = 640;
GLUI *glui;
int listboxindx = 0;
int spinnerintegervar = 0;
int checkbox = false; //1=>checked 0=>cancle
float spinnerfloatvar;
neoVec2 mp,pre_mp;
int angToY=0, angToX=0;
bool isMouseRightPressed = false;
bool isMouseLeftPressed = false;

}
//-----------------------------------------------------------------------------
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
 {
 case 27:
     exit(0);
  break;
    default:
        break;
 }
}
void KeyboardSpecial(int key, int x, int y)
{
    switch (key)
 {
 case GLUT_ACTIVE_SHIFT:
        break;
 case GLUT_KEY_LEFT:
        cout << checkbox << "," << listboxindx << endl;
  break;
 case GLUT_KEY_RIGHT:
  break;
 case GLUT_KEY_DOWN:
  break;
 case GLUT_KEY_UP:
  break;
 case GLUT_KEY_HOME:
  break;
 }
}
void Resize(int newWidth, int newHeight)
{
   glViewport(0, 0,(GLsizei) newWidth,(GLsizei) newHeight);
   winWidth  = newWidth;
   winHeight = newHeight;
   glClear(GL_COLOR_BUFFER_BIT);
}
void Mouse(int button, int state, int x, int y)
{
 // Use the mouse to move things around.
 if (button == GLUT_LEFT_BUTTON)
 {
  int specialKey  = glutGetModifiers();
  if (state == GLUT_DOWN)
  {
   if (specialKey  == GLUT_ACTIVE_SHIFT)
   {
                 cout << "GLUT_LEFT_BUTTON click with SHIFT" << endl;
   }
   else
   {
                 cout << "GLUT_LEFT_BUTTON click" << endl;
   }
  }

  if (state == GLUT_UP)
  {
                cout << "GLUT_LEFT_BUTTON release" << endl;
  }
 }
 else if (button == GLUT_RIGHT_BUTTON)
 {
  if (state == GLUT_DOWN)
  {
      isMouseRightPressed = true;
            cout << "GLUT_RIGHT_BUTTON click" << endl;
  }

  if (state == GLUT_UP)
  {
      isMouseRightPressed = false;
            cout << "GLUT_RIGHT_BUTTON release" << endl;
  }
 }
}
void MouseWheel(int wheel, int direction, int x, int y)
{
 if (direction > 0)
 {
   cout << "wheel in" << endl;
 }
 else
 {
   cout << "wheel out" << endl;
 }
}
void MouseMotion(int x, int y)
{
    if(isMouseRightPressed)
    {
        int distanceX = x - pre_mp.x;
        int distanceY = y - pre_mp.y;
        pre_mp.Set(x,y);
        angToY += distanceX;
        angToX += distanceY;
        glutPostRedisplay(); //要求重畫視窗
    }
    cout << "Mouse clicked and x=" << mp.x << ",y=" << mp.y << endl;
}
//glutPassiveMotionFunc(int x,int y);
//glutEntryFunc(processMouseEntryWindow);
void Exit(int code)
{
#ifndef __APPLE__
 glutLeaveMainLoop();
#endif
 exit(code);
}
void Timer(int)
{
    cout << "time up!" << endl;
 glutSetWindow(mainWindow);
    glutPostRedisplay(); //要求重畫視窗
 glutTimerFunc(16, Timer, 0); //Do Repeat
}
void DrawPoint(const neoVec2& p, float size, const neoColor& color)
{
    glPointSize(size);
    glBegin(GL_POINTS);
 glColor3f(color.r, color.g, color.b);
 glVertex2f(p.x, p.y);
    glEnd();
 glPointSize(1.0f);
}
void DrawCircle(const neoVec2& center, float radius, const neoColor& color)
{
 const float k_segments = 16.0f;
 const float k_increment = 2.0f * PI / k_segments;
 float theta = 0.0f;
 glColor3f(color.r, color.g, color.b);
 glBegin(GL_LINE_LOOP);
 for (int i = 0; i < k_segments; ++i)
 {
     neoVec2 v = center +  radius * neoVec2(cosf(theta), sinf(theta)) ;
  glVertex2f(v.x, v.y);
  theta += k_increment;
 }
 glEnd();
}

void DrawSegment(const neoVec2& p1, const neoVec2& p2, const neoColor& color,  bool realline)
{
     if (!realline)
    {
        glLineStipple (1, 0x1C47);
        glEnable(GL_LINE_STIPPLE);
    }
    glBegin(GL_LINES);
 glColor3f(color.r, color.g, color.b);
 glVertex2f(p1.x, p1.y);
 glVertex2f(p2.x, p2.y);
 glEnd();
 if (!realline) glDisable(GL_LINE_STIPPLE);
}
void DrawPolygon(const neoVec2* vertices, int vertexCount, const neoColor& color)
{
 glColor3f(color.r, color.g, color.b);
 glBegin(GL_LINE_LOOP);
 for (int i = 0; i < vertexCount; ++i)
 {
  glVertex2f(vertices[i].x, vertices[i].y);
 }
 glEnd();
}
void DrawString(int x, int y, const char *string, ...)
{
 char buffer[128];

 va_list arg;
 va_start(arg, string);
 vsprintf(buffer, string, arg);
 va_end(arg);

 glMatrixMode(GL_PROJECTION);
 glPushMatrix();
 glLoadIdentity();
 int w = glutGet(GLUT_WINDOW_WIDTH);
 int h = glutGet(GLUT_WINDOW_HEIGHT);
 gluOrtho2D(0, w, h, 0);
 glMatrixMode(GL_MODELVIEW);
 glPushMatrix();
 glLoadIdentity();

 glColor3f(0.9f, 0.6f, 0.6f);
 glRasterPos2i(x, y);
 int length = (int)strlen(buffer);
 for (int i = 0; i < length; ++i)
 {
  glutBitmapCharacter(GLUT_BITMAP_8_BY_13, buffer[i]);
 }

 glPopMatrix();
 glMatrixMode(GL_PROJECTION);
 glPopMatrix();
 glMatrixMode(GL_MODELVIEW);
}

void display()
{
    //glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(angToX, 1.0, 0.0, 0.0);
    glRotatef(angToY, 0.0, 1.0, 0.0);
//---------------------------------------------------------------------
DrawSegment(neoVec2(0,0),neoVec2(0.1,0.1),neoColor(0.5,1,1),false);
DrawPoint(neoVec2(0,0),5,neoColor(1,1,1));
DrawPoint(neoVec2(0.1,0.1),5,neoColor(1,0,1));
DrawCircle(neoVec2(0,0),0.1,neoColor(1,1,1));
neoVec2 list[3] = {neoVec2(0,0), neoVec2(-0.1,0.1), neoVec2(-0.1,0)};
DrawPolygon(list,3,neoColor(0.5,1,1));
DrawString(100,200,"abc");
//---------------------------------------------------------------------
    glPopMatrix();
    glutSwapBuffers();
    glFlush();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
 glutInitWindowSize(winWidth, winHeight);
    glutInitWindowPosition(200, 50);
    gluOrtho2D(0, winWidth,winHeight, 0);
    mainWindow = glutCreateWindow( "Neo Glui" );
    glClearColor(0.0, 0.0, 0.0, 0.0); //指定填視窗背景的顏色
    glViewport(0, 0, (GLsizei) winWidth, (GLsizei) winHeight);
    glutDisplayFunc(display);
    GLUI_Master.set_glutReshapeFunc(Resize);
 GLUI_Master.set_glutKeyboardFunc(Keyboard);
 GLUI_Master.set_glutSpecialFunc(KeyboardSpecial);
    GLUI_Master.set_glutMouseFunc(Mouse);
    glutMouseWheelFunc(MouseWheel);
    glutMotionFunc(MouseMotion);
    //------------------------------------------------
    glui = GLUI_Master.create_glui_subwindow(mainWindow, GLUI_SUBWINDOW_RIGHT );
 glui->add_statictext("Text");
 GLUI_Listbox* selectbox = glui->add_listbox("", &listboxindx);
 selectbox->add_item(0, "One");
 selectbox->add_item(1, "Two");
 selectbox->add_item(2, "Three");

 GLUI_Spinner* spinnerinteger = glui->add_spinner("spin1", GLUI_SPINNER_INT, &spinnerintegervar);
 spinnerinteger->set_int_limits(1,10);
    GLUI_Spinner* spinnerfloat = glui->add_spinner("spin2", GLUI_SPINNER_FLOAT, &spinnerfloatvar);
 spinnerfloat->set_float_limits(5.0f, 10.0f);

    glui->add_checkbox("checkbox", &checkbox);
  glui->add_separator();
    GLUI_Panel* panel = glui->add_panel("panel");
    glui->add_checkbox_to_panel(panel,"Panel checkbox",&checkbox);
 glui->add_separator();
 glui->add_button("Exit", 0, (GLUI_Update_CB)Exit);
 glui->set_main_gfx_window( mainWindow );
    glutTimerFunc(16, Timer, 0);

    glutMainLoop();
    cout << "中文!" << endl;
    return 0;

沒有留言:

張貼留言