5.4.6. Modified draw loop

The code in the original draw loop is moved to a draw() method.

The FPS calculation has been removed. See Example 5.25:

Example 5.25. Modified draw functionality in lightshow.cpp

void LightShow::draw() {
    Context* context = getApplication()->getContext();

    float deltat = getTime() * 0.3f - time;
    time = getTime() * 0.3f;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    float scenetime = time;
    while (scenetime > 10)
        scenetime -= 10;

    MDE::Program* prg;
    if (styggShader)
        prg = prg_gl1;
    else
        prg = prg_gl2;

    mat4 rootTransform = mat4::identity();
    traverseTransform(scene->getTree(0), rootTransform, scenetime);
    vec3 camPos = camNode->getAbsolutePosition();
    vec3 targetPos = targetNode->getAbsolutePosition();

    mat4 view = mat4::lookAt(camPos, targetPos, vec3(0, 0, 1));
    mat4 proj = mat4::perspective(80, 1.33333f, 1, 500);

    context->setProgram(prg);
    context->setUniformMatrix("VIEW_PROJECTION", proj * view);

    vec3 light0position = light0Node->getAbsolutePosition();
    vec3 light0color(0.66f + fabs(sinf(time * 0.1f)) * 0.4f,
                     0.66f + fabs(sinf(time * 0.12f + 0.4f)) * 0.4f,
                     0.66f + fabs(sinf(time * 0.18f + 0.989f)) * 0.4f);
    context->setUniform("light0position", light0position);
    context->setUniform("light0color", light0color);

    vec3 light1position = light1Node->getAbsolutePosition();
    vec3 light1color(0.66f + fabs(sinf(time * 0.1f)) * 0.4f,
                     0.66f + fabs(sinf(time * 0.12f + 0.14f)) * 0.4f,
                     0.66f + fabs(sinf(time * 0.18f + 0.389f)) * 0.4f);
    context->setUniform("light1position", light1position);
    context->setUniform("light1color", light1color);

    context->setUniform("cameraPosition", camPos);

    float cyl_radius = sqrt(30.0f);

    vec3 shadow_info[2] = 
     { calculate_cyl_info(vec2(-30.0, -30.0), cyl_radius, 
                          vec2(light0position.x,light0position.y), 0.0),
     calculate_cyl_info(vec2(30.0, 30.0), cyl_radius, 
                          vec2(light0position.x, light0position.y), 0.0) };

    context->setUniform("shadow_info[0]", shadow_info[0]);
    context->setUniform("shadow_info[1]", shadow_info[1]);

    glEnable(GL_DEPTH_TEST);
    traverseDraw(scene->getTree(0), context);

    glDepthMask(GL_FALSE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    context->setProgram(flarePrg);
    context->setUniformMatrix("VIEW", view);
    context->setUniformMatrix("PROJECTION", proj);
    context->setUniformSampler("colorTex", flareTex);
    drawFlare(context, light0position, light0color, 10);
    glDepthMask(GL_TRUE);
    glDisable(GL_BLEND);

    // new code to make the widget visible and draw the image
    wg->setVisible(true);
    Applet::draw();
}


Copyright © 2010 ARM. All rights reserved.ARM DUI 0527A‑02a
Non-Confidential - Draft - BetaID070710