2017-05-01 54 views
1

我有一個有趣的bug,現在已經「困擾」了我幾天。OGLFT在使用GLStipple時繪製文字

我目前使用OpenGL在屏幕上繪製文本。我正在使用OGLFT庫來協助繪圖。這個庫實際上使用了freetype2庫。我其實沒有對文本做任何特別的事情。我只尋找單色文字。

無論如何,在實現庫之後,我注意到只有在啓用了glStipple的情況下,纔會繪製正確的文本。我相信OGLFT庫和我所支持的之間存在一些干擾問題。

我想知道是否有人在那裏使用OGLFT庫的一些經驗。我發佈了一個我的代碼的簡約示例,以演示發生了什麼事情:

(請注意,有一些變量用於表示我的glCanvas的縮放因子和相機的位置,並且這只是對於2D)

double _zoomX = 1.0; 

double _zoomY = 1.0; 

double _cameraX = 0; 

double _cameraY = 0; 



/* This function gets called everytime a draw routine is needed */ 
void modelDefinition::onPaintCanvas(wxPaintEvent &event) 
{ 
    wxGLCanvas::SetCurrent(*_geometryContext);// This will make sure the the openGL commands are routed to the wxGLCanvas object 
    wxPaintDC dc(this);// This is required for drawing 

     glMatrixMode(GL_MODELVIEW); 
     glClear(GL_COLOR_BUFFER_BIT); 

     updateProjection(); 

    OGLFT::Monochrome *testface = new OGLFT::Monochrome("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 8); 
     testface->draw(0, 0, "test"); 
     glEnable(GL_LINE_STIPPLE);// WHen I comment out this line, the text is unable to be drawn 

     glLineStipple(1, 0b0001100011000110); 
     glBegin(GL_LINES); 
      glVertex2d(_startPoint.x, _startPoint.y); 
      glVertex2d(_endPoint.x, _endPoint.y); 
     glEnd(); 
     glDisable(GL_LINE_STIPPLE); 

    SwapBuffers(); 
} 

void modelDefinition::updateProjection() 
{ 
     // First, load the projection matrix and reset the view to a default view 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    glOrtho(-_zoomX, _zoomX, -_zoomY, _zoomY, -1.0, 1.0); 

    //Reset to modelview matrix 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glViewport(0, 0, (double)this->GetSize().GetWidth(), (double)this->GetSize().GetHeight()); 
    /* This section will handle the translation (panning) and scaled (zooming). 
    * Needs to be called each time a draw occurs in order to update the placement of all the components */ 
    if(_zoomX < 1e-9 || _zoomY < 1e-9) 
    { 
     _zoomX = 1e-9; 
     _zoomY = _zoomX; 
    } 

    if(_zoomX > 1e6 || _zoomY > 1e6) 
    { 
     _zoomX = 1e6; 
     _zoomY = _zoomX; 
    } 

    glTranslated(-_cameraX, -_cameraY, 0.0); 
} 

另外有一點要注意的是,glEnable(GL_LINE_STIPPLE);下面的代碼是必需的。就好像glStipple需要正確繪製才能正確顯示文本。

回答

1

翻遍你的代碼,我相信你的意圖是將其渲染爲灰度?如果是這樣,那麼你可以簡單地使用OGLFT::Grayscale *testface = new OGLFT::Grayscale("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 8);

這將得到你所需要的,而不必擔心你發佈的問題。事實上,我也建議這樣做。

+0

是的!就是這樣,現在一切正常。非常感謝! – codingDude