OpenGL에서 프랙탈 생성에 약간의 문제가 있습니다. 현재 코드가 여기에 있습니다OpenGL에서 프랙탈 트리 만들기
void generateTree(){
Point3f startPoint({0.0f,0.0f,0.0f});
Point3f endPoint({1.0f,0.0f,0.0f});
float rotation = 90.0f;
glutWireSphere(0.05, 4, 4);
_generateTreeBranches(startPoint,1.0f,rotation,0);
}
void _generateTreeBranches(const Point3f& newPosition,
float length,
float rotation,
const int depth)
{
if(depth > MAX_DEPTH) return;
cout << "at depth = " << depth << endl;
if(depth == 0){
glColor3f(1.0f,1.0f,1.0f);
}else if(depth == 1){
glColor3f(1.0f,0.0f,0.0f);
}else{
glColor3f(0.0f,1.0f,0.0f);
}
glTranslatef(newPosition.x,newPosition.y,newPosition.z);
glRotatef(rotation, 0.0f, 0.0f, 1.0f);
drawLine(length);
glRotatef(-rotation, 0.0f, 0.0f, 1.0f);
glTranslatef(-newPosition.x,-newPosition.y,-newPosition.z);
const float newLength = length * BRANCH_LENGTH_DECREASE_FACTOR;
int nextDepth = depth + 1;
Point3f nextPosition = {newPosition.x+length, newPosition.y, newPosition.z};
float leftRotation = rotation + CHILD_BRANCH_ANGLE * nextDepth;
_generateTreeBranches(nextPosition,newLength,leftRotation,nextDepth);
float rightRotation = rotation - CHILD_BRANCH_ANGLE * nextDepth;
_generateTreeBranches(nextPosition,newLength,rightRotation,nextDepth);
}
회전이 올바르지 만 위치가 올바르지 않습니다. 새로운 지점은 부모 지점의 끝점에서 시작하여 그려지지 않습니다. 누군가가이 문제를 해결하는 데 나를 도울 수 있습니까? 현재 분기 그것은 (정확히 확인하시기 바랍니다) 같은 것을해야한다
Point3f nextPosition = {newPosition.x+length, newPosition.y, newPosition.z};
에 직면 전체 코드 here
프랙탈 트리를 만들기 위해 재귀 호출을 통해 glTranslatef 및 glRotatef 함수를 스태킹하는 것에 의존하고 있습니다. 이것은 최종 결과가 무엇이고 실수를하기 쉬운 지 항상 명확하지 않기 때문에 좋지 않습니다. 현재 분기의 회전과 변환에 대한 계산을 수학적으로 수행하고 glPushMatrix/glPopMatrix를 사용하여 각 분기마다 glRotatef + glTranslatef 한 쌍을 한 번 적용하여 다음 분기를 위해 재설정하는 것이 좋습니다. 혼란이 적고 수학적으로 소리가 많이납니다. – ksming
도움을 주셔서 감사합니다. 내가 수동 계산을 피하기 위해 노력하고 있었고, 그냥 OpenGL을 할 일을하지만,이 경우는 아마 더 쉽고, 만약 내가 수동으로 그것을 밖으로 읽을 수 – dchhetri