Sorry for the delay,
Considering I'm a complete beginner at VBOs, I don't really know what I can cut, so this is the smallest I can get the source code:
// Link statically with GLEW
#define GLEW_STATIC
// Headers
#include <GL/glew.h>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
// Shader sources
const char* vertexSource =
"#version 150\n"
"in vec2 position;"
"in vec3 color;"
"out vec3 Color;"
"void main() {"
" Color = color;"
" gl_Position = vec4( position, 0.0, 1.0 );"
"}";
const char* fragmentSource =
"#version 150\n"
"in vec3 Color;"
"out vec4 outColor;"
"void main() {"
" outColor = vec4( Color, 1.0 );"
"}";
int main()
{
int state = 1; //IF 0 = PROGRAM ENDS
sf::RenderWindow window( sf::VideoMode( 800, 600, 32 ), "OpenGL", sf::Style::Titlebar | sf::Style::Close );
// Initialize GLEW
glewExperimental = GL_TRUE;
glewInit();
// Create Vertex Array Object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers( 1, &vbo );
float vertices[] = {
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f // Bottom-left
};
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
// Create an element array
GLuint ebo;
glGenBuffers( 1, &ebo );
GLuint elements[] = {
0, 1, 2,
2, 3, 0
};
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ebo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( elements ), elements, GL_STATIC_DRAW );
// Create and compile the vertex shader
GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &vertexSource, NULL );
glCompileShader( vertexShader );
// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragmentSource, NULL );
glCompileShader( fragmentShader );
// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, vertexShader );
glAttachShader( shaderProgram, fragmentShader );
glBindFragDataLocation( shaderProgram, 0, "outColor" );
glLinkProgram( shaderProgram );
glUseProgram( shaderProgram );
// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation( shaderProgram, "position" );
glEnableVertexAttribArray( posAttrib );
glVertexAttribPointer( posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof( float ), 0 );
GLint colAttrib = glGetAttribLocation( shaderProgram, "color" );
glEnableVertexAttribArray( colAttrib );
glVertexAttribPointer( colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof( float ), (void*)( 2 * sizeof( float ) ) );
while ( state != 0 )
{
sf::Event windowEvent;
while ( window.pollEvent( windowEvent ) )
{
switch ( windowEvent.type )
{
case sf::Event::Closed:
state = 0;
break;
}
}
// Clear the screen to black
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0 );
window.pushGLStates();
//RENDER SFML HERE
window.popGLStates();
//If disabled, nothing gets drawn and no crash occurs
glDisableClientState(GL_VERTEX_ARRAY);
//Both has to be disabled or a crash occurs. If GL_VERTEX_ARRAY is enabled and
//GL_COLOR_ARRAY and GL_TEXTURE_COORD_ARRAY are disabled, then a white box is drawn
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
window.display();
}
glDeleteProgram( shaderProgram );
glDeleteShader( fragmentShader );
glDeleteShader( vertexShader );
glDeleteBuffers( 1, &ebo );
glDeleteBuffers( 1, &vbo );
glDeleteVertexArrays( 1, &vao );
}