Example #1
0
void setPerspective(int w, int h) {

	float  Proj[4][4];             		// Projection matrix
	memset(Proj, 0, sizeof(Proj));
	// Setup the Projection matrix
	Persp(Proj, w, h, 40.0f, 0.1f, 200.0f);
	glUniformMatrix4fv      (iProj, 1, false, (const float *)&Proj[0][0]);
	// Ortho(Proj, -0.5, 0.05, -0.5*Surface->w/(float)Surface->h, 0.5*Surface->w/(float)Surface->h, 0.1, 100);

}
void DemoInit(NaCLContext* psNaCLContext, int width, int height)
{
    PP_Resource context = psNaCLContext->hRenderContext;
    PPB_OpenGLES2* gl = psNaCLContext->psGL;

    const float aspectRatio = (float)width / (float)height;
    const float fieldOfView = 45.0f;

    if(!psDemoContext)
    {
        psDemoContext = new DemoContext();

#ifdef ENABLE_LOADING_ICON
        DownloadContext* psLoadingTextureDownloadContext = &psDemoContext->sDownloadLoadingTexture;
        psLoadingTextureDownloadContext->id = 2;
        psLoadingTextureDownloadContext->iDownloaded = 0;

        StartDownload(psNaCLContext, "dload/loading.jpg", psLoadingTextureDownloadContext, FileDownloaded);
#endif

        DownloadContext* psBaseTextureDownloadContext = &psDemoContext->sDownloadBaseTexture;
        psBaseTextureDownloadContext->id = 0;
        psBaseTextureDownloadContext->iDownloaded = 0;

        StartDownload(psNaCLContext, "dload/checkerboard.jpg", psBaseTextureDownloadContext, FileDownloaded);

        DownloadContext* psMeshDownloadContext = &psDemoContext->sDownloadMesh;
        psMeshDownloadContext->id = 1;
        psMeshDownloadContext->iDownloaded = 0;

        StartDownload(psNaCLContext, "dload/cube.json", psMeshDownloadContext, FileDownloaded);
    }

#if 0
#ifdef ENABLE_LOADING_ICON
    if(psDemoContext->sDownloadLoadingTexture.iDownloaded == 2)
    {
        //Re-create GL objects
        psDemoContext->sDownloadLoadingTexture.iDownloaded = 1;
    }
#endif

    if(psDemoContext->sDownloadBaseTexture.iDownloaded == 2)
    {
        //Re-create GL objects
        psDemoContext->sDownloadBaseTexture.iDownloaded = 1;
    }

    if(psDemoContext->sDownloadMesh.iDownloaded == 2)
    {
        //Re-create GL objects
        psDemoContext->sDownloadMesh.iDownloaded = 1;
    }
#endif

    Identity(psDemoContext->sMeshTransform.projection);
    Persp(psDemoContext->sMeshTransform.projection, fieldOfView, aspectRatio,
        1.0, 1000.0);

    if(!psDemoContext->sDisplacementMap.image) //First time init has been called
    {
        int i;

        GLubyte* textureImage = new GLubyte[iDispMapWidth*iDispMapHeight];

        for(i=0; i<(iDispMapWidth*iDispMapHeight); ++i)
        {
            textureImage[i] = 0;
        }

        psDemoContext->sDisplacementMap.image = textureImage;

        gettimeofday(&psDemoContext->startTime, NULL);

        psDemoContext->ui64BaseTimeMS = GetElapsedTimeMS();
        psDemoContext->fAngleY = 0;
        psDemoContext->fAngleX = 0;
        psDemoContext->fDispCoeff = 0.5f;
    }

    psDemoContext->hDisplacementMapShader = CreateProgram(context, gl, psz_dispmapped_vs, psz_textured_ps);

#ifdef ENABLE_LOADING_ICON
    psDemoContext->hBaseMapShader = CreateProgram(context, gl, psz_textured_vs, psz_textured_ps);

    CreateQuadMesh(context,
        gl,
        &psDemoContext->sQuad.iIdxCount,
        &psDemoContext->sQuad.iVtxCount,
        &psDemoContext->sQuad.hVBO,
        &psDemoContext->sQuad.hIBO);
#endif

    gl->GenTextures(context, 1, &psDemoContext->sDisplacementMap.hTextureGL);

    gl->BindTexture(context, GL_TEXTURE_2D, psDemoContext->sDisplacementMap.hTextureGL);

    gl->TexImage2D(context, GL_TEXTURE_2D, 0, GL_LUMINANCE, iDispMapWidth, iDispMapHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, psDemoContext->sDisplacementMap.image);

    gl->TexParameteri(context, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    gl->TexParameteri(context, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    Identity(psDemoContext->sMeshTransform.camera);
    LookAt(psDemoContext->sMeshTransform.camera,
        0.f,0.f,3.f,
        0.f,0.f,-5.f,
        0.f,1.f,0.f);

    gl->Enable(context, GL_CULL_FACE);
}