<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Thinking in Java
    Java開發技巧與實踐
    posts - 9,comments - 6,trackbacks - 0
    寫在前面:其實本不應該發在Java方面的,只是最近在做的項目因為實時應用的關系沒有使用Java,因此借這里記錄一下心得。
    最近在一個在Linux下開放的項目中用到了wxWidgets,當時在GTK+、QT和wx之間選擇了很久,最終確定選擇wxWidgets。有關這個框架的詳細信息,請自行google之。

    1. 整合OpenGL

    WxWidgets中整合OpenGL是十分簡單的,因為wxWidgets本身對OpenGL進行了封裝,因此只需要按照example中的例子進行編寫即可。一種常見的方法是繼承wxGLCanvas類,將EVT_PAINT的回調函數進行重載即可。一段示例代碼如下:
    static int attriblist[] = {
        WX_GL_RGBA, WX_GL_MIN_RED, 
    1, WX_GL_MIN_GREEN, 1,
        WX_GL_MIN_BLUE, 
    1, WX_GL_DEPTH_SIZE, 1, WX_GL_DOUBLEBUFFER, None
    };

    BEGIN_EVENT_TABLE(UIOpenGLCanvas, wxGLCanvas)
        EVT_SIZE(UIOpenGLCanvas::OnSize)
        EVT_PAINT(UIOpenGLCanvas::OnPaint)
        EVT_MOUSE_EVENTS(UIOpenGLCanvas::OnMouseEvent)
    END_EVENT_TABLE()

    UIOpenGLCanvas::UIOpenGLCanvas(wxWindow 
    *parent, const wxString &caption)
    //    :wxGLCanvas(parent, wxID_ANY, attriblist, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE, wxT("GLCanvas"), wxNullPalette)
    //    :wxGLCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE, wxT("GLCanvas"))
        :wxGLCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE, wxT("GLCanvas"), attriblist, wxNullPalette)
        ,m_caption(caption), count(
    0) {
        
    int argc = 1;
        
    char* argv[1= { wxString((wxTheApp->argv)[0]).char_str() };

        
    /*
        NOTE: this example uses GLUT in order to have a free teapot model
        to display, to show 3D capabilities. GLUT, however, seems to cause problems
        on some systems. If you meet problems, first try commenting out glutInit(),
        then try comeenting out all glut code
        
    */
        glutInit(
    &argc, argv);
    }

    UIOpenGLCanvas::
    ~UIOpenGLCanvas() {

    }

    void UIOpenGLCanvas::OnSize(wxSizeEvent& event) {
        
    // this is also necessary to update the context on some platforms
        wxGLCanvas::OnSize(event);

        
    // set GL viewport (not called by wxGLCanvas::OnSize on all platforms)
        int w, h;
        GetClientSize(
    &w, &h);

        
    if (GetContext()) {
            SetCurrent();
            glViewport(
    00, (GLint) w, (GLint) h);
        }
    }

    void UIOpenGLCanvas::OnMouseEvent(wxMouseEvent& event) {
        
    static int dragging = 0;
        
    static float last_x, last_y;

    //    printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
        if(event.LeftIsDown()) {
            
    if(!dragging) {
                dragging 
    = 1;
            } 
    else {
                yrot 
    += (event.GetX() - last_x)*1.0;
                xrot 
    += (event.GetY() - last_y)*1.0;
                Refresh(
    false);
            }
            last_x 
    = event.GetX();
            last_y 
    = event.GetY();
        } 
    else
            dragging 
    = 0;

    }


    void UIOpenGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
        Render();
    }

    void UIOpenGLCanvas::Render() {
    /* 此處很關鍵 */
        wxPaintDC(
    this);
        
    if (!GetContext())
            
    return;
        SetCurrent();

        glClearColor(
    0.00.00.00.0);
        glClear(GL_COLOR_BUFFER_BIT 
    | GL_DEPTH_BUFFER_BIT);

        glViewport(
    00, (GLint)GetSize().x, (GLint)GetSize().y);
        glBegin(GL_POLYGON);
        glColor3f(
    1.00.00.0);
        glVertex2f(
    0.10.1);
        glVertex2f(
    -0.10.1);
        glVertex2f(
    -0.1-0.1);
        glVertex2f(
    0.1-0.1);
        glEnd();

    // using a little of glut
        glColor4f(0,0,1,1);
        glutWireTeapot(
    0.4);

        glPopMatrix();

        glFlush();
        SwapBuffers();
    }

    2. 整合OpenCV

    這個話題在網上有過討論,在OpenCV中文論壇中提出了一種向HDC繪圖的方法。不過這種方法的局限在于:HDC是Windows平臺下特有的結構,在Linux下是不可行的。
    其實OpenCV的核心是IplImage結構,基本上所有的OpenCV繪圖語句以及相應的算法都可以通過這個結構衍生出來。因此,一種直接的想法是如何將IplImage轉換為wxWidgets中的wxImage類型,這樣就可以直接在wxWidgets繪制IplImage類型的數據了。于是在網上尋找后,在一個國外論壇中找到了現成的代碼如下:
    void copy_and_swap_rb(char *s, char *d, int size)
    {
        
    // Copy image data source s to destination d, swapping R and B channels.
        
    // Assumes 8 bit depth, 3 interleaved channels, and width_step = width*3
        const int step = 3;
        
    char *end = s + size;
        
    while (s<end) {
            d[
    0= s[2];
            d[
    1= s[1];
            d[
    2= s[0];
            d 
    += step;
            s 
    += step;
        }
    }

    void wx2cv(wxImage &wx, IplImage *ipl)
    {
        
    // Copy image data from wxWidgets image to Ipl (opencv) image
        
    // Assumes ipl image has seq "GBR", depth 8, and 3 channels, and
        
    // has the same size as the wxImage, and width_step = width*3.
        copy_and_swap_rb((char*)wx.GetData(), ipl->imageData, ipl->imageSize);
    }

    void cv2wx(IplImage *ipl, wxImage &wx )
    {
        
    // Copy image data from Ipl (opencv) image to wxImage
        
    // Assumes ipl image has seq "GBR", depth 8, and 3 channels, and
        
    // has the same size as the wxImage, and width_step = width*3.
        copy_and_swap_rb( ipl->imageData, (char*)wx.GetData(),
                          wx.GetWidth()
    *wx.GetHeight()*3);
    }

    IplImage 
    *cv_from_wx(wxImage &wx)
    {
        
    // Return a new IplImage copied from a wxImage.
        
    // Must be freed by user with cvReleaseImage().
        IplImage *ret = cvCreateImage(cvSize(wx.GetWidth(), wx.GetHeight()),
                                      IPL_DEPTH_8U, 
    3);
        wx2cv(wx, ret);
        
    return ret;
    }

    wxImage wx_from_cv( IplImage 
    *cx)
    {
        
    // Return new wxImage copied from a compatible IplImage.
        
    // Assumes ipl image has seq "GBR", depth 8, and 3 channels
        
    // Fear not. The copy on return is cheap; does not deep-copy the data.
        wxImage wx(cx->width, cx->height, (unsigned char*) malloc(cx->imageSize), false);
        cv2wx(cx, wx);
        
    return wx;
    }
    進行這樣的轉換后,我們就直接可以在wxWidgets中使用OpenCV的接口。

    3. 整合MathPlot

    MathPlot是sourceforge上的一個開源項目,其功能是使用wxWidgets提供的繪圖方法構建操作DC繪圖的高級接口。這個項目的源代碼十分簡單,只有兩個文件,但是功能卻很實用。我在sourceforge上給了好評。
    MathPlot內部實現了坐標軸的拖拽、平移和縮放,將圖形劃分為Layer,并且引入了動態Layer的概念,即在這個Layer上繪制的圖形可以通過重設局部坐標系的原點基準坐標實現移動,并繪制軌跡。而且,MathPlot內部實現了雙緩沖,因此,這個框架對于需要實時顯示軌跡的簡單應用來說具有很好使用價值。
    由于MathPlot直接使用了wxWidgets的繪圖接口,因此其整合十分簡單,只需要在需要繪制的Panel上使用MathPlot提供的接口即可實現整合。

    以上簡單說明了wxWidgets如何整合OpenGL、OpenCV和MathPlot三種不同的繪圖框架,最后給一個將三種繪圖方法用在同一個窗口中實現不同功能的實例:
    整合示例


    無人分享的快樂不是真快樂,沒人分擔的痛苦是真痛苦。
    posted on 2011-01-16 11:03 Feenn 閱讀(5586) 評論(2)  編輯  收藏

    FeedBack:
    # re: wxWidgets整合OpenGL+OpenCV+MathPlot(一種Windows和Linux通用的方法)
    2011-02-26 23:16 | leoxiaofei
    正在學這個,文章很有用,謝謝。  回復  更多評論
      
    # re: wxWidgets整合OpenGL+OpenCV+MathPlot(一種Windows和Linux通用的方法)
    2012-12-13 21:16 | hand916
    可以把opengl源程序給我一下QQ731479833  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 亚洲日本va中文字幕久久| 亚洲综合色视频在线观看| 免费一级毛片不卡不收费| 亚洲综合久久夜AV | 午夜影视日本亚洲欧洲精品一区 | 麻豆一区二区免费播放网站| 成人免费视频小说| 亚洲一区二区三区国产精品| 亚洲日本精品一区二区| 亚洲国产成人精品无码区花野真一 | 亚洲av不卡一区二区三区| 99久久婷婷国产综合亚洲| 免费人妻精品一区二区三区| 中文字幕一区二区免费| 久久笫一福利免费导航| 亚洲第一网站男人都懂| 99亚洲精品高清一二区| 亚洲精品乱码久久久久蜜桃| 中国好声音第二季免费播放| 免费毛片a在线观看67194| 亚洲欧洲一区二区三区| 久久亚洲精品无码VA大香大香| 亚洲AV无码专区亚洲AV桃| 永久免费av无码入口国语片| 性色av免费观看| 国产亚洲福利精品一区| 亚洲熟伦熟女专区hd高清| a级男女仿爱免费视频| 成人免费无毒在线观看网站| 永久亚洲成a人片777777| 亚洲va在线va天堂va手机| 一级免费黄色毛片| 国产免费av片在线看| 亚洲精品国产精品乱码在线观看| 色天使亚洲综合在线观看| a级片在线免费看| 日本久久久免费高清| 2022年亚洲午夜一区二区福利 | 久久综合图区亚洲综合图区| 亚洲成AV人影片在线观看| 99re6热视频精品免费观看|