Skip to content

Instantly share code, notes, and snippets.

@lag945
Last active July 11, 2022 09:27
Show Gist options
  • Save lag945/654a2d9144e1be27b83f4a7161759a2a to your computer and use it in GitHub Desktop.
Save lag945/654a2d9144e1be27b83f4a7161759a2a to your computer and use it in GitHub Desktop.
cross-platform gles with glfw+angle
#include <iostream>
#include <GLES3/gl3.h>
#include <GLFW/glfw3.h>
using namespace std;
void glErrorLog() {
GLuint glError = glGetError();
if (glError != GL_NO_ERROR) {
cout << "OpenGL Error : " << glError << "\n";
}
}
void outputGLESInfo() {
cout << "GL_VENDOR = " << glGetString(GL_VENDOR) << "\n";
cout << "GL_RENDERER = " << glGetString(GL_RENDERER) << "\n";
cout << "GL_VERSION = " << glGetString(GL_VERSION) << "\n";
cout << "GL_SHADING_LANGUAGE_VERSION = " << glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n";
//TL;DW
/*cout << "Extensions :\n";
string extBuffer;
stringstream extStream;
extStream << glGetString(GL_EXTENSIONS);
while (extStream >> extBuffer) {
cout << extBuffer << "\n";
}
*/
}
int main()
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(800, 600, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
outputGLESInfo();
//init scene
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
const char* vertex_shader =
"#version 100\n"
"attribute vec3 vp;"
"void main() {"
" gl_Position = vec4(vp, 1.0);"
"}";
const char* fragment_shader =
"#version 100\n"
"void main() {"
" gl_FragColor = vec4(0.5, 0.0, 0.5, 1.0);"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLint vertex_compiled;
glGetShaderiv(vs, GL_COMPILE_STATUS, &vertex_compiled);
if (vertex_compiled != GL_TRUE)
{
GLsizei log_length = 0;
GLchar message[1024];
glGetShaderInfoLog(vs, 1024, &log_length, message);
// Write the error to a log
cout << "vertex_compiled: " << message << endl;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
GLint fragment_compiled;
glGetShaderiv(fs, GL_COMPILE_STATUS, &fragment_compiled);
if (fragment_compiled != GL_TRUE)
{
GLsizei log_length = 0;
GLchar message[1024];
glGetShaderInfoLog(fs, 1024, &log_length, message);
// Write the error to a log
cout << "fragment_compiled: " << message << endl;
}
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
glErrorLog();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(shader_programme);
glBindVertexArray(vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays(GL_TRIANGLES, 0, 3);
// update other events like input handling
glfwPollEvents();
// put the stuff we've been drawing onto the display
glfwSwapBuffers(window);
glErrorLog();
}
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment