opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit f76ada37bb1470a7c15a1f56f0b83accead0097a
parent d9d61540c09c9eca4f90e4311eda21effff8268a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 25 Jun 2023 12:33:38 +0900

shaders

Diffstat:
Mmain.c | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 102 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c @@ -1,9 +1,46 @@ #include <stdio.h> #include <stdlib.h> +#include <math.h> #include <glad/glad.h> #include <GLFW/glfw3.h> +float vertices[] = { + 0.5, 0.5, 0.0, + 0.5, -0.5, -0.0, + -0.5, -0.5, 0.0, + -0.5, 0.5, 0.0, +}; + +unsigned int indices[] = { + 0, 1, 3, + 1, 2, 3, +}; + +const char *vertexShaderSource = " + #version 330 core + layout (location = 0) in vec3 aPos; + + void + main() + { + gl_Position = vec4(aPos, 1.0); + } +"; + +const char *fragmentShaderSource = " + #version 330 core + uniform vec4 ourColor; + out vec4 FragColor; + + void + main() + { + FragColor = ourColor; + } +"; + + void framebuffer_size_callback(GLFWwindow *window, int width, int height) { @@ -44,16 +81,79 @@ main(void) // register callback functions glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + // setup shaders + unsigned int vertexShader; + int success; + char log[512]; + vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShader); + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertexShader, sizeof(log), NULL, log); + fprintf(stderr, "compile vertex shader: %s\n", log); + exit(1); + } + unsigned int fragmentShader; + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragmentShader, sizeof(log), NULL, log); + fprintf(stderr, "compile fragment shader: %s\n", log); + exit(1); + } + unsigned int shaderProgram; + shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + glGetShaderiv(shaderProgram, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(shaderProgram, sizeof(log), NULL, log); + fprintf(stderr, "link shaders: %s\n", log); + exit(1); + } + glUseProgram(shaderProgram); + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + // setup vbo + unsigned int VAO, VBO, EBO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glGenBuffers(1, &EBO); + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), + (void *)0); + glEnableVertexAttribArray(0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, + GL_STATIC_DRAW); + while (!glfwWindowShouldClose(window)) { processInput(window); - glClearColor(0.2, 0.3, 0.3, 1.0); + glClearColor(1.0, 1.0, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); + + float timeValue = glfwGetTime(); + float greenValue = sinf(timeValue) / 2.0 + 0.5; + int vertexColorLocation = + glGetUniformLocation(shaderProgram, "ourColor"); + glUniform4f(vertexColorLocation, 0.0, greenValue, 0.0, 1.0); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glfwPollEvents(); glfwSwapBuffers(window); } - + + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shaderProgram); glfwTerminate(); return 0; }