1
Graphics / sfml2 RenderWindow + OpenGL
« on: February 13, 2012, 11:39:07 am »
Thank you so much for your help. It worked just fine to disable all arrays I didn't need. Now I can go on.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#include <iostream>
#include <glew.h>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
void handleEvents(RenderWindow*);
void fillVBO();
void drawGL();
bool running = true;
GLuint vertexBuffer;
static const GLfloat vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
int main() {
VideoMode mode;
mode.BitsPerPixel = 32;
mode.Width = 1024;
mode.Height = 768;
ContextSettings cs;
cs.AntialiasingLevel = 4;
cs.DepthBits = 32;
cs.StencilBits = 16;
cs.MajorVersion = 3;
cs.MinorVersion = 3;
RenderWindow App(mode, "SFML window", Style::Close|Style::Resize, cs);
cout << "Window OK" << endl;
if(glewInit() != GLEW_OK) {
cout << "Unable it initialize Glew!" << endl;
return -1;
}
else {
cout << "GLEW initialization OK" << endl;
}
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
fillVBO();
cout << "Fill VBO OK" << endl;
while(running) {
App.SetActive();
handleEvents(&App);
cout << "Handle Events OK" << endl;
drawGL();
cout << "Draw GL OK" << endl;
App.Display();
}
return EXIT_SUCCESS;
}
void handleEvents(RenderWindow* wnd) {
Event ev;
while(wnd->PollEvent(ev)) {
switch(ev.Type) {
case Event::Closed:
running = false;
break;
case Event::KeyPressed:
switch(ev.Key.Code) {
case Keyboard::Escape:
running = false;
break;
default:
break;
}
break;
case Event::Resized:
glViewport(0, 0, ev.Size.Width, ev.Size.Height);
break;
default:
break;
}
}
}
void fillVBO() {
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
}
void drawGL() {
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
}