// Callback function called by GLUT to render sub-window content void DisplaySubWindow(void) { float v[4]; // will be used to set light parameters float mat[4*4]; // rotation matrix SubWindowData *win; win = GetCurrentSubWindowData(); if (win == NULL) return; // Tell AntTweakBar which is the current window TwSetCurrentWindow(win->WinID); // Clear frame buffer glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glEnable(GL_NORMALIZE); // Set light glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); v[0] = v[1] = v[2] = win->LightMultiplier*0.4f; v[3] = 1.0f; glLightfv(GL_LIGHT0, GL_AMBIENT, v); v[0] = v[1] = v[2] = win->LightMultiplier*0.8f; v[3] = 1.0f; glLightfv(GL_LIGHT0, GL_DIFFUSE, v); v[0] = -win->LightDirection[0]; v[1] = -win->LightDirection[1]; v[2] = -win->LightDirection[2]; v[3] = 0.0f; glLightfv(GL_LIGHT0, GL_POSITION, v); // Set material glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, win->MatAmbient); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, win->MatDiffuse); // Rotate and draw shape glPushMatrix(); glTranslatef(0.5f, -0.3f, 0.0f); if( win->AutoRotate ) { float axis[3] = { 0, 1, 0 }; float angle = (float)(GetTimeMs()-win->RotateTime)/1000.0f; float quat[4]; SetQuaternionFromAxisAngle(axis, angle, quat); MultiplyQuaternions(win->RotateStart, quat, win->Rotation); } ConvertQuaternionToMatrix(win->Rotation, mat); glMultMatrixf(mat); glScalef(win->Zoom, win->Zoom, win->Zoom); glCallList(win->ObjectShape); glPopMatrix(); // Draw tweak bars TwDraw(); // Present frame buffer glutSwapBuffers(); // Recall Display at next frame glutPostRedisplay(); }
GLWindow::GLWindow():PythonObject<GLWindow>(this), glfwWindowPointer_(glfwCreateWindow(600,600,"scigma", NULL,Application::get_instance()->get_master_window())), glContext_(glfwWindowPointer_), viewingArea_(glfwWindowPointer_,&glContext_), viewingVolume_(&glContext_), cursorX_(cursorPosition_), cursorY_(cursorPosition_+1), cursorLastX_(cursorPosition_+2), cursorLastY_(cursorPosition_+3) { Application::get_instance()->glfw_window_set_callbacks(glfwWindowPointer_); glfwSetWindowUserPointer(glfwWindowPointer_,static_cast<void*>(this)); TwSetCurrentWindow(size_t(&glContext_)); TwDefine(" GLOBAL iconpos=topright "); TwDefine(" GLOBAL iconalign=horizontal "); TwDefine(" GLOBAL contained=true "); TwDefine(" GLOBAL buttonalign=right"); TwDefine(" GLOBAL fontsize=3"); TwDefine(" GLOBAL fontresizable=false"); TwDefine(" TW_HELP visible=false "); *cursorX_=-1e10f; *cursorY_=-1e10f; *cursorLastX_=-1e10f; *cursorLastY_=-1e10f; set_theme(DARK); }
void ViewingArea::set_size(GLint width, GLint height, bool calledByUser) { if(calledByUser) { glfwSetWindowSize(glfwWindowPointer_,width,height); TwSetCurrentWindow(size_t(glContext_)); TwWindowSize(width,height); } GLfloat hCenterAndRelative=GLfloat(width)/2; GLfloat vCenterAndRelative=GLfloat(height)/2; GLfloat hChange=hCenterAndRelative-centerAndRelative_[X_INDEX]; GLfloat vChange=vCenterAndRelative-centerAndRelative_[Y_INDEX]; insetCenter_[X_INDEX]+=hChange; insetCenter_[Y_INDEX]+=vChange; insetRelative_[X_INDEX]+=hChange; insetRelative_[Y_INDEX]+=vChange; centerAndRelative_[X_INDEX]=deviceToScreenMatrix_[X_INDEX*N_ROWS+X_INDEX]=hCenterAndRelative; centerAndRelative_[Y_INDEX]=deviceToScreenMatrix_[Y_INDEX*N_ROWS+Y_INDEX]=vCenterAndRelative; screenToDeviceMatrix_[X_INDEX*N_ROWS+X_INDEX]=1/deviceToScreenMatrix_[X_INDEX*N_ROWS+X_INDEX]; screenToDeviceMatrix_[Y_INDEX*N_ROWS+Y_INDEX]=1/deviceToScreenMatrix_[Y_INDEX*N_ROWS+Y_INDEX]; deviceToScreenMatrix_[N_SPATIAL_DIMENSIONS*N_ROWS+X_INDEX]=hCenterAndRelative; deviceToScreenMatrix_[N_SPATIAL_DIMENSIONS*N_ROWS+Y_INDEX]=vCenterAndRelative; screenToDeviceMatrix_[N_SPATIAL_DIMENSIONS*N_ROWS+X_INDEX]=-1.0f; screenToDeviceMatrix_[N_SPATIAL_DIMENSIONS*N_ROWS+Y_INDEX]=-1.0f; update_projection_matrices(); EventSource<ResizeEvent>::Type::emit(width,height); glContext_->update_global_uniform_4x4(GLContext::SCREEN_TO_DEVICE_MATRIX,screenToDeviceMatrix_); glContext_->update_global_uniform_4x4(GLContext::DEVICE_TO_SCREEN_MATRIX,deviceToScreenMatrix_); glContext_->set_viewport(0,0,width,height); }
void setupAntTweakBar() { TwInit(TW_OPENGL_CORE, NULL); #if 0 barWindow = glfwCreateWindow(400, 400, "Param", NULL, NULL); TwSetCurrentWindow(barWindow); #endif bar = TwNewBar("Param"); WindowSizeCB(window, width, height); glfwSetWindowSizeCallback(window, WindowSizeCB); glfwSetMouseButtonCallback(window, TwEventMouseButtonGLFW); glfwSetCursorPosCallback(window, TwEvenCursorPosGLFW); glfwSetScrollCallback(window, TwEventScrollGLFW); glfwSetKeyCallback(window, TwEventKeyGLFW); glfwSetCharCallback(window, TwEventCharGLFW); TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar. }
// Callback function called when the 'AutoRotate' variable value of the tweak bar has changed void TW_CALL SetAutoRotateCB(const void *value, void *clientData) { SubWindowData *win; win = (SubWindowData *)clientData; win->AutoRotate = *(const int *)value; // copy value to win->AutoRotate if (win->AutoRotate != 0) { // init rotation win->RotateTime = GetTimeMs(); win->RotateStart[0] = win->Rotation[0]; win->RotateStart[1] = win->Rotation[1]; win->RotateStart[2] = win->Rotation[2]; win->RotateStart[3] = win->Rotation[3]; } // make Rotation variable read-only or read-write TwSetCurrentWindow(win->WinID); TwSetParam(win->Bar, "ObjRotation", "readonly", TW_PARAM_INT32, 1, &win->AutoRotate); }
// Callback function called by GLUT when sub-window size has changed void ReshapeSubWindow(int width, int height) { SubWindowData *win; win = GetCurrentSubWindowData(); if (win == NULL) return; // Set OpenGL viewport and camera glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40, (double)width/height, 1, 10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,5, 0,0,0, 0,1,0); glTranslatef(0, 0.6f, -1); // Send the new window size to AntTweakBar TwSetCurrentWindow(win->WinID); TwWindowSize(width, height); }
// Setup new sub-window void SetupSubWindow(int subWinIdx) { float axis[] = { 0.7f, 0.7f, 0.0f }; // initial model rotation float angle = 0.8f; SubWindowData *win; win = &g_SubWindowData[subWinIdx]; win->ObjectShape = (subWinIdx == 0) ? SHAPE_TEAPOT : SHAPE_TORUS; win->Zoom = 1; win->AutoRotate = (subWinIdx == 0); win->MatAmbient[0] = (subWinIdx == 1) ? 0.0f : 0.5f;; win->MatAmbient[1] = win->MatAmbient[2] = 0.2f; win->MatAmbient[3] = 1; win->MatDiffuse[0] = (subWinIdx == 1) ? 0.0f : 1.0f; win->MatDiffuse[1] = 1; win->MatDiffuse[2] = 0; win->MatDiffuse[3] = 1; win->LightMultiplier = 1; win->LightDirection[0] = win->LightDirection[1] = win->LightDirection[2] = -0.57735f; win->RotateTime = GetTimeMs(); SetQuaternionFromAxisAngle(axis, angle, win->Rotation); SetQuaternionFromAxisAngle(axis, angle, win->RotateStart); glutSetWindow(win->WinID); // Set GLUT callbacks glutDisplayFunc(DisplaySubWindow); glutReshapeFunc(ReshapeSubWindow); // Set GLUT event callbacks // - Register mouse button events callback glutMouseFunc((GLUTmousebuttonfun)MouseButtonCB); // - Register mouse motion events callback glutMotionFunc((GLUTmousemotionfun)MouseMotionCB); // - Register mouse "passive" motion events (same as Motion) glutPassiveMotionFunc((GLUTmousemotionfun)MouseMotionCB); // - Register keyboard events callback glutKeyboardFunc((GLUTkeyboardfun)KeyboardCB); // - Register special key events callback glutSpecialFunc((GLUTspecialfun)SpecialKeyCB); // - Send 'glutGetModifers' function pointer to AntTweakBar; // required because the GLUT key event functions do not report key modifiers states. TwGLUTModifiersFunc(glutGetModifiers); // Create some 3D objects (stored in display lists) glNewList(SHAPE_TEAPOT, GL_COMPILE); glutSolidTeapot(1.0); glEndList(); glNewList(SHAPE_TORUS, GL_COMPILE); glutSolidTorus(0.3, 1.0, 16, 32); glEndList(); glNewList(SHAPE_CONE, GL_COMPILE); glutSolidCone(1.0, 1.5, 64, 4); glEndList(); // Declare this window as current for AntTweakBar. // Here, this will create a new AntTweakBar context for this window, // which will be identified by the number WinID. TwSetCurrentWindow(win->WinID); // Create a tweak bar win->Bar = TwNewBar("TweakBar"); TwDefine(" GLOBAL help='This example shows how to use AntTweakBar with multiple windows.' "); // Message added to the help bar. TwDefine(" TweakBar size='200 400' color='96 216 224' "); // change default tweak bar size and color // Add 'win->Zoom' to 'bar': this is a modifable (RW) variable of type TW_TYPE_FLOAT. Its key shortcuts are [z] and [Z]. TwAddVarRW(win->Bar, "Zoom", TW_TYPE_FLOAT, &win->Zoom, " min=0.01 max=2.5 step=0.01 keyIncr=z keyDecr=Z help='Scale the object (1=original size).' "); // Add 'win->Rotation' to 'bar': this is a variable of type TW_TYPE_QUAT4F which defines the object's orientation TwAddVarRW(win->Bar, "ObjRotation", TW_TYPE_QUAT4F, &win->Rotation, " label='Object rotation' open help='Change the object orientation.' "); // Add callback to toggle auto-rotate mode (callback functions are defined above). TwAddVarCB(win->Bar, "AutoRotate", TW_TYPE_BOOL32, SetAutoRotateCB, GetAutoRotateCB, win, " label='Auto-rotate' key=space help='Toggle auto-rotate mode.' "); // Add 'win->LightMultiplier' to 'bar': this is a variable of type TW_TYPE_FLOAT. Its key shortcuts are [+] and [-]. TwAddVarRW(win->Bar, "Multiplier", TW_TYPE_FLOAT, &win->LightMultiplier, " label='Light booster' min=0.1 max=4 step=0.02 keyIncr='+' keyDecr='-' help='Increase/decrease the light power.' "); // Add 'win->LightDirection' to 'bar': this is a variable of type TW_TYPE_DIR3F which defines the light direction TwAddVarRW(win->Bar, "LightDir", TW_TYPE_DIR3F, &win->LightDirection, " label='Light direction' open help='Change the light direction.' "); // Add 'win->MatAmbient' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored) // and is inserted into a group named 'Material'. TwAddVarRW(win->Bar, "Ambient", TW_TYPE_COLOR3F, &win->MatAmbient, " group='Material' "); // Add 'win->MatDiffuse' to 'bar': this is a variable of type TW_TYPE_COLOR3F (3 floats color, alpha is ignored) // and is inserted into group 'Material'. TwAddVarRW(win->Bar, "Diffuse", TW_TYPE_COLOR3F, &win->MatDiffuse, " group='Material' "); // Add the enum variable 'win->ObjectShape' to 'bar' // (before adding an enum variable, its enum type must be declared to AntTweakBar as follow) { // ShapeEV associates Shape enum values with labels that will be displayed instead of enum values TwEnumVal shapeEV[NUM_SHAPES] = { {SHAPE_TEAPOT, "Teapot"}, {SHAPE_TORUS, "Torus"}, {SHAPE_CONE, "Cone"} }; // Create a type for the enum shapeEV TwType shapeType = TwDefineEnum("ShapeType", shapeEV, NUM_SHAPES); // add 'win->CurrentShape' to 'bar': this is a variable of type ShapeType. Its key shortcuts are [<] and [>]. TwAddVarRW(win->Bar, "Shape", shapeType, &win->ObjectShape, " keyIncr='<' keyDecr='>' help='Change object shape.' "); } }
// Special key event callbacks int SpecialKeyCB(int glutKey, int mouseX, int mouseY) { TwSetCurrentWindow(glutGetWindow()); return TwEventSpecialGLUT(glutKey,mouseX,mouseY); }
// Keyboard event callbacks int KeyboardCB(unsigned char glutKey, int mouseX, int mouseY) { TwSetCurrentWindow(glutGetWindow()); return TwEventKeyboardGLUT(glutKey,mouseX,mouseY); }
// Mouse Motion event callbacks int MouseMotionCB(int mouseX, int mouseY) { TwSetCurrentWindow(glutGetWindow()); return TwEventMouseMotionGLUT(mouseX,mouseY); }
// Mouse Button event callbacks int MouseButtonCB(int glutButton, int glutState, int mouseX, int mouseY) { TwSetCurrentWindow(glutGetWindow()); return TwEventMouseButtonGLUT(glutButton,glutState,mouseX,mouseY); }