Skip to content

Instantly share code, notes, and snippets.

@zrbecker
Created March 7, 2013 23:15
Show Gist options
  • Save zrbecker/5112741 to your computer and use it in GitHub Desktop.
Save zrbecker/5112741 to your computer and use it in GitHub Desktop.
#include <SFML/Window.hpp>
#include <glload/gl_3_3.h>
#include <glload/gll.h>
#include <vector>
#include "util.h"
GLuint theProgram;
const float vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};
GLuint positionBufferObject;
GLuint vao;
const std::string strVertexShader(
"#version 330\n"
"layout(location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n"
);
const std::string strFragmentShader(
"#version 330\n"
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
" outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\n"
);
void initialize()
{
// Initialize Shader Program
std::vector<GLuint> shaderList;
shaderList.push_back(CreateShader(GL_VERTEX_SHADER, strVertexShader));
shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, strFragmentShader));
theProgram = CreateProgram(shaderList);
std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
// Initialize Vertex Buffer
glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
}
void render()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(theProgram);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glUseProgram(0);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc, char *argv[])
{
sf::Window window(sf::VideoMode(800, 600, 32), "OpenGL Template");
sf::Clock timer;
if (LoadFunctions() == LS_LOAD_FAILED)
return 1;
initialize();
int now = timer.getElapsedTime().asMilliseconds();
int last = now;
reshape(window.getSize().x, window.getSize().y);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
reshape(window.getSize().x, window.getSize().y);
break;
}
}
render();
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment