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

Author Topic: OpenGL and SFML Mixing in same Render Window  (Read 5629 times)

0 Members and 1 Guest are viewing this topic.

eawerbaneth

  • Newbie
  • *
  • Posts: 7
    • View Profile
OpenGL and SFML Mixing in same Render Window
« on: April 07, 2012, 08:14:19 am »
Hi,

So I've restructured my program to have the OpenGL and SFML share the same window, but I'm having trouble getting the GUI to display. I am using a different method than that shown in the OpenGL sample project in the SDK, so I wonder if there is something I am forgetting to do. My GUI will show up until I initialize my VBO (even if I don't render it), and then I can't isolate where things are going on. Just to clarify - my OpenGL cube is appearing properly, but any 2D elements fail to appear.

I am still fairly new to OpenGL, so I'm afraid that I may just be missing something small.


void CreateVBO()
{
        OpenGLObject new_obj;

        Vertices = &(new_obj.aux_vertices[0]);
        Indices = &(new_obj.aux_indices[0]);
       
        //Create a VAO and make it active
    glGenVertexArrays(1, &VaoId);
    glBindVertexArray(VaoId);
        glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    GLenum ErrorCheckValue = glGetError();
   
        const size_t BufferSize = (sizeof(*Vertices))*new_obj.aux_vertices.size();
        const size_t VertexSize = sizeof(*Vertices);
        fprintf(stderr, "Full vertex size: %d\n",sizeof(*Vertices));
        const size_t RGBOffset = sizeof((*Vertices).XYZW);
        fprintf(stderr, "RGB offset: %d\n",sizeof((*Vertices).XYZW));

    glGenBuffers(1, &BufferId);

    glBindBuffer(GL_ARRAY_BUFFER, BufferId);
    glBufferData(GL_ARRAY_BUFFER, BufferSize, Vertices, GL_STATIC_DRAW);
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, VertexSize, (GLvoid *)0);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, VertexSize, (GLvoid*)RGBOffset);

        //Create buffers for vertex indices.
    glGenBuffers(1, &IndexBufferId);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(*Indices)*new_obj.aux_indices.size(), Indices, GL_STATIC_DRAW);
       
}

void Initialize(sf::RenderWindow &window) {

        glEnable(GL_DEPTH_TEST);
        OnGLError("Depth test");
        glDepthMask(GL_TRUE);

        CreateShaders();
    CreateVBO();

}

void RenderFunction(sf::RenderWindow &window){
    window.SetActive();

        glDisable(GL_COLOR_ARRAY);
        glDisable(GL_TEXTURE_COORD_ARRAY);

        glUseProgram(ProgramId);
    OnGLError("DRAW_ERROR: Could not use the shader program");

    glClear(/*GL_COLOR_BUFFER_BIT | */GL_DEPTH_BUFFER_BIT);
        glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ModelMatrix));
        glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ViewMatrix));
        glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(ProjectionMatrix));
        OnGLError("ERROR: Could not set the shader uniforms");

        glBindVertexArray(VaoId);
        OnGLError("ERROR: Could not bind the VAO for drawing purposes");

        glDrawElements(GL_TRIANGLES, 10000, GL_UNSIGNED_INT, (GLvoid*)0);
        OnGLError("ERROR: Could not draw the cube");

}

int main(){
        // Create main window
        sf::RenderWindow window(sf::VideoMode(800, 600), "Sculptor");
        window.EnableVerticalSync(true);

        window.SetActive();
        ...
        Initialize(window);

        sf::Clock clock;

        // start game loop
        while(window.IsOpen()){
                ...
                //display rendered frame on screen
                RenderFunction(window);

                window.PushGLStates();
                //this does not show up
                sf::Text text("testing");
                window.Draw(text);
                window.PopGLStates();

                //display rendered frame on screen
                window.Display();

        }
        ...
}

 

eawerbaneth

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: OpenGL and SFML Mixing in same Render Window
« Reply #1 on: April 16, 2012, 04:25:18 pm »
Does anyone know if there is anything preventing the use of both a VBO-based OpenGL implementation and SFML 2D elements in the same window?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL and SFML Mixing in same Render Window
« Reply #2 on: April 16, 2012, 04:33:52 pm »
Have you tried the latest revision of SFML (2.0 RC is out)?

Have you tried to make it as simple as possible (no shader, simple quad, ...) to isolate the source of the problem?
Laurent Gomila - SFML developer

eawerbaneth

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: OpenGL and SFML Mixing in same Render Window
« Reply #3 on: April 16, 2012, 06:30:54 pm »
I am using 2.0, and I'm not having a problem displaying the OpenGL elements, I just couldn't get the SFML 2D elements to show up.

 RenderFunction(window);
 window.PushGLStates();
 //this does not show up
sf::Text text("testing");
 window.Draw(text);
 window.PopGLStates();
 

Since I posted the first time, I was able to build the program and getting it working using:

glBegin(GL_TRIANGLES)
//...
glEnd();
 

I'm concerned that as the project becomes bigger, it won't be efficient enough if I don't cache the information with a VBO. If you aren't aware of any blaring reason why it would work with the glBegin method and not the VBO method, I'll try scaling everything back down again and check to see if I was making a simple error.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL and SFML Mixing in same Render Window
« Reply #4 on: April 16, 2012, 06:41:58 pm »
First update your version, I made some changes that might solve your problem.
Laurent Gomila - SFML developer

 

anything