Hello everyone, I hope your projects are all working fine. Because mine, not so much.
I recently started adding OpenGL to my project, to do the 3d rendering while SFML is supposed to manage the windowing and GUI parts. The GUI system itself works very fine without OpenGL, but since I added it graphical bugs have risen.
Some pin-pointing and debugging led me to the conclusion that the command "glBindBuffer" is responsible for my misery, because without it my GUI system works like usual.
Here are the important code chunks :
1)The drawing function of the Container class. This class contains other widgets/parts of the GUI system and renders them if asked. With VBO generated, it only has the color given in"myRenderTexture.get()->clear(sf::Color(0,0,0,200));"
void Container::draw(sf::RenderTarget& target) const
{
if (updated)//We only do that if there is a need to re-draw the Container
{
background.setPosition(sf::Vector2f(0,0));
myRenderTexture.get()->clear(sf::Color(0,0,0,200));
background.draw(*myRenderTexture.get());
for (auto it = allWidgets.begin() ; it != allWidgets.end() ; it++)
if (it->second.hideState == false)
renderWidgetDirection(*((it->second).widget), *myRenderTexture.get());
myRenderTexture.get()->display();
renderSprite.setTexture(myRenderTexture.get()->getTexture());
}
target.draw(renderSprite);
}
2)Most of the Container class declaration
class Container : public Widget{//Ok
public:
/** Irrelevant stuff */
virtual void update(sf::RenderWindow& window, sf::Vector2f origin = sf::Vector2f(0,0));
virtual void draw(sf::RenderTarget& target) const;
private:
/** Irrelevant stuff */
sf::Sprite renderSprite;
std::shared_ptr<sf::RenderTexture> myRenderTexture;
};
3)Constructor of the OpenGLRenderer class. This is where the faulty code bits lie
OpenGLRenderer::OpenGLRenderer()
{
if (gl3wInit())
{
std::cerr << "Failed to initialize gl3w\n";
}
if (!sf::Shader::isAvailable())
{
std::cerr << "Can't use shaders. Maybe your graphic card is too old ? Check your drivers too !\n";
}
GLfloat g_vertex_buffer_data[] = {
0.1, 0.0, 0.5, -0.5f, -0.5f, 0.0f,
0.5, 0.4, 0.1, 0.5f, -0.5f, 0.0f,
1.0, 0.7, 0.0, 0.0f, 0.5f, 0.0f
};
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
shader.loadFromFile("src/shaders/vertex_test.txt", "src/shaders/frag_test.txt");
}
I really have no idea why this bug occurs. My understanding is that sfml uses opengl, so maybe there is some deadly overlap somewhere ? If anyone could explain me why this doesn't work, I would be thankful forever !