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

Author Topic: Combining OpenGL and SFML graphics  (Read 2721 times)

0 Members and 1 Guest are viewing this topic.

sludge

  • Newbie
  • *
  • Posts: 24
    • View Profile
Combining OpenGL and SFML graphics
« on: July 23, 2010, 05:20:53 pm »
I suppose that this question is more related to my lack of understanding of OpenGL than anything else, but I figured I'd ask away.  

So I have a program that draws some things using functions native SFML, and I am trying to draw a three dimensional shape in the same window using OpenGL.

I am able to get both APIs to render their images independently but not concurrently.

When I initialize and draw the graphics from OpenGL to the window, the entire screen appears back, and then a few lines that I defined are drawn in the lower-left corner (where I set the viewport).

My guess is that the entire OpenGL buffer is being rendered to the entire screen, as opposed to the few lines that I would like to draw, and in the process prints over the images drawn by SFML.  I suspect that I need to render the buffer to only a small section of the window instead.

Let me post a couple snip-its of code that include the rendering and initialization of OpenGL and the rendering of the SFML and OpenGL graphics.

Code: [Select]

bool Simulation::Initialize()
{
    // Allows us to render both SFML and OpenGL functions
    MainWindow.PreserveOpenGLStates(true);  

    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glClearDepth(1.f);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.f, 1.f, 1.f, 500.f);

    glViewport(0, 0, MainWindow.GetWidth()/2, MainWindow.GetHeight()/2);

//more stuff
}

void Simulation::Render()
{
        // These functions draw to the MainWindow using SFML
DrawVariableLabelValues();
DrawTextNames();
DrawTextCursor();

// This is function draws using OpenGL functions
DrawGraph();

    // Render the graphics drawn onto the MainWindow
    MainWindow.Display();
}

//Below I try to simply draw the coordinate system on which I will display a dynamics plotting system
void Simulation::DrawGraph()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.f, 0.f, -1.f);

    // Draw the axes of the graph
    glBegin(GL_LINES);

// x-axis
        glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(200.0f, 0.0f, 0.0f);

// y-axis
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 200.0f, 0.0f);

// z-axis
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 200.0f);
    glEnd();
}




Another thing I would like to mention is that when I try to call glTranslate using x and y arguments other than 0, the lines that I want to draw disappear.  Can anyone give me a hand here.  I just starting using OpenGL for the first time today.

Lastly, do I need to call clear on the MainWindow anymore?  It seems that like that clears out everything that I have told OpenGL to draw.

Thank you

NoIdea

  • Full Member
  • ***
  • Posts: 149
    • View Profile
Combining OpenGL and SFML graphics
« Reply #1 on: October 03, 2010, 02:15:36 pm »
I'll take a guess and say that this makes your problem :
Code: [Select]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
It might not be the case because I don't really know what glPreserveOpenGLStates does "behind" but if it doesn't save that, then your line just erases all you've done before.