Пример #1
0
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);
    
}
Пример #2
0
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);
}
Пример #3
0
void mrgss_renderer_resize_window(mrgss_screen* window) {
    kmMat4 ortho2D;
    mrgss_renderer* renderer = window->renderer;
    kmMat4OrthographicProjection(&ortho2D, 0.0f, window->width, window->height, 0.0f, 0.0f, 1.0f);
    glUseProgram(renderer->shader);
    glUniformMatrix4fv(glGetUniformLocation(renderer->shader, "orthoView"), 1, GL_FALSE, ortho2D.mat);
}
Пример #4
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);
    }
}
Пример #5
0
/* \brief set 2D projection matrix, and identity view matrx
 * 2D projection matrix sets screen coordinates where (0,0) == TOP LEFT (W,H) == BOTTOM RIGHT
 *
 *  TEXT    WORLD/3D
 *   -y        y
 * -x | x   -x | x
 *    y       -y
 *
 */
GLHCKAPI void glhckRenderProjection2D(int width, int height, kmScalar near, kmScalar far)
{
   kmMat4 ortho;
   GLHCK_INITIALIZED();
   CALL(2, "%d, %d, %f, %f", width, height, near, far);
   assert(width > 0 && height > 0);
   kmMat4OrthographicProjection(&ortho, 0, (kmScalar)width, (kmScalar)height, 0, near, far);
   glhckRenderProjectionOnly(&ortho);
   glhckRenderFlip(1);
}
Пример #6
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();
}
Пример #7
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);
    }
}
Пример #8
0
/* \brief set renderer's viewport */
GLHCKAPI void glhckRenderViewport(const glhckRect *viewport)
{
   kmMat4 ortho;
   GLHCK_INITIALIZED();
   CALL(1, RECTS, RECT(viewport));
   assert(viewport->x >= 0 && viewport->y >= 0 && viewport->w > 0 && viewport->h > 0);
   if (!_glhckRenderInitialized()) return;

   /* set viewport on render */
   GLHCKRA()->viewport(viewport->x, viewport->y, viewport->w, viewport->h);
   memcpy(&GLHCKRP()->viewport, viewport, sizeof(glhckRect));

   /* update orthographic matrix */
   kmMat4OrthographicProjection(&ortho, viewport->x, viewport->w, viewport->h, viewport->y, -1.0f, 1.0f);
   GLHCKRA()->setOrthographic(&ortho);
   memcpy(&GLHCKRD()->view.orthographic, &ortho, sizeof(kmMat4));
}
Пример #9
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();
}
Пример #10
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();
}
Пример #11
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);
}
Пример #12
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;
}
Пример #13
0
/* \brief calculate projection matrix */
static void _glhckCameraProjectionMatrix(glhckCamera *object)
{
   kmScalar w, h, distanceFromTarget;
   kmVec3 toTarget;

   CALL(2, "%p", object);
   assert(object);
   assert(object->view.viewport.w > 0.0f && object->view.viewport.h > 0.0f);

   switch(object->view.projectionType) {
      case GLHCK_PROJECTION_ORTHOGRAPHIC:
	 w = object->view.viewport.w > object->view.viewport.h ? 1 :
	    object->view.viewport.w / object->view.viewport.h;
	 h = object->view.viewport.w < object->view.viewport.h ? 1 :
	    object->view.viewport.h / object->view.viewport.w;

	 kmVec3Subtract(&toTarget, &object->object->view.translation, &object->object->view.target);
	 distanceFromTarget = kmVec3Length(&toTarget);

	 w *= (distanceFromTarget+object->view.near)/2;
	 h *= (distanceFromTarget+object->view.near)/2;

	 kmMat4OrthographicProjection(&object->view.projection,
	       -w, w, -h, h, object->view.near, object->view.far);
	 break;

      case GLHCK_PROJECTION_PERSPECTIVE:
      default:
	 kmMat4PerspectiveProjection(
	       &object->view.projection,
	       object->view.fov,
	       (float)object->view.viewport.w/(float)object->view.viewport.h,
	       object->view.near, object->view.far);
	 break;
   }
}
Пример #14
0
void Camera::set_orthographic_projection(double left, double right, double bottom, double top, double near, double far) {
    kmMat4OrthographicProjection(&projection_matrix_, left, right, bottom, top, near, far);
    update_frustum();
}
Пример #15
0
void initSprite(int w, int h)
{
    const GLfloat quadVertices[] = {
        -.5,	-.5,	0,
        .5,  	 .5,	0,
        .5,	-.5,	0,

        .5,  	 .5,	0,
        -.5,	-.5,	0,
        -.5,	 .5,	0
    };

    const GLfloat texCoord[] = {
        0,		0,
        1.,		1,
        1.,		0,
        1.,		1.,
        0,		0,
        0,		1.
    };


    kmMat4OrthographicProjection(&__spr.opm, 0, w, h, 0, -10, 10); // support layers ?

    GLuint vs, fs;
    vs = create_shader("resources/shaders/sprite.vert", GL_VERTEX_SHADER);
    fs = create_shader("resources/shaders/sprite.frag", GL_FRAGMENT_SHADER);

    __spr.spriteProg = glCreateProgram();
    glAttachShader(__spr.spriteProg, vs);
    glAttachShader(__spr.spriteProg, fs);
    glLinkProgram(__spr.spriteProg);
    int link_ok;
    glGetProgramiv(__spr.spriteProg, GL_LINK_STATUS, &link_ok);
    if (!link_ok) {
        printf("glLinkProgram:");
        print_log(__spr.spriteProg);
        printf("\n");
    }

    __spr.u_size = getShaderLocation(shaderUniform, __spr.spriteProg, "u_size");
    __spr.opm_uniform =
        getShaderLocation(shaderUniform, __spr.spriteProg, "opm_uniform");
    __spr.texture_uniform =
        getShaderLocation(shaderUniform, __spr.spriteProg, "texture_uniform");

    __spr.vert_attrib = getShaderLocation(shaderAttrib, __spr.spriteProg, "vert_attrib");
    __spr.uv_attrib = getShaderLocation(shaderAttrib, __spr.spriteProg, "uv_attrib");


    glGenBuffers(1, &__spr.quadvbo);
    glBindBuffer(GL_ARRAY_BUFFER, __spr.quadvbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, quadVertices,
                 GL_STATIC_DRAW);

    glGenBuffers(1, &__spr.texvbo);
    glBindBuffer(GL_ARRAY_BUFFER, __spr.texvbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 6, texCoord,
                 GL_STATIC_DRAW);

}
Пример #16
0
void initGlPrint(int w, int h)
{
    const GLfloat quadVertices[] = {
        0,	0,	0,
        16, 16, 0,
        16,	0,	0,

        16, 16, 0,
        0,	0,	0,
        0,	16,	0
    };

    const GLfloat texCoord[] = {
        0,			0,
        1. / 16,	1. / 16,
        1. / 16,	0,
        1. / 16,	1. / 16,
        0,			0,
        0,			1. / 16
    };


    kmMat4OrthographicProjection(&__glp.opm, 0, w, h, 0, -10, 10);

    GLuint vs, fs;
    vs = create_shader("resources/shaders/glprint.vert", GL_VERTEX_SHADER);
    fs = create_shader("resources/shaders/glprint.frag", GL_FRAGMENT_SHADER);

    __glp.printProg = glCreateProgram();
    glAttachShader(__glp.printProg, vs);
    glAttachShader(__glp.printProg, fs);
    glLinkProgram(__glp.printProg);
    int link_ok;
    glGetProgramiv(__glp.printProg, GL_LINK_STATUS, &link_ok);
    if (!link_ok) {
        printf("glLinkProgram:");
        print_log(__glp.printProg);
        printf("\n");
    }

    __glp.cx_uniform = getShaderLocation(shaderUniform, __glp.printProg, "cx");
    __glp.cy_uniform = getShaderLocation(shaderUniform, __glp.printProg, "cy");
    __glp.opm_uniform =
        getShaderLocation(shaderUniform, __glp.printProg, "opm_uniform");
    __glp.texture_uniform =
        getShaderLocation(shaderUniform, __glp.printProg, "texture_uniform");

    __glp.vert_attrib = getShaderLocation(shaderAttrib, __glp.printProg, "vert_attrib");
    __glp.uv_attrib = getShaderLocation(shaderAttrib, __glp.printProg, "uv_attrib");

    __glp.fonttex = loadPNG("resources/textures/font.png");

    glGenBuffers(1, &__glp.quadvbo);
    glBindBuffer(GL_ARRAY_BUFFER, __glp.quadvbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, quadVertices,
                 GL_STATIC_DRAW);

    glGenBuffers(1, &__glp.texvbo);
    glBindBuffer(GL_ARRAY_BUFFER, __glp.texvbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 6, texCoord,
                 GL_STATIC_DRAW);

}