Example #1
0
void CCGridBase::afterDraw(cocos2d::CCNode *pTarget)
{
    m_pGrabber->afterRender(m_pTexture);

    // restore projection
    CCDirector *director = CCDirector::sharedDirector();
    director->setProjection(m_directorProjection);

    if (pTarget->getCamera()->isDirty())
    {
        CCPoint offset = pTarget->getAnchorPointInPoints();

        //
        // XXX: Camera should be applied in the AnchorPoint
        //
        kmGLTranslatef(offset.x, offset.y, 0);
        pTarget->getCamera()->locate();
        kmGLTranslatef(-offset.x, -offset.y, 0);
    }

    ccGLBindTexture2D(m_pTexture->getName());

    // restore projection for default FBO .fixed bug #543 #544
//TODO:         CCDirector::sharedDirector()->setProjection(CCDirector::sharedDirector()->getProjection());
//TODO:         CCDirector::sharedDirector()->applyOrientation();
    blit();
}
Example #2
0
void GridBase::afterDraw(cocos2d::Node *target)
{
    _grabber->afterRender(_texture);

    // restore projection
    Director *director = Director::getInstance();
    director->setProjection(_directorProjection);

    if (target->getCamera()->isDirty())
    {
        Point offset = target->getAnchorPointInPoints();

        //
        // XXX: Camera should be applied in the AnchorPoint
        //
        kmGLTranslatef(offset.x, offset.y, 0);
        target->getCamera()->locate();
        kmGLTranslatef(-offset.x, -offset.y, 0);
    }

    GL::bindTexture2D(_texture->getName());

    // restore projection for default FBO .fixed bug #543 #544
//TODO:         Director::getInstance()->setProjection(Director::getInstance()->getProjection());
//TODO:         Director::getInstance()->applyOrientation();
    blit();
}
Example #3
0
void GameCamera::updateLSD(float dt)
{
	if (m_lsd)
	{
		kmGLMatrixMode(KM_GL_MODELVIEW);
		kmGLTranslatef(800.0f, 450.0f, 0.0f);
		kmGLRotatef(45.0f * dt, 1.0f, -1.0f, 1.0f);
		m_rot += 45.0f * dt;
		kmGLTranslatef(-800.0f, -450.0f, 0.0f);
	}
	else if (m_rot != 0.0f)
	{
		kmGLMatrixMode(KM_GL_MODELVIEW);
		kmGLTranslatef(800.0f, 450.0f, 0.0f);
		kmGLRotatef(-m_rot, 1.0f, -1.0f, 1.0f);
		m_rot = 0.0f;
		kmGLTranslatef(-800.0f, -450.0f, 0.0f);
	}
}
void CAView::transform()
{
    kmMat4 transfrom4x4;
    
    // Convert 3x3 into 4x4 matrix
    CATransformation tmpAffine = this->nodeToParentTransform();
    CGAffineToGL(&tmpAffine, transfrom4x4.mat);
    
    // Update Z vertex manually
    transfrom4x4.mat[14] = m_fVertexZ;
    
    kmGLMultMatrix( &transfrom4x4 );
    
    
    // XXX: Expensive calls. Camera should be integrated into the cached affine matrix
    if ( m_pCamera != NULL)
    {
        DPoint anchorPointInPoints = DPoint(m_obAnchorPointInPoints.x,
                                              m_obContentSize.height - m_obAnchorPointInPoints.y);
        
        bool translate = (anchorPointInPoints.x != 0.0f || anchorPointInPoints.y != 0.0f);

        if( translate )
            kmGLTranslatef(RENDER_IN_SUBPIXEL(anchorPointInPoints.x),
                           RENDER_IN_SUBPIXEL(anchorPointInPoints.y),
                           0 );
        
        m_pCamera->locate();
        
        if( translate )
            kmGLTranslatef(RENDER_IN_SUBPIXEL(-anchorPointInPoints.x),
                           RENDER_IN_SUBPIXEL(-anchorPointInPoints.y),
                           0 );
    }
    
}
Example #5
0
void CCSprite3D::transform(void)
{
    kmGLTranslatef(m_modelPosition.x, m_modelPosition.y, m_modelPosition.z);
    kmGLScalef(m_modelScale.x, m_modelScale.y, m_modelScale.z);
    kmGLRotatef(getRotation(), 0, 0, 1.0f);
}
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);

}