SFML community forums

Help => Window => Topic started by: drummerp on May 31, 2011, 06:31:05 am

Title: OpenGL not rendering
Post by: drummerp on May 31, 2011, 06:31:05 am
My SFML window, rather than rendering a picture, appears as just a black box. Here's the relevant code:

Code: [Select]
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.
Title: OpenGL not rendering
Post by: Fouf on June 02, 2011, 05:47:37 pm
Code: [Select]

    glTranslatef(0.0f, 0.0f, 200.0f);

    //Sample model...
    glBegin(GL_QUADS);
        glColor3f(0.1f, 1.0f, 0.1f); ...


try placing a glLoadIdentity(); right before the translate.

Code: [Select]
   glLoadIdentity();
    glTranslatef(0.0f, 0.0f, 200.0f);

    //Sample model...
    glBegin(GL_QUADS);
        glColor3f(0.1f, 1.0f, 0.1f);...
Title: OpenGL not rendering
Post by: drummerp on June 03, 2011, 02:24:59 am
I'm afraid that didn't work, no... Thanks for trying.
Title: OpenGL not rendering
Post by: Fouf on June 03, 2011, 03:14:41 am
Ah sorry, I'm not familiar with wx, but I placed that load identity there because it would keep translating until you couldn't see it. load identity would reset the origin
Title: OpenGL not rendering
Post by: drummerp on June 03, 2011, 05:48:47 am
Yes, this part is basically the wxWidgets-SFML sample, except I changed the resize and rendering functions and added a setup one. The problem is that the clear colour is appearing properly, but then the actual model itself isn't appearing, leaving just a black window.
Title: OpenGL not rendering
Post by: drummerp on June 05, 2011, 03:59:22 am
I could really use some help with this, please. This is like the one major bug I have left with this project, and I can't see any reason why this would be happening...
Title: OpenGL not rendering
Post by: drummerp on June 19, 2011, 12:08:37 am
I'd still really, really appreciate if I could get some help with this. If there is any information you need me to supply, I would be more than willing to do so. It just doesn't make any sense to me at all.
Title: OpenGL not rendering
Post by: Nexus on June 19, 2011, 12:44:33 am
Sorry that I can't contribute something to the actual topic, but I think nobody has answered because of two reasons: Few people in this forum are familiar with wxWidgets, and your code is rather big and not really minimal. Have you already tried a very simple OpenGL or an SFML rendering example?
Title: OpenGL not rendering
Post by: drummerp on June 20, 2011, 05:46:26 pm
Quote from: "Nexus"
Sorry that I can't contribute something to the actual topic, but I think nobody has answered because of two reasons: Few people in this forum are familiar with wxWidgets, and your code is rather big and not really minimal. Have you already tried a very simple OpenGL or an SFML rendering example?


Yes, both the wxWidgets sample and the OpenGL sample provided with the full SFML SDK work fine.
Title: OpenGL not rendering
Post by: Waterlimon on June 21, 2011, 01:25:52 pm
Umm... Try making the camera spin to see if the box appears anywhere in the scene? ._.
Title: OpenGL not rendering
Post by: drummerp on June 21, 2011, 06:59:48 pm
Quote from: "Waterlimon"
Umm... Try making the camera spin to see if the box appears anywhere in the scene? ._.


Okay, I'll to to get at least basic camera movement implemented here, and then I'll try that. Thanks.

No, I'm afraid that didn't work. :? Any other advice? I really appreciate the effort. I don't know why a normal OpenGL sample and a wxWidgets sample would work fine but my code doesn't...
Title: OpenGL not rendering
Post by: drummerp on August 02, 2011, 05:53:10 am
I hate to bump a thread like this, but I seem to still be having this trouble. I created a smaller sample for you which may help, as it only uses SFML and OpenGL, removing wxWidgets from it entirely. It is a truly minimal sample. This is it:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

bool initialised = false;

sf::RenderWindow App;

void setup() {
    cout << "Setting up rendering context." << endl;
    cout << "OpenGL vendor: " << glGetString(GL_VENDOR) << endl;
    cout << "OpenGL renderer: " << glGetString(GL_RENDERER) << endl;
    cout << "OpenGL version: " << glGetString(GL_VERSION) << endl;
    App.PreserveOpenGLStates(true);
    GLfloat ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
    App.SetActive(true);
    initialised = true;
}

void changeSize(GLfloat w, GLfloat h) {
    if (initialised) {
        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();
    }
}

void render() {
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -200.0f);

    glBegin(GL_QUADS);
        glColor3f(0.1f, 1.0f, 0.1f);

        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);

        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();
}

int main()
{
    // Create the main window
    App.Create(sf::VideoMode(800, 600), "SFML window");

    setup();

// Start the game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if (Event.Type == sf::Event::Resized)
                changeSize(Event.Size.Width, Event.Size.Height);
        }

        // Clear screen
        App.Clear();

        render();

        // Update the window
        App.Display();
    }

    return EXIT_SUCCESS;
}


It still displays no more than a blank screen, and I cannot determine why. Please, any explanation is greatly appreciated, let alone help. I just want to get this working.
Title: OpenGL not rendering
Post by: Laurent on August 02, 2011, 07:51:05 am
Are you sure that your cube is not too big compared to the projection and modelview matrices that you use? If you change the ambient color to something else than white, does it change the output?
Title: OpenGL not rendering
Post by: drummerp on August 02, 2011, 06:17:24 pm
Quote from: "Laurent"
Are you sure that your cube is not too big compared to the projection and modelview matrices that you use? If you change the ambient color to something else than white, does it change the output?


I tried changing the cube to be half the size. Same output. I made the ambient colour [0.2, 0.6, 0.9] (random colour I came up with), same output. You can see the output below:

(http://oi53.tinypic.com/ou83df.jpg)
Title: OpenGL not rendering
Post by: Laurent on August 02, 2011, 06:22:07 pm
Have you tried the Window or OpenGL examples provided with the SFML SDK?
Title: OpenGL not rendering
Post by: drummerp on August 03, 2011, 04:34:09 am
Quote from: "Laurent"
Have you tried the Window or OpenGL examples provided with the SFML SDK?


Yes, both of them work just fine, which confuses me greatly.
Title: OpenGL not rendering
Post by: Laurent on August 03, 2011, 08:00:27 am
Ok, so you can start from them and try to build your own code on top of that.
Title: OpenGL not rendering
Post by: SCPM on August 04, 2011, 09:34:26 pm
Quote from: "drummerp"
It still displays no more than a blank screen, and I cannot determine why. Please, any explanation is greatly appreciated, let alone help. I just want to get this working.


Hello:
In your code at the end of your setup function, you need to setup a perspective projection like so:
Code: [Select]
   glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);


And in your render function you need to add glMatrixMode(GL_MODELVIEW); like so:

Code: [Select]
void render() {
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

I hope that helps!