summary refs log tree commit diff
diff options
context:
space:
mode:
authormakefunstuff <[email protected]>2024-08-07 20:50:16 +0200
committermakefunstuff <[email protected]>2024-08-07 20:50:16 +0200
commit6b94ae24f09a48f7d20942d48741251ada2bda43 (patch)
tree6eedc283460affbae8a0392a6246b8879e9571ef
parent07d31cd578dbc13024b15b60a8ae4c5eb1b06609 (diff)
downloadogl-6b94ae24f09a48f7d20942d48741251ada2bda43.tar.gz
upd
-rw-r--r--Makefile2
-rw-r--r--src/main.c58
2 files changed, 46 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 998e57c..5e8d103 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ CFLAGS_RELEASE = $(CFLAGS) -O2
 TARGET = ogl
 SRC_DIR = src
 OUT_DIR = out
-LIBS = -lglfw -framework OpenGL
+LIBS = -lglfw -lGLEW -lGL
 DEBUG_DIR = $(OUT_DIR)/debug
 RELEASE_DIR = $(OUT_DIR)/release
 INSTALL_DIR = /usr/local/bin
diff --git a/src/main.c b/src/main.c
index a333712..aa042b0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,3 +1,4 @@
+#include <GL/glew.h>
 #include <GLFW/glfw3.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -14,10 +15,9 @@ int main() {
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
 
   // Create a windowed mode window and its OpenGL context
-  GLFWwindow *window = glfwCreateWindow(800, 600, "Blue screen", NULL, NULL);
+  GLFWwindow *window = glfwCreateWindow(320, 240, "Blue screen", NULL, NULL);
   if (!window) {
     glfwTerminate();
     return -1;
@@ -25,7 +25,18 @@ int main() {
 
   glfwMakeContextCurrent(window);
 
-  float vertices[6] = {-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, -0.5f};
+  // Initialize GLEW
+  GLenum glewStatus = glewInit();
+  if (glewStatus != GLEW_OK) {
+    printf("Failed to initialize GLEW: %s\n", glewGetErrorString(glewStatus));
+    return -1;
+  }
+
+  float vertices[] = {
+      -0.5f, -0.5f, 0.0f,
+       0.5f, -0.5f, 0.0f,
+       0.0f,  0.5f, 0.0f
+  };
 
   unsigned int VBO;
   glGenBuffers(1, &VBO);
@@ -40,8 +51,7 @@ int main() {
       "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
       "}\0";
 
-  unsigned int vertex_shader;
-  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
+  unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
   glShaderSource(vertex_shader, 1, &vertex_shader_src, NULL);
   glCompileShader(vertex_shader);
 
@@ -51,7 +61,7 @@ int main() {
 
   if (!success) {
     glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
-    printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED %s\n", info_log);
+    printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", info_log);
   }
 
   const char *fragment_shader_src =
@@ -62,26 +72,48 @@ int main() {
       " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
       "}\0";
 
-  unsigned int fragment_shader;
-  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
+  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 %s\n", 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);
+
+  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);
   }
 
-  unsigned int shader_program;
-  shader_program = glCreateProgram();
+  glDeleteShader(vertex_shader);
+  glDeleteShader(fragment_shader);
+
+  unsigned int VAO;
+  glGenVertexArrays(1, &VAO);
+  glBindVertexArray(VAO);
+
+  glBindBuffer(GL_ARRAY_BUFFER, VBO);
+  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
+  glEnableVertexAttribArray(0);
 
+  // Main loop
   while (!glfwWindowShouldClose(window)) {
-    glClearColor(0.1f, 0.1f, 0.25f, 0.0f);
+    glClearColor(0.1f, 0.1f, 0.25f, 1.0f);
     glClear(GL_COLOR_BUFFER_BIT);
 
-    glfwSwapBuffers(window);
+    glUseProgram(shader_program);
+    glBindVertexArray(VAO);
+    glDrawArrays(GL_TRIANGLES, 0, 3);
 
+    glfwSwapBuffers(window);
     glfwPollEvents();
   }