1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <SFML/Graphics.hpp>
#include <glew.h>
#include <SFML/OpenGL.hpp>
int main()
{
// create the window
sf::Window window(sf::VideoMode(800, 600), "My window", sf::Style::Default, sf::ContextSettings(32));
GLuint vbo;
glewInit();
glGenBuffers(1, &vbo);
std::vector<sf::Vertex> vertices;
vertices.push_back(sf::Vertex(sf::Vector2f(0, 0)));
vertices.push_back(sf::Vertex(sf::Vector2f(100, 0)));
vertices.push_back(sf::Vertex(sf::Vector2f(100, 50)));
vertices.push_back(sf::Vertex(sf::Vector2f(0, 50)));
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,vertices.size() * sizeof(sf::Vertex),&vertices[0],GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::Resized)
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
// clear the window with black color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const char* data = reinterpret_cast<const char*>(&vertices[0]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, sizeof(sf::Vertex), data + 0);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(sf::Vertex), data + 8);
glTexCoordPointer(2, GL_FLOAT, sizeof(sf::Vertex), data + 12);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_QUADS, 0, vertices.size());
// draw everything here...
// window.draw(...);
// end the current frame
window.display();
}
glDeleteBuffers(1, &vbo);
return 0;
} |
Partager