2009-12-11 3 views
-1

나는 나무를 표시하고 움직이는 프로젝트에 참여하고 있습니다. 나는이 프로젝트에서 C++과 OpenGL을 사용하고있다. 나무는 일정한 수의 가지, 자국으로 자랍니다. 나무의 잎은 땅에 떨어집니다. Lidenmayer 시스템을 사용하여 그래픽으로 표현하기 때문에 구조를 만들고 트리의 각 요소를 정의하는 것이 더 쉬운 대안으로 보였습니다. 지금까지 나는 나무와 하늘의 트렁크를 표시 할 수 있지만, 실제로 나무를 새로운 나뭇 가지로 성장시키는 방법에 관해서는 분실되어 있습니다. 아마도 이것을 제어하기 위해 for 루프를 수행하는 것으로 생각했지만 완료하는 방법을 알지 못했습니다. 이것을 처리하는 좋은 방법은 무엇입니까?내 코드로 내 L-System 패턴을 제어하려면 어떻게해야합니까?

내 L-시스템 규칙 :

도출 길이 : 10 공리 : F F -> G [-F] + F] GF G - 회전> GG

각도 = 15.0도 (.2618 라디안)

업데이트 : 여기까지 내가 지금까지 얻은 바가 있습니다.

#include <math.h> 
#include <windows.h> 
#include <GL/gl.h> 
#include <glut.h> 

int treeSeed = 0; 
float branchAngle = .2618; 

/* 
    Controls the growth of the tree. Factor 
    of growth is inverse to that of the ShrinkTree 
    method 

*/ 

//void GrowTree() 
//{ 
    /*Tree grows in a direction depending 
    on the L-System rules */ 
// 
//  int i; 
//  for(i = 0; i <=10 ; i++) 
//  { 
//   
//   //grow tree 
// 
//  } 

//} 

/* 
Tree shrinks in a direction depending 
on the L-System rules. Factor of shrinkage is 
inverse to that of the GrowTree method.*/ 

void ShrinkTree() 
{ 
    /*int j; 
    for (j = -; j <=10 ; j++) 
    { 

    }*/ 

} 

//void treeAninmation() 
//{ 
// 
// glutPostRedisplay(); 
//} 


/* 
    Draws a leaf on the branch. 
    If the seed axiom is at zero, 
    a new leaf is drawn. Otherwise, 
    the branch shrinks to accomodate 
    existing leaves. 


*/ 

void DrawLeaf() 
{ 


    if(treeSeed==0) 
    { 
     glBegin(GL_QUADS); 
     glColor3f(0.0f,1.0f,0.0f); 

     glVertex2f(-0.05, -40.5); 
     glVertex2f(-0.05, -20.5); 
     glVertex2f(-0.02, -40.5); 
     glVertex2f(-0.02,-20.5); 

     glEnd(); 

    } 
    /*else 
    { 

     GrowTree(); 
     DrawTwig(treeSeed-1); 
     glPushMatrix(); 
       glRotate(branchAngle,1.0f,0.0f,0.0f); 
       DrawLeaf(treeSeed-1); 
     glPopMatrix(); 
     glPushMatrix(); 
       glRotate(branchAngle,); 
       DrawLeaf(treeSeed-1); 
     glPopMatrix(); 
    }*/ 

} 


void DrawTwig() 
{ 

    /* 

     Draws trunk of tree 
     Represents base scenario of 
     recursion 

    */ 
    if(treeSeed==0) 
    { 

     glLineWidth(5.0); 

     glBegin(GL_LINE_STRIP); 

     glColor3f(0.60f,0.40f,0.12f); 

     glVertex2f(-0.10, -80.5); 
     glVertex2f(-0.10, -100.5); 

     glEnd(); 

    } 

    /*else 
    { 
     ShrinkTree(); 
     DrawTwig(treeSeed-1); 

    }*/ 
} 



/* Draws the tree with leaves * 
    Uses recursion to draw tree structure 
    Relies on L-System formula in order 
    to produce a tree 

*/ 

void DrawTree() 
{ 

    DrawTwig(treeSeed); 
    //DrawLeaf(treeSeed); 


} 




/* Draws the horizon, the sky, and the ground*/ 

void RenderScene(void) 
{ 
    // clears color buffer 
    glClear(GL_COLOR_BUFFER_BIT); 

    /* 
    //GLfloat y; 
    GLfloat fSizes[2]; // Store supported line width range 
    GLfloat size; // Store supported line width increments 
    */ 

    //The horizon lies in the xy plane 

    glBegin(GL_LINE_STRIP); 

    glColor3f(0.0f,0.0f,0.0f); //sets color of horizon to black 
    glVertex2f(-135.0,0.0); 
    glVertex2f(135.0,0.0); 

    glEnd(); 

    //The sky lies in the xy plane 
    //Starts at horizon line, 
    //goes upwards to height edge of screen, 
    //and goes rightwards to width edge of screen. 


    glBegin(GL_QUADS); 

    glColor3f(0.0f,0.0f,1.0f); 

    glVertex2f(135.0, 0.0); 
    glVertex2f(-135.0, 0.0); 
    glVertex2f(-135.0, 100.0); 
    glVertex2f(135.0,100.0); 


    glEnd(); 

    DrawTree(); 

    glFlush(); 

} 


void SetupRC(void) 
{ 

    glClearColor(1.0f,1.0f,1.0f,1.0f); 

} 


void ChangeSize(GLsizei w, GLsizei h) 
{ 
    GLfloat aspectRatio; 

    //prevents divison by zero 
    if(h ==0) 
     h = 1; 

    //set Viewport to window dimensions 
    glViewport(0,0,w,h); 

    // Reset coordinate system 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    // Establish clipping volume (left, right,bottom,top,near,far 

    aspectRatio = (GLfloat)w/(GLfloat)h; 

    if (w <= h) 
     glOrtho (-100.0, 100.0, -100/aspectRatio, 100/aspectRatio,1.0,-1.0); 
    else 
     glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

void main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 

/* Testing basic display functions for now */ 

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(1000, 1000); 
    glutCreateWindow("falling leaves"); 
    glutReshapeFunc(ChangeSize); 
    glutDisplayFunc(RenderScene); 
    //glutIdleFunc(treeAnimation); 
    //glutDisplayFunc(DrawTree); 
    SetupRC(); 
    glutMainLoop(); 
} 
+0

그래, GroworShrink 메서드를 Grow 및 Shrink라는 두 개의 별도 메서드로 분할하기로 결정했습니다. 여기 내 업데이트 된 코드가 있습니다. /* \t Controls the growth of the tree */ //void Grow() //{ \t /*Tree grows in a direction depending \t on the L-System rules */ \t \t int i; \t \t for(i = 0; i <=10 ; i++) \t \t { \t \t \t \t \t \t //grow tree \t \t } \t \t } void Shrink() { \t int j; \t for (j = -; j <=10 ; j++) \t { \t } } user230013

+0

읽기 어려운 스 니펫에 대해 유감스럽게 생각하지만 현재 질문을 수정하는 방법을 잘 모르겠습니다. – user230013

+0

트리 표현 방법을 모른 채로 대답하는 것은 불가능합니다. –

답변

1

나는 한 번에 너무 많은 것을 풀려고 노력한다고 생각합니다. 다음을 시도합니다 :

  • 재귀 트리 구조를 평가하기위한 공식 언어를 작성하십시오.
  • "나이"에 대해 매개 변수화 된 트리 규칙을 지정하는 방법을 빌드하십시오.
  • 주어진 트리 규칙을 렌더링합니다.

이 문제를 가장 큰 차원에서 풀기 전에 특수한 경우를 해결할 것입니다.

  1. 그 구성 요소 (나뭇 가지의 길이, 지점 수 등)를 제어 할 나무에 매개 변수를 추가 하드 코딩 구성 요소 트리를 렌더링
  2. 저를 제어 "나이"에 의해 파라미터 기능을 쓰기 레벨 트리 구성 요소. 조나단은 동시에 너무 많은 일을하려고하는 것 같습니다 상태로
  3. 는 낮은 수준의 트리 매개 변수
1

에 L 시스템 규칙에서 변환 기능을 작성합니다. 일반적으로 L 시스템에서는 트리 생성이 렌더링과 분리됩니다.

내가 제대로 규칙을 이해하면 :

Axiom: F 
F --> G[-F][+F]GF 
G --> GG 

그런 다음 시작 트리입니다

tree(0): F 

즉 트렁크.

tree(1): G[-F][+F]GF 
또 다른 반복이 그것을 더 성장할 것

및 수율 :

tree(2): GG[-G[-F][+F]GF][+G[-F][+F]GF]GGG[-F][+F]GF 

등 다음을 얻을 것이 경우에 트리에 규칙의 하나의 응용 프로그램을 만들어 나무를 성장 .

일반적으로 이것을 문자열 단위로 반복하여 구현합니다. 규칙과 일치하는 각 문자에 대해 rhs 규칙을 대상 문자열에 추가합니다. 그렇지 않으면 문자를 추가합니다. 트리 문자열을 반복하여 다시 수행 트리 렌더링

이때 그것을 등 유닛은 전방 선 그리기 커서를 +와 같은 회전 커서가 왼쪽으로 렌더링 명령어, 즉, F의 세트를 해석

한 번 반복하여 트리를 성장시킨 다음 렌더링하면 트리가 커지는 것처럼 보입니다.

개념적으로 공리, 규칙 등 및 현재 트리 문자열을 캡슐화하는 Tree 클래스로 시작하는 것이 좋습니다. 이 클래스는 나무를 어떻게 키울 지 알지만 렌더링하는 방법은 알 수 없습니다. 그래서 당신은 렌더러 클래스 OpenGLRenderer을 처리 할 것입니다.

while (someFlag) 
{ 
    tree->grow(); 
    renderer->render (tree.stringRep()); 
} 

대부분 어느 난 아직도 당신이 요구하는지 정확히 이해하지 않는 경우에, 당신이 이미 알고 있습니다 L-시스템, 단지 기본 위치 : 다음 루프 따라서 같은이 .

+0

다행이 게시물을 찾았습니다! 파이썬으로 내 l-systems 프로젝트를 진행하는 데 많은 도움이되었습니다. 무리 감사! – mdegges

관련 문제