My SFML window, rather than rendering a picture, appears as just a black box. Here's the relevant code:
BEGIN_EVENT_TABLE(wxSFMLCanvas, wxControl)
EVT_IDLE(wxSFMLCanvas::OnIdle)
EVT_PAINT(wxSFMLCanvas::OnPaint)
EVT_SIZE(wxSFMLCanvas::OnSize)
EVT_MOUSE_EVENTS(wxSFMLCanvas::OnClick)
EVT_ERASE_BACKGROUND(wxSFMLCanvas::OnEraseBackground)
END_EVENT_TABLE()
void wxSFMLCanvas::OnUpdate() {
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0f, 0.0f, 200.0f);
//Sample model...
glBegin(GL_QUADS);
glColor3f(0.1f, 1.0f, 0.1f);
glVertex3f(100.0f, 0.0f, 100.0f);
glNormal3f(100.0f, 1.0f, 100.0f);
glVertex3f(100.0f, 0.0f, -100.0f);
glNormal3f(100.0f, 1.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, -100.0f);
glNormal3f(-100.0f, 1.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glNormal3f(-100.0f, 1.0f, 100.0f);
glVertex3f(-100.0f, 100.0f, 100.0f);
glNormal3f(-100.0f, 101.0f, 100.0f);
glVertex3f(-100.0f, 100.0f, -100.0f);
glNormal3f(-100.0f, 101.0f, -100.0f);
glVertex3f(100.0f, 100.0f, -100.0f);
glNormal3f(100.0f, 101.0f, -100.0f);
glVertex3f(100.0f, 100.0f, 100.0f);
glNormal3f(100.0f, 101.0f, 100.0f);
glEnd();
glFlush();
}
void wxSFMLCanvas::OnIdle(wxIdleEvent& event) {
//Send a paint message when the display is idle, for maximum framerate
Refresh();
}
void wxSFMLCanvas::OnPaint(wxPaintEvent& event) {
//Prepare the control to be repainted
wxPaintDC dc(this);
//Create the actual image
OnUpdate();
//Display the image on-screen
Display();
}
void wxSFMLCanvas::OnEraseBackground(wxEraseEvent& event){
}
void wxSFMLCanvas::OnClick(wxMouseEvent& event) {
((wxFrame*)GetParent())->SetStatusText(wxT("Clicked the edit window."));
}
void wxSFMLCanvas::OnSize(wxSizeEvent& event) {
if (initialised)
ChangeSize(event.GetSize().GetWidth(), event.GetSize().GetHeight());
}
void wxSFMLCanvas::Setup() {
std::cout << "Setting up rendering context." << std::endl;
std::cout << "OpenGL vendor: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "OpenGL renderer: " << glGetString(GL_RENDERER) << std::endl;
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
this->PreserveOpenGLStates(true);
glEnable(GL_MULTISAMPLE);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
GLfloat ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
this->SetActive(true);
initialised = true;
}
void wxSFMLCanvas::ChangeSize(GLfloat w, GLfloat h) {
GLfloat fAspect;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
fAspect = (GLfloat)w / (GLfloat)h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the clipping volume
gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style)
{
std::cout << "wxSFMLCanvas constructor entered." << std::endl;
#ifdef __WXGTK__
// GTK implementation requires to go deeper to find the
// low-level X11 identifier of the widget
std::cout << "Realizing wxWidget window." << std::endl;
gtk_widget_realize(m_wxwindow);
std::cout << "Removing double-buffering from GTK." << std::endl;
gtk_widget_set_double_buffered(m_wxwindow, false);
std::cout << "Getting X11 handle." << std::endl;
GdkWindow* Win = GTK_PIZZA(m_wxwindow)->bin_window;
std::cout << "Flushing buffer." << std::endl;
XFlush(GDK_WINDOW_XDISPLAY(Win));
std::cout << "Creating RenderWindow." << std::endl;
sf::RenderWindow::Create(GDK_WINDOW_XWINDOW(Win));
#else
// Tested under Windows XP only (should work with X11
// and other Windows versions - no idea about MacOS)
sf::RenderWindow::Create(GetHandle());
#endif
Setup();
}
wxSFMLCanvas::~wxSFMLCanvas() {
}
In theory, this should be drawing a green box in the centre of the screen, yes? But it's not working... If anyone could provide any help, I'd really appreciate it. If there's any more information you need, I'd be glad to supply.