赞
踩
把向量proj拆成a+b再算uv时 发现向量b*uv可以消掉,n便多余了,故可以改成:
- #include <zeno/zeno.h>
- #include <zeno/types/PrimitiveObject.h>
- #include <zeno/types/NumericObject.h>
- #include <stdexcept>
- #include "zeno/utils/log.h"
- #define CMP(x, y) \
- (fabsf(x - y) <= FLT_EPSILON * fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))))
-
- namespace zeno {
- struct UVProjectFromPlane : zeno::INode {
- virtual void apply() override {
- auto prim = get_input<PrimitiveObject>("prim");
- auto &uv = prim->verts.add_attr<vec3f>("uv");
- auto refPlane = get_input<PrimitiveObject>("refPlane");
-
- // bool PointOnPlane(const Point& point, const Plane& plane) {
- // // This should probably use an epsilon!
- // //return Dot(point, plane.normal) - plane.distance == 0.0f;
- // return CMP(Dot(point, plane.normal) - plane.distance, 0.0f);
- // }
-
- if (refPlane->verts.size() != 4) {
- zeno::log_error("refPlane must be 1 * 1 plane!");
- throw zeno::makeError("refPlane must be 1 * 1 plane!");
- }
- auto originPos = refPlane->verts[2];
- auto xOffset = refPlane->verts[0];
- auto yOffset = refPlane->verts[3];
- // zeno::log_info("xOffset:{}, originPos: {}", xOffset, originPos);
- auto uDir = zeno::normalize(xOffset - originPos);
- auto vDir = zeno::normalize(yOffset - originPos);
- auto uLength = zeno::length(xOffset - originPos);
- auto vLength = zeno::length(yOffset - originPos);
- // auto n = zeno::cross(uDir, vDir);
- // zeno::log_info("uDir:{], uLength: {}, n: {}", uDir, uLength, n);
- for (auto i = 0; i < prim->size(); i++) {
- auto &vert = prim->verts[i];
- auto offset = vert - originPos;
- auto proj = offset;
- auto u = zeno::clamp(zeno::dot(proj, uDir) / uLength, 0, 1);
- auto v = zeno::clamp(zeno::dot(proj, vDir) / vLength, 0, 1);
- uv[i] = zeno::vec3f(u, v, 0);
- }
- set_output("outPrim", std::move(prim));
- }
- };
-
- ZENDEFNODE(UVProjectFromPlane, {
- {
- {"PrimitiveObject", "prim"},
- {"PrimitiveObject", "refPlane"},
- },
- {
- {"PrimitiveObject", "outPrim"}
- },
- {},
- {"primitive"},
- });
- }
最后的初始化和绘制代码现在看起来像这样:
- // ..:: 初始化代码 :: ..
- // 1. 绑定顶点数组对象
- glBindVertexArray(VAO);
- // 2. 把我们的顶点数组复制到一个顶点缓冲中,供OpenGL使用
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- // 3. 复制我们的索引数组到一个索引缓冲中,供OpenGL使用
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- // 4. 设定顶点属性指针
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
-
- [...]
-
- // ..:: 绘制代码(渲染循环中) :: ..
- glUseProgram(shaderProgram);
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。