Skip to content

Instantly share code, notes, and snippets.

@GunnarKarlsson
Created January 28, 2018 02:56
Show Gist options
  • Save GunnarKarlsson/15aeb569823753f4e330694fa97ebbc2 to your computer and use it in GitHub Desktop.
Save GunnarKarlsson/15aeb569823753f4e330694fa97ebbc2 to your computer and use it in GitHub Desktop.
OSX OpenGL basic example
#include <SFML/Window.hpp>
#include <OpenGL/gl3.h>
#include <iostream>
#include <fstream>
using namespace std;
// Read a shader source from a file
// store the shader source in a std::vector<char>
void read_shader_src(const char *fname, std::vector<char> &buffer) {
std::ifstream in;
in.open(fname, std::ios::binary);
if(in.is_open()) {
// Get the number of bytes stored in this file
in.seekg(0, std::ios::end);
size_t length = (size_t)in.tellg();
// Go to start of the file
in.seekg(0, std::ios::beg);
// Read the content of the file in a buffer
buffer.resize(length + 1);
in.read(&buffer[0], length);
in.close();
// Add a valid C - string end
buffer[length] = '\0';
}
else {
std::cerr << "Unable to open " << fname << " I'm out!" << std::endl;
exit(-1);
}
}
// Compile a shader
GLuint load_and_compile_shader(const char *fname, GLenum shaderType) {
// Load a shader from an external file
std::vector<char> buffer;
read_shader_src(fname, buffer);
const char *src = &buffer[0];
// Compile the shader
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
// Check the result of the compilation
GLint test;
glGetShaderiv(shader, GL_COMPILE_STATUS, &test);
if(!test) {
std::cerr << "Shader compilation failed with this message:" << glGetString(GL_VERSION) << std::endl;
std::vector<char> compilation_log(512);
glGetShaderInfoLog(shader, compilation_log.size(), NULL, &compilation_log[0]);
std::cerr << &compilation_log[0] << std::endl;
exit(-1);
}
return shader;
}
// Create a program from two shaders
GLuint create_program(const char *path_vert_shader, const char *path_frag_shader) {
// Load and compile the vertex and fragment shaders
GLuint vertexShader = load_and_compile_shader(path_vert_shader, GL_VERTEX_SHADER);
GLuint fragmentShader = load_and_compile_shader(path_frag_shader, GL_FRAGMENT_SHADER);
// Attach the above shader to a program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
// Flag the shaders for deletion
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// Link and use the program
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
return shaderProgram;
}
void initialize(GLuint &vao) {
// Use a Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// 4 triangles to be rendered
GLfloat vertices_position[24] = {
0.0, 0.0,
0.5, 0.0,
0.5, 0.5,
0.0, 0.0,
0.0, 0.5,
-0.5, 0.5,
0.0, 0.0,
-0.5, 0.0,
-0.5, -0.5,
0.0, 0.0,
0.0, -0.5,
0.5, -0.5,
};
// Create a Vector Buffer Object that will store the vertices on video memory
GLuint vbo;
glGenBuffers(1, &vbo);
// Allocate space and upload the data from CPU to GPU
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_position), vertices_position, GL_STATIC_DRAW);
GLuint shaderProgram = create_program("vert.shader", "frag.shader");
// Get the location of the attributes that enters in the vertex shader
GLint position_attribute = glGetAttribLocation(shaderProgram, "position");
// Specify how the data for position can be accessed
glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Enable the attribute
glEnableVertexAttribArray(position_attribute);
}
// Render scene
void display(GLuint &vao) {
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 12);
}
int main()
{
// create the window
sf::Window window(sf::VideoMode(1200, 1200), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
// Create a vertex array object
GLuint vao;
// Initialize the data to be rendered
initialize(vao);
// run the main loop
bool running = true;
while (running)
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
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 buffers
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(1.0f, 0.5f, 0.5f, 1.0f);
display(vao);
// end the current frame (internally swaps the front and back buffers)
window.display();
}
// release resources...
return 0;
}
//////
attribute vec4 position;
void main() {
gl_Position = position;
}
//////
uniform vec4 out_color;
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment