Beispiel #1
0
void EGLView::setProjection(ccDirectorProjection kProjection)
{
	CCSize size = m_obDesignResolutionSize;

	setViewPortInPoints(0, 0, m_obDesignResolutionSize.width, m_obDesignResolutionSize.height);

	switch (kProjection)
	{
	case kCCDirectorProjection2D:
		{
			kmGLMatrixMode(KM_GL_PROJECTION);
			kmGLLoadIdentity();
			kmMat4 orthoMatrix;
			kmMat4OrthographicProjection(&orthoMatrix, 0, size.width, size.height, 0, -1024, 1024 );
			kmGLMultMatrix(&orthoMatrix);
			kmGLMatrixMode(KM_GL_MODELVIEW);
			kmGLLoadIdentity();
		}
		break;

	case kCCDirectorProjection3D:
		{
			float zeye = m_obDesignResolutionSize.height / 1.1566f;

			kmMat4 matrixPerspective, matrixLookup;

			kmGLMatrixMode(KM_GL_PROJECTION);
			kmGLLoadIdentity();

			// issue #1334
			kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, zeye*2);
			// kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 1500);

			kmGLMultMatrix(&matrixPerspective);

			kmGLMatrixMode(KM_GL_MODELVIEW);
			kmGLLoadIdentity();
			kmVec3 eye, center, up;
			kmVec3Fill( &eye, size.width/2, size.height/2, zeye );
			kmVec3Fill( &center, size.width/2, size.height/2, 0.0f );
			kmVec3Fill( &up, 0.0f, 1.0f, 0.0f);
			kmMat4LookAt(&matrixLookup, &eye, &center, &up);
			kmGLMultMatrix(&matrixLookup);
		}
		break;
	default:
		CCLOG("cocos2d: Director: unrecognized projection");
		break;
	}

	m_eProjection = kProjection;
	ccSetProjectionMatrixDirty();
}
//-------------------------------------------------------------------------
void FKCW_3D_Root::visit()
{
	//store depthTest state
	GLboolean isDoDepthTestOld;
	glGetBooleanv(GL_DEPTH_TEST,&isDoDepthTestOld);
	//push matrix
	kmGLMatrixMode(KM_GL_PROJECTION);
	kmGLPushMatrix();
	kmGLMatrixMode(KM_GL_MODELVIEW);
	kmGLPushMatrix();
	{
		//apply projection matrix
		getCamera3D()->applyProjection();
		//no matter what the modelview is, set it to identity
		kmGLMatrixMode(KM_GL_MODELVIEW);
		kmGLLoadIdentity();
		//apply view matrix
		getCamera3D()->locate();

		//call FKCW_3D_Node's visit
		FKCW_3D_Node::visit();
	}
	//pop matrix
	kmGLMatrixMode(KM_GL_PROJECTION);
	kmGLPopMatrix();
	kmGLMatrixMode(KM_GL_MODELVIEW);
	kmGLPopMatrix();
	//resotre depthTest state
	CCDirector::sharedDirector()->setDepthTest(static_cast<bool>(isDoDepthTestOld));
	//disable array attribute
	// KCW_3D_IndexVBO3d::disableAttribArrays();
}
void ClippingNode::drawFullScreenQuadClearStencil()
{
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLPushMatrix();
    kmGLLoadIdentity();
    
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLPushMatrix();
    kmGLLoadIdentity();
    
    DrawPrimitives::drawSolidRect(Point(-1,-1), Point(1,1), Color4F(1, 1, 1, 1));
    
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLPopMatrix();
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLPopMatrix();
}
Beispiel #4
0
void GridBase::set2DProjection()
{
    Director *director = Director::getInstance();

    Size    size = director->getWinSizeInPixels();

    glViewport(0, 0, (GLsizei)(size.width), (GLsizei)(size.height) );
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLLoadIdentity();

    kmMat4 orthoMatrix;
    kmMat4OrthographicProjection(&orthoMatrix, 0, size.width, 0, size.height, -1, 1);
    kmGLMultMatrix( &orthoMatrix );

    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLLoadIdentity();

    GL::setProjectionMatrixDirty();
}
Beispiel #5
0
void CCGridBase::set2DProjection()
{
    CCDirector *director = CCDirector::sharedDirector();

    CCSize    size = director->getWinSizeInPixels();

    glViewport(0, 0, (GLsizei)(size.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(size.height * CC_CONTENT_SCALE_FACTOR()) );
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLLoadIdentity();

    kmMat4 orthoMatrix;
    kmMat4OrthographicProjection(&orthoMatrix, 0, size.width * CC_CONTENT_SCALE_FACTOR(), 0, size.height * CC_CONTENT_SCALE_FACTOR(), -1, 1);
    kmGLMultMatrix( &orthoMatrix );

    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLLoadIdentity();


    ccSetProjectionMatrixDirty();
}
Beispiel #6
0
//设置一下观察与投影矩阵
void	C3DLayer::UpdateViewProjMatrix()
{
	//观察矩阵的设置
	Vec3 tEyeResult = Vec3(0,0,-2);
	Vec3 tLookAtResult = Vec3(0,0,0);
	Vec3 tUpResult = Vec3(0,1,0);
	Mat4 tViewMatrix;
	kmMat4LookAt(&tViewMatrix, &tEyeResult, &tLookAtResult, &tUpResult);
    
	kmGLMatrixMode(KM_GL_MODELVIEW);
	kmGLLoadIdentity();
	kmGLMultMatrix(&tViewMatrix);
    
	//投影矩阵的设置
	Mat4 matrixPerspective;
	kmGLMatrixMode(KM_GL_PROJECTION);
	kmGLLoadIdentity();
	CCSize size =  CCDirector::sharedDirector()->getVisibleSize();
	kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 500.0f);
	kmGLMultMatrix(&matrixPerspective);
	
}
Beispiel #7
0
void cxEngineLayout(cxInt width,cxInt height)
{
    CX_LOGGER("openGL layout width=%d height=%d",width,height);
    cxEngine engine = cxEngineInstance();
    engine->winsize = cxSize2fv(width, height);
    cxViewSetSize(engine->window, engine->winsize);
    if(!cxSize2Zero(engine->dessize)){
        engine->scale = cxVec2fv(engine->winsize.w/engine->dessize.w, engine->winsize.h/engine->dessize.h);
    }
    //
    if(!engine->isInit){
        cxOpenGLCheckFeature();
        cxEngineMain(engine);
    }
    //
    cxFloat zeye = engine->winsize.h / 1.1566f;
    kmMat4 perspective={0};
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLLoadIdentity();
    //
    kmMat4PerspectiveProjection(&perspective, 60.0f, engine->winsize.w / engine->winsize.h, 0.0f, zeye * 2);
    kmGLMultMatrix(&perspective);
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLLoadIdentity();
    //
    cxOpenGLViewport(cxBox4fv(0, engine->winsize.w, 0, engine->winsize.h));
    //
    cxMatrix4f matrix;
    cxEngineLookAt(&matrix,cxVec2fv(0, 0));
    kmGLMultMatrix(&matrix);
    //
    engine->lastTime = cxTimestamp();
    if(!engine->isInit){
        cxViewEnter(engine->window);
    }
    cxViewLayout(engine->window);
    engine->isInit = true;
}
Beispiel #8
0
//------------------------------------------------------------------------------
bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
#ifdef _WIN32
	pDirector->setProjection(kCCDirectorProjection2D);

    // Set EGLView frame size to *device* w,h
    float w = WINDOW_ACTUAL_W;
    float h = WINDOW_ACTUAL_H;
    CCEGLView::sharedOpenGLView()->setFrameSize(w, h);
    
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLLoadIdentity();

    // New code: rotate to achieve landscape orientation
    int angle = 0;
    kmGLRotatef((float)angle, 0, 0, 1);
    
    kmMat4 orthoMatrix;
    kmMat4OrthographicProjection(&orthoMatrix, 0, w, 0, h, -1024, 1024 );
    kmGLMultMatrix(&orthoMatrix);
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLLoadIdentity();
#endif

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // OpenQuick stuff
    InitLuaSystem();
    InitLuaMiddleware();
    InitLuaApp();

    return true;
}
Beispiel #9
0
void Layout::stencilClippingVisit()
{
    if (!_clippingStencil || !_clippingStencil->isVisible())
    {
        CCNode::visit();
        return;
    }
    if (g_sStencilBits < 1)
    {
        CCNode::visit();
        return;
    }
    static GLint layer = -1;
    if (layer + 1 == g_sStencilBits)
    {
        static bool once = true;
        if (once)
        {
            char warning[200] = {0};
            snprintf(warning, sizeof(warning), "Nesting more than %d stencils is not supported. Everything will be drawn without stencil for this node and its childs.", g_sStencilBits);
            CCLOG("%s", warning);
            
            once = false;
        }
        CCNode::visit();
        return;
    }
    layer++;
    GLint mask_layer = 0x1 << layer;
    GLint mask_layer_l = mask_layer - 1;
    GLint mask_layer_le = mask_layer | mask_layer_l;
    GLboolean currentStencilEnabled = GL_FALSE;
    GLuint currentStencilWriteMask = ~0;
    GLenum currentStencilFunc = GL_ALWAYS;
    GLint currentStencilRef = 0;
    GLuint currentStencilValueMask = ~0;
    GLenum currentStencilFail = GL_KEEP;
    GLenum currentStencilPassDepthFail = GL_KEEP;
    GLenum currentStencilPassDepthPass = GL_KEEP;
    currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST);
    glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)&currentStencilWriteMask);
    glGetIntegerv(GL_STENCIL_FUNC, (GLint *)&currentStencilFunc);
    glGetIntegerv(GL_STENCIL_REF, &currentStencilRef);
    glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&currentStencilValueMask);
    glGetIntegerv(GL_STENCIL_FAIL, (GLint *)&currentStencilFail);
    glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)&currentStencilPassDepthFail);
    glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)&currentStencilPassDepthPass);
    glEnable(GL_STENCIL_TEST);
    CHECK_GL_ERROR_DEBUG();
    glStencilMask(mask_layer);
    GLboolean currentDepthWriteMask = GL_TRUE;
    glGetBooleanv(GL_DEPTH_WRITEMASK, &currentDepthWriteMask);
    glDepthMask(GL_FALSE);
    glStencilFunc(GL_NEVER, mask_layer, mask_layer);
    glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLPushMatrix();
    kmGLLoadIdentity();
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLPushMatrix();
    kmGLLoadIdentity();
    ccDrawSolidRect(CCPoint(-1,-1), CCPoint(1,1), ccc4f(1, 1, 1, 1));
    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLPopMatrix();
    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLPopMatrix();
    glStencilFunc(GL_NEVER, mask_layer, mask_layer);
    glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
    kmGLPushMatrix();
    transform();
    _clippingStencil->visit();
    kmGLPopMatrix();
    glDepthMask(currentDepthWriteMask);
    glStencilFunc(GL_EQUAL, mask_layer_le, mask_layer_le);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    CCNode::visit();
    glStencilFunc(currentStencilFunc, currentStencilRef, currentStencilValueMask);
    glStencilOp(currentStencilFail, currentStencilPassDepthFail, currentStencilPassDepthPass);
    glStencilMask(currentStencilWriteMask);
    if (!currentStencilEnabled)
    {
        glDisable(GL_STENCIL_TEST);
    }
    layer--;
}
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);

}