commit b53e91acfe690b85440ecb832459fe4ec21728ee
Author: John Kubach <mail@johnkubach.com>
Date: Sat, 10 Dec 2022 12:17:57 -0500
Draw Triangle
Diffstat:
7 files changed, 225 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,16 @@
+OBJS = src/main.c src/window.c src/triangle.c
+
+CC = tcc
+
+CFLAGS = -Wall -run
+
+LDLIBS=-lglfw -lGL -lSDL2 -lGLEW
+OBJ_NAME = main
+
+all : $(OBJS)
+ $(CC) $(OBJS) $(CFLAGS) $(LDLIBS) -o $(OBJ_NAME)
+
+clean:
+ rm -f *.o main
+
+.PHONY: all clean
diff --git a/src/main.c b/src/main.c
@@ -0,0 +1,37 @@
+#include "window.h"
+#include "triangle.h"
+#include "objload.h"
+#include <stdio.h>
+#include <stdbool.h>
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+#define SDL_DISABLE_IMMINTRIN_H
+#include <SDL2/SDL.h>
+
+// #ifdef __TINYC__
+// #undef main
+// #endif
+//
+
+SDL_Event event;
+
+int main()
+{
+ bool quit = false;
+
+ create_window();
+ triangle();
+
+ while (!quit) {
+ while(SDL_PollEvent(&event)) {
+ if(event.type == SDL_QUIT)
+ quit = true;
+ }
+ color_window();
+ draw_triangle();
+ draw();
+ }
+ cleanup();
+
+ return 0;
+}
diff --git a/src/main.h b/src/main.h
@@ -0,0 +1 @@
+
diff --git a/src/triangle.c b/src/triangle.c
@@ -0,0 +1,81 @@
+#include "triangle.h"
+// #include "window.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+
+const char *vertexShaderSource = "#version 330 core\n"
+ "layout (location = 0) in vec3 aPos;\n"
+ "void main()\n"
+ "{\n"
+ " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
+ "}\0";
+const char *fragmentShaderSource = "#version 330 core\n"
+ "out vec4 FragColor;\n"
+ "void main()\n"
+ "{\n"
+ " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
+ "}\n\0";
+
+void triangle()
+{
+
+ vertexShader = glCreateShader(GL_VERTEX_SHADER);
+ glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
+ glCompileShader(vertexShader);
+
+ int success;
+ glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
+
+ fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
+ glCompileShader(fragmentShader);
+
+ shaderProgram = glCreateProgram();
+ glAttachShader(shaderProgram, vertexShader);
+ glAttachShader(shaderProgram, fragmentShader);
+ glLinkProgram(shaderProgram);
+ glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
+ if (!success) {
+ exit(fprintf(stderr, "ERROR::SHADER::PROGRAM::LINKING_FAILED\n"));
+ }
+
+ glDeleteShader(vertexShader);
+ glDeleteShader(fragmentShader);
+
+ // set up vertex data (and buffer(s)) and configure vertex attributes
+ // ------------------------------------------------------------------
+ float vertices[] = {
+ -0.5f, -0.5f, 0.0f, // left
+ 0.5f, -0.5f, 0.0f, // right
+ 0.0f, 0.5f, 0.0f // top
+ };
+
+ glGenVertexArrays(1, &VAO);
+ glGenBuffers(1, &VBO);
+ // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
+ 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);
+
+ // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+ // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
+ // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
+ glBindVertexArray(0);
+}
+
+void draw_triangle()
+{
+ glUseProgram(shaderProgram);
+ glBindVertexArray(VAO);
+ glDrawArrays(GL_TRIANGLES, 0, 3);
+
+}
diff --git a/src/triangle.h b/src/triangle.h
@@ -0,0 +1,7 @@
+int compile_shaders();
+float * create_triangle();
+void triangle();
+void draw_triangle();
+
+unsigned int VBO, VAO;
+unsigned int shaderProgram, vertexShader, fragmentShader;
diff --git a/src/window.c b/src/window.c
@@ -0,0 +1,74 @@
+#include "window.h"
+#include "objload.h"
+#include "triangle.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+#define SDL_DISABLE_IMMINTRIN_H
+#include <SDL2/SDL.h>
+
+#define BG_RED 0.7
+#define BG_GREEN 0.0
+#define BG_BLUE 1.0
+#define BG_ALPHA 1.0
+
+SDL_Window *window;
+SDL_GLContext ctx;
+SDL_Renderer *renderer;
+
+
+void create_window()
+{
+ setup_sdl();
+ setup_gl();
+}
+
+void setup_sdl()
+{
+ SDL_Init(SDL_INIT_VIDEO);
+ window = SDL_CreateWindow("sedona", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+ WINDOW_W, WINDOW_H, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
+
+ if(!window) exit(fprintf(stderr, "%s\n", SDL_GetError()));
+}
+
+void setup_gl()
+{
+ SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
+ SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
+ SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
+
+ ctx = SDL_GL_CreateContext(window);
+
+ if(!ctx) exit(fprintf(stderr, "Could not create GL context\n"));
+
+ SDL_GL_SetSwapInterval(1);
+
+#ifndef __APPLE__
+ glewExperimental = GL_TRUE;
+ glewInit();
+#endif
+ glViewport(0, 0, WINDOW_W, WINDOW_H);
+}
+
+void color_window()
+{
+ glClearColor(BG_RED, BG_GREEN, BG_BLUE, BG_ALPHA);
+ glClear(GL_COLOR_BUFFER_BIT);
+}
+
+
+void draw()
+{
+ SDL_GL_SwapWindow(window);
+}
+
+void cleanup()
+{
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ glDeleteVertexArrays(1, &VAO);
+ glDeleteBuffers(1, &VBO);
+ glDeleteProgram(shaderProgram);
+}
diff --git a/src/window.h b/src/window.h
@@ -0,0 +1,9 @@
+#define WINDOW_W 1920
+#define WINDOW_H 1080
+
+void create_window();
+void setup_sdl();
+void setup_gl();
+void color_window();
+void draw();
+void cleanup();