From e4aba7feaabb78d4845e17e5063f7e22f0628e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Sun, 19 May 2019 20:59:10 +0200 Subject: [PATCH] Convert triangle-opengl example to 3.3 core --- docs/quick.dox | 9 +++++++-- examples/triangle-opengl.c | 24 +++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/quick.dox b/docs/quick.dox index 1ad9be0f..80433371 100644 --- a/docs/quick.dox +++ b/docs/quick.dox @@ -135,9 +135,14 @@ require a minimum OpenGL version by setting the `GLFW_CONTEXT_VERSION_MAJOR` and `GLFW_CONTEXT_VERSION_MINOR` hints _before_ creation. If the required minimum version is not supported on the machine, context (and window) creation fails. +You can select the OpenGL profile by setting the `GLFW_OPENGL_PROFILE` hint. +This program uses the core profile as that is the only profile macOS supports +for OpenGL 3.x and 4.x. + @code -glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); -glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); +glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); +glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); +glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL); if (!window) { diff --git a/examples/triangle-opengl.c b/examples/triangle-opengl.c index 67457745..cc2ee16f 100644 --- a/examples/triangle-opengl.c +++ b/examples/triangle-opengl.c @@ -47,11 +47,11 @@ static const Vertex vertices[3] = }; static const char* vertex_shader_text = -"#version 110\n" +"#version 330\n" "uniform mat4 MVP;\n" -"attribute vec3 vCol;\n" -"attribute vec2 vPos;\n" -"varying vec3 color;\n" +"in vec3 vCol;\n" +"in vec2 vPos;\n" +"out vec3 color;\n" "void main()\n" "{\n" " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" @@ -59,11 +59,12 @@ static const char* vertex_shader_text = "}\n"; static const char* fragment_shader_text = -"#version 110\n" -"varying vec3 color;\n" +"#version 330\n" +"in vec3 color;\n" +"out vec4 fragment;\n" "void main()\n" "{\n" -" gl_FragColor = vec4(color, 1.0);\n" +" fragment = vec4(color, 1.0);\n" "}\n"; static void error_callback(int error, const char* description) @@ -84,8 +85,9 @@ int main(void) if (!glfwInit()) exit(EXIT_FAILURE); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL); if (!window) @@ -124,6 +126,9 @@ int main(void) const GLint vpos_location = glGetAttribLocation(program, "vPos"); const GLint vcol_location = glGetAttribLocation(program, "vCol"); + GLuint vertex_array; + glGenVertexArrays(1, &vertex_array); + glBindVertexArray(vertex_array); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, pos)); @@ -148,6 +153,7 @@ int main(void) glUseProgram(program); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); + glBindVertexArray(vertex_array); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window);