hello square

This commit is contained in:
ktyl 2021-07-05 01:05:37 +01:00
parent f78c7eb549
commit 76cfb120e8
4 changed files with 59 additions and 52 deletions

View File

@ -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;
}

View File

@ -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();

View File

@ -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;

View File

@ -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