Archived

This topic is now archived and is closed to further replies.

Problem with wxWindows and OpenGL

This topic is 5116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been trying to port the NeHe tutorials to wxWindows. I''m pulling my hair out trying to figure out what is wrong with my code. It works fine until I try to do a glTranslatef in the Z axis. Translations in the X and Y axis work fine. When I do a translation in the Z direction, however, the triangle I''m trying to render doesn''t appear. The demo programs that come with wxWindows work, so I must just be missing something. If anyone has a minute to look at the following I would be very, very appreciative. The code I''m referring to is the last function listed here: nehe-two.h: #ifndef _NEHE_TWO_H_ #define _NEHE_TWO_H_ #include <wx/glcanvas.h> class MyApp: public wxApp { public: virtual bool OnInit(); }; class MyGLCanvas: public wxGLCanvas { public: MyGLCanvas(wxWindow *parent, const wxWindowID id = -1, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize, long style = 0, const wxString &name = "TestGLCanvas"); void OnPaint(wxPaintEvent &event); void OnSize(wxSizeEvent &event); void DrawGLScene(); void InitGL(); bool gl_initialized; DECLARE_EVENT_TABLE() }; class MyFrame: public wxFrame { public: MyFrame(wxFrame *frame, const wxString &title, const wxPoint &pos, const wxSize &size, long style = wxDEFAULT_FRAME_STYLE); MyGLCanvas *canvas; }; #endif nehe-two.cpp: #include "wx/wxprec.h" #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "nehe-two.h" IMPLEMENT_APP(MyApp); MyFrame::MyFrame(wxFrame *frame, const wxString &title, const wxPoint &pos, const wxSize &size, long style) :wxFrame(frame, -1, title, pos, size, style) { canvas = NULL; } BEGIN_EVENT_TABLE(MyGLCanvas, wxGLCanvas) EVT_PAINT(MyGLCanvas::OnPaint) EVT_SIZE(MyGLCanvas::OnSize) END_EVENT_TABLE() MyGLCanvas::MyGLCanvas(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, long style, const wxString &name): wxGLCanvas(parent, (wxGLCanvas*)NULL, id, pos, size, style, name) { } void MyGLCanvas::OnSize(wxSizeEvent &event) { wxGLCanvas::OnSize(event); GLsizei width = event.GetSize().GetWidth(); GLsizei height = event.GetSize().GetHeight(); glViewport(0, 0, width, height); } void MyGLCanvas::OnPaint(wxPaintEvent &event) { wxPaintDC dc(this); DrawGLScene(); } bool MyApp::OnInit() { MyFrame *frame = new MyFrame(NULL, _("NeHe Lesson Two"), wxPoint(50, 50), wxSize(400, 300)); SetTopWindow(frame); frame->canvas = new MyGLCanvas(frame, -1, wxPoint(0,0), wxSize(200,200)); frame->Show(TRUE); frame->canvas->SetCurrent(); //wxSafeYield(); frame->canvas->InitGL(); return TRUE; } void MyGLCanvas::InitGL() { glShadeModel(GL_SMOOTH); glClearColor(0.0, 0.0, 0.0, 0.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } void MyGLCanvas::DrawGLScene() { wxPaintDC dc(this); SetCurrent(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // If I change this to (0.0, 0.0, 0.0) the triangle // displays fine. (0.0, 0.0, 0.0) to (0.0, 0.0, -1.0) // all display the same as (0.0, 0.0, 0.0). When it gets to // (0.0, 0.0, -1.1) the triangle disappears. glTranslatef(0.0, 0.0, -6.0); glBegin(GL_TRIANGLES); glVertex3f(0.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); glFlush(); SwapBuffers(); }

Share this post


Link to post
Share on other sites