2009-06-21 59 views
3

嘿,我已經得到了我的基本L系統的工作,我決定嘗試優化應用程序的渲染。以前我用一個開關盒和繪圖循環了L系統的整個系列...更好,但我會告訴你我在做什麼。OpenGL L系統渲染

for(unsigned int stringLoop = 0; stringLoop < _buildString.length(); stringLoop++) 
{ 

     switch(_buildString.at(stringLoop)) 
     { 
     case'X': 
      //Do Nothing 
      //X is there just to facilitate the Curve of the plant 
      break; 
     case'F': 

      _prevState = _currState; 
      _currState.position += _currState.direction * stdBranchLength; 
      //Set the previous state to the current state 
      _graphics.SetColour3f(0.0f, 1.0f, 0.0f); 

      _graphics.Begin(OGLFlags::LINE_STRIP); 
       _graphics.Vertex3f(_prevState.position.X(), _prevState.position.Y(), _prevState.position.Z()); 
       _graphics.Vertex3f(_currState.position.X(), _currState.position.Y(), _currState.position.Z()); 
      _graphics.End(); 

      break; 
     case'[': 
      _prevStack.push(_currState); 
      break; 
     case']': 
      _prevState = _currState; 
      _currState = _prevStack.top(); 
      _prevStack.pop();    
      break; 
     case'-': 

      _currState.direction = _currState.direction.RotatedAboutZ(-(ROTATION) * Math::DegreesToRadians); 

      break; 
     case'+': 
      _currState.direction = _currState.direction.RotatedAboutZ(ROTATION * Math::DegreesToRadians); 

      break; 
     }; 

} 

我刪除了所有這一切,因爲我是從字面上解析樹的每一個幀,我改變了這種循環,這樣它會保存所有的verticies在一個std向量。

for(unsigned int stringLoop = 0; stringLoop < _buildString.length(); stringLoop++) 
{ 

     switch(_buildString.at(stringLoop)) 
     { 
     case'X': 
      break; 
     case'F': 

      //_prevState = _currState; 
      _currState.position += _currState.direction * stdBranchLength; 
      _vertexVector.push_back(_currState.position); 

      break; 
     case'[': 
      _prevStack.push(_currState); 
      break; 
     case']': 
      _currState = _prevStack.top(); 
      _prevStack.pop();    
      break; 
     case'-':    
      _currState.direction = _currState.direction.RotatedAboutZ(-(ROTATION) * Math::DegreesToRadians); 
      break; 
     case'+': 
      _currState.direction = _currState.direction.RotatedAboutZ(ROTATION * Math::DegreesToRadians); 
      break; 
     }; 
} 

現在我改變了我的渲染循環,所以我直接從矢量數組中讀取。

DesignPatterns::Facades::OpenGLFacade _graphics = DesignPatterns::Facades::openGLFacade::Instance(); 

_graphics.Begin(OGLFlags::LINE_STRIP); 
    for(unsigned int i = 0; i < _vertexVector.size(); i++) 
    { 
     _graphics.Vertex3f(_vertexVector.at(i).X(), _vertexVector.at(i).Y(), _vertexVector.at(i).Z()); 
    } 
_graphics.End(); 

現在我的問題是,當我使用矢量數組,我用線條我得到額外的神器。 第一個圖像是未經優化的原始渲染,然後是第二個是運行速度更快的新渲染,第三個渲染是龍的曲線,它不像前兩個使用的那樣使用push和pops(我是很確定推動和流行是問題出現的地方)。 這是我的邏輯存儲verticies的問題,還是因爲我使用線條?我只是用線條,但它根本不起作用,最終看起來更像是line_stipple。 也對此帖的長度感到抱歉。

alt text http://img197.imageshack.us/img197/8030/bettera.jpg alt text http://img23.imageshack.us/img23/3924/buggyrender.jpg alt text http://img8.imageshack.us/img8/627/dragoncurve.jpg

+0

問題是你的紅樹在X中被「壓扁」了嗎? – 2009-06-22 00:15:11

回答

3

由於你使用的是LINE_STRIP得到那些多餘的線條。

在你的'F'情況下,將你的線的兩個端點插入向量中(就像你最初做的那樣)。

_vertexVector.push_back(_prevState.position); 
_vertexVector.push_back(_currState.position); 

而當您繪製時,請改爲使用LINE_LIST。

+0

非常感謝,這就是爲什麼我愛堆棧溢出 – Craig 2009-06-22 00:29:37