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( ¢er, size.width/2, size.height/2, 0.0f ); kmVec3Fill( &up, 0.0f, 1.0f, 0.0f); kmMat4LookAt(&matrixLookup, &eye, ¢er, &up); kmGLMultMatrix(&matrixLookup); } break; default: CCLOG("cocos2d: Director: unrecognized projection"); break; } m_eProjection = kProjection; ccSetProjectionMatrixDirty(); }
//------------------------------------------------------------------------- void FKCW_3D_Node::transform3D() { // visit()中调用 //for ordinary FKCW_3D_Node, we only mult matrix, not locate camrea kmMat4 transfrom4x4; memcpy(transfrom4x4.mat, m_mat.getArray(), 16*sizeof(float)); kmGLMultMatrix( &transfrom4x4 ); }
void CCRenderTexture::begin() { // Save the current matrix kmGLPushMatrix(); const CCSize& texSize = m_pTexture->getContentSizeInPixels(); // Calculate the adjustment ratios based on the old and new projections CCDirector *director = CCDirector::sharedDirector(); 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); }
void CARenderImage::begin() { kmGLMatrixMode(KM_GL_PROJECTION); kmGLPushMatrix(); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLPushMatrix(); // Calculate the adjustment ratios based on the old and new projections DSize size = CAApplication::getApplication()->getWinSize(); float widthRatio = size.width / m_uPixelsWide; float heightRatio = size.height / m_uPixelsHigh; // Adjust the orthographic projection and viewport glViewport(0, 0, (GLsizei)m_uPixelsWide, (GLsizei)m_uPixelsHigh); 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); }
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 CC3DSprite::transform() { kmMat4 transfrom4x4; // Update Z vertex manually transfrom4x4.mat[14]; //kmGLLoadIdentity() kmGLMultMatrix( &transfrom4x4 ); }
//设置一下观察与投影矩阵 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); }
void Node::transform() { #if CC_USE_PHYSICS updatePhysicsTransform(); #endif kmMat4 transfrom4x4 = this->getNodeToParentTransform(); kmGLMultMatrix( &transfrom4x4 ); // saves the MV matrix kmGLGetMatrix(KM_GL_MODELVIEW, &_modelViewTransform); }
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; }
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 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(); }
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(); }
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); }
//------------------------------------------------------------------------------ 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; }
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 ); } }
//------------------------------------------------------------------------------ void QNodeProps::draw() { QVec2 points[MAX_SHAPE_POINTS + 1]; QVec2* pVerts = points; int numVerts = 0; if (debugDraw == true) { // Assume the GL transform is correct (pos, rot, scale and anchor already accommodated) // Draw Box2D shape if (radius > 0.0f) { // Circle numVerts = g_QDirector->m_CircleNumVerts + 1; pVerts = (QVec2*)g_QDirector->m_CircleVerts; // Scale rotational part of transform by object scale // NOTE: WE ASSUME NO MORE RENDERING WITHIN THIS NODE'S SPACE AFTER THIS! float xCentre = radius - m_Node->xAnchor*radius*2; float yCentre = radius - m_Node->yAnchor*radius*2; // Initialise null 4x4 matrix kmMat4 t44; for (int m = 0; m < 16; m++) t44.mat[m] = 0.0f; // Set up to scale QDirector fixed circle vertex data t44.mat[0] = radius / 100.0f; t44.mat[5] = t44.mat[0]; t44.mat[10] = 1.0f; t44.mat[15] = 1.0f; // Offset because existing GL matrix is set up to render object from bottom-left t44.mat[12] = radius; t44.mat[13] = radius; kmGLMultMatrix(&t44); } else if (m_NumShapePoints > 0) { // Polygon numVerts = m_NumShapePoints + 1; // close shape // TODO OPTIMISE... REMOVE ARRAY COPY? float xOfs = m_Node->w * m_Node->xScale * m_Node->xAnchor; float yOfs = m_Node->h * m_Node->yScale * m_Node->yAnchor; int i = 0; for (; i < numVerts-1; i++) points[i].Set( xOfs + g_Sim->scaleP2D(m_ShapePoints[i].x), yOfs + g_Sim->scaleP2D(m_ShapePoints[i].y)); points[i].Set( xOfs + g_Sim->scaleP2D(m_ShapePoints[0].x), yOfs + g_Sim->scaleP2D(m_ShapePoints[0].y)); } else { // Rectangle points[0].Set(0.0f, 0.0f); points[1].Set(m_Node->w, 0.0f); points[2].Set(m_Node->w, m_Node->h); points[3].Set(0.0f, m_Node->h); numVerts = 4; } QDirector::DrawColor4B(debugDrawColor.r, debugDrawColor.g, debugDrawColor.b, 0xff); QDirector::DrawPolyVert2F((const ccVertex2F*)pVerts, numVerts, true); } }