Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL not rendering  (Read 5754 times)

0 Members and 1 Guest are viewing this topic.

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« 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.

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
OpenGL not rendering
« Reply #1 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);...
- Fouf

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #2 on: June 03, 2011, 02:24:59 am »
I'm afraid that didn't work, no... Thanks for trying.

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
OpenGL not rendering
« Reply #3 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
- Fouf

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #4 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.

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #5 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...

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #6 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6276
  • Thor Developer
    • View Profile
    • Bromeon
OpenGL not rendering
« Reply #7 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #8 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.

Waterlimon

  • Newbie
  • *
  • Posts: 7
    • View Profile
OpenGL not rendering
« Reply #9 on: June 21, 2011, 01:25:52 pm »
Umm... Try making the camera spin to see if the box appears anywhere in the scene? ._.
This sententace makes me more unique. Too lazy to upload avatar. Nobody reads usernames.

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #10 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...

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #11 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL not rendering
« Reply #12 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?
Laurent Gomila - SFML developer

drummerp

  • Newbie
  • *
  • Posts: 18
    • View Profile
OpenGL not rendering
« Reply #13 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:


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
OpenGL not rendering
« Reply #14 on: August 02, 2011, 06:22:07 pm »
Have you tried the Window or OpenGL examples provided with the SFML SDK?
Laurent Gomila - SFML developer

 

anything