Following this tutorial
https://open.gl/ (specifically this one:
https://open.gl/drawing) I tried to compile my first OpenGL program. The program is supposed to show a triangle on the screen. I'm not sure if this is a problem about SFML or OpenGL. When I run the code nothing is shown (just a black screen). What needs to be changed to display the triangle correctly?
SFML version: 2.1
OpenGL version used (checked in windows.getSettings()): 3.0
OS: Linux (Debian Jessie 8.7) 64-bit
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <vector>
int main()
{
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 2;
settings.majorVersion = 3;
settings.minorVersion = 2;
sf::Window window{sf::VideoMode{800, 600}, "opengl", sf::Style::Default, settings};
window.setActive();
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
std::vector<float> vertices
{
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size(), &vertices[0], GL_STATIC_DRAW);
const char* vertexSource
{
R"glsl(
#version 150 core
in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
)glsl"
};
GLuint vertexShader{glCreateShader(GL_VERTEX_SHADER)};
glShaderSource(vertexShader, 1, &vertexSource, nullptr);
glCompileShader(vertexShader);
const char* fragmentSource
{
R"glsl(
#version 150 core
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
)glsl"
};
GLuint fragmentShader{glCreateShader(GL_FRAGMENT_SHADER)};
glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
glCompileShader(fragmentShader);
GLuint shaderProgram{glCreateProgram()};
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
GLint posAttrib{glGetAttribLocation(shaderProgram, "position")};
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
bool running{true};
while (running)
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
running = false;
else if (event.type == sf::Event::KeyPressed)
if (event.key.code == sf::Keyboard::Return)
running = false;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
window.display();
}
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
window.close();
}