void TMXIsoObjectsTest::onDraw() { kmMat4 oldMat; kmGLGetMatrix(KM_GL_MODELVIEW, &oldMat); kmGLLoadMatrix(&_modelViewTransform); auto map = (TMXTiledMap*) getChildByTag(kTagTileMap); auto group = map->getObjectGroup("Object Group 1"); auto& objects = group->getObjects(); for (auto& obj : objects) { ValueMap& dict = obj.asValueMap(); float x = dict["x"].asFloat(); float y = dict["y"].asFloat(); float width = dict["width"].asFloat(); float height = dict["height"].asFloat(); glLineWidth(3); DrawPrimitives::drawLine( Point(x,y), Point(x+width,y) ); DrawPrimitives::drawLine( Point(x+width,y), Point(x+width,y+height) ); DrawPrimitives::drawLine( Point(x+width,y+height), Point(x,y+height) ); DrawPrimitives::drawLine( Point(x,y+height), Point(x,y) ); glLineWidth(1); } kmGLLoadMatrix(&oldMat); }
void Label::drawShadowWithoutBlur() { _position.x += _shadowOffset.width; _position.y += _shadowOffset.height; _transformDirty = _inverseDirty = true; Color3B oldColor = _realColor; GLubyte oldOPacity = _displayedOpacity; _displayedOpacity = _shadowOpacity * _displayedOpacity; setColor(_effectColor); _modelViewTransform = transform(_parentTransform); kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); _shaderProgram->setUniformsForBuiltins(_modelViewTransform); for(const auto &child: _children) { child->updateTransform(); } for (const auto& batchNode:_batchNodes) { batchNode->getTextureAtlas()->drawQuads(); } _position.x -= _shadowOffset.width; _position.y -= _shadowOffset.height; _transformDirty = _inverseDirty = true; _displayedOpacity = oldOPacity; setColor(oldColor); _modelViewTransform = transform(_parentTransform); kmGLLoadMatrix(&_modelViewTransform); //kmGLPopMatrix(); }
void RenderTexture::onBegin() { // Director *director = Director::getInstance(); Size size = director->getWinSizeInPixels(); kmGLGetMatrix(KM_GL_PROJECTION, &_oldProjMatrix); kmGLMatrixMode(KM_GL_PROJECTION); kmGLLoadMatrix(&_projectionMatrix); kmGLGetMatrix(KM_GL_MODELVIEW, &_oldTransMatrix); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadMatrix(&_transformMatrix); const Size& texSize = _texture->getContentSizeInPixels(); if(!_keepMatrix) { director->setProjection(director->getProjection()); // Calculate the adjustment ratios based on the old and new projections float widthRatio = size.width / texSize.width; float heightRatio = size.height / texSize.height; kmMat4 orthoMatrix; kmMat4OrthographicProjection(&orthoMatrix, (float)-1.0 / widthRatio, (float)1.0 / widthRatio, (float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1,1 ); kmGLMultMatrix(&orthoMatrix); } //calculate viewport { Rect viewport; viewport.size.width = _fullviewPort.size.width; viewport.size.height = _fullviewPort.size.height; float viewPortRectWidthRatio = float(viewport.size.width)/_fullRect.size.width; float viewPortRectHeightRatio = float(viewport.size.height)/_fullRect.size.height; viewport.origin.x = (_fullRect.origin.x - _rtTextureRect.origin.x) * viewPortRectWidthRatio; viewport.origin.y = (_fullRect.origin.y - _rtTextureRect.origin.y) * viewPortRectHeightRatio; //glViewport(_fullviewPort.origin.x, _fullviewPort.origin.y, (GLsizei)_fullviewPort.size.width, (GLsizei)_fullviewPort.size.height); glViewport(viewport.origin.x, viewport.origin.y, (GLsizei)texSize.width, (GLsizei)texSize.height); } // Adjust the orthographic projection and viewport glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO); glBindFramebuffer(GL_FRAMEBUFFER, _FBO); //TODO move this to configration, so we don't check it every time /* Certain Qualcomm Andreno gpu's will retain data in memory after a frame buffer switch which corrupts the render to the texture. The solution is to clear the frame buffer before rendering to the texture. However, calling glClear has the unintended result of clearing the current texture. Create a temporary texture to overcome this. At the end of RenderTexture::begin(), switch the attached texture to the second one, call glClear, and then switch back to the original texture. This solution is unnecessary for other devices as they don't have the same issue with switching frame buffers. */ if (Configuration::getInstance()->checkForGLExtension("GL_QCOM")) { // -- bind a temporary texture so we can clear the render buffer without losing our texture glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _textureCopy->getName(), 0); CHECK_GL_ERROR_DEBUG(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture->getName(), 0); } }
void Box2DView::onDraw() { kmMat4 oldMat; kmGLGetMatrix(KM_GL_MODELVIEW, &oldMat); kmGLLoadMatrix(&_modelViewTransform); GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); m_test->m_world->DrawDebugData(); CHECK_GL_ERROR_DEBUG(); kmGLLoadMatrix(&oldMat); }
void LabelFNTSpriteActions::onDraw() { kmMat4 oldMat; kmGLGetMatrix(KM_GL_MODELVIEW, &oldMat); kmGLLoadMatrix(&_modelViewTransform); auto s = Director::getInstance()->getWinSize(); DrawPrimitives::drawLine( Point(0, s.height/2), Point(s.width, s.height/2) ); DrawPrimitives::drawLine( Point(s.width/2, 0), Point(s.width/2, s.height) ); kmGLLoadMatrix(&oldMat); }
// override visit. // Don't call visit on it's children void ParticleBatchNode::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { // CAREFUL: // This visit is almost identical to Node#visit // with the exception that it doesn't call visit on it's children // // The alternative is to have a void Sprite#visit, but // although this is less maintainable, is faster // if (!_visible) { return; } bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); draw(renderer, _modelViewTransform, dirty); kmGLPopMatrix(); }
void RenderTexture::onEnd() { Director *director = Director::getInstance(); glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO); // restore viewport director->setViewport(); // kmGLMatrixMode(KM_GL_PROJECTION); kmGLLoadMatrix(&_oldProjMatrix); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadMatrix(&_oldTransMatrix); }
void Sprite::drawDebugData() { kmMat4 oldModelView; kmGLGetMatrix(KM_GL_MODELVIEW, &oldModelView); kmGLLoadMatrix(&_modelViewTransform); // draw bounding box Point vertices[4] = { Point( _quad.bl.vertices.x, _quad.bl.vertices.y ), Point( _quad.br.vertices.x, _quad.br.vertices.y ), Point( _quad.tr.vertices.x, _quad.tr.vertices.y ), Point( _quad.tl.vertices.x, _quad.tl.vertices.y ), }; DrawPrimitives::drawPoly(vertices, 4, true); kmGLLoadMatrix(&oldModelView); }
void BatchNode::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { // quick return if not visible. children won't be drawn. if (!_visible) { return; } bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); sortAllChildren(); draw(renderer, _modelViewTransform, dirty); // reset for next frame _orderOfArrival = 0; kmGLPopMatrix(); }
void RenderTexture::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { // override visit. // Don't call visit on its children if (!_visible) { return; } bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); _sprite->visit(renderer, _modelViewTransform, dirty); draw(renderer, _modelViewTransform, dirty); kmGLPopMatrix(); _orderOfArrival = 0; }
void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { if (! _visible || _originalUTF8String.empty()) { return; } if (_fontDirty) { updateFont(); } if (_contentDirty) { updateContent(); } bool dirty = parentTransformUpdated || _transformUpdated; if (_shadowEnabled && _shadowBlurRadius <= 0 && (_shadowDirty || dirty)) { _position.x += _shadowOffset.width; _position.y += _shadowOffset.height; _transformDirty = _inverseDirty = true; _shadowTransform = transform(parentTransform); _position.x -= _shadowOffset.width; _position.y -= _shadowOffset.height; _transformDirty = _inverseDirty = true; _shadowDirty = false; } if(dirty) { _modelViewTransform = transform(parentTransform); } _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); if (_textSprite) { drawTextSprite(renderer,dirty); } else { draw(renderer, _modelViewTransform, dirty); } kmGLPopMatrix(); setOrderOfArrival(0); }
void HelloWorld::onDraw(const kmMat4 &transform, bool transformUpdated) { kmGLPushMatrix(); kmGLLoadMatrix(&transform); GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); world->DrawDebugData(); CHECK_GL_ERROR_DEBUG(); kmGLPopMatrix(); }
void Box2DView::onDraw(const kmMat4 &transform, bool transformUpdated) { kmGLPushMatrix(); kmGLLoadMatrix(&transform); GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); m_test->Step(&settings); m_test->m_world->DrawDebugData(); CHECK_GL_ERROR_DEBUG(); kmGLPopMatrix(); }
void Node::visit(Renderer* renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { // quick return if not visible. children won't be drawn. if (!_visible) { return; } bool dirty = _transformUpdated || parentTransformUpdated; if(dirty) _modelViewTransform = this->transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); int i = 0; if(!_children.empty()) { sortAllChildren(); // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { auto node = _children.at(i); if ( node && node->_localZOrder < 0 ) node->visit(renderer, _modelViewTransform, dirty); else break; } // self draw this->draw(renderer, _modelViewTransform, dirty); for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) (*it)->visit(renderer, _modelViewTransform, dirty); } else { this->draw(renderer, _modelViewTransform, dirty); } // reset for next frame _orderOfArrival = 0; kmGLPopMatrix(); }
void LabelFNTBounds::onDraw() { kmMat4 oldMat; kmGLGetMatrix(KM_GL_MODELVIEW, &oldMat); kmGLLoadMatrix(&_modelViewTransform); auto labelSize = label1->getContentSize(); auto origin = Director::getInstance()->getWinSize(); origin.width = origin.width / 2 - (labelSize.width / 2); origin.height = origin.height / 2 - (labelSize.height / 2); Point vertices[4]= { Point(origin.width, origin.height), Point(labelSize.width + origin.width, origin.height), Point(labelSize.width + origin.width, labelSize.height + origin.height), Point(origin.width, labelSize.height + origin.height) }; DrawPrimitives::drawPoly(vertices, 4, true); kmGLLoadMatrix(&oldMat); }
void CCRenderTexture::begin() { kmGLMatrixMode(KM_GL_PROJECTION); kmGLPushMatrix(); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLPushMatrix(); CCDirector *director = CCDirector::sharedDirector(); director->setProjection(director->getProjection()); #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 kmMat4 modifiedProjection; kmGLGetMatrix(KM_GL_PROJECTION, &modifiedProjection); kmMat4Multiply(&modifiedProjection, CCEGLView::sharedOpenGLView()->getReverseOrientationMatrix(), &modifiedProjection); kmGLMatrixMode(KM_GL_PROJECTION); kmGLLoadMatrix(&modifiedProjection); kmGLMatrixMode(KM_GL_MODELVIEW); #endif const CCSize& texSize = m_pTexture->getContentSizeInPixels(); // Calculate the adjustment ratios based on the old and new projections CCSize size = director->getWinSizeInPixels(); float widthRatio = size.width / texSize.width; float heightRatio = size.height / texSize.height; // Adjust the orthographic projection and viewport glViewport(0, 0, (GLsizei)texSize.width, (GLsizei)texSize.height); kmMat4 orthoMatrix; kmMat4OrthographicProjection(&orthoMatrix, (float)-1.0 / widthRatio, (float)1.0 / widthRatio, (float)-1.0 / heightRatio, (float)1.0 / heightRatio, -1,1 ); kmGLMultMatrix(&orthoMatrix); glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_nOldFBO); glBindFramebuffer(GL_FRAMEBUFFER, m_uFBO); /* Certain Qualcomm Andreno gpu's will retain data in memory after a frame buffer switch which corrupts the render to the texture. The solution is to clear the frame buffer before rendering to the texture. However, calling glClear has the unintended result of clearing the current texture. Create a temporary texture to overcome this. At the end of CCRenderTexture::begin(), switch the attached texture to the second one, call glClear, and then switch back to the original texture. This solution is unnecessary for other devices as they don't have the same issue with switching frame buffers. */ if (CCConfiguration::sharedConfiguration()->checkForGLExtension("GL_QCOM")) { // -- bind a temporary texture so we can clear the render buffer without losing our texture glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_pTextureCopy->getName(), 0); CHECK_GL_ERROR_DEBUG(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_pTexture->getName(), 0); } }
void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { if (! _visible || _originalUTF8String.empty()) { return; } if (_contentDirty) { updateContent(); } if (! _textSprite && _currLabelEffect == LabelEffect::SHADOW && _shadowBlurRadius <= 0) { _parentTransform = parentTransform; draw(renderer, _modelViewTransform, true); } else { bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); if (_textSprite) { _textSprite->visit(); } else { draw(renderer, _modelViewTransform, dirty); } kmGLPopMatrix(); } setOrderOfArrival(0); }
void LabelAtlas::drawDebugData(const kmMat4& transform, bool transformUpdated) { kmGLPushMatrix(); kmGLLoadMatrix(&transform); auto size = getContentSize(); Point vertices[4]= { Point::ZERO, Point(size.width, 0), Point(size.width, size.height), Point(0, size.height) }; DrawPrimitives::drawPoly(vertices, 4, true); kmGLPopMatrix(); }
void Building::onDraw( const kmMat4 &transform, bool transformUpdated ) { kmGLPushMatrix(); kmGLLoadMatrix(&transform); if (isNeedShowGrid()) { Size size = getContentSize(); Point p1 = Point(size.width/2, size.height); Point p2 = Point(0, size.height/2); Point p3 = Point(size.width/2, 0); Point p4 = Point(size.width, size.height/2); Point points[4] = {p1, p2, p3, p4}; if (_isillegal) { DrawPrimitives::drawSolidPoly(points, 4, Color4F(1,0,0,1)); } else { DrawPrimitives::drawSolidPoly(points, 4, Color4F(1,1,0,1)); } float w = (p1.x-p2.x)/m_nGridCount; float h = (p1.y-p2.y)/m_nGridCount; DrawPrimitives::setDrawColor4B(0, 255, 0, 255); for (int i = 0; i <= m_nGridCount; i++) { DrawPrimitives::drawLine(Point(p1.x + w*i, p1.y - h*i), Point(p2.x + w*i, p2.y - h*i)); } for (int i = 0; i <= m_nGridCount; i++) { DrawPrimitives::drawLine(Point(p1.x - w*i, p1.y - h*i), Point(p4.x - w*i, p4.y - h*i)); } } kmGLPopMatrix(); }
// draw void Sprite::ManualDraw() { Mat4 parentTransform; kmGLGetMatrix(KM_GL_MODELVIEW, &parentTransform); _modelViewTransform = this->transform(parentTransform); auto shader = getShaderProgram(); shader->use(); shader->setUniformsForBuiltins(_modelViewTransform); kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX ); GL::blendFunc(_blendFunc.src, _blendFunc.dst); GL::bindTexture2D( getTexture()->getName()); // // Attributes // #define kQuadSize sizeof(_quad.bl) size_t offset = (size_t)&_quad; // vertex int diff = offsetof( V3F_C4B_T2F, vertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff)); // texCoods diff = offsetof( V3F_C4B_T2F, texCoords); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); // color diff = offsetof( V3F_C4B_T2F, colors); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff)); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4); kmGLPopMatrix(); }
void Label::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { if (! _visible) { return; } bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); draw(renderer, _modelViewTransform, dirty); kmGLPopMatrix(); setOrderOfArrival(0); }
void BaseScene::myDraw(const Mat4 &transform, uint32_t flags){ kmGLPushMatrix(); kmGLLoadMatrix(&transform); //draw CHECK_GL_ERROR_DEBUG(); // draw a simple line // The default state is: // Line Width: 1 // color: 255,255,255,255 (white, non-transparent) // Anti-Aliased // glEnable(GL_LINE_SMOOTH); DrawPrimitives::drawLine(VisibleRect::leftBottom(), VisibleRect::rightTop()); CHECK_GL_ERROR_DEBUG(); // line: color, width, aliased // glLineWidth > 1 and GL_LINE_SMOOTH are not compatible // GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone // glDisable(GL_LINE_SMOOTH); glLineWidth(5.0f); DrawPrimitives::setDrawColor4B(255, 0, 0, 255); DrawPrimitives::drawLine(VisibleRect::leftTop(), VisibleRect::rightBottom()); CHECK_GL_ERROR_DEBUG(); // TIP: // If you are going to use always thde same color or width, you don't // need to call it before every draw // // Remember: OpenGL is a state-machine. // draw big point in the center DrawPrimitives::setPointSize(64); DrawPrimitives::setDrawColor4B(0, 0, 255, 128); DrawPrimitives::drawPoint(VisibleRect::center()); CHECK_GL_ERROR_DEBUG(); // draw 4 small points Point points[] = { Point(60, 60), Point(70, 70), Point(60, 70), Point(70, 60) }; DrawPrimitives::setPointSize(4); DrawPrimitives::setDrawColor4B(0, 255, 255, 255); DrawPrimitives::drawPoints(points, 4); CHECK_GL_ERROR_DEBUG(); // draw a green circle with 10 segments glLineWidth(16); DrawPrimitives::setDrawColor4B(0, 255, 0, 255); DrawPrimitives::drawCircle(VisibleRect::center(), 100, 0, 10, false); CHECK_GL_ERROR_DEBUG(); // draw a green circle with 50 segments with line to center glLineWidth(2); DrawPrimitives::setDrawColor4B(0, 255, 255, 255); DrawPrimitives::drawCircle(VisibleRect::center(), 50, CC_DEGREES_TO_RADIANS(90), 50, true); CHECK_GL_ERROR_DEBUG(); // draw a pink solid circle with 50 segments glLineWidth(2); DrawPrimitives::setDrawColor4B(255, 0, 255, 255); DrawPrimitives::drawSolidCircle(VisibleRect::center() + Point(140, 0), 40, CC_DEGREES_TO_RADIANS(90), 50, 1.0f, 1.0f); CHECK_GL_ERROR_DEBUG(); // open yellow poly DrawPrimitives::setDrawColor4B(255, 255, 0, 255); glLineWidth(10); Point vertices[] = { Point(0, 0), Point(50, 50), Point(100, 50), Point(100, 100), Point(50, 100) }; DrawPrimitives::drawPoly(vertices, 5, false); CHECK_GL_ERROR_DEBUG(); // filled poly glLineWidth(1); Point filledVertices[] = { Point(0, 120), Point(50, 120), Point(50, 170), Point(25, 200), Point(0, 170) }; DrawPrimitives::drawSolidPoly(filledVertices, 5, Color4F(0.5f, 0.5f, 1, 1)); // closed purble poly DrawPrimitives::setDrawColor4B(255, 0, 255, 255); glLineWidth(2); Point vertices2[] = { Point(30, 130), Point(30, 230), Point(50, 200) }; DrawPrimitives::drawPoly(vertices2, 3, true); CHECK_GL_ERROR_DEBUG(); // draw quad bezier path DrawPrimitives::drawQuadBezier(VisibleRect::leftTop(), VisibleRect::center(), VisibleRect::rightTop(), 50); CHECK_GL_ERROR_DEBUG(); // draw cubic bezier path DrawPrimitives::drawCubicBezier(VisibleRect::center(), Point(VisibleRect::center().x + 30, VisibleRect::center().y + 50), Point(VisibleRect::center().x + 60, VisibleRect::center().y - 50), VisibleRect::right(), 100); CHECK_GL_ERROR_DEBUG(); //draw a solid polygon Point vertices3[] = { Point(60, 160), Point(70, 190), Point(100, 190), Point(90, 160) }; DrawPrimitives::drawSolidPoly(vertices3, 4, Color4F(1, 1, 0, 1)); // restore original values glLineWidth(1); DrawPrimitives::setDrawColor4B(255, 255, 255, 255); DrawPrimitives::setPointSize(1); CHECK_GL_ERROR_DEBUG(); //end draw kmGLPopMatrix(); }
void Skeleton::onDraw () { CC_NODE_DRAW_SETUP(); GL::blendFunc(blendFunc.src, blendFunc.dst); Color3B color = getColor(); skeleton->r = color.r / (float)255; skeleton->g = color.g / (float)255; skeleton->b = color.b / (float)255; skeleton->a = getOpacity() / (float)255; if (premultipliedAlpha) { skeleton->r *= skeleton->a; skeleton->g *= skeleton->a; skeleton->b *= skeleton->a; } int additive = 0; TextureAtlas* textureAtlas = 0; V3F_C4B_T2F_Quad quad; quad.tl.vertices.z = 0; quad.tr.vertices.z = 0; quad.bl.vertices.z = 0; quad.br.vertices.z = 0; for (int i = 0, n = skeleton->slotCount; i < n; i++) { spSlot* slot = skeleton->drawOrder[i]; if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue; spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment; TextureAtlas* regionTextureAtlas = getTextureAtlas(attachment); if (slot->data->additiveBlending != additive) { if (textureAtlas) { textureAtlas->drawQuads(); textureAtlas->removeAllQuads(); } additive = !additive; GL::blendFunc(blendFunc.src, additive ? GL_ONE : blendFunc.dst); } else if (regionTextureAtlas != textureAtlas && textureAtlas) { textureAtlas->drawQuads(); textureAtlas->removeAllQuads(); } textureAtlas = regionTextureAtlas; setFittedBlendingFunc(textureAtlas); ssize_t quadCount = textureAtlas->getTotalQuads(); if (textureAtlas->getCapacity() == quadCount) { textureAtlas->drawQuads(); textureAtlas->removeAllQuads(); if (!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return; } spRegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha); textureAtlas->updateQuad(&quad, quadCount); } if (textureAtlas) { textureAtlas->drawQuads(); textureAtlas->removeAllQuads(); } if(debugBones || debugSlots) { kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); if (debugSlots) { // Slots. DrawPrimitives::setDrawColor4B(0, 0, 255, 255); glLineWidth(1); Point points[4]; V3F_C4B_T2F_Quad tmpQuad; for (int i = 0, n = skeleton->slotCount; i < n; i++) { spSlot* slot = skeleton->drawOrder[i]; if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue; spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment; spRegionAttachment_updateQuad(attachment, slot, &tmpQuad); points[0] = Point(tmpQuad.bl.vertices.x, tmpQuad.bl.vertices.y); points[1] = Point(tmpQuad.br.vertices.x, tmpQuad.br.vertices.y); points[2] = Point(tmpQuad.tr.vertices.x, tmpQuad.tr.vertices.y); points[3] = Point(tmpQuad.tl.vertices.x, tmpQuad.tl.vertices.y); DrawPrimitives::drawPoly(points, 4, true); } } if (debugBones) { // Bone lengths. glLineWidth(2); DrawPrimitives::setDrawColor4B(255, 0, 0, 255); for (int i = 0, n = skeleton->boneCount; i < n; i++) { spBone *bone = skeleton->bones[i]; float x = bone->data->length * bone->m00 + bone->worldX; float y = bone->data->length * bone->m10 + bone->worldY; DrawPrimitives::drawLine(Point(bone->worldX, bone->worldY), Point(x, y)); } // Bone origins. DrawPrimitives::setPointSize(4); DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue. for (int i = 0, n = skeleton->boneCount; i < n; i++) { spBone *bone = skeleton->bones[i]; DrawPrimitives::drawPoint(Point(bone->worldX, bone->worldY)); if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255); } } kmGLPopMatrix(); } }
void HelloWorld::onDraw() { kmMat4 oldProj; kmMat4 oldMv; // //先获得旧的matrix,然后设置新的matrix kmGLGetMatrix(KM_GL_MODELVIEW, &oldMv); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadMatrix(&_modelViewMV); kmGLGetMatrix(KM_GL_PROJECTION, &oldProj); kmGLMatrixMode(KM_GL_PROJECTION); //想显示3d cubic的时候,不能够把projection matrix设置为对等矩阵 // kmGLLoadIdentity(); kmGLMatrixMode(KM_GL_MODELVIEW); //mShaderProgram->use(); //we don't need any builtin Uniforms, since we will add our mvp matrix all by hand typedef struct { float Position[3]; float Color[4]; float tex[2]; } Vertex; #define TEX_COORD_MAX 1 Vertex Vertices[] = { // Front {{1, -1, 0}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, 0}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, 0}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, 0}, {0, 0, 0, 1}, {0, 0}}, // Back {{1, 1, -2}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{-1, -1, -2}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{1, -1, -2}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, 1, -2}, {0, 0, 0, 1}, {0, 0}}, // Left {{-1, -1, 0}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{-1, 1, 0}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, -2}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, -2}, {0, 0, 0, 1}, {0, 0}}, // Right {{1, -1, -2}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, -2}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{1, 1, 0}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{1, -1, 0}, {0, 0, 0, 1}, {0, 0}}, // Top {{1, 1, 0}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, -2}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, -2}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, 1, 0}, {0, 0, 0, 1}, {0, 0}}, // Bottom {{1, -1, -2}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, -1, 0}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, -1, 0}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, -2}, {0, 0, 0, 1}, {0, 0}} }; int vertexCount = sizeof(Vertices) / sizeof(Vertices[0]); GLubyte Indices[] = { // Front 0, 1, 2, 2, 3, 0, // Back 4, 5, 6, 4, 5, 7, // Left 8, 9, 10, 10, 11, 8, // Right 12, 13, 14, 14, 15, 12, // Top 16, 17, 18, 18, 19, 16, // Bottom 20, 21, 22, 22, 23, 20 }; //set color for each vertex note:the color value is between 0-1 // mShaderProgram->setUniformLocationWith4f(mColorLocation, 0.5, 0.5, 0.5, 1); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); _positionLocation = glGetAttribLocation(mShaderProgram->getProgram(), "Position"); _colorLocation = glGetAttribLocation(mShaderProgram->getProgram(), "SourceColor"); _projectionUniform = glGetUniformLocation(mShaderProgram->getProgram(), "Projection"); _modelViewUniform = glGetUniformLocation(mShaderProgram->getProgram(), "Modelview"); _textureLocation = glGetAttribLocation(mShaderProgram->getProgram(), "TextureCoord"); _textureUniform = glGetUniformLocation(mShaderProgram->getProgram(), "CC_Texture0"); kmGLPushMatrix(); kmGLLoadIdentity(); auto winSize = Director::getInstance()->getWinSizeInPixels(); // float zeye = Director::getInstance()->getZEye(); // float zfarPlane = zeye + winSize.height/2; static float rotation = 0; //kmGLRotatef(20, 0, 0, 1); //the order to call translate/rotate/scale matters //in kzMath, the order should be translate -> rotate -> scale //in generate mv = t * r * s * m; kmGLTranslatef(winSize.width/2, winSize.height/2, 0 ); kmGLRotatef(rotation++, 1, 1, 1); kmGLScalef(150, 150, 150 ); //we must scale the model coordinate such that we can see the model with the zNear and far plane if (rotation >= 360 ) { rotation = 0; } //kmGLTranslatef(0, 0, -0.1); kmMat4 modelView; kmGLGetMatrix(KM_GL_MODELVIEW, &modelView); mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); mShaderProgram->setUniformLocationWithMatrix4fv(_modelViewUniform, modelView.mat , 1); kmGLPopMatrix(); // GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); //this two calls are replacement for the above call glEnableVertexAttribArray(_positionLocation); glEnableVertexAttribArray(_colorLocation); glEnableVertexAttribArray(_textureLocation); glVertexAttribPointer(_positionLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); glVertexAttribPointer(_colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),(GLvoid*)(sizeof(float) * 3)); glVertexAttribPointer(_textureLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 7)); // //set sampler GL::bindTexture2D(_textureID); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, 0); mShaderProgram->setUniformLocationWith1i(_textureUniform, 0); GL::bindTexture2D(_textureID2); glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, (void*)(18 * sizeof(GLubyte))); mShaderProgram->setUniformLocationWith1i(_textureUniform, 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,vertexCount); CHECK_GL_ERROR_DEBUG(); kmGLMatrixMode(KM_GL_PROJECTION); kmGLLoadMatrix(&oldProj); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadMatrix(&oldMv); }
void NodeGrid::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { // quick return if not visible. children won't be drawn. if (!_visible) { return; } _groupCommand.init(_globalZOrder); renderer->addCommand(&_groupCommand); renderer->pushGroup(_groupCommand.getRenderQueueID()); bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = this->transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); Director::Projection beforeProjectionType; if(_nodeGrid && _nodeGrid->isActive()) { beforeProjectionType = Director::getInstance()->getProjection(); _nodeGrid->set2DProjection(); } _gridBeginCommand.init(_globalZOrder); _gridBeginCommand.func = CC_CALLBACK_0(NodeGrid::onGridBeginDraw, this); renderer->addCommand(&_gridBeginCommand); if(_gridTarget) { _gridTarget->visit(renderer, _modelViewTransform, dirty); } int i = 0; if(!_children.empty()) { sortAllChildren(); // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { auto node = _children.at(i); if ( node && node->getLocalZOrder() < 0 ) node->visit(renderer, _modelViewTransform, dirty); else break; } // self draw,currently we have nothing to draw on NodeGrid, so there is no need to add render command this->draw(renderer, _modelViewTransform, dirty); for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) { (*it)->visit(renderer, _modelViewTransform, dirty); } } else { this->draw(renderer, _modelViewTransform, dirty); } // reset for next frame _orderOfArrival = 0; if(_nodeGrid && _nodeGrid->isActive()) { // restore projection Director *director = Director::getInstance(); director->setProjection(beforeProjectionType); } _gridEndCommand.init(_globalZOrder); _gridEndCommand.func = CC_CALLBACK_0(NodeGrid::onGridEndDraw, this); renderer->addCommand(&_gridEndCommand); renderer->popGroup(); kmGLPopMatrix(); }
void ClippingNode::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated) { if(!_visible) return; bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); //Add group command _groupCommand.init(_globalZOrder); renderer->addCommand(&_groupCommand); renderer->pushGroup(_groupCommand.getRenderQueueID()); _beforeVisitCmd.init(_globalZOrder); _beforeVisitCmd.func = CC_CALLBACK_0(ClippingNode::onBeforeVisit, this); renderer->addCommand(&_beforeVisitCmd); if (_alphaThreshold < 1) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) #else // since glAlphaTest do not exists in OES, use a shader that writes // pixel only if greater than an alpha threshold GLProgram *program = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV); GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE); // set our alphaThreshold program->use(); program->setUniformLocationWith1f(alphaValueLocation, _alphaThreshold); // we need to recursively apply this shader to all the nodes in the stencil node // XXX: we should have a way to apply shader to all nodes without having to do this setProgram(_stencil, program); #endif } _stencil->visit(renderer, _modelViewTransform, dirty); _afterDrawStencilCmd.init(_globalZOrder); _afterDrawStencilCmd.func = CC_CALLBACK_0(ClippingNode::onAfterDrawStencil, this); renderer->addCommand(&_afterDrawStencilCmd); int i = 0; if(!_children.empty()) { sortAllChildren(); // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { auto node = _children.at(i); if ( node && node->getLocalZOrder() < 0 ) node->visit(renderer, _modelViewTransform, dirty); else break; } // self draw this->draw(renderer, _modelViewTransform, dirty); for(auto it=_children.cbegin()+i; it != _children.cend(); ++it) (*it)->visit(renderer, _modelViewTransform, dirty); } else { this->draw(renderer, _modelViewTransform, dirty); } _afterVisitCmd.init(_globalZOrder); _afterVisitCmd.func = CC_CALLBACK_0(ClippingNode::onAfterVisit, this); renderer->addCommand(&_afterVisitCmd); renderer->popGroup(); kmGLPopMatrix(); }
void CScrollView::visit(Renderer *renderer, const kmMat4& parentTransform, bool parentTransformUpdated) { // quick return if not visible if (!isVisible()) { return; } bool dirty = parentTransformUpdated || _transformUpdated; if(dirty) _modelViewTransform = this->transform(parentTransform); _transformUpdated = false; // IMPORTANT: // To ease the migration to v3.0, we still support the kmGL stack, // but it is deprecated and your code should not rely on it kmGLPushMatrix(); kmGLLoadMatrix(&_modelViewTransform); this->beforeDraw(); if (!_children.empty()) { int i=0; sortAllChildren(); // draw children zOrder < 0 for( ; i < _children.size(); i++ ) { Node *child = _children.at(i); if ( child->getLocalZOrder() < 0 ) { child->visit(renderer, _modelViewTransform, dirty); } else { break; } } // this draw this->draw(renderer, _modelViewTransform, dirty); // draw children zOrder >= 0 for( ; i < _children.size(); i++ ) { Node *child = _children.at(i); child->visit(renderer, _modelViewTransform, dirty); } } else { this->draw(renderer, _modelViewTransform, dirty); } this->afterDraw(); kmGLPopMatrix(); }