2017-01-04 1 views
0

그래서 개체 (구)가 있고 구의 y 축을 사용 된 벡터와 정렬하려고합니다. 그것의 중간 점을 그렸다.개체의 Y 축을 부모의 중심점에서 방사하는 벡터로 맞추는 방법

생성 된 모든 객체의 축은 아래와 같이 설정되어 있으므로 월드 공간에서 생성 된 모든 객체는 아래 그림과 같이 축 방향을 갖습니다. 그러나 지역 좌표계의 구체 축이 부모 개체 (서라운드 영역)의 방향으로 y 축을 갖기를 원하지만이 변환을 위해 각 개체에 대해 수행 할 수있는 블랭킷 변환을 확신 할 수 없습니다. 나오다. 아래 그림

enter image description here

은 개체의 원하는 방향이다.

Desired orientation of object axis'

나는이 방향을 필요로하므로 아이는 모두 위쪽을 가리키는들은 아래 두 그림에서처럼, 퐁은 바깥쪽으로 방향을 기준 대신 분야. 따라서 바닥에 구를 생성하려고하면 자식 구가 x 축 상단에서 생성되고 큰 구면과 병합되면서 큰 중심 구에서 바깥쪽으로 향합니다.

아래는 내가 지금 아래

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; 
} 

(I, 즉 9 그림에 있지만 설명의 사진에 존재하는 구체의 양이 줄어들 이미지를 혼란하지 않는 것보다이 코드에 더 구체를 생성) 구체를 생성하는 코드는 내 개체에서 수행 할 수있는 가능한 변환

#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

즉, 부모 영역에서 자식 구체를 가리 키기를 원합니다.

부모로부터 어린이 위치를 빼서 (부모의 중심을 나타내는 Y 축을 얻으려면) 새 Y 축 방향을 계산하고이를 정규화합니다. 새 Y 축 벡터를 기존 Z 축 벡터 (왼손잡이 시스템에서)와 교차시켜 동일한 방향 또는 정확히 정반대 방향을 가리키는 경우를 처리하도록주의를 기울여 새로운 X 축을 계산합니다. 오류). 그런 다음이 새 X 축 벡터를 새 Y 축 벡터와 교차시켜 새 Z 축 벡터를 얻습니다. 자녀를위한 오리엔테이션 매트릭스에 이것을 보관하십시오.

관련 문제