void motion(int x, int y) { Real32 w = win->getWidth(), h = win->getHeight(); Real32 a = -2. * ( lastx / w - .5 ), b = -2. * ( .5 - lasty / h ), c = -2. * ( x / w - .5 ), d = -2. * ( .5 - y / h ); if ( mouseb & ( 1 << GLUT_LEFT_BUTTON ) ) { tball.updateRotation( a, b, c, d ); } else if ( mouseb & ( 1 << GLUT_MIDDLE_BUTTON ) ) { tball.updatePosition( a, b, c, d ); } else if ( mouseb & ( 1 << GLUT_RIGHT_BUTTON ) ) { tball.updatePositionNeg( a, b, c, d ); } lastx = x; lasty = y; }
Window2D::Window2D(WindowPtr ptr) : _ptr(ptr) { _rend = SDL_CreateRenderer(_ptr->getSDLWindow(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); Image img(glm::ivec2(ptr->getWidth(), ptr->getHeight())); SDL_SetRenderDrawColor(_rend, 0, 255, 255, 255); SDL_RenderClear(_rend); }
boolean RendererD3D11::assembleUnit() { ProcessingUnitPtr windowUnit = ProcessingUnitPtr::null; window->getData( windowUnit ); if( !windowUnit.isNull() ) { WindowPtr wnd = staticCast<Window>( windowUnit ); RenderDeviceD3D11* rd = static_cast<RenderDeviceD3D11*>( owner ); IDXGIFactory* dxgiFactory = rd->getDXGIFactory(); DXGI_SWAP_CHAIN_DESC desc; desc.BufferCount = 1; desc.BufferDesc.Width = wnd->getWidth(); desc.BufferDesc.Height = wnd->getHeight(); desc.BufferDesc.Format = MapperD3D11::mapPixelFormat( Render::PF_RGBA8UN ); desc.BufferDesc.RefreshRate.Numerator = 60; desc.BufferDesc.RefreshRate.Denominator = 1; desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; desc.OutputWindow = (HWND)wnd->getHandle(); desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Windowed = !wnd->isFullscreen(); desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; IDXGISwapChain* sc = 0; HRESULT hr = dxgiFactory->CreateSwapChain( rd->getD3DDevice(), &desc, &sc ); if( SUCCEEDED( hr ) ) { swapchains.add( sc ); wnd->setSwapChain( sc ); return true; } } return false; }
/** get graphics performance * * This is a rough estimation of rendering costst for visible faces, * faces outside of the viewport and size dependent rendering costs. * <pre> * // face cost calculation * cost = invisible * invisibleFaceCost + * max( visible * visibleFaceCost + * pixel * pixelCost) * </pre> * **/ void RenderNode::determinePerformance( WindowPtr &window ) { int c; double faces=0; double A,B,C; double t; setVendor((const char*)glGetString(GL_VENDOR)); setRenderer((const char*)glGetString(GL_RENDERER)); // try to find precalculated values for(c=0;_prefefined[c]!=NULL;++c) { if(_prefefined[c]->getVendor() == getVendor() && _prefefined[c]->getRenderer() == getRenderer()) { SLOG << "Predefined performance values used." << endl; *this=*_prefefined[c]; return; } } SLOG << "Start rendering benchmark" << endl; window->activate(); // create display list GLuint dList1 = glGenLists(1); glNewList(dList1, GL_COMPILE); float step = .1; int count = 400; for(float y=0;y<(1-step/2);y+=step) { glBegin(GL_TRIANGLE_STRIP); glVertex3f(0,y ,-1); glVertex3f(0,y+step,-1); for(float x=step;x<(1+step/2);x+=step) { glVertex3f(x,y ,-1); glVertex3f(x,y+step,-1); faces+=2; } glEnd(); } glEndList(); glFinish(); GLuint dList2 = glGenLists(1); glNewList(dList2, GL_COMPILE); for(c=0;c<count;++c) { glCallList(dList1); } glEndList(); glFlush(); t=runFaceBench(dList2,128,128,1.0); count=(int)(count/t); glNewList(dList2, GL_COMPILE); for(c=0;c<count;++c) { glCallList(dList1); } glEndList(); runFaceBench(dList2,1,1,1.0); glFinish(); Real32 aSize=2; Real32 bSize=2; Real32 cSize=128; do { for(A=0,c=0;A<1.0;++c) { A += runFaceBench(dList2,aSize,aSize,1.000); } A/=c*count; for(B=0,c=0;B<1.0;++c) { B += runFaceBench(dList2,bSize,bSize,0.001); } B/=c*count; C = runFaceBench(dList2,cSize,cSize,1.000)/count; } while(A>C); _visibleFaceCost =A/faces; _invisibleFaceCost =B/faces; _drawPixelCost =C/(cSize*cSize); glViewport(0, 0, window->getWidth(), window->getHeight()); UInt32 width,height; // test write performance glPixelStorei(GL_PACK_ALIGNMENT,1); glPixelStorei(GL_UNPACK_ALIGNMENT,1); vector<UInt8> pixels; width =window->getWidth(); height=window->getHeight(); pixels.resize(width*height*4); glFlush(); t=-getSystemTime(); for(c=0;c<2;++c) glReadPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,&pixels[0]); glFlush(); t+=getSystemTime(); _readPixelCost=t/(c*width*height); // test write performance glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0,width,0,height); glRasterPos2i(0,0); glDisable(GL_DEPTH_TEST); glFlush(); t=-getSystemTime(); for(c=0;c<2;++c) glDrawPixels(width,height,GL_RGB,GL_UNSIGNED_BYTE,&pixels[0]); glFlush(); t+=getSystemTime(); _writePixelCost=t/(c*width*height); glEnable(GL_DEPTH_TEST); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); SLOG << "End rendering benchmark" << endl; glDeleteLists(dList2,1); glDeleteLists(dList1,1); }
void MultiDisplayWindow::serverRender(WindowPtr serverWindow, UInt32 id, DrawActionBase *action ) { TileCameraDecoratorPtr deco; ViewportPtr serverPort; ViewportPtr clientPort; StereoBufferViewportPtr clientStereoPort; UInt32 sv,cv; Int32 l,r,t,b; Int32 cleft,cright,ctop,cbottom; // sync, otherwise viewports will be out of date if(!getHServers()) { setHServers(getServers().size()); } if(!getVServers()) { setVServers(1); } UInt32 row =id/getHServers(); UInt32 column=id%getHServers(); // calculate width and height from local width and height UInt32 width = serverWindow->getWidth() ; UInt32 height = serverWindow->getHeight(); if(getWidth()==0) { setWidth( width*getHServers() ); } if(getHeight()==0) { setHeight( height*getVServers() ); } Int32 left = column * width - column * getXOverlap(); Int32 bottom = row * height - row * getYOverlap(); Int32 right = left + width - 1; Int32 top = bottom + height - 1; Real64 scaleCWidth = ((width - getXOverlap()) * (getHServers() - 1) + width) / (float)getWidth(); Real64 scaleCHeight = ((height - getYOverlap())* (getVServers() - 1) + height)/ (float)getHeight(); bool isVirtualPort = false; // duplicate viewports for(cv = 0, sv = 0; cv < getPort().size(); ++cv) { clientPort = getPort()[cv]; #if 0 isVirtualPort = clientPort->getType().isDerivedFrom(FBOViewport::getClassType()); if(isVirtualPort) { // TODO -- seems wrong to render this on all servers, though rendering // then transmitting the texture doesn't seem like a good idea either. if(serverWindow->getPort().size() <= sv) { serverPort = ViewportPtr::dcast(clientPort->shallowCopy()); beginEditCP(serverWindow); serverWindow->addPort(serverPort); endEditCP(serverWindow); } else { serverPort = serverWindow->getPort()[sv]; if(serverWindow->getPort()[sv]->getType() != clientPort->getType()) { // there is a viewport with the wrong type subRefCP(serverWindow->getPort()[sv]); serverPort = ViewportPtr::dcast(clientPort->shallowCopy()); beginEditCP(serverWindow); { serverWindow->getPort()[sv] = serverPort; } endEditCP(serverWindow); } } // update changed viewport fields updateViewport(serverPort,clientPort); } else #endif { clientStereoPort = dynamic_cast<StereoBufferViewportPtr>(clientPort); cleft = (Int32)(clientPort->getPixelLeft() * scaleCWidth) ; cbottom = (Int32)(clientPort->getPixelBottom() * scaleCHeight) ; cright = (Int32)((clientPort->getPixelRight()+1) * scaleCWidth) -1; ctop = (Int32)((clientPort->getPixelTop()+1) * scaleCHeight)-1; if(cright < left || cleft > right || ctop < bottom || cbottom > top ) { // invisible on this server screen continue; } // calculate overlapping viewport l = osgMax(cleft ,left ) - left; b = osgMax(cbottom,bottom) - bottom; r = osgMin(cright ,right ) - left; t = osgMin(ctop ,top ) - bottom; if(serverWindow->getPort().size() <= sv) { serverPort = dynamic_cast<ViewportPtr>(clientPort->shallowCopy()); deco = TileCameraDecorator::create(); serverWindow->addPort(serverPort); serverPort->setCamera(deco); } else { serverPort = serverWindow->getPort()[sv]; deco = dynamic_cast<TileCameraDecoratorPtr>( serverPort->getCamera()); if(serverWindow->getPort()[sv]->getType() != clientPort->getType()) { // there is a viewport with the wrong type serverPort = dynamic_cast<ViewportPtr>(clientPort->shallowCopy()); serverWindow->replacePort(sv, serverPort);//[sv] = serverPort; serverPort->setCamera(deco); } else { deco = dynamic_cast<TileCameraDecoratorPtr>( serverPort->getCamera()); } } // update changed viewport fields updateViewport(serverPort,clientPort); // set viewport size serverPort->setSize(Real32(l),Real32(b),Real32(r),Real32(t)); // use pixel even if pixel = 1 if(serverPort->getLeft() == 1.0) serverPort->setLeft(1.0001); if(serverPort->getRight() == 1.0) serverPort->setRight(1.0001); if(serverPort->getTop() == 1.0) serverPort->setTop(1.0001); if(serverPort->getBottom() == 1.0) serverPort->setBottom(1.0001); // calculate tile parameters deco->setFullWidth ( cright-cleft ); deco->setFullHeight( ctop-cbottom ); deco->setSize( ( l+left-cleft ) / (float)( cright-cleft ), ( b+bottom-cbottom ) / (float)( ctop-cbottom ), ( r+left-cleft ) / (float)( cright-cleft ), ( t+bottom-cbottom ) / (float)( ctop-cbottom ) ); deco->setDecoratee( clientPort->getCamera() ); } sv++; } // remove unused ports while(serverWindow->getPort().size()>sv) { serverWindow->subPort(sv); } Inherited::serverRender(serverWindow,id,action); }
// Setup default render states static void setupOpenGL(){ //first try to init extensions: if (glewInit() != GLEW_OK || !GLEW_ARB_multitexture || !GLEW_ARB_vertex_buffer_object){ throw std::runtime_error("error loading OpenGL extensions using GLEW"); } glClearColor(0.3f, 0.4f, 0.5f, 1.0f); glClearDepth(1.0f); if(gLinePolygonMode) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_DEPTH_TEST); if(gLinePolygonMode || gDrawBothFaces){ glDisable(GL_CULL_FACE); } else{ glEnable(GL_CULL_FACE); } glViewport(0, 0, gWindow->getWidth(), gWindow->getHeight()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f, ((float)gWindow->getWidth()/(float)gWindow->getHeight()), 1.0f, 10000.0f); float ambientColor[] = {0.3f, 0.3f, 0.3f, 1.0f}; float diffuseColor[] = {0.9f, 0.9f, 0.9f, 1.0f}; float specularColor[] = {0.0f, 0.0f, 0.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor); glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor); glLightfv(GL_LIGHT0, GL_POSITION, gLightPosition); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); glEnable(GL_LIGHT0); if(gLight)glEnable(GL_LIGHTING); glDepthFunc(GL_LEQUAL); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glShadeModel(GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); glEnable(GL_NORMALIZE); glActiveTextureARB(GL_TEXTURE0_ARB); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //set up tex. coord. generator for reflection map (1 tex. unit) glActiveTextureARB(GL_TEXTURE1_ARB); glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glDisable(GL_TEXTURE_2D); glActiveTextureARB(GL_TEXTURE0_ARB); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //drop old lists and textures(if any) BOOST_FOREACH(VarrayIds varId, gVarrays){ glDeleteBuffersARB(1, &varId.VBO); glDeleteBuffersARB(1, &varId.IBO); }