Created
May 23, 2020 09:23
-
-
Save alexmercerind/1760b4f48a78c188fdbac1fe55ebf20d to your computer and use it in GitHub Desktop.
Colored Square in SFML GLEW C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GL/glew.h> | |
#include <SFML/Graphics.hpp> | |
#include <iostream> | |
#include <string> | |
#include <shader_compiler.h> | |
int main() { | |
using namespace sf; | |
RenderWindow window(VideoMode(800, 800), "Alex Mercer", Style::Default); | |
window.setFramerateLimit(60); | |
glewInit(); | |
compile_shader shader("vertexshader.vert", "fragmentshader.frag"); | |
GLfloat vertices[12] = { | |
-0.5f, -0.5f, 0.0f, | |
-0.5f, 0.5f, 0.0f, | |
0.5f, 0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f, | |
}; | |
GLfloat colors[12] = { | |
1.0f, 0.0f, 0.0f, | |
0.0f, 1.0f, 0.0f, | |
0.0f, 0.0f, 1.0f, | |
1.0f, 1.0f, 1.0f, | |
}; | |
GLuint indices[6] = { | |
0, 1, 2, | |
2, 3, 0, | |
}; | |
GLuint VBOVertices, VBOColors, VAO, VBOIndices; | |
glGenBuffers(1, &VBOVertices); | |
glBindBuffer(GL_ARRAY_BUFFER, VBOVertices); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
glGenVertexArrays(1, &VAO); | |
glBindVertexArray(VAO); | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat) * 0)); | |
glEnableVertexAttribArray(0); | |
glGenBuffers(1, &VBOColors); | |
glBindBuffer(GL_ARRAY_BUFFER, VBOColors); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); | |
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (GLvoid*)(sizeof(GLfloat) * 0)); | |
glEnableVertexAttribArray(1); | |
glGenBuffers(1, &VBOIndices); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOIndices); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); | |
glBindVertexArray(0); | |
while (window.isOpen()) { | |
Event event; | |
while (window.pollEvent(event)) { | |
if (event.type == Event::Closed) { | |
window.close(); | |
}; | |
}; | |
glClear(GL_COLOR_BUFFER_BIT); | |
glClearColor(0.0, 0.0, 0.0, 1.0); | |
shader.use_shader(); | |
glBindVertexArray(VAO); | |
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL); | |
glBindVertexArray(0); | |
window.display(); | |
}; | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment