[SFML/C++] Simple OpenGL Triangle

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
";
	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!

Is your state system being updated properly?

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

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.

I tried to recreate your problem and couldn’t. Could you post your whole loop please?

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.

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;
}

@Poppie
Why would that make a difference? (Genuinely interested)

And no, it didn’t.

OMG I FIXED IT!
I HAD TO USE ‘pushGLStates()’ AND ‘popGLStates’

EXCUSE MY CAPS, I’M HAPPY.

Haha good job.

Sorry, I didn’t thank you for your assistance.
Thank you!