bool RCViewableTransform::unitViewportCoordinates(xuint32 x, xuint32 y, float &xUnit, float &yUnit) const { xint32 adjX = x - viewportX(); xint32 adjY = y - viewportY(); if(adjX < 0 || adjY < 0 || adjX > (xint32)viewportWidth() || adjY > (xint32)viewportHeight()) { xUnit = 0.0f; yUnit = 0.0f; return false; } xUnit = (float)adjX/viewportWidth(); yUnit = (float)adjY/viewportHeight(); xUnit *= 2.0f; yUnit *= 2.0f; xUnit -= 1.0f; yUnit -= 1.0f; // viewport units are in positive down, flip to posisitve up yUnit *= -1.0f; return true; }
void Camera::getViewport( int* x, int* y, int* width, int* height ) const { if (x) *x = m_x; if (y) *y = m_y; if (width) *width = viewportWidth(); if (height) *height = viewportHeight(); }
void fillViewportLocationFromViewport(SEXP vp, LViewportLocation *vpl) { vpl->x = viewportX(vp); vpl->y = viewportY(vp); vpl->width = viewportWidth(vp); vpl->height = viewportHeight(vp); vpl->hjust = viewportHJust(vp); vpl->vjust = viewportVJust(vp); }
void LayerRendererChromium::viewportChanged() { if (m_context) m_context->reshape(std::max(1, viewportWidth()), std::max(1, viewportHeight())); // Reset the current render surface to force an update of the viewport and // projection matrix next time useRenderSurface is called. m_currentRenderSurface = 0; }
void RCViewableTransform::approximatePixelSizeAtDistance(float distanceFromCamera, float &xScale, float &yScale) const { xuint32 x = viewportX() + ((float)viewportWidth()/2.0f); xuint32 y = viewportY() + ((float)viewportHeight()/2.0f); Eks::Vector3D a = worldSpaceAtDepthFromScreenSpace(x, y, distanceFromCamera); Eks::Vector3D b = worldSpaceAtDepthFromScreenSpace(x+1, y, distanceFromCamera); Eks::Vector3D c = worldSpaceAtDepthFromScreenSpace(x, y+1, distanceFromCamera); xScale = (a - b).norm(); yScale = (a - c).norm(); }
void LayerRendererChromium::getFramebufferPixels(void *pixels, const IntRect& rect) { ASSERT(rect.maxX() <= viewportWidth() && rect.maxY() <= viewportHeight()); if (!pixels) return; makeContextCurrent(); GLC(m_context.get(), m_context->readPixels(rect.x(), rect.y(), rect.width(), rect.height(), GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE, pixels)); }
bool RCViewableTransform::screenViewportCoordinates(float xUnit, float yUnit, float &x, float &y) const { xUnit += 1.0f; yUnit += 1.0f; xUnit /= 2.0f; yUnit /= -2.0f; xUnit *= viewportWidth(); yUnit *= viewportHeight(); x = viewportX() + xUnit; y = viewportY() + yUnit; return true; }
void Camera::render() { synchronized( s_globalMutex ); // - prepare scene rendering // - render objects in n passes // - update statistics // - reset temporaries gd::GraphicsDevice* dev = Context::device(); int renderedPrimitives = dev->renderedPrimitives(); int renderedTriangles = dev->renderedTriangles(); int materialChanges = dev->materialChanges(); // - DEBUG: warn if we have non-square pixels Matrix4x4 proj = projectionTransform(); Vector4 cp( 1,1,10,1 ); Vector4 pp = (proj * cp); pp *= 1.f / pp.w; pp.x = pp.x * (viewportWidth()*.5f); pp.y = pp.y * (viewportHeight()*.5f); if ( Math::abs(pp.x-pp.y) > 1.f ) Debug::println( "Non-square pixels! (fov={0}, w={1}, h={2})", Math::toDegrees(horizontalFov()), viewportWidth(), viewportHeight() ); prepareRender(); // - render objects in n passes // pass 1<<0: solid objects which are affected by shadows // pass 1<<1: (unused) // pass 1<<2: shadow volumes // pass 1<<3: shadow filler polygon // pass 1<<4: solid objects which are not affected by shadows // pass 1<<5: transparent objects which are not affected by shadows // the first pass is front to back... //Debug::println( "{0}({1})", __FILE__, __LINE__ ); {//dev::Profile pr( "Camera.render( pass 1 )" ); int i; for ( i = 0 ; i < (int)s_objs.size() ; ++i ) { Node* obj = s_objs[i]; //Debug::println( "rendering {0}({1})", obj->name(), i ); obj->render( this, 1 ); } } // ...and the rest back to front //Debug::println( "{0}({1})", __FILE__, __LINE__ ); {//dev::Profile pr( "Camera.render( other passes )" ); for ( int pass = 2 ; pass <= LAST_RENDERING_PASS ; pass <<= 1 ) { for ( int i = (int)s_objs.size() ; i-- > 0 ; ) { Node* obj = s_objs[i]; obj->render( this, pass ); } } } // - update statistics m_renderedPrimitives += dev->renderedPrimitives() - renderedPrimitives; m_renderedTriangles += dev->renderedTriangles() - renderedTriangles; m_materialChanges += dev->materialChanges() - materialChanges; // - reset temporaries m_worldToCamera = Matrix4x4(0); dev->setViewport( 0, 0, dev->width(), dev->height() ); //Debug::println( "{0}({1})", __FILE__, __LINE__ ); }
void Camera::prepareRender() { //Profile pr( "camera.prepareRender" ); // - update transform hierarchy // - find out front and back plane distances // - collect visible objects and lights // - update node visibility // - sort visible objects by ascending distance // - set viewport, view- and projection transformation // - add affecting lights to rendering device // - set fog and ambient if any // - update transform hierarchy Node* root = this->root(); Scene* scene = dynamic_cast<Scene*>( root ); root->validateHierarchy(); updateCachedTransforms(); Vector3 camWorldPos = cachedWorldTransform().translation(); Vector3 camWorldDir = cachedWorldTransform().rotation().getColumn(2); // - collect visible objects and lights s_objs.clear(); s_lights.clear(); for ( Node* obj = root ; obj ; ) { //assert( obj->name().length() > 0 ); obj->m_flags &= ~NODE_RENDEREDINLASTFRAME; if ( obj->enabled() ) { if ( obj->renderable() ) { Light* light = dynamic_cast<Light*>( obj ); if ( light ) { s_lights.add( light ); ++m_renderedLights; } else { obj->m_distanceToCamera = obj->boundSphere() + (obj->m_worldTransform.translation() - camWorldPos).dot( camWorldDir ); if ( obj->updateVisibility(this) ) { obj->m_flags |= NODE_RENDEREDINLASTFRAME; s_objs.add( obj ); ++m_renderedObjects; } } } } ++m_processedObjects; obj = obj->nextInHierarchy( Node::NODE_ENABLED ); } // - sort visible objects by ascending distance std::sort( s_objs.begin(), s_objs.end(), NodeDistanceToCameraLess() ); // - set viewport, view- and projection transformation gd::GraphicsDevice* dev = Context::device(); dev->setViewport( m_x, m_y, viewportWidth(), viewportHeight() ); dev->setViewTransform( m_worldToCamera ); dev->setProjectionTransform( projectionTransform() ); // - add affecting lights to rendering device dev->removeLights(); for ( int i = 0 ; i < (int)s_lights.size() ; ++i ) s_lights[i]->apply(); // - set fog and ambient if any if ( scene ) { setFog( dev, scene ); Colorf amb = Colorf( scene->ambientColor() ); dev->setAmbient( Color(amb) ); } }
void LayerRendererChromium::drawLayersInternal() { if (viewportSize().isEmpty() || !rootLayer()) return; TRACE_EVENT("LayerRendererChromium::drawLayers", this, 0); CCLayerImpl* rootDrawLayer = rootLayer(); makeContextCurrent(); if (!rootDrawLayer->renderSurface()) rootDrawLayer->createRenderSurface(); rootDrawLayer->renderSurface()->setContentRect(IntRect(IntPoint(), viewportSize())); rootDrawLayer->setScissorRect(IntRect(IntPoint(), viewportSize())); CCLayerList renderSurfaceLayerList; renderSurfaceLayerList.append(rootDrawLayer); m_defaultRenderSurface = rootDrawLayer->renderSurface(); m_defaultRenderSurface->clearLayerList(); { TRACE_EVENT("LayerRendererChromium::drawLayersInternal::calcDrawEtc", this, 0); CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(rootDrawLayer, rootDrawLayer, m_zoomAnimatorTransform, m_zoomAnimatorTransform, renderSurfaceLayerList, m_defaultRenderSurface->layerList(), &m_layerSorter, m_capabilities.maxTextureSize); } // The GL viewport covers the entire visible area, including the scrollbars. GLC(m_context.get(), m_context->viewport(0, 0, viewportWidth(), viewportHeight())); m_windowMatrix = screenMatrix(0, 0, viewportWidth(), viewportHeight()); // Bind the common vertex attributes used for drawing all the layers. m_sharedGeometry->prepareForDraw(); GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST)); GLC(m_context.get(), m_context->disable(GraphicsContext3D::DEPTH_TEST)); GLC(m_context.get(), m_context->disable(GraphicsContext3D::CULL_FACE)); useRenderSurface(m_defaultRenderSurface); if (m_zoomAnimatorTransform.isIdentity()) // Clear to blue to make it easier to spot unrendered regions. m_context->clearColor(0, 0, 1, 1); else // Clear to grey, as zoom animation may leave unrendered regions. // FIXME(wjmaclean): Render some interesting texture in unrendered regions. m_context->clearColor(0.25, 0.25, 0.25, 1); m_context->colorMask(true, true, true, true); m_context->clear(GraphicsContext3D::COLOR_BUFFER_BIT); GLC(m_context.get(), m_context->enable(GraphicsContext3D::BLEND)); GLC(m_context.get(), m_context->blendFunc(GraphicsContext3D::ONE, GraphicsContext3D::ONE_MINUS_SRC_ALPHA)); GLC(m_context.get(), m_context->enable(GraphicsContext3D::SCISSOR_TEST)); // Update the contents of the render surfaces. We traverse the array from // back to front to guarantee that nested render surfaces get rendered in the // correct order. for (int surfaceIndex = renderSurfaceLayerList.size() - 1; surfaceIndex >= 0 ; --surfaceIndex) { CCLayerImpl* renderSurfaceLayer = renderSurfaceLayerList[surfaceIndex].get(); CCRenderSurface* renderSurface = renderSurfaceLayer->renderSurface(); ASSERT(renderSurface); renderSurface->setSkipsDraw(true); // Render surfaces whose drawable area has zero width or height // will have no layers associated with them and should be skipped. if (!renderSurface->layerList().size()) continue; // Skip completely transparent render surfaces. if (!renderSurface->drawOpacity()) continue; if (useRenderSurface(renderSurface)) { renderSurface->setSkipsDraw(false); if (renderSurfaceLayer != rootDrawLayer) { GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST)); GLC(m_context.get(), m_context->clearColor(0, 0, 0, 0)); GLC(m_context.get(), m_context->clear(GraphicsContext3D::COLOR_BUFFER_BIT)); GLC(m_context.get(), m_context->enable(GraphicsContext3D::SCISSOR_TEST)); } const CCLayerList& layerList = renderSurface->layerList(); for (unsigned layerIndex = 0; layerIndex < layerList.size(); ++layerIndex) drawLayer(layerList[layerIndex].get(), renderSurface); } } if (m_headsUpDisplay->enabled()) { GLC(m_context.get(), m_context->enable(GraphicsContext3D::BLEND)); GLC(m_context.get(), m_context->blendFunc(GraphicsContext3D::ONE, GraphicsContext3D::ONE_MINUS_SRC_ALPHA)); GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST)); useRenderSurface(m_defaultRenderSurface); m_headsUpDisplay->draw(); } GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST)); GLC(m_context.get(), m_context->disable(GraphicsContext3D::BLEND)); }