PDA

View Full Version : [SFML/C++] Simple OpenGL Triangle



DrHaximus
01-21-2013, 04:28 PM
Hi!
I'm using SFML, which uses OpenGL, and have found that a simple OpenGL triangle will only display under weird circumstances. Here's the code for the triangle:



void drawGL() {
std::cout<<"GL\n";
glBegin(GL_TRIANGLES);
glVertex2f(0.0f,0.5);
glVertex2f(-0.5f,-0.5f);
glVertex2f(0.5f,-0.5f);
glEnd();
}


This is the part of my window loops that concerns the OpenGL portion of the window:



window.clear();
if(state==1) {
drawInterfaces();
}
else {
drawGL();
}
window.display();


drawInterfaces() is a function to display some 2D sprites. state is changed to 2 when I want the triangle to be displayed.

This will only work if I remove the state system all together and just allow drawGL() to display by its own. Example: the above window portion will NOT work. This, however, WILL work.



window.clear();
drawGL();
window.display();


I'm pretty sure this isn't SFML's fault because I can still initialize all of my sprites, load the images and put them into sprites before calling drawGL() and it works. :doh:

Thanks for reading!

Poppit
01-21-2013, 04:34 PM
Is your state system being updated properly?

For example, during the loop are you using something like getState(), or checkState()?

DrHaximus
01-21-2013, 04:35 PM
What I mean is, I have an integer to see whether or not the program should draw the interface or OpenGL. At the moment it will draw the 2D interface to begin with, but when the state is changed I get a black background with no triangle.

Poppit
01-21-2013, 04:48 PM
I tried to recreate your problem and couldn't. Could you post your whole loop please?

DrHaximus
01-21-2013, 04:53 PM
Nothing else in the loop really concerns the problem. The only relevant information to give you is that before I start the window's frame loop, I initialize some SFML sprites and textures. This doesn't seem to effect anything when I start the loop with drawGL(), though. I think this is my fault. I think I'll just do the usual "stare at the code until it shows itself" technique.

Poppit
01-21-2013, 04:57 PM
Try and put your state into a switch statement and see if that solves it


switch(state){
case 1:
drawMenu() // Whatever you called it
break;
case 2:
drawTriangle() // Draw the triangle
break;
}

DrHaximus
01-21-2013, 05:12 PM
@Poppie
Why would that make a difference? (Genuinely interested)

And no, it didn't.

DrHaximus
01-21-2013, 05:16 PM
OMG I FIXED IT!
I HAD TO USE 'pushGLStates()' AND 'popGLStates'

EXCUSE MY CAPS, I'M HAPPY.

Poppit
01-21-2013, 05:22 PM
Haha good job.

DrHaximus
01-21-2013, 05:33 PM
Sorry, I didn't thank you for your assistance.
Thank you!