Exemplo n.º 1
0
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);
    }
}
Exemplo n.º 2
0
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 Cocos2dRenderManager::initialise()
	{
		CCDirector *pDirector = CCDirector::sharedDirector();
		MYGUI_PLATFORM_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
		MYGUI_PLATFORM_LOG(Info, "* Initialise: " << getClassTypeName());

		CCSize s = pDirector->getWinSizeInPixels();

		this->setPosition(0, 0);
		this->setContentSize(s);
		setViewSize(int(s.width), int(s.height));

		// 绑定到cocos2d节点
		pDirector->setNotificationNode(this);

		mInfo.pixWidth = s.width;
		mInfo.pixHeight = s.height;

		mVertexFormat = VertexColourType::ColourABGR;

		mUpdate = true;

		kmMat4 tmp;
		kmGLGetMatrix(KM_GL_PROJECTION, &tmp);
		kmMat4Inverse(&mMatrix, &tmp);

		MYGUI_PLATFORM_LOG(Info, getClassTypeName() << " successfully initialized");
		mIsInitialise = true;

		CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
			callfuncO_selector(Cocos2dRenderManager::listenForeToBackground),
			EVENT_COME_TO_BACKGROUND,
			NULL);
		pDirector->getScheduler()->scheduleUpdateForTarget(this, kCCPriorityNonSystemMin, false);
	}
Exemplo n.º 4
0
void Node::visit()
{
    auto renderer = Director::getInstance()->getRenderer();
    kmMat4 parentTransform;
    kmGLGetMatrix(KM_GL_MODELVIEW, &parentTransform);
    visit(renderer, parentTransform, true);
}
Exemplo n.º 5
0
void Skin::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
    kmMat4 mv;
    kmGLGetMatrix(KM_GL_MODELVIEW, &mv);

    //TODO implement z order
    _quadCommand.init(_globalZOrder, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv);
    renderer->addCommand(&_quadCommand);
}
Exemplo n.º 6
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);
}
Exemplo n.º 7
0
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);
}
Exemplo n.º 8
0
void Node::transform()
{
#if CC_USE_PHYSICS
    updatePhysicsTransform();
#endif

    kmMat4 transfrom4x4 = this->getNodeToParentTransform();

    kmGLMultMatrix( &transfrom4x4 );

    // saves the MV matrix
    kmGLGetMatrix(KM_GL_MODELVIEW, &_modelViewTransform);
}
Exemplo n.º 9
0
void RenderTexture::begin()
{
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLPushMatrix();
    kmGLGetMatrix(KM_GL_PROJECTION, &_projectionMatrix);
    
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLPushMatrix();
    kmGLGetMatrix(KM_GL_MODELVIEW, &_transformMatrix);
    
    if(!_keepMatrix)
    {
        Director *director = Director::getInstance();
        director->setProjection(director->getProjection());
        
        const Size& texSize = _texture->getContentSizeInPixels();
        
        // Calculate the adjustment ratios based on the old and new projections
        Size size = director->getWinSizeInPixels();
        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);
    }

    _groupCommand.init(_globalZOrder);

    Renderer *renderer =  Director::getInstance()->getRenderer();
    renderer->addCommand(&_groupCommand);
    renderer->pushGroup(_groupCommand.getRenderQueueID());

    _beginCommand.init(_globalZOrder);
    _beginCommand.func = CC_CALLBACK_0(RenderTexture::onBegin, this);

    Director::getInstance()->getRenderer()->addCommand(&_beginCommand);
}
Exemplo n.º 10
0
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);
    }
}
Exemplo n.º 11
0
void CAFlashView::draw()
{
    CAView::draw();
    ccGLBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
    kmMat4 matrixP;
    kmMat4 matrixMV;
    kmMat4 matrixMVP;
    kmGLGetMatrix(KM_GL_PROJECTION, &matrixP );
    kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV );
    matrixMV.mat[13] = this->getFrame().size.height + matrixMV.mat[13];
    kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV);
#if CC_ENABLE_GL_STATE_CACHE
    ccGLUseProgram(0);//valid program is NON_ZERO unsigned int
#endif
    CCSize size = this->getFrame().size;
    float localScaleX = size.width/ m_pFlash->getWidth();
    float localScaleY = -(size.height / m_pFlash->getHeight());
    float localScale = localScaleX > -localScaleY ? -localScaleY : localScaleX;
    kmMat4 matrixs;
    kmMat4Scaling(&matrixs, localScale, -localScale, getZOrder());
    kmMat4Multiply(&matrixMVP, &matrixMVP, &matrixs);
    m_pFlash->display(&matrixMVP);
    CHECK_GL_ERROR_DEBUG();
}
Exemplo n.º 12
0
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 HelloWorld::draw(cocos2d::Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
    Layer::draw(renderer, transform, transformUpdated);
    
    
    kmGLPushMatrix();
    kmGLGetMatrix(KM_GL_MODELVIEW, &_modelViewMV);
   
    
    //send custom command to tell the renderer to call opengl commands
    _customCommand.init(_globalZOrder);
    _customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw, this);
    renderer->addCommand(&_customCommand);
    
    
    kmGLPopMatrix();
}
Exemplo n.º 14
0
void Skin::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
	//TODO skin effect
	if ( _effectType == SkinEffect_None )
	{
		kmMat4 mv;
		kmGLGetMatrix(KM_GL_MODELVIEW, &mv);

		//TODO implement z order
		_quadCommand.init(_globalZOrder, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv);
		renderer->addCommand(&_quadCommand);
	}
	else
	{
		_customCommand.init(_globalZOrder);
		_customCommand.func = CC_CALLBACK_0(Skin::draw_callback, this,renderer, transform, transformUpdated);
		renderer->addCommand(&_customCommand);
	}
}
Exemplo n.º 15
0
// 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();
}
Exemplo n.º 16
0
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);
}
Exemplo n.º 17
0
void SpriteBatchNode::draw()
{
    // Optimization: Fast Dispatch
    if( _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    for(const auto &child: _children)
        child->updateTransform();

    kmMat4 mv;
    kmGLGetMatrix(KM_GL_MODELVIEW, &mv);

    _quadCommand.init(0,
              _vertexZ,
              _textureAtlas->getTexture()->getName(),
              _shaderProgram,
              _blendFunc,
              _textureAtlas->getQuads(),
              _textureAtlas->getTotalQuads(),
              mv);
    Director::getInstance()->getRenderer()->addCommand(&_quadCommand);
}
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);

}
Exemplo n.º 19
0
void CAView::visit()
{
    CC_RETURN_IF(!m_bVisible);
    
    kmGLPushMatrix();

    this->transform();
    
    bool isScissor = (bool)glIsEnabled(GL_SCISSOR_TEST);
    DRect restoreScissorRect = DRectZero;
    if (isScissor)
    {
        GLfloat params[4];
        glGetFloatv(GL_SCISSOR_BOX, params);
        restoreScissorRect = DRect(params[0], params[1], params[2], params[3]);
    }

    if (!m_bDisplayRange)
    {
        kmMat4 modelview;
        kmGLGetMatrix(KM_GL_MODELVIEW, &modelview);
        kmMat4 tm;
        kmMat4Identity(&tm);
        tm.mat[12] = m_obContentSize.width;
        tm.mat[13] = m_obContentSize.height;
        kmMat4 tm2;
        kmMat4Multiply(&tm2, &modelview, &tm);

        DPoint point = DPoint(modelview.mat[12], modelview.mat[13]);
        
        static CAApplication* application = CAApplication::getApplication();
        if (application->getProjection() == CAApplication::P3D)
        {
            point = ccpAdd(point, application->getWinSize() / 2);
        }
        
        DSize size = DSize(tm2.mat[12] - modelview.mat[12], tm2.mat[13] - modelview.mat[13]);
        DRect frame = DRect(point.x, point.y, size.width, size.height);
        
        
        if (isScissor)
        {
            float x1 = MAX(s_dip_to_px(frame.getMinX()), restoreScissorRect.getMinX());
            float y1 = MAX(s_dip_to_px(frame.getMinY()), restoreScissorRect.getMinY());
            float x2 = MIN(s_dip_to_px(frame.getMaxX()) + 0.5f, restoreScissorRect.getMaxX());
            float y2 = MIN(s_dip_to_px(frame.getMaxY()) + 0.5f, restoreScissorRect.getMaxY());
            float width = MAX(x2-x1, 0);
            float height = MAX(y2-y1, 0);
            glScissor(x1, y1, width, height);
        }
        else
        {
            glEnable(GL_SCISSOR_TEST);
            glScissor(s_dip_to_px(frame.origin.x),
                      s_dip_to_px(frame.origin.y),
                      s_dip_to_px(frame.size.width) + 0.5f,
                      s_dip_to_px(frame.size.height) + 0.5f);
        }
    }

    this->sortAllSubviews();
    
    CAVector<CAView*>::iterator itr=m_obSubviews.begin();
    while (itr!=m_obSubviews.end())
    {
        CC_BREAK_IF((*itr)->m_nZOrder >= 0);
        (*itr)->visit();
        itr++;
    }
    
    this->draw();
    
    while (itr!=m_obSubviews.end())
    {
        (*itr)->visit();
        itr++;
    }
    
    //m_uOrderOfArrival = 0;
    
    if (!m_bDisplayRange)
    {
        if (isScissor)
        {
            glScissor(restoreScissorRect.origin.x,
                      restoreScissorRect.origin.y ,
                      restoreScissorRect.size.width,
                      restoreScissorRect.size.height);
        }
        else
        {
            glDisable(GL_SCISSOR_TEST);
        }
    }

    kmGLPopMatrix();
}