Esempio n. 1
0
const char*
scale_scene_node(const char *node, point_t center, scalar_t factor[3] ) 
{
    scene_node_t *nodePtr;
    matrixgl_t matrix;

    if ( get_scene_node( node, &nodePtr ) != TCL_OK ) {
        return "No such node";
    } 

    make_translation_matrix( matrix, -center.x, -center.y, -center.z );
    multiply_matrices( nodePtr->trans, nodePtr->trans, matrix );
    make_translation_matrix( matrix, center.x, center.y, center.z );
    multiply_matrices( nodePtr->invtrans, matrix, nodePtr->invtrans );

    make_scaling_matrix( matrix, factor[0], factor[1], factor[2] );
    multiply_matrices( nodePtr->trans, nodePtr->trans, matrix );
    make_scaling_matrix( matrix, 1./factor[0], 1./factor[1], 1./factor[2] );
    multiply_matrices( nodePtr->invtrans, matrix, nodePtr->invtrans );

    make_translation_matrix( matrix, center.x, center.y, center.z );
    multiply_matrices( nodePtr->trans, nodePtr->trans, matrix );
    make_translation_matrix( matrix, -center.x, -center.y, -center.z );
    multiply_matrices( nodePtr->invtrans, matrix, nodePtr->invtrans );

    return NULL;
}
Esempio n. 2
0
void make_inverse_transformation_matrix(
    Matrix4* mat, const Vector3& pos, const Quaternion& ori, const Vector3& scl )
{
    // assumes orientation is normalized
    Matrix4 sclmat, orimat, trnmat;

    make_scaling_matrix( &sclmat, Vector3( 1.0 / scl.x, 1.0 / scl.y, 1.0 / scl.z ) );
    conjugate( ori ).to_matrix( &orimat );
    make_translation_matrix( &trnmat, -pos );

    *mat = sclmat * orimat * trnmat;
}
Esempio n. 3
0
void make_transformation_matrix(
    Matrix4* mat, const Vector3& pos, const Quaternion& ori, const Vector3& scl )
{
    Matrix4 sclmat, orimat;

    ori.to_matrix( &orimat );
    make_scaling_matrix( &sclmat, scl );
    *mat = orimat * sclmat;
    // don't need to actually do the multiplication, can take shortcut
    // since we're multiplying translation by a linear matrix
    mat->m[12] = pos.x;
    mat->m[13] = pos.y;
    mat->m[14] = pos.z;
}
    void on_draw() override
    {
        glfwMakeContextCurrent(window);

        if (igm) igm->begin_frame();

        glEnable(GL_CULL_FACE);
        glEnable(GL_DEPTH_TEST);

        int width, height;
        glfwGetWindowSize(window, &width, &height);
        glViewport(0, 0, width, height);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(1.0f, 0.1f, 0.0f, 1.0f);

        ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);

        ImGui::SliderFloat("Projectile Velocity", &turret.velocity, 0.f, 100.f);

        if (ImGui::Button("Fire"))
        {
            turret.projectile = BallisticProjectile();
            turret.projectile.p = turret.source.pose;
            turret.projectile.lastPos = turret.source.pose.position;
            turret.projectile.gravity = 0.0;

            /*
            float3 firingSolutionLow;
            float3 firingSolutionHigh;
            auto numSolutions = solve_ballistic_arc(turret.source.pose.position, turret.velocity, turret.target.pose.position, 9.8f, firingSolutionLow, firingSolutionHigh);
            */

            float3 fireVelocity;
            float gravity;
            auto canFire = solve_ballistic_arc_lateral(turret.source.pose.position, turret.velocity, turret.target.pose.position, 10.f, fireVelocity, gravity);

            //std::cout << "Num Solutions: " << numSolutions << std::endl;
            //std::cout << "Low Solution: " << firingSolutionLow << std::endl;
            std::cout << "Fire Velocity: " << fireVelocity << std::endl;

            if (canFire)
            {
                turret.projectile.gravity = gravity;
                turret.projectile.add_impulse(fireVelocity);
                turret.fired = true;
            }
        }

        ImGui::Spacing();

        if (ImGui::SliderInt("M", &ssM, 1, 30)) regeneratePointer = true;
        if (ImGui::SliderInt("N1", &ssN1, 1, 30)) regeneratePointer = true;
        if (ImGui::SliderInt("N2", &ssN2, 1, 30)) regeneratePointer = true;
        if (ImGui::SliderInt("N3", &ssN3, 1, 30)) regeneratePointer = true;

        ImGui::Spacing();

        ImGui::BeginGroup();

        if (ImGui::SliderFloat3("Position", &params.position.x, -5, 5))
            parabolicPointer = make_parabolic_pointer(worldSurface, params);

        if (ImGui::SliderFloat3("Velocity", &params.velocity.x, -1.f, 1.f))
            parabolicPointer = make_parabolic_pointer(worldSurface, params);

        if (ImGui::SliderFloat("Point Spacing", &params.pointSpacing, 0.5, 2.0))
            parabolicPointer = make_parabolic_pointer(worldSurface, params);

        if (ImGui::SliderFloat("Point Count", &params.pointCount, 16, 64))
            parabolicPointer = make_parabolic_pointer(worldSurface, params);

        ImGui::EndGroup();

        const auto proj = camera.get_projection_matrix((float) width / (float) height);
        const float4x4 view = camera.get_view_matrix();
        const float4x4 viewProj = mul(proj, view);

        glViewport(0, 0, width, height);

        skydome.render(viewProj, camera.get_eye_point(), camera.farClip);
        grid.render(proj, view);

        {
            simpleShader->bind();

            simpleShader->uniform("u_eye", camera.get_eye_point());
            simpleShader->uniform("u_viewProj", viewProj);

            simpleShader->uniform("u_emissive", float3(0, 0, 0));
            simpleShader->uniform("u_diffuse", float3(0.0f, 1.0f, 0.0f));

            for (int i = 0; i < 2; i++)
            {
                simpleShader->uniform("u_lights[" + std::to_string(i) + "].position", lights[i].position);
                simpleShader->uniform("u_lights[" + std::to_string(i) + "].color", lights[i].color);
            }

            for (const auto & model : shadedModels)
            {
                simpleShader->uniform("u_modelMatrix", model.get_model());
                simpleShader->uniform("u_modelMatrixIT", inv(transpose(model.get_model())));
                model.draw();
            }

            {
                simpleShader->uniform("u_modelMatrix", turret.source.get_model());
                simpleShader->uniform("u_modelMatrixIT", inv(transpose(turret.source.get_model())));
                turret.source.draw();

                simpleShader->uniform("u_modelMatrix", turret.target.get_model());
                simpleShader->uniform("u_modelMatrixIT", inv(transpose(turret.target.get_model())));
                turret.target.draw();

                auto projectileMat = turret.projectile.p.matrix();
                simpleShader->uniform("u_modelMatrix", projectileMat);
                simpleShader->uniform("u_modelMatrixIT", inv(transpose(projectileMat)));
                turret.bullet.draw();
            }

            simpleShader->unbind();
        }

        {
            normalDebugShader->bind();
            normalDebugShader->uniform("u_viewProj", viewProj);

            // Some debug models
            for (const auto & model : debugModels)
            {
                normalDebugShader->uniform("u_modelMatrix", model.get_model());
                normalDebugShader->uniform("u_modelMatrixIT", inv(transpose(model.get_model())));
                model.draw();
            }

            // Supershape
            {
                normalDebugShader->uniform("u_modelMatrix", supershape.get_model());
                normalDebugShader->uniform("u_modelMatrixIT", inv(transpose(supershape.get_model())));
                supershape.draw();
            }

            // Parabolic pointer
            {
                normalDebugShader->uniform("u_modelMatrix", parabolicPointer.get_model());
                normalDebugShader->uniform("u_modelMatrixIT", inv(transpose(parabolicPointer.get_model())));
                parabolicPointer.draw();
            }

            // Parallel transport boxes
            for (int i = 0; i < ptfBoxes.size(); ++i)
            {
                auto & model = ptfBoxes[i];
                auto modelMat = ptf[i];
                normalDebugShader->uniform("u_modelMatrix", mul(modelMat, make_scaling_matrix(0.01)));
                normalDebugShader->uniform("u_modelMatrixIT", inv(transpose(modelMat)));
                model.draw();
            }

            normalDebugShader->unbind();
        }

        gl_check_error(__FILE__, __LINE__);
        if (igm) igm->end_frame();
        glfwSwapBuffers(window);
        frameCount++;
    }