所以我有一个物体(球体),我想将球体的y轴与用于绘制其中点的矢量对齐。
现在,所有创建的对象都有轴'如下所示设置,因此在世界空间中创建的所有内容都具有轴方向,如下图所示。但是我希望球体的轴在局部坐标中的y轴在它们的父对象(它们所围绕的球体)的方向上,但是我不确定我可以为每个对象做什么毯子转换为了这个转型发生。
下图是物体的理想方向。
我需要这个方向,所以孩子的球体而不是像他们在下面的两张图片中那样向上指向,根据方向向外突出。因此,如果我要在底部生成一个球体,而不是在其x轴顶部生成子球体,然后将其与大中心球体合并,则它们向外指向远离大中心球体
下面是我生成球体的代码(我在这段代码中产生了比图9更多的球体,但是减少了图片中存在的球体数量,以便澄清并且不会使图像混乱)
bool static CreateSphereLevels(Sphere *parent, float childRadiusRatio, int levels,
Material** materials)
{
if (levels == 1) {
for (int i = 0; i < 6; i++) {
Sphere * s = new Sphere(childRadiusRatio, materials[i % 6]);
parent->AddChild(s);
float rm = parent->GetRadius() + s->GetRadius();
if (i == 0)
s->SetPosition(vec3(0.0f, rm, 0.0f));
if (i >= 1 && i < 6)
s->SetPosition(vec3(RotationY(60 * i) * vec4(0.0f, rm, 0.0f, 0.0f)));
if (i == 1 || i == 3 || i == 5) {
Sphere * sn = new Sphere(childRadiusRatio, materials[i]);
parent->AddChild(sn);
sn->SetPosition(vec3(RotationY(60 * i) * RotationZ(60) * vec4(0.0f, rm, 0.0f, 0.0f)));
}
}
return true;
}
else {
for (int i = 0; i < 6; i++) {
Sphere * s = new Sphere(childRadiusRatio, materials[i % 6]);
parent->AddChild(s);
int newLevels = levels - 1;
CreateSphereLevels(s, (childRadiusRatio / 3), newLevels, materials);
float rm = parent->GetRadius() + s->GetRadius();
if (i == 0)
s->SetPosition(vec3(0.0f, rm, 0.0f));
if (i >= 1 && i < 6)
s->SetPosition(vec3(RotationY(60 * i) * vec4(0.0f, rm, 0.0f, 0.0f)));
if (i == 1 || i == 3 || i == 5) {
Sphere * sn = new Sphere(childRadiusRatio, materials[i]);
parent->AddChild(sn);
CreateSphereLevels(sn, (childRadiusRatio / 3), newLevels, materials);
sn->SetPosition(vec3(RotationY(60 * i) * RotationZ(60) * vec4(0.0f, rm, 0.0f, 0.0f)));
}
}
}
return true;
}
下面是可以对我的对象执行的可能转换
#ifndef RAYTRACER_SCENES_SCENEOBJECT_H
#define RAYTRACER_SCENES_SCENEOBJECT_H
#include <Raytracer/Scenes/SceneObjectType.h>
#include <vector>
namespace Raytracer
{
namespace Scenes
{
class SceneObject
{
private:
/**
* The transformation matrix to transform a point from world coordinates to local
* coordinates.
*/
glm::mat4x4 globalTransformation;
glm::mat4x4 transformation;
glm::mat4x4 globalToLocal;
/**
* Updates the global transformation based on the current transformation. This
* includes child objects.
*/
void UpdateTransformations();
std::vector<SceneObject *> children;
SceneObject * parent;
public:
/**
* Constructs a new SceneObject.
*/
SceneObject();
/**
* Destructs a SceneObject and deletes all child objects.
*/
virtual ~SceneObject();
/**
* Adds a new child to this object.
*
* @param child The new child object. This object becomes child's parent object. This
* object takes ownership of child.
* @return true if the child was added successfully, false otherwise.
*/
bool AddChild(SceneObject *child);
/**
* Retrieves a list of all children of this object.
*
* @return A list of all children of this object
*/
const std::vector<SceneObject *> &GetChildren() const;
/**
* Retrieves the position of this object in world space, i.e. the translation component
* of the global transformation matrix.
*
* @return The global position of this object
*/
const glm::vec3 GetGlobalPosition() const;
/**
* Retrieves a matrix that can be used to transform coordinates from world space to
* object space. This is the inverse of the global transformation matrix.
*
* @return The inverse of the global transformation matrix
*/
const glm::mat4x4 &GetGlobalToLocal() const;
/**
* Retrieves the global transformation matrix. The global transformation matrix is used
* to transform coordinates from object space to world space.
*
* @return The global transformation matrix
*/
const glm::mat4x4 &GetGlobalTransformation() const;
/**
* Retrieves the parent object of this object.
*
* @param The parent object of this object or NULL if this object has no parent
*/
SceneObject *GetParent() const;
/**
* Retrieves the position of this object, i.e. the translation component of the
* transformation matrix.
*
* @return The position of the object
*/
const glm::vec3 GetPosition() const;
/**
* Retrieves the transformation matrix. The transformation matrix is used to transform
* coordinates from object space to the object space of the parent object (or world
* space if this object has no parent).
*
* @return The transformation matrix
*/
const glm::mat4x4 &GetTransformation() const;
/**
* Checks whether this instance is of the given type.
*
* @param type The type to check against
* @return true if this object is of type type, false otherwise
*/
virtual bool IsInstanceOf(SceneObjectType type) const = 0;
/**
* Sets the global transformation matrix. The global transformation matrix is used
* to transform coordinates from object space to world space.
*
* @param transformation The new global transformation matrix
*/
void SetGlobalTransformation(const glm::mat4x4 &transformation);
/**
* Sets the position of this object, i.e. the translation component of the
* transformation matrix.
*
* @param position The new position
*/
void SetPosition(const glm::vec3 &position);
/**
* Sets the transformation matrix. The transformation matrix is used to transform
* coordinates from object space to the object space of the parent object (or world
* space if this object has no parent).
*
* @param transformation The new transformation matrix
*/
void SetTransformation(const glm::mat4x4 &transformation);
void ChildRemove(SceneObject * child);
};
}
}
#endif // RAYTRACER_SCENES_SCENEOBJECT_H
答案 0 :(得分:0)
换句话说,您想要一种方法将您的子球体指向父球体。
通过从父项中减去子项的位置(以获得指向父项中心的Y轴)来计算子项的新Y轴方向并对其进行标准化。通过将新的Y轴向量与现有的Z轴向量(在左手系统中)交叉来计算新的X轴,注意处理它们指向相同方向或彼此完全相反的情况(可能正规化以移除错误)。然后将这个新的X轴向量与新的Y轴向量交叉以获得新的Z轴向量。将其存储在孩子的方向矩阵中。