crgd12吧 关注:5贴子:104
  • 3回复贴,共1

转载wxWdigets整合OpenGL

只看楼主收藏回复


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(0, 0, (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.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, (GLint)GetSize().x, (GLint)GetSize().y);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0.1, 0.1);
glVertex2f(-0.1, 0.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();
}


1楼2012-03-24 02:44回复
    nclude <iostream>
    #include <string>
    #include <cassert>
    #include <cmath>
    #include <wx/wx.h>
    #include <wx/glcanvas.h>
    #include <wx/notebook.h>
    class GL_Window : public wxGLCanvas
    {
    public:
    GL_Window(float c, wxWindow * parent, wxWindowID id,
    const wxPoint & pos, const wxSize& size, long style=0,
    const wxString & name = _("GLCanvas"), int * attribList = 0,
    const wxPalette & palette = wxNullPalette)
    : wxGLCanvas(parent, id, pos, size, style, name, attribList, palette),
    c_(c), rotate_(c) { }
    virtual ~GL_Window() { }
    void draw() {
    rotate_ += 0.01;
    SetCurrent();
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, (GLint)200, (GLint)200);
    glColor3f(1.0, c_, c_);
    glBegin(GL_POLYGON);
    glVertex3f(-0.5, -0.5, 5 * cos(rotate_));
    glVertex3f(-0.5, 0.5, 5 * cos(rotate_));
    glVertex3f(0.5, 0.5, -5 * cos(rotate_));
    glVertex3f(0.5, -0.5, -5 * cos(rotate_));
    glEnd();
    SwapBuffers();
    }
    void OnIdle(wxIdleEvent & event) {
    draw();
    event.RequestMore();
    }
    private:
    float c_;
    float rotate_;
    DECLARE_EVENT_TABLE();
    };
    class MyApp: public wxApp
    {
    virtual bool OnInit();
    };
    IMPLEMENT_APP(MyApp)
    bool MyApp::OnInit()
    {
    wxFrame* frame = new wxFrame((wxFrame *) NULL, -1,
    _("Hello GL World"), wxPoint(50, 50), wxSize(450, 340) );
    wxNotebook* book = new wxNotebook(frame, -1,
    wxPoint(-1, -1), wxSize(200, 200));
    GL_Window* MyGLCanvas = new GL_Window(1, book, -1, wxPoint(-1, -1),
    wxSize(200, 200), wxSUNKEN_BORDER, _("some text"));
    book->AddPage(MyGLCanvas, _("One"));
    MyGLCanvas = new GL_Window(0, book, -1, wxPoint(-1,-1),
    wxSize(200,200), wxSUNKEN_BORDER, _("some text"));
    book->AddPage(MyGLCanvas, _("Two"));
    frame->Show(true);
    return true;
    }
    BEGIN_EVENT_TABLE(GL_Window, wxGLCanvas)
    EVT_IDLE(GL_Window::OnIdle)
    END_EVENT_TABLE()


    2楼2012-03-25 01:38
    回复
      nclude "wx/glcanvas.h"class MyContext : public wxGLContext
      {
      public:
      MyContext(wxGLCanvas *canvas); void DrawRotatedCube();
      };class MyCanvas : public wxGLCanvas
      {
      public:
      MyCanvas(wxWindow *parent);private:
      void OnPaint(wxPaintEvent& event);
      DECLARE_EVENT_TABLE()
      };class MyFrame : public wxFrame
      {
      public:
      MyFrame();private:
      DECLARE_EVENT_TABLE()
      };class MyApp : public wxApp
      {
      public:
      MyApp() { m_glContext = NULL; } MyContext& GetContext(wxGLCanvas *canvas); virtual bool OnInit();
      virtual int OnExit();private: MyContext *m_glContext;
      };


      3楼2012-03-25 02:24
      回复
        nclude "wx/wx.h"
        #include "main.h"
        MyContext::MyContext(wxGLCanvas *canvas)
        : wxGLContext(canvas)
        {
        SetCurrent(*canvas);
        }
        void MyContext::DrawRotatedCube()
        {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glFlush();}BEGIN_EVENT_TABLE(MyCanvas, wxGLCanvas)
        EVT_PAINT(MyCanvas::OnPaint)
        END_EVENT_TABLE()MyCanvas::MyCanvas(wxWindow *parent)
        : wxGLCanvas(parent, wxID_ANY, NULL /* attribs */,
        wxDefaultPosition, wxDefaultSize,
        wxFULL_REPAINT_ON_RESIZE)
        {
        }void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
        {
        wxPaintDC dc(this);
        const wxSize ClientSize = GetClientSize();
        MyContext& canvas = wxGetApp().GetContext(this);
        glViewport(0, 0, ClientSize.x, ClientSize.y);
        canvas.DrawRotatedCube();
        SwapBuffers();
        }
        MyFrame::MyFrame()
        : wxFrame(NULL, wxID_ANY, wxT("wxWidgets OpenGL Cube Sample"))
        {
        new MyCanvas(this);
        SetClientSize(400, 400);
        Show();
        }IMPLEMENT_APP(MyApp)bool MyApp::OnInit()
        {
        if ( !wxApp::OnInit() )
        return false; new MyFrame(); return true;
        }int MyApp::OnExit()
        {
        delete m_glContext; return wxApp::OnExit();
        }MyContext& MyApp::GetContext(wxGLCanvas *canvas)
        {
        if ( !m_glContext )
        {
        m_glContext = new MyContext(canvas);
        } m_glContext->SetCurrent(*canvas); return *m_glContext;
        }


        4楼2012-03-25 02:24
        回复