summary refs log tree commit diff
path: root/src/main.c
diff options
context:
space:
mode:
authoriurii plugatarov <[email protected]>2024-08-10 14:22:45 +0200
committeriurii plugatarov <[email protected]>2024-08-10 14:22:45 +0200
commit90e85b2fe655a2643535b0f49d99836ac72e724c (patch)
tree66c0656984da7b2b8a6c06c4b0af764919e0e318 /src/main.c
parent86eb1bfcf21819492e0ec55cd08127fa8a5c6769 (diff)
downloadogl-90e85b2fe655a2643535b0f49d99836ac72e724c.tar.gz
custom shader loader
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c67
1 files changed, 9 insertions, 58 deletions
diff --git a/src/main.c b/src/main.c
index a125823..96882b6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "shader.h"
 
 void on_resize(GLFWwindow *win, int width, int height) {
   glViewport(0, 0, width, height);
@@ -53,65 +54,15 @@ int main() {
   unsigned int VBO;
   glGenBuffers(1, &VBO);
   glBindBuffer(GL_ARRAY_BUFFER, VBO);
-  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, vertices,
-               GL_STATIC_DRAW);
-
-  const char *vertex_shader_src =
-      "#version 330 core\n"
-      "layout (location = 0) in vec3 aPos;\n"
-      "layout (location = 1) in vec3 aColor;\n"
-      "out vec3 ourColor;\n"
-      "void main()\n"
-      "{\n"
-      "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
-      "   ourColor = aColor;\n"
-      "}\0";
-
-  unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
-  glShaderSource(vertex_shader, 1, &vertex_shader_src, NULL);
-  glCompileShader(vertex_shader);
-
-  int success;
-  char info_log[512];
-  glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
-
-  if (!success) {
-    glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
-    printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", info_log);
-  }
-
-  const char *fragment_shader_src = "#version 330 core\n"
-                                    "out vec4 FragColor;\n"
-                                    "in vec3 ourColor;\n"
-                                    "void main()\n"
-                                    "{\n"
-                                    " FragColor = vec4(ourColor, 1.0);\n"
-                                    "}\0";
-
-  unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
-  glShaderSource(fragment_shader, 1, &fragment_shader_src, NULL);
-  glCompileShader(fragment_shader);
-  glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
-
-  if (!success) {
-    glGetShaderInfoLog(fragment_shader, 512, NULL, info_log);
-    printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", info_log);
-  }
-
-  unsigned int shader_program = glCreateProgram();
-  glAttachShader(shader_program, vertex_shader);
-  glAttachShader(shader_program, fragment_shader);
-  glLinkProgram(shader_program);
+  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 
-  glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
-  if (!success) {
-    glGetProgramInfoLog(shader_program, 512, NULL, info_log);
-    printf("ERROR::SHADER::PROGRAM::LINK_FAILED\n%s\n", info_log);
+  // Create and compile shaders using our new shader implementation
+  Shader* shader = shader_create("src/shaders/triangle.vert.glsl", "src/shaders/triangle.frag.glsl");
+  if (!shader) {
+    glfwTerminate();
+    return -1;
   }
 
-  glDeleteShader(vertex_shader);
-  glDeleteShader(fragment_shader);
-
   unsigned int VAO;
   glGenVertexArrays(1, &VAO);
   glBindVertexArray(VAO);
@@ -123,7 +74,7 @@ int main() {
                         (void *)(3 * sizeof(float)));
   glEnableVertexAttribArray(1);
 
-  glUseProgram(shader_program);
+  shader_use(shader);
   while (!glfwWindowShouldClose(window)) {
     glClearColor(0.1f, 0.1f, 0.25f, 1.0f);
     glClear(GL_COLOR_BUFFER_BIT);
@@ -137,7 +88,7 @@ int main() {
 
   glDeleteVertexArrays(1, &VAO);
   glDeleteBuffers(1, &VBO);
-  glDeleteProgram(shader_program);
+  shader_delete(shader);
 
   glfwTerminate();
   return 0;