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" #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 // load sdl modules
if (SDL_Init(SDL_INIT_VIDEO) != 0) 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_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_Window* window = SDL_CreateWindow( sdlWindow = SDL_CreateWindow(
"oglc", "oglc",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
@ -22,21 +33,26 @@ SDL_Window* createWindow()
600, 600,
SDL_WINDOW_OPENGL); SDL_WINDOW_OPENGL);
return window; sdlContext = SDL_GL_CreateContext(sdlWindow);
}
struct GraphicsContext createContext()
{
SDL_Window* window = createWindow();
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
glewInit(); 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) GLuint compileShader(const char* path, GLenum type)
@ -74,7 +90,5 @@ GLuint compileShader(const char* path, GLenum type)
exit(1); exit(1);
} }
printf(":: . compiled shader %s! :D\n", path);
return shader; return shader;
} }

View File

@ -8,13 +8,10 @@
#include "io.h" #include "io.h"
struct GraphicsContext void gfxInit();
{ SDL_Window* getWindow();
SDL_Window* window; SDL_GLContext* getContext();
SDL_GLContext* context;
};
SDL_Window* createWindow();
struct GraphicsContext createContext();
GLuint compileShader(const char* path, GLenum type); GLuint compileShader(const char* path, GLenum type);
unsigned int compileShaderProgram();

View File

@ -2,48 +2,39 @@
#include "gfx.h" #include "gfx.h"
// data !
const char* vertShaderPath = "res/shader/shader.vert";
const char* fragShaderPath = "res/shader/shader.frag";
float vertices[] = { float vertices[] = {
-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, // bottom right
0.0f, 0.5f, 0.0f -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 // forward declarations
int checkQuit(); int checkQuit();
int main() int main()
{ {
// initialise opengl graphics context gfxInit();
const struct GraphicsContext ctx = createContext(); SDL_Window* window = getWindow();
// compile shaders unsigned int shaderProgram = compileShaderProgram();
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);
// TODO: check program linking success // TODO: check program linking success
// set up vertex array object // vertex array object
unsigned int VAO; unsigned int VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
// vertex buffer object
// set up vertex buffer object
unsigned int VBO; unsigned int VBO;
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
// element buffer object
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
@ -51,18 +42,22 @@ int main()
glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind it to the GL_ARRAY_BUFFER target 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 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 // set vertex attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); // TODO: wtf glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); // TODO: wtf
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// wait for exit // render loop
while (!checkQuit()) while (!checkQuit())
{ {
glUseProgram(shaderProgram); 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; return 0;

View File

@ -1,3 +1,4 @@
* [x] basic opengl initialisation * [x] basic opengl initialisation
* [ ] render a texture to a full-screen quad * [ ] render a texture to a full-screen quad
* [ ] output image to a file * [ ] output image to a file
* [ ] render image with compute shader