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

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

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

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

    1. 整合OpenGL

    WxWidgets中整合OpenGL是十分簡單的,因為wxWidgets本身對OpenGL進行了封裝,因此只需要按照example中的例子進行編寫即可。一種常見的方法是繼承wxGLCanvas類,將EVT_PAINT的回調(diào)函數(shù)進行重載即可。一段示例代碼如下:
    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() {
    /* 此處很關(guān)鍵 */
        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

    這個話題在網(wǎng)上有過討論,在OpenCV中文論壇中提出了一種向HDC繪圖的方法。不過這種方法的局限在于:HDC是Windows平臺下特有的結(jié)構(gòu),在Linux下是不可行的。
    其實OpenCV的核心是IplImage結(jié)構(gòu),基本上所有的OpenCV繪圖語句以及相應(yīng)的算法都可以通過這個結(jié)構(gòu)衍生出來。因此,一種直接的想法是如何將IplImage轉(zhuǎn)換為wxWidgets中的wxImage類型,這樣就可以直接在wxWidgets繪制IplImage類型的數(shù)據(jù)了。于是在網(wǎng)上尋找后,在一個國外論壇中找到了現(xiàn)成的代碼如下:
    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;
    }
    進行這樣的轉(zhuǎn)換后,我們就直接可以在wxWidgets中使用OpenCV的接口。

    3. 整合MathPlot

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

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


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

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 99精品视频免费观看| 看成年女人免费午夜视频| 久久精品免费一区二区三区| 国产美女亚洲精品久久久综合| 成年网在线观看免费观看网址| 又粗又硬又黄又爽的免费视频| 亚洲AV无码AV吞精久久| 国产免费131美女视频| 黄色a级片免费看| 国产zzjjzzjj视频全免费 | 69视频免费在线观看| 久久久无码精品亚洲日韩按摩| 四虎1515hh永久久免费| 2020天堂在线亚洲精品专区| 色播在线永久免费视频| 色爽黄1000部免费软件下载| 亚洲精品白浆高清久久久久久| 免费人成毛片动漫在线播放| 亚洲精品视频在线观看视频| 成人免费无码大片a毛片软件| 亚洲AV无码一区二区三区性色 | 在线看片免费不卡人成视频| 亚洲欧美国产日韩av野草社区| 亚洲毛片av日韩av无码| 成全高清在线观看免费| 亚洲不卡中文字幕| 亚洲国产成人久久精品99 | 国产yw855.c免费视频| 国内精品免费视频精选在线观看 | 午夜视频在线在免费| 深夜a级毛片免费视频| 久久综合图区亚洲综合图区| 午夜宅男在线永久免费观看网| 成人嫩草影院免费观看| 亚洲色图黄色小说| 国产一区二区三区在线免费 | 亚洲免费黄色网址| 男男gvh肉在线观看免费| 国外成人免费高清激情视频| 亚洲男女性高爱潮网站| h视频在线观看免费网站|