void StateManagerGL::setClearState(const gl::State &state, GLbitfield mask) { // Only apply the state required to do a clear setScissor(state.getScissor()); setViewport(state.getViewport()); if ((mask & GL_COLOR_BUFFER_BIT) != 0) { setClearColor(state.getColorClearValue()); const gl::BlendState &blendState = state.getBlendState(); setColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, blendState.colorMaskBlue, blendState.colorMaskAlpha); } if ((mask & GL_DEPTH_BUFFER_BIT) != 0) { setClearDepth(state.getDepthClearValue()); setDepthMask(state.getDepthStencilState().depthMask); } if ((mask & GL_STENCIL_BUFFER_BIT) != 0) { setClearStencil(state.getStencilClearValue()); setStencilMask(state.getDepthStencilState().stencilMask); } }
void Camera::setOptionalsToDefaults() { GeometricalTransformation::setOptionalsToDefaults(); ProjectionTransformation::setOptionalsToDefaults(); setScissor( vgm::Rectangle2i(0, 0, 1600, 1200) ); setAspect( (1) ); setZNear( (0.01f) ); setZFar( (3996.f) ); }
GLState &GLState::setNormalizedScissor(Rectanglef const &normScissorRect) { Rectangleui vp = viewport(); Rectangleui scis(Vector2ui(normScissorRect.left() * vp.width(), normScissorRect.top() * vp.height()), Vector2ui(std::ceil(normScissorRect.right() * vp.width()), std::ceil(normScissorRect.bottom() * vp.height()))); return setScissor(scis); }
void OpenGL::setViewport(const OpenGL::Viewport &v) { glViewport(v.x, v.y, v.w, v.h); state.viewport = v; // glScissor starts from the lower left, so we compensate when setting the // scissor. When the viewport is changed, we need to manually update the // scissor again. setScissor(state.scissor); }
void VkPPRenderState::RenderScreenQuad(VkPPRenderPassSetup *passSetup, VulkanDescriptorSet *descriptorSet, VulkanFramebuffer *framebuffer, int framebufferWidth, int framebufferHeight, int x, int y, int width, int height, const void *pushConstants, uint32_t pushConstantsSize, bool stencilTest) { auto fb = GetVulkanFrameBuffer(); auto cmdbuffer = fb->GetDrawCommands(); VkViewport viewport = { }; viewport.x = x; viewport.y = y; viewport.width = width; viewport.height = height; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; VkRect2D scissor = { }; scissor.offset.x = 0; scissor.offset.y = 0; scissor.extent.width = framebufferWidth; scissor.extent.height = framebufferHeight; RenderPassBegin beginInfo; beginInfo.setRenderPass(passSetup->RenderPass.get()); beginInfo.setRenderArea(0, 0, framebufferWidth, framebufferHeight); beginInfo.setFramebuffer(framebuffer); beginInfo.addClearColor(0.0f, 0.0f, 0.0f, 1.0f); VkBuffer vertexBuffers[] = { static_cast<VKVertexBuffer*>(screen->mVertexData->GetBufferObjects().first)->mBuffer->buffer }; VkDeviceSize offsets[] = { 0 }; cmdbuffer->beginRenderPass(beginInfo); cmdbuffer->bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, passSetup->Pipeline.get()); cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, passSetup->PipelineLayout.get(), 0, descriptorSet); cmdbuffer->bindVertexBuffers(0, 1, vertexBuffers, offsets); cmdbuffer->setViewport(0, 1, &viewport); cmdbuffer->setScissor(0, 1, &scissor); if (stencilTest) cmdbuffer->setStencilReference(VK_STENCIL_FRONT_AND_BACK, screen->stencilValue); if (pushConstantsSize > 0) cmdbuffer->pushConstants(passSetup->PipelineLayout.get(), VK_SHADER_STAGE_FRAGMENT_BIT, 0, pushConstantsSize, pushConstants); cmdbuffer->draw(4, 1, FFlatVertexBuffer::PRESENT_INDEX, 0); cmdbuffer->endRenderPass(); }
void StateManagerGL::syncState(const gl::State &state, const gl::State::DirtyBits &dirtyBits) { // TODO(jmadill): Investigate only syncing vertex state for active attributes for (auto dirtyBit : angle::IterateBitSet(dirtyBits | mLocalDirtyBits)) { switch (dirtyBit) { case gl::State::DIRTY_BIT_SCISSOR_TEST_ENABLED: setScissorTestEnabled(state.isScissorTestEnabled()); break; case gl::State::DIRTY_BIT_SCISSOR: setScissor(state.getScissor()); break; case gl::State::DIRTY_BIT_VIEWPORT: setViewport(state.getViewport()); break; case gl::State::DIRTY_BIT_DEPTH_RANGE: setDepthRange(state.getNearPlane(), state.getFarPlane()); break; case gl::State::DIRTY_BIT_BLEND_ENABLED: setBlendEnabled(state.isBlendEnabled()); break; case gl::State::DIRTY_BIT_BLEND_COLOR: setBlendColor(state.getBlendColor()); break; case gl::State::DIRTY_BIT_BLEND_FUNCS: { const auto &blendState = state.getBlendState(); setBlendFuncs(blendState.sourceBlendRGB, blendState.destBlendRGB, blendState.sourceBlendAlpha, blendState.destBlendAlpha); break; } case gl::State::DIRTY_BIT_BLEND_EQUATIONS: { const auto &blendState = state.getBlendState(); setBlendEquations(blendState.blendEquationRGB, blendState.blendEquationAlpha); break; } case gl::State::DIRTY_BIT_COLOR_MASK: { const auto &blendState = state.getBlendState(); setColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, blendState.colorMaskBlue, blendState.colorMaskAlpha); break; } case gl::State::DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED: setSampleAlphaToCoverageEnabled(state.isSampleAlphaToCoverageEnabled()); break; case gl::State::DIRTY_BIT_SAMPLE_COVERAGE_ENABLED: setSampleCoverageEnabled(state.isSampleCoverageEnabled()); break; case gl::State::DIRTY_BIT_SAMPLE_COVERAGE: setSampleCoverage(state.getSampleCoverageValue(), state.getSampleCoverageInvert()); break; case gl::State::DIRTY_BIT_DEPTH_TEST_ENABLED: setDepthTestEnabled(state.isDepthTestEnabled()); break; case gl::State::DIRTY_BIT_DEPTH_FUNC: setDepthFunc(state.getDepthStencilState().depthFunc); break; case gl::State::DIRTY_BIT_DEPTH_MASK: setDepthMask(state.getDepthStencilState().depthMask); break; case gl::State::DIRTY_BIT_STENCIL_TEST_ENABLED: setStencilTestEnabled(state.isStencilTestEnabled()); break; case gl::State::DIRTY_BIT_STENCIL_FUNCS_FRONT: { const auto &depthStencilState = state.getDepthStencilState(); setStencilFrontFuncs(depthStencilState.stencilFunc, state.getStencilRef(), depthStencilState.stencilMask); break; } case gl::State::DIRTY_BIT_STENCIL_FUNCS_BACK: { const auto &depthStencilState = state.getDepthStencilState(); setStencilBackFuncs(depthStencilState.stencilBackFunc, state.getStencilBackRef(), depthStencilState.stencilBackMask); break; } case gl::State::DIRTY_BIT_STENCIL_OPS_FRONT: { const auto &depthStencilState = state.getDepthStencilState(); setStencilFrontOps(depthStencilState.stencilFail, depthStencilState.stencilPassDepthFail, depthStencilState.stencilPassDepthPass); break; } case gl::State::DIRTY_BIT_STENCIL_OPS_BACK: { const auto &depthStencilState = state.getDepthStencilState(); setStencilBackOps(depthStencilState.stencilBackFail, depthStencilState.stencilBackPassDepthFail, depthStencilState.stencilBackPassDepthPass); break; } case gl::State::DIRTY_BIT_STENCIL_WRITEMASK_FRONT: setStencilFrontWritemask(state.getDepthStencilState().stencilWritemask); break; case gl::State::DIRTY_BIT_STENCIL_WRITEMASK_BACK: setStencilBackWritemask(state.getDepthStencilState().stencilBackWritemask); break; case gl::State::DIRTY_BIT_CULL_FACE_ENABLED: setCullFaceEnabled(state.isCullFaceEnabled()); break; case gl::State::DIRTY_BIT_CULL_FACE: setCullFace(state.getRasterizerState().cullMode); break; case gl::State::DIRTY_BIT_FRONT_FACE: setFrontFace(state.getRasterizerState().frontFace); break; case gl::State::DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED: setPolygonOffsetFillEnabled(state.isPolygonOffsetFillEnabled()); break; case gl::State::DIRTY_BIT_POLYGON_OFFSET: { const auto &rasterizerState = state.getRasterizerState(); setPolygonOffset(rasterizerState.polygonOffsetFactor, rasterizerState.polygonOffsetUnits); break; } case gl::State::DIRTY_BIT_RASTERIZER_DISCARD_ENABLED: setRasterizerDiscardEnabled(state.isRasterizerDiscardEnabled()); break; case gl::State::DIRTY_BIT_LINE_WIDTH: setLineWidth(state.getLineWidth()); break; case gl::State::DIRTY_BIT_PRIMITIVE_RESTART_ENABLED: setPrimitiveRestartEnabled(state.isPrimitiveRestartEnabled()); break; case gl::State::DIRTY_BIT_CLEAR_COLOR: setClearColor(state.getColorClearValue()); break; case gl::State::DIRTY_BIT_CLEAR_DEPTH: setClearDepth(state.getDepthClearValue()); break; case gl::State::DIRTY_BIT_CLEAR_STENCIL: setClearStencil(state.getStencilClearValue()); break; case gl::State::DIRTY_BIT_UNPACK_ALIGNMENT: // TODO(jmadill): split this setPixelUnpackState(state.getUnpackState()); break; case gl::State::DIRTY_BIT_UNPACK_ROW_LENGTH: // TODO(jmadill): split this setPixelUnpackState(state.getUnpackState()); break; case gl::State::DIRTY_BIT_UNPACK_IMAGE_HEIGHT: // TODO(jmadill): split this setPixelUnpackState(state.getUnpackState()); break; case gl::State::DIRTY_BIT_UNPACK_SKIP_IMAGES: // TODO(jmadill): split this setPixelUnpackState(state.getUnpackState()); break; case gl::State::DIRTY_BIT_UNPACK_SKIP_ROWS: // TODO(jmadill): split this setPixelUnpackState(state.getUnpackState()); break; case gl::State::DIRTY_BIT_UNPACK_SKIP_PIXELS: // TODO(jmadill): split this setPixelUnpackState(state.getUnpackState()); break; case gl::State::DIRTY_BIT_PACK_ALIGNMENT: // TODO(jmadill): split this setPixelPackState(state.getPackState()); break; case gl::State::DIRTY_BIT_PACK_REVERSE_ROW_ORDER: // TODO(jmadill): split this setPixelPackState(state.getPackState()); break; case gl::State::DIRTY_BIT_PACK_ROW_LENGTH: // TODO(jmadill): split this setPixelPackState(state.getPackState()); break; case gl::State::DIRTY_BIT_PACK_SKIP_ROWS: // TODO(jmadill): split this setPixelPackState(state.getPackState()); break; case gl::State::DIRTY_BIT_PACK_SKIP_PIXELS: // TODO(jmadill): split this setPixelPackState(state.getPackState()); break; case gl::State::DIRTY_BIT_DITHER_ENABLED: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_GENERATE_MIPMAP_HINT: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_SHADER_DERIVATIVE_HINT: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_READ_FRAMEBUFFER_BINDING: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_READ_FRAMEBUFFER_OBJECT: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_DRAW_FRAMEBUFFER_OBJECT: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_RENDERBUFFER_BINDING: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_VERTEX_ARRAY_BINDING: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_VERTEX_ARRAY_OBJECT: state.getVertexArray()->syncImplState(); break; case gl::State::DIRTY_BIT_PROGRAM_BINDING: // TODO(jmadill): implement this break; case gl::State::DIRTY_BIT_PROGRAM_OBJECT: // TODO(jmadill): implement this break; default: { ASSERT(dirtyBit >= gl::State::DIRTY_BIT_CURRENT_VALUE_0 && dirtyBit < gl::State::DIRTY_BIT_CURRENT_VALUE_MAX); size_t attribIndex = static_cast<size_t>(dirtyBit) - gl::State::DIRTY_BIT_CURRENT_VALUE_0; setAttributeCurrentData(attribIndex, state.getVertexAttribCurrentValue( static_cast<unsigned int>(attribIndex))); break; } } mLocalDirtyBits.reset(); } }
void OpenGL::setViewport(const Viewport &viewport) { this->viewport = viewport; glViewport(this->viewport.x, this->viewport.y, this->viewport.width, this->viewport.height); setScissor(this->scissor); }
gl::Error StateManagerGL::setGenericDrawState(const gl::Data &data) { const gl::State &state = *data.state; const gl::Caps &caps = *data.caps; const gl::Program *program = state.getProgram(); const ProgramGL *programGL = GetImplAs<ProgramGL>(program); useProgram(programGL->getProgramID()); // TODO: Only apply textures referenced by the program. for (auto textureTypeIter = mTextures.begin(); textureTypeIter != mTextures.end(); textureTypeIter++) { GLenum textureType = textureTypeIter->first; // Determine if this texture type can exist in the source context bool validTextureType = (textureType == GL_TEXTURE_2D || textureType == GL_TEXTURE_CUBE_MAP || (textureType == GL_TEXTURE_2D_ARRAY && data.clientVersion >= 3) || (textureType == GL_TEXTURE_3D && data.clientVersion >= 3)); const std::vector<GLuint> &textureVector = textureTypeIter->second; for (size_t textureUnitIndex = 0; textureUnitIndex < textureVector.size(); textureUnitIndex++) { const gl::Texture *texture = nullptr; bool validTextureUnit = textureUnitIndex < caps.maxCombinedTextureImageUnits; if (validTextureType && validTextureUnit) { texture = state.getSamplerTexture(textureUnitIndex, textureType); } if (texture != nullptr) { const TextureGL *textureGL = GetImplAs<TextureGL>(texture); textureGL->syncSamplerState(texture->getSamplerState()); if (mTextures[textureType][textureUnitIndex] != textureGL->getTextureID()) { activeTexture(textureUnitIndex); bindTexture(textureType, textureGL->getTextureID()); } // TODO: apply sampler object if one is bound } else { if (mTextures[textureType][textureUnitIndex] != 0) { activeTexture(textureUnitIndex); bindTexture(textureType, 0); } } } } const gl::Framebuffer *framebuffer = state.getDrawFramebuffer(); const FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(framebuffer); bindFramebuffer(GL_DRAW_FRAMEBUFFER, framebufferGL->getFramebufferID()); setScissor(state.getScissor()); setViewport(state.getViewport()); const gl::BlendState &blendState = state.getBlendState(); setColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, blendState.colorMaskBlue, blendState.colorMaskAlpha); const gl::DepthStencilState &depthStencilState = state.getDepthStencilState(); setDepthMask(depthStencilState.depthMask); setStencilMask(depthStencilState.stencilMask); return gl::Error(GL_NO_ERROR); }
GLState &GLState::setScissor(Rectanglei const &scissorRect) { return setScissor(scissorRect.toRectangleui()); }
//---------- void Assets::refreshView() { auto filterString = ofToLower(this->filter.get()); this->view->clear(); this->ownedElements.clear(); auto filterWidget = Widgets::EditableValue<string>::make(this->filter); auto filterWidgetWeak = weak_ptr<Element>(filterWidget); filterWidget->onValueChange += [this, filterWidgetWeak](const string &) { auto filterWidget = filterWidgetWeak.lock(); this->refreshView(); }; this->view->add(filterWidget); auto addReloadButton = [this](ElementPtr element, shared_ptr<ofxAssets::BaseAsset> asset) { auto button = Widgets::Button::make("Reload", [asset]() { asset->reload(); }); this->ownedElements.push_back(button); button->addListenersToParent(element); element->onBoundsChange += [button](BoundsChangeArguments & args) { auto bounds = args.localBounds; const int width = 80; bounds.width = width; bounds.x = args.localBounds.width - width; button->setBounds(bounds); }; }; auto & assetRegister = ofxAssets::Register::X(); this->view->add("Images"); { const auto & images = assetRegister.getImages(); const auto & names = images.getNames(); for(const auto & name : names) { if (!filterString.empty()) { if(ofToLower(name).find(filterString) == string::npos) { continue; } } auto asset = images[name]; auto & image = asset->get(); auto width = image.getWidth(); auto height = image.getHeight(); auto element = make_shared<Element>(); float longestAxis = max(width, height); auto scaleFactor = 48.0f / longestAxis; auto drawWidth = width * scaleFactor; auto drawHeight = height * scaleFactor; element->onDraw += [asset, name, width, height, drawWidth, drawHeight](DrawArguments & args) { stringstream text; text << name << endl; text << width << "x" << height; ofxCvGui::Utils::drawText(text.str(), 58, 0); asset->get().draw(5, 0, drawWidth, drawHeight); }; element->setHeight(58.0f); this->view->add(element); addReloadButton(element, asset); } } this->view->add("Shaders"); { const auto & shaders = assetRegister.getShaders(); const auto & names = shaders.getNames(); for(const auto & name : names) { if (!filterString.empty()) { if(ofToLower(name).find(filterString) == string::npos) { continue; } } auto asset = shaders[name]; auto & shader = asset->get(); bool loaded = shader.isLoaded(); auto element = make_shared<Element>(); element->onDraw += [name, loaded](DrawArguments & args) { stringstream text; text << name << endl; text << "Loaded : " << (loaded ? "true" : "false"); ofxCvGui::Utils::drawText(text.str(), 5, 0); }; element->setHeight(58.0f); this->view->add(element); addReloadButton(element, asset); } } this->view->add("Fonts"); { auto fonts = assetRegister.getFonts(); auto names = fonts.getNames(); for(const auto & name : names) { auto sizes = assetRegister.getFontSizes(name); for(const auto & size : sizes) { auto sizeString = ofToString(size); if (!filterString.empty()) { if(ofToLower(name).find(filterString) == string::npos && filterString != sizeString) { continue; } } auto asset = fonts[name]; auto element = make_shared<Element>(); element->onDraw += [name, size, asset](DrawArguments & args) { stringstream text; text << name << ", size " << size; ofxCvGui::Utils::drawText(text.str(), 5, 38); asset->get(size).drawString("AaBbCcDdEeFf0123456789", 5, 35); }; element->setScissor(true); element->setHeight(58.0f); this->view->add(element); addReloadButton(element, asset); } } } this->view->add("Sounds"); { auto sounds = assetRegister.getSounds(); auto names = sounds.getNames(); for(const auto & name : names) { if (!filterString.empty()) { if(ofToLower(name).find(filterString) == string::npos) { continue; } } auto asset = sounds[name]; auto & buffer = asset->getSoundBuffer(); auto duration = buffer.getDurationMS(); auto channels = buffer.getNumChannels(); auto element = make_shared<Element>(); element->onDraw += [name, duration, channels](DrawArguments & args) { stringstream text; text << name << endl; text << "Duration = " << duration << ". Channels = " << channels; ofxCvGui::Utils::drawText(text.str(), 0, 5); }; element->setHeight(58.0f); this->view->add(element); auto playButton = Widgets::Toggle::make("Play", [asset]() { return asset->getSoundPlayer().isPlaying(); }, [asset](bool play) { if(play) { asset->getSoundPlayer().play(); } }); this->ownedElements.push_back(playButton); playButton->addListenersToParent(element); element->onBoundsChange += [playButton](BoundsChangeArguments & args) { auto bounds = args.localBounds; bounds.width = 64; bounds.x = args.localBounds.width - (80 + 64); playButton->setBounds(bounds); }; addReloadButton(element, asset); } } this->viewDirty = false; }
/** * Defines the scissor box. * The \p left, \p bottom, \p right and \p top parameters are in windows coordinates. * See also http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml for more information. */ void setScissor(int x, int y, int width, int height) { setScissor( RectI(x,y,width,height) ); }