opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit 45b21710cecd9275479b1cefd7a9870f53a0866c
parent 633aaf0ca205d94558f4231bc3f5874ada89f21a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 25 Jun 2023 15:10:37 +0900

add shader struct

Diffstat:
Mmain.c | 103++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 68 insertions(+), 35 deletions(-)

diff --git a/main.c b/main.c @@ -12,11 +12,74 @@ float vertices[] = { -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, }; +char errstr[512]; + unsigned int indices[] = { 0, 1, 3, 1, 2, 3, }; +typedef struct Shader { + unsigned int ID; +} Shader; + +Shader * +ShaderInit(const char *vertexShaderSource, const char *fragmentShaderSource) +{ + 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); + snprintf(errstr, sizeof(errstr), + "compile vertex shader: %s\n", log); + return NULL; + } + 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); + snprintf(errstr, sizeof(errstr), + "compile fragment shader: %s\n", log); + return NULL; + } + 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); + snprintf(errstr, sizeof(errstr), + "link shaders: %s\n", log); + return NULL; + } + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + Shader *shader = (Shader *) malloc(sizeof(Shader)); + shader->ID = shaderProgram; + return shader; +} + +void +ShaderUse(Shader *s) { + glUseProgram(s->ID); +} + +void +ShaderDelete(Shader *s) { + glDeleteProgram(s->ID); +} + const char *vertexShaderSource = " #version 330 core layout (location = 0) in vec3 aPos; @@ -85,42 +148,12 @@ main(void) 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); + Shader *shader = ShaderInit(vertexShaderSource, fragmentShaderSource); + if (shader == NULL) { + fprintf(stderr, "init shader: %s\n", errstr); 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); + ShaderUse(shader); // setup vbo unsigned int VAO, VBO, EBO; @@ -154,7 +187,7 @@ main(void) glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); - glDeleteProgram(shaderProgram); + ShaderDelete(shader); glfwTerminate(); return 0; }