当前位置:   article > 正文

【学习日志】2022.09.15 game-physics-cookbook---Point Test ​OpenGL - Drawing polygons​_gamephysicscookbook

gamephysicscookbook

vdoc.pub_game-physics-cookbook 

b7a231c5156d4b109443a85b1a680ed1.png 

 97bbb8d2ad5b4c039353a8cc6888b15d.png

396c140302884ec9b4bb42f6bc8dfab8.png 

4a85b7e97c5f4e6babc9b20b8b3b33db.png 

0de9d6fe5dd84a6b94a085ea2ae66269.png 

5cde97b1d1414960bd2b50cc0453d81c.png 

b6a7695eb14e42f8a475c3eaa292c537.png 

5baa5f7a8fb1406b8aec93453ca576d1.png 

主要用到这个:Point and plane 

c01e2137609c4494a8f2718d21006764.png

7e3181a0049d4f7ca305dd5905c141c6.png 

6b52771535d64b4aa8578e74667f1d1c.png 

fac0810676164bec851cfb7d6eb36143.png 

dc842aff2b6a448d8e922fafb552c996.png 

2b7eb780884249c4810a92033817c8b9.png 


 ZENO代码改进

 37ade7c4629c46e79f918550128b4783.png

把向量proj拆成a+b再算uv时  发现向量b*uv可以消掉,n便多余了,故可以改成:

  1. #include <zeno/zeno.h>
  2. #include <zeno/types/PrimitiveObject.h>
  3. #include <zeno/types/NumericObject.h>
  4. #include <stdexcept>
  5. #include "zeno/utils/log.h"
  6. #define CMP(x, y) \
  7. (fabsf(x - y) <= FLT_EPSILON * fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))))
  8. namespace zeno {
  9. struct UVProjectFromPlane : zeno::INode {
  10. virtual void apply() override {
  11. auto prim = get_input<PrimitiveObject>("prim");
  12. auto &uv = prim->verts.add_attr<vec3f>("uv");
  13. auto refPlane = get_input<PrimitiveObject>("refPlane");
  14. // bool PointOnPlane(const Point& point, const Plane& plane) {
  15. // // This should probably use an epsilon!
  16. // //return Dot(point, plane.normal) - plane.distance == 0.0f;
  17. // return CMP(Dot(point, plane.normal) - plane.distance, 0.0f);
  18. // }
  19. if (refPlane->verts.size() != 4) {
  20. zeno::log_error("refPlane must be 1 * 1 plane!");
  21. throw zeno::makeError("refPlane must be 1 * 1 plane!");
  22. }
  23. auto originPos = refPlane->verts[2];
  24. auto xOffset = refPlane->verts[0];
  25. auto yOffset = refPlane->verts[3];
  26. // zeno::log_info("xOffset:{}, originPos: {}", xOffset, originPos);
  27. auto uDir = zeno::normalize(xOffset - originPos);
  28. auto vDir = zeno::normalize(yOffset - originPos);
  29. auto uLength = zeno::length(xOffset - originPos);
  30. auto vLength = zeno::length(yOffset - originPos);
  31. // auto n = zeno::cross(uDir, vDir);
  32. // zeno::log_info("uDir:{], uLength: {}, n: {}", uDir, uLength, n);
  33. for (auto i = 0; i < prim->size(); i++) {
  34. auto &vert = prim->verts[i];
  35. auto offset = vert - originPos;
  36. auto proj = offset;
  37. auto u = zeno::clamp(zeno::dot(proj, uDir) / uLength, 0, 1);
  38. auto v = zeno::clamp(zeno::dot(proj, vDir) / vLength, 0, 1);
  39. uv[i] = zeno::vec3f(u, v, 0);
  40. }
  41. set_output("outPrim", std::move(prim));
  42. }
  43. };
  44. ZENDEFNODE(UVProjectFromPlane, {
  45. {
  46. {"PrimitiveObject", "prim"},
  47. {"PrimitiveObject", "refPlane"},
  48. },
  49. {
  50. {"PrimitiveObject", "outPrim"}
  51. },
  52. {},
  53. {"primitive"},
  54. });
  55. }

opengl知识点a84788c769b04e06b009442e1fff22a1.png

987fd15ce62e412b978f64f42f517f56.png

a7c9a46057244e7fbd7f7cf57b7bda9d.png 

cfeaefbc08f7d5f2df240969079f35fa.png

 a39d94587b4c4bd7b87486f224b29a17.png

最后的初始化和绘制代码现在看起来像这样: 

  1. // ..:: 初始化代码 :: ..
  2. // 1. 绑定顶点数组对象
  3. glBindVertexArray(VAO);
  4. // 2. 把我们的顶点数组复制到一个顶点缓冲中,供OpenGL使用
  5. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  6. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  7. // 3. 复制我们的索引数组到一个索引缓冲中,供OpenGL使用
  8. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  9. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  10. // 4. 设定顶点属性指针
  11. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  12. glEnableVertexAttribArray(0);
  13. [...]
  14. // ..:: 绘制代码(渲染循环中) :: ..
  15. glUseProgram(shaderProgram);
  16. glBindVertexArray(VAO);
  17. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  18. glBindVertexArray(0);

222b136cc5e940da82c5f18c2ae181d5.png

 OpenGL - Drawing polygons

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/520583
推荐阅读
相关标签
  

闽ICP备14008679号