From 76cfb120e8b61bb7690ac26b46690d80d648b096 Mon Sep 17 00:00:00 2001 From: ktyl Date: Mon, 5 Jul 2021 01:05:37 +0100 Subject: [PATCH] hello square --- src/gfx.c | 42 +++++++++++++++++++++++++++-------------- src/gfx.h | 13 +++++-------- src/main.c | 55 +++++++++++++++++++++++++----------------------------- todo.md | 1 + 4 files changed, 59 insertions(+), 52 deletions(-) diff --git a/src/gfx.c b/src/gfx.c index fd498ca..eaad344 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -1,6 +1,17 @@ #include "gfx.h" -SDL_Window* createWindow() +const char* vertShaderPath = "res/shader/shader.vert"; +const char* fragShaderPath = "res/shader/shader.frag"; + +SDL_Window* sdlWindow; +SDL_GLContext* sdlContext; + +SDL_Window* getWindow() { return sdlWindow; } +SDL_GLContext* getContext() { return sdlContext; } + +GLuint compileShader(const char* path, GLenum type); + +void gfxInit() { // load sdl modules if (SDL_Init(SDL_INIT_VIDEO) != 0) @@ -14,7 +25,7 @@ SDL_Window* createWindow() SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - SDL_Window* window = SDL_CreateWindow( + sdlWindow = SDL_CreateWindow( "oglc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -22,21 +33,26 @@ SDL_Window* createWindow() 600, SDL_WINDOW_OPENGL); - return window; -} - -struct GraphicsContext createContext() -{ - SDL_Window* window = createWindow(); - - SDL_GLContext context = SDL_GL_CreateContext(window); + sdlContext = SDL_GL_CreateContext(sdlWindow); glewExperimental = GL_TRUE; glewInit(); +} - struct GraphicsContext ctx = {window, context}; +unsigned int compileShaderProgram() +{ + GLuint vs = compileShader(vertShaderPath, GL_VERTEX_SHADER); + GLuint fs = compileShader(fragShaderPath, GL_FRAGMENT_SHADER); - return ctx; + unsigned int shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vs); + glAttachShader(shaderProgram, fs); + glLinkProgram(shaderProgram); + + glDeleteShader(vs); + glDeleteShader(fs); + + return shaderProgram; } GLuint compileShader(const char* path, GLenum type) @@ -74,7 +90,5 @@ GLuint compileShader(const char* path, GLenum type) exit(1); } - printf(":: . compiled shader %s! :D\n", path); - return shader; } diff --git a/src/gfx.h b/src/gfx.h index e7a5fc5..0f08199 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -8,13 +8,10 @@ #include "io.h" -struct GraphicsContext -{ - SDL_Window* window; - SDL_GLContext* context; -}; - -SDL_Window* createWindow(); -struct GraphicsContext createContext(); +void gfxInit(); +SDL_Window* getWindow(); +SDL_GLContext* getContext(); GLuint compileShader(const char* path, GLenum type); +unsigned int compileShaderProgram(); + diff --git a/src/main.c b/src/main.c index 10e14ba..5ebdc77 100644 --- a/src/main.c +++ b/src/main.c @@ -2,48 +2,39 @@ #include "gfx.h" -// data ! -const char* vertShaderPath = "res/shader/shader.vert"; -const char* fragShaderPath = "res/shader/shader.frag"; - float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f + 0.5f, 0.5f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f // top left +}; + +unsigned int indices[] = { + 0, 1, 3, + 1, 2, 3 }; // forward declarations int checkQuit(); - int main() { - // initialise opengl graphics context - const struct GraphicsContext ctx = createContext(); + gfxInit(); + SDL_Window* window = getWindow(); - // compile shaders - printf(":: compiling shaders\n"); - GLuint vertShader = compileShader(vertShaderPath, GL_VERTEX_SHADER); - GLuint fragShader = compileShader(fragShaderPath, GL_FRAGMENT_SHADER); - - printf(":: linking shader program\n"); - unsigned int shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertShader); - glAttachShader(shaderProgram, fragShader); - glLinkProgram(shaderProgram); - - glDeleteShader(vertShader); - glDeleteShader(fragShader); + unsigned int shaderProgram = compileShaderProgram(); // TODO: check program linking success - // set up vertex array object + // vertex array object unsigned int VAO; glGenVertexArrays(1, &VAO); - - // set up vertex buffer object + // vertex buffer object unsigned int VBO; glGenBuffers(1, &VBO); + // element buffer object + unsigned int EBO; + glGenBuffers(1, &EBO); glBindVertexArray(VAO); @@ -51,18 +42,22 @@ int main() glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind it to the GL_ARRAY_BUFFER target glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy user-defined data into currently bound buffer + // copy indices into a buffer for opengl + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + // set vertex attributes glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); // TODO: wtf glEnableVertexAttribArray(0); - // wait for exit + // render loop while (!checkQuit()) { glUseProgram(shaderProgram); - glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); - SDL_GL_SwapWindow(ctx.window); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + + SDL_GL_SwapWindow(window); } return 0; diff --git a/todo.md b/todo.md index 1dc2960..7449f52 100644 --- a/todo.md +++ b/todo.md @@ -1,3 +1,4 @@ * [x] basic opengl initialisation * [ ] render a texture to a full-screen quad * [ ] output image to a file +* [ ] render image with compute shader