// Request a 24-bits depth buffer when creating the window
ContextSettings contextSettings;
contextSettings.depthBits = 24;
// Create the main window
RenderWindow window(VideoMode(800, 600), "SFML graphics with OpenGL", Style::Default, contextSettings);
window.setVerticalSyncEnabled(true);
// SFML loading
RectangleShape rect;
rect.setSize(Vector2f(100, 100));
rect.setFillColor(Color::Green);
// Make the window the active target for OpenGL calls
// Note: If using Texture or Shader with OpenGL,
// be sure to call Texture::getMaximumSize() and/or
// Shader::isAvailable() at least once before calling
// setActive(), as those functions will cause a context switch
window.setActive();
GLenum err = glewInit();
if(err != GLEW_OK)
{
// Problem: glewInit failed, something is seriously wrong.
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("vertexShader.vertexshader", "fragmentShader.fragmentshader");
glViewport(0, 0, window.getSize().x, window.getSize().y);
glClearColor(0.0f, 0.0f, 0.9f, 0.5f);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Projection matrix : 45° Field of View, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float)window.getSize().x / (float)window.getSize().y, 0.1f, 100.0f);
// Or, for an ortho camera :
//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 mvp = Projection * View * Model;
// Get a handle for our "MVP" uniform
// Only during the initialisation
GLuint MatrixID = glGetUniformLocation(programID, "MVP");