1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-26 20:28:49 -05:00

Convert OpenGL triangle example to C99

This commit is contained in:
Camilla Löwy 2019-05-19 19:30:40 +02:00
parent c415c71947
commit f61d0916fd

View File

@ -76,10 +76,6 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action,
int main(void) int main(void)
{ {
GLFWwindow* window;
GLuint vertex_buffer, vertex_shader, fragment_shader, program;
GLint mvp_location, vpos_location, vcol_location;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
if (!glfwInit()) if (!glfwInit())
@ -88,7 +84,7 @@ int main(void)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL); GLFWwindow* window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@ -103,26 +99,27 @@ int main(void)
// NOTE: OpenGL error checks have been omitted for brevity // NOTE: OpenGL error checks have been omitted for brevity
GLuint vertex_buffer;
glGenBuffers(1, &vertex_buffer); glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
vertex_shader = glCreateShader(GL_VERTEX_SHADER); const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader); glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader); glCompileShader(fragment_shader);
program = glCreateProgram(); const GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader); glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader); glAttachShader(program, fragment_shader);
glLinkProgram(program); glLinkProgram(program);
mvp_location = glGetUniformLocation(program, "MVP"); const GLint mvp_location = glGetUniformLocation(program, "MVP");
vpos_location = glGetAttribLocation(program, "vPos"); const GLint vpos_location = glGetAttribLocation(program, "vPos");
vcol_location = glGetAttribLocation(program, "vCol"); const GLint vcol_location = glGetAttribLocation(program, "vCol");
glEnableVertexAttribArray(vpos_location); glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
@ -133,16 +130,14 @@ int main(void)
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
float ratio;
int width, height; int width, height;
mat4x4 m, p, mvp;
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height; const float ratio = width / (float) height;
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
mat4x4 m, p, mvp;
mat4x4_identity(m); mat4x4_identity(m);
mat4x4_rotate_Z(m, m, (float) glfwGetTime()); mat4x4_rotate_Z(m, m, (float) glfwGetTime());
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);