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

Author Topic: sfml2 RenderWindow + OpenGL  (Read 1561 times)

0 Members and 1 Guest are viewing this topic.

Jean-Claude

  • Newbie
  • *
  • Posts: 3
    • View Profile
sfml2 RenderWindow + OpenGL
« on: February 12, 2012, 11:05:25 pm »
Hi,
I'm trying to set up an OpenGL context with sfml2. Everthing worked fine and I was able to draw a triangle. In the next step I want to draw some 2D elements with SFML on top of the GL stuff. So I first changed all "Window" entries to "RenderWindow" to be able to draw something. There are no errors but when I compile the program, but it always crashes before drawing, and I don't know why. With sfml1.6 it worked with a RenderWindow. What makes it crash?

this is my code:
Code: [Select]

#include <iostream>
#include <glew.h>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>


using namespace std;
using namespace sf;

void handleEvents(RenderWindow*);
void fillVBO();
void drawGL();

bool running = true;
GLuint vertexBuffer;

static const GLfloat vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
};

int main() {
    VideoMode mode;
    mode.BitsPerPixel = 32;
    mode.Width = 1024;
    mode.Height = 768;
    ContextSettings cs;
    cs.AntialiasingLevel = 4;
    cs.DepthBits = 32;
    cs.StencilBits = 16;
    cs.MajorVersion = 3;
    cs.MinorVersion = 3;

    RenderWindow App(mode, "SFML window", Style::Close|Style::Resize, cs);

    cout << "Window OK" << endl;

    if(glewInit() != GLEW_OK) {
        cout << "Unable it initialize Glew!" << endl;
        return -1;
    }
    else {
        cout << "GLEW initialization OK" << endl;
    }
    glClearColor(0.0f, 0.0f, 0.3f, 0.0f);

    fillVBO();

    cout << "Fill VBO OK" << endl;

    while(running) {
        App.SetActive();
        handleEvents(&App);
        cout << "Handle Events OK" << endl;
        drawGL();
        cout << "Draw GL OK" << endl;

        App.Display();
    }
    return EXIT_SUCCESS;
}

void handleEvents(RenderWindow* wnd) {
    Event ev;
    while(wnd->PollEvent(ev)) {
        switch(ev.Type) {
        case Event::Closed:
            running = false;
            break;
        case Event::KeyPressed:
            switch(ev.Key.Code) {
            case Keyboard::Escape:
                running = false;
                break;
            default:
                break;
            }
            break;
        case Event::Resized:
            glViewport(0, 0, ev.Size.Width, ev.Size.Height);
            break;
        default:
            break;
        }
    }
}

void fillVBO() {
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
}

void drawGL() {
    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(
       0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
       3,                  // size
       GL_FLOAT,           // type
       GL_FALSE,           // normalized?
       0,                  // stride
       (void*)0            // array buffer offset
    );

    // Draw the triangle !
    glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

    glDisableVertexAttribArray(0);
}

I hope you can help me with this. Thanks in advance.

Kind regards,

Jean-Claude

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sfml2 RenderWindow + OpenGL
« Reply #1 on: February 13, 2012, 08:16:24 am »
SFML internally enables vertex, color and texcoords components for drawing. You must disable those that you don't use.

It will be fixed soon.
Laurent Gomila - SFML developer

Jean-Claude

  • Newbie
  • *
  • Posts: 3
    • View Profile
sfml2 RenderWindow + OpenGL
« Reply #2 on: February 13, 2012, 09:08:10 am »
Okay, I understand. But I'm pretty much at the beginning of 3D programming and using SFML so I'm not completely sure what I have to change now. Could you give me another hint in the right direction. I tried just to leave out the glEnableVertexAttribArray(0) call but that didn't make a change?
And one more question. Can you tell me why it works with the Window class but not with the RenderWindow class? Is it because RenderWindow also uses OpenGL for drawing and its calls and mine come into conflict?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sfml2 RenderWindow + OpenGL
« Reply #3 on: February 13, 2012, 09:46:25 am »
See https://github.com/SFML/SFML/issues/167

Quote
Is it because RenderWindow also uses OpenGL for drawing and its calls and mine come into conflict?

Yes.
Laurent Gomila - SFML developer

Jean-Claude

  • Newbie
  • *
  • Posts: 3
    • View Profile
sfml2 RenderWindow + OpenGL
« Reply #4 on: February 13, 2012, 11:39:07 am »
Thank you so much for your help. It worked just fine to disable all arrays I didn't need. Now I can go on. :)