Convert triangle-opengl example to 3.3 core

This commit is contained in:
Camilla Löwy 2019-05-19 20:59:10 +02:00
parent a639d6e635
commit e4aba7feaa
2 changed files with 22 additions and 11 deletions

View File

@ -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 `GLFW_CONTEXT_VERSION_MINOR` hints _before_ creation. If the required minimum
version is not supported on the machine, context (and window) creation fails. 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 @code
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL); GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
if (!window) if (!window)
{ {

View File

@ -47,11 +47,11 @@ static const Vertex vertices[3] =
}; };
static const char* vertex_shader_text = static const char* vertex_shader_text =
"#version 110\n" "#version 330\n"
"uniform mat4 MVP;\n" "uniform mat4 MVP;\n"
"attribute vec3 vCol;\n" "in vec3 vCol;\n"
"attribute vec2 vPos;\n" "in vec2 vPos;\n"
"varying vec3 color;\n" "out vec3 color;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
@ -59,11 +59,12 @@ static const char* vertex_shader_text =
"}\n"; "}\n";
static const char* fragment_shader_text = static const char* fragment_shader_text =
"#version 110\n" "#version 330\n"
"varying vec3 color;\n" "in vec3 color;\n"
"out vec4 fragment;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" gl_FragColor = vec4(color, 1.0);\n" " fragment = vec4(color, 1.0);\n"
"}\n"; "}\n";
static void error_callback(int error, const char* description) static void error_callback(int error, const char* description)
@ -84,8 +85,9 @@ int main(void)
if (!glfwInit()) if (!glfwInit())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL); GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL);
if (!window) if (!window)
@ -124,6 +126,9 @@ int main(void)
const GLint vpos_location = glGetAttribLocation(program, "vPos"); const GLint vpos_location = glGetAttribLocation(program, "vPos");
const GLint vcol_location = glGetAttribLocation(program, "vCol"); const GLint vcol_location = glGetAttribLocation(program, "vCol");
GLuint vertex_array;
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
glEnableVertexAttribArray(vpos_location); glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (void*) offsetof(Vertex, pos)); sizeof(Vertex), (void*) offsetof(Vertex, pos));
@ -148,6 +153,7 @@ int main(void)
glUseProgram(program); glUseProgram(program);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window); glfwSwapBuffers(window);