Skip to content

Instantly share code, notes, and snippets.

@NeuronQ
Created Oct 20, 2019
Embed
What would you like to do?
version working on macOS 10.14.6, GPU Radeon 555X, with Xcode 10.3, GLEW 2.1.0, GLFW 3.3
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
static unsigned int CompileShader(unsigned int type, const string& source) {
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
// Replaces the source code in a shader object - http://docs.gl/gl4/glShaderSource
glShaderSource(id, 1, &src, nullptr);
// Compiles a shader object - http://docs.gl/gl4/glCompileShader
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
// char* message = (char*) alloca(length * sizeof(char));
char message[length];
glGetShaderInfoLog(id, length, &length, message);
cout << "ERROR: Failed to compile " <<
(type == GL_VERTEX_SHADER ? "vertex" : "fragment") <<
" shader: " << endl;
cout << message << endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const string& vertexShader, const string& fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
int main(int argc, char* argv[]) {
/* Initialize GLFW library */
if (!glfwInit())
return -1;
/* Needed to ensure non-ancient version of OpenGL gets used on MacOS */
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
/* Create a windowed mode window and its OpenGL context */
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
GLenum err = glewInit();
if (err != GLEW_OK)
cout << "ERROR: glewInit failed: " << glewGetErrorString(err) << endl;
cout << "OpenGL version: " << glGetString(GL_VERSION) << endl;
cout << "Status: Using GLEW: " << glewGetString(GLEW_VERSION) << endl;
float positions[6] = {
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f,
};
unsigned int buffer;
// generate buffer object names - http://docs.gl/gl4/glBindBuffer
glGenBuffers(1, &buffer);
// binds a buffer object to the specified buffer binding point - http://docs.gl/gl4/glBindBuffer
glBindBuffer(GL_ARRAY_BUFFER, buffer);
// creates and initializes a buffer object's data store - http://docs.gl/gl4/glBufferData
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
unsigned int vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
// Enable or disable a generic vertex attribute array - http://docs.gl/gl4/glEnableVertexAttribArray
glEnableVertexAttribArray(0);
// define an array of generic vertex attribute data - http://docs.gl/gl4/glVertexAttribPointer
glVertexAttribPointer(
0, // index
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized
sizeof(float) * 2, // stride
0 // pointer
);
string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";
string fragmentShader =
"#version 330 core\n"
"\n"
"out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
// render primitives from array data - http://docs.gl/gl4/glDrawArrays
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(shader);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment