GAMES101-计算机图形学-作业8

最后一次了,断断续续终于炫完了入门课,泪目
你应该修改的函数是:
• rope.cpp 中的Rope::rope(…)
• rope.cpp 中的void Rope::simulateEuler(…)
• rope.cpp 中的void Rope::simulateVerlet(…)

Rope::rope(…):
直接初始化完事

Rope::Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k, vector<int> pinned_nodes)
    {
        // TODO (Part 1): Create a rope starting at `start`, ending at `end`, and containing `num_nodes` nodes.

//        Comment-in this part when you implement the constructor
//        for (auto &i : pinned_nodes) {
//            masses[i]->pinned = true;
//        }
        Vector2D step = (end - start) / (num_nodes + 1);
        for (int i = 0; i < num_nodes + 2; i++) {
            Mass* nodeCur = new Mass(start + i * step, node_mass, false);
            this->masses.push_back(nodeCur);
            if (i == 0)continue;
            Spring* springCur = new Spring(this->masses[i - 1], this->masses[i], k);
            this->springs.push_back(springCur);
        }
        for (auto& i : pinned_nodes) {
            masses[i]->pinned = true;
        }
    }

void Rope::simulateEuler(…):
gravity变量怎么加似乎有争议,看到有直接加到F上的,也有直接加到a上的,这里我是直接加到F上

void Rope::simulateEuler(float delta_t, Vector2D gravity)
    {
        for (auto &s : springs)
        {
            // TODO (Part 2): Use Hooke's law to calculate the force on a node
            Vector2D Vb2a = s->m2->position - s->m1->position;
            double dis = Vb2a.norm();
            Vb2a = Vb2a / dis;
            Vector2D Fb2a = -s->k * Vb2a * (dis - s->rest_length);
            Vector2D Fa2b = -Fb2a;
            s->m1->forces += Fa2b;
            s->m2->forces += Fb2a;

            // 加上Dumping
            Vector2D Vba = s->m2->velocity - s->m1->velocity;
            Vector2D Fa = -0.05 * (Vb2a.x * Vba.x + Vb2a.y * Vba.y) * (s->m2->position - s->m1->position) / dis;
            Vector2D Fb = -Fa;
            s->m1->forces += Fb;
            s->m2->forces += Fa;
        }

        for (auto &m : masses)
        {
            if (!m->pinned)
            {
                // TODO (Part 2): Add the force due to gravity, then compute the new velocity and position

                // TODO (Part 2): Add global damping
                m->forces += gravity;
                Vector2D a = m->forces / m->mass;
                m->velocity += a * delta_t;
                // m->position += m->velocity * delta_t;   //显式欧拉
                Vector2D vplus = m->velocity + a * delta_t;
                m->position += vplus * delta_t; //半隐式
            }

            // Reset all forces on each mass
            m->forces = Vector2D(0, 0);
        }
    }

void Rope::simulateVerlet(…)
没有细讲,但是直接抄公式然后更新last_position就行

void Rope::simulateVerlet(float delta_t, Vector2D gravity)
    {
        for (auto &s : springs)
        {
            // TODO (Part 3): Simulate one timestep of the rope using explicit Verlet (solving constraints)
            Vector2D Vb2a = s->m2->position - s->m1->position;
            double dis = Vb2a.norm();
            Vb2a = Vb2a / dis;
            Vector2D Fb2a = -s->k * Vb2a * (dis - s->rest_length);
            Vector2D Fa2b = -Fb2a;
            s->m1->forces += Fa2b;
            s->m2->forces += Fb2a;
        }

        for (auto &m : masses)
        {
            if (!m->pinned)
            {
                Vector2D temp_position = m->position;
                // TODO (Part 3.1): Set the new position of the rope mass
                
                // TODO (Part 4): Add global Verlet damping
                m->forces += gravity;
                Vector2D pos = m->position;
                m->position += (1 - 0.00005) * (m->position - m->last_position) +
                    m->forces / m->mass * pow(delta_t, 2);
                m->last_position = pos;
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在这部分的课程中,我们将专注于使用光线追踪来渲染图像。在光线追踪中 最重要的操作之一就是找到光线与物体的交点。一旦找到光线与物体的交点,就 可以执行着色并返回像素颜色。在这次作业中,我们需要实现两个部分:光线的 生成和光线与三角的相交。本次代码框架的工作流程为: 1. 从 main 函数开始。我们定义场景的参数,添加物体(球体或三角形)到场景 中,并设置其材质,然后将光源添加到场景中。 2. 调用 Render(scene) 函数。在遍历所有像素的循环里,生成对应的光线并将 返回的颜色保存在帧缓冲区(framebuffer)中。在渲染过程结束后,帧缓冲 区中的信息将被保存为图像。 3. 在生成像素对应的光线后,我们调用 CastRay 函数,该函数调用 trace 来 查询光线与场景中最近的对象的交点。 4. 然后,我们在此交点执行着色。我们设置了三种不同的着色情况,并且已经 为你提供了代码。 你需要修改的函数是: • Renderer.cpp 中的 Render():这里你需要为每个像素生成一条对应的光 线,然后调用函数 castRay() 来得到颜色,最后将颜色存储在帧缓冲区的相 应像素中。 • Triangle.hpp 中的 rayTriangleIntersect(): v0, v1, v2 是三角形的三个 顶点, orig 是光线的起点, dir 是光线单位化的方向向量。 tnear, u, v 是你需 要使用我们课上推导的 Moller-Trumbore 算法来更新的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值