void OGLViewer::keyPressEvent(QKeyEvent *e) { if (e->key() == Qt::Key_W) { mViewCamera->zoom(0.0, 0.0, 0.10); } else if (e->key() == Qt::Key_S) { mViewCamera->zoom(0.0, 0.0, -0.10); } else if (e->key() == Qt::Key_Q) { mViewCamera->zoom(0.10, 0.0, 0.0); } else if (e->key() == Qt::Key_A) { mViewCamera->zoom(-0.10, 0.0, 0.0); } else if (e->key() == Qt::Key_E) { mViewCamera->zoom(0.0, 0.10, 0.0); } else if (e->key() == Qt::Key_D) { mViewCamera->zoom(0.0, -0.10, 0.0); } // Save frame buffer else if (e->key() == Qt::Key_P && e->modifiers() == Qt::ControlModifier) { saveFrameBuffer(); } ////////////////////////////////////////////////////////////////////////// else { QOpenGLWidget::keyPressEvent(e); } update(); }
void GLRenderWidget::createContextMenu() { mContextMenu = new QMenu("Rendering", this); QMenu *openglMenu = new QMenu(tr("OpenGL settings")); mActionWireframe = new QAction("wireframe-mode",this); mActionWireframe->setCheckable(true); connect(mActionWireframe, SIGNAL(toggled(bool)), this, SLOT(toggleWireframe(bool))); mActionShowMeta = new QAction("show voxelspace meta", this); mActionShowMeta->setCheckable(true); connect(mActionShowMeta, SIGNAL(toggled(bool)), this, SLOT(toggleShowMeta(bool))); mActionInvertBackground = new QAction("invert background", this); mActionInvertBackground->setCheckable(true); connect(mActionInvertBackground, SIGNAL(toggled(bool)), this, SLOT(toggleBackgroundColor(bool))); mGrabBufferAction = new QAction(QIcon(":/icons/24/camera-video.png"), trUtf8("grab framebuffer"), this); connect(mGrabBufferAction, SIGNAL(triggered()), this, SLOT(saveFrameBuffer())); mContextMenu->addAction(mGrabBufferAction); mPauseAction = new QAction(QIcon(":/icons/24/media-playback-pause.png"), trUtf8("pause rendering"), this); mPauseAction->setCheckable(true); connect(mPauseAction, SIGNAL(triggered()), this, SLOT(togglePause())); mContextMenu->addAction(mPauseAction); openglMenu->addAction(mActionWireframe); openglMenu->addAction(mActionShowMeta); openglMenu->addAction(mActionInvertBackground); // ************************** RENDERING ************************************ mLowShadingAction = new QAction("low quality", this); mLowShadingAction->setCheckable(true); connect(mLowShadingAction, SIGNAL(triggered()), this, SLOT(setLowQualityRendering())); mMediumShadingAction = new QAction("medium quality", this); mMediumShadingAction->setCheckable(true); connect(mMediumShadingAction, SIGNAL(triggered()), this, SLOT(setMediumQualityRendering())); mHighShadingAction = new QAction("high quality", this); mHighShadingAction->setCheckable(true); connect(mHighShadingAction, SIGNAL(triggered()), this, SLOT(setHighQualityRendering())); QActionGroup *qualityGroup = new QActionGroup(this); qualityGroup->addAction(mLowShadingAction); qualityGroup->addAction(mMediumShadingAction); qualityGroup->addAction(mHighShadingAction); openglMenu->addSeparator()->setText(trUtf8("quality")); openglMenu->addAction(mLowShadingAction); openglMenu->addAction(mMediumShadingAction); openglMenu->addAction(mHighShadingAction); openglMenu->addSeparator(); mHighShadingAction->setChecked(true); //************************************************************************* mCamerasMenu = new QMenu(tr("Viewpoints")); mLightingsMenu = new QMenu(tr("Lightings")); mEnginesMenu = new QMenu(tr("render engines")); mContextMenu->addMenu(openglMenu); mContextMenu->addMenu(mCamerasMenu); mContextMenu->addMenu(mLightingsMenu); mContextMenu->addMenu(mEnginesMenu); }
int main(int argc, char *argv[]) { char *shader = "mandelbrot (copy).frag"; if (argc > 1) { shader = argv[1]; } glfwInit(); /*int major, minor, rev; glfwGetGLVersion(&major, &minor, &rev); printf("using openGL %d.%d.%d\n", major, minor, rev);*/ GLFWvidmode mode; glfwGetDesktopMode( &mode ); window_width = mode.Width; window_height = mode.Height; printf("%dx%d\n", window_width, window_height); //glfwOpenWindow(window_width, window_height, 5, 6, 5, 0, 8, 0, GLFW_FULLSCREEN); glfwOpenWindow(mode.Width, mode.Height, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 0, 0, GLFW_FULLSCREEN); // setup opengl perspective stuff. glMatrixMode(GL_PROJECTION); float aspect_ratio = ((float)window_height) / window_width; glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50); glDisable( GL_DEPTH_TEST ); glTranslatef(0.0, 0.0, -1.00); glClearColor(0.0, 1.0, 1.0, 0.0); // will create and set shader program. GLhandleARB program = SetupFragmentShader(shader); // get the location of all the uniforms prepareUniforms(program); // must call use program before setting uniforms values glUseProgram(program); uint param_i = 0; parameters[WIDTH] = createParameter1i(program, "wW", window_width ); parameters[HEIGHT] = createParameter1i(program, "wH", window_height ); parameters[TIME] = createParameter1f(program, "time", 0.0 ); parameters[MINX] = createParameter1f(program, "minx", -2.0); parameters[MINY] = createParameter1f(program, "miny", 1.0); parameters[DELTA] = createParameter1f(program, "deltax", 3.0); parameters[THETA] = createParameter1f(program, "theta", 1.0); //synchParameters ( parameters ); /*for ( param_i = 0; param_i < NPARAMS; param_i++ ) { Parameter *param = ¶meters[param_i]; switch ( param->utype ) { case INT: glUniform1i(param->loc, param->u.ival); break; case FLOAT: glUniform1f(param->loc, param->u.fval); break; default: break; } }*/ // set the dimension, must be after program is used. //glUniform1i(wWLoc, window_width ); //glUniform1i(wHLoc, window_height ); printInstructions(); double frame_start = glfwGetTime(); double frame_time = 0.0; double temp_time = 0.0; //bool need_draw = true; bool need_view_synch = true; int mouseWheel = 0; int mouseWheelDelta = 0; double fIterations = (double)(iterations); int mouseX, mouseY, curMouseX, curMouseY; mouseX = mouseY = 0; bool joystickPresent = glfwGetJoystickParam( GLFW_JOYSTICK_1, GLFW_PRESENT ); if(joystickPresent) { printf("Joystick present. Can be used to navigate in addition to mouse.\n"); }else { printf("no joystick present.\n"); } // set up fonts FTGLfont *font = NULL; //font = ftglCreateBufferFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); font = ftglCreatePixmapFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); if( !font ) { printf("failed to load font."); } ftglSetFontFaceSize(font, 50, 50); //printf("%d\n", ftglGetFontError(font)); //ftglSetFontCharMap(font, ft_encoding_unicode); //printf("%d\n", ftglGetFontError(font)); bool show_fps = true; char text[256]; // keep on rendering the frame until escape is pressed. while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS) { temp_time = glfwGetTime(); frame_time = temp_time - frame_start; frame_start = temp_time; // process input if(glfwGetKey('E') == GLFW_PRESS) { printf("minx: %.31f\n", minx); printf("miny: %.31f\n", miny); printf("deltax: %.31f\n",deltax); printf("deltay: %.31f\n",deltay); printf("====================================\n"); }else if(glfwGetKey('P') == GLFW_PRESS) { saveFrameBuffer(); }else if(glfwGetKey('R') == GLFW_PRESS) { minx = -2.0; miny = -1.0; deltax = 3.0; deltay = 2.0; need_view_synch = true; }else if(glfwGetKey('F') == GLFW_PRESS) { show_fps=~show_fps; //printf("fps display toggled to: %d\n", show_fps); } else if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS) { fIterations += 5*(60*frame_time); iterations = floor(fIterations); need_view_synch = true; }else if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS) { if(iterations>1) { fIterations -= 5*(60*frame_time); } iterations = floor(fIterations); need_view_synch = true; } // zooming using the arrow keys if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS) { zoomIn(2.0*zoom_factor*(frame_time*60.0)); need_view_synch = true; }else if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS) { zoomIn(-2.0*zoom_factor*(frame_time*60.0)); need_view_synch = true; } //changing the julia set constant if(glfwGetKey('W') == GLFW_PRESS) { realc += .0025*frame_time*60.0; need_view_synch = true; }else if(glfwGetKey('S') == GLFW_PRESS) { realc -= .0025*frame_time*60.0; need_view_synch = true; } if(glfwGetKey('A') == GLFW_PRESS) { imagc += .0025*frame_time*60.0; need_view_synch = true; }else if(glfwGetKey('D') == GLFW_PRESS) { imagc -= .0025*frame_time*60.0; need_view_synch = true; } // changing the area visible (panning left and right) glfwGetMousePos(&curMouseX, &curMouseY); int mouseDeltax = curMouseX - mouseX; int mouseDeltay = curMouseY - mouseY; if(mouseDeltax||mouseDeltay)//if one of them is different { //printf("delta coordinates: (%d, %d)\n", mouseDeltax, mouseDeltay); mouseX=curMouseX; mouseY=curMouseY; //printf("new mouse coordinates: (%d, %d)\n", x, y); pan(zoom_factor*deltax*mouseDeltax/20.0, -1*zoom_factor*deltay*mouseDeltay/20.0); need_view_synch = true; } if(joystickPresent) { float pos[4]; glfwGetJoystickPos(GLFW_JOYSTICK_1, pos, 4); //the above function gives us bad results for my particular joystick :(, must fiddle around with numbers to get correct answer. float xJoyPos = pos[0];//(pos[0]*32767)/128.0 - 1.0; float yJoyPos = pos[1];//(pos[1]*32767)/128.0 + 1.0; float zJoyPos = -1*pos[2];//-1*((pos[2]*32767)/128.0 - 1.0); float rJoyPos = pos[3]; float temp = 0.0; //printf("%f\n", xJoyPos); if(xJoyPos>.02||xJoyPos<-.02) { temp = zoom_factor*xJoyPos*deltax*1.0*frame_time*60.0; pan(temp, 0); need_view_synch = true; } if(yJoyPos>.02||yJoyPos<-.02) { temp = zoom_factor*yJoyPos*deltay*1.0*frame_time*60.0; pan(0, temp); need_view_synch = true; } if(zJoyPos>.04||zJoyPos<-.04) { zoomIn(zJoyPos*scroll_zoom_factor*(frame_time*10.0)); need_view_synch = true; } if(rJoyPos>.02 || rJoyPos<-.02) { rotate( rJoyPos/5.0*frame_time ); need_view_synch = true; } unsigned char buttons[5]; glfwGetJoystickButtons(GLFW_JOYSTICK_1, buttons, 4); if(buttons[0]==GLFW_PRESS) { saveFrameBuffer(); } if(buttons[1]==GLFW_PRESS) { rotate( 0.35*frame_time ); need_view_synch = true; }else if(buttons[2]==GLFW_PRESS) { rotate( -0.35*frame_time ); need_view_synch = true; } //printf("x:%f, y:%f\n", xJoyPos, yJoyPos); } // zooming using the mouse scroll mouseWheelDelta = glfwGetMouseWheel() - mouseWheel; mouseWheel = mouseWheel+mouseWheelDelta; if(mouseWheelDelta != 0) { zoomIn(mouseWheelDelta*scroll_zoom_factor*(frame_time*60.0)); need_view_synch = true; } //printf("mouse wheel: %d\n", mouseWheelDelta); if(need_view_synch) { synchVariableUniforms(); need_view_synch = false; } // update the time inside the shader for cool animations. parameters[2].val.fval = frame_start; parameters[2].needs_update = true; synchParameters ( parameters ); //glUniform1f(timeLoc, frame_start); // write fps and iterations to a string. sprintf(text, "FPS: %4.1d; ITER: %d; ZOOMX: %-10.1f", (int)(floor(1.0/frame_time)), iterations, (3.0/deltax)); //sprintf("fps %f\n", 1.0/frame_time); // clear the buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //since all calculations are being done in the fragment shader, all we draw is a surface. glUseProgram(program); glRects(-1, -1, 1, 1); // render font on top of this? if(show_fps != 0){ glUseProgram(0); glColor3f(0.0f, 0.0f, 0.0f); ftglRenderFont(font, text, FTGL_RENDER_ALL); glUseProgram(program); } // note: swap buffers also updates the input events for glfw glfwSwapBuffers(); } // clean up the font and exit. ftglDestroyFont(font); return 1; }