diff --git a/projects/apis/opengl/loader/opengl_device_loader.c b/projects/apis/opengl/loader/opengl_device_loader.c index f5b9408..683f82a 100644 --- a/projects/apis/opengl/loader/opengl_device_loader.c +++ b/projects/apis/opengl/loader/opengl_device_loader.c @@ -11,6 +11,7 @@ #include "framebuffer/opengl_framebuffer.h" #include "graphics_pipeline/opengl_graphics_pipeline.h" #include "submit/opengl_submit.h" +#include "present/opengl_present.h" gnDeviceFunctions loadOpenGLDeviceFunctions() { return (gnDeviceFunctions){ @@ -53,7 +54,7 @@ gnDeviceFunctions loadOpenGLDeviceFunctions() { ._gnDestroyTexture = openglDestroyTexture, ._gnSubmit = openglSubmit, - ._gnPresent = NULL, + ._gnPresent = openglPresent, ._gnWaitForDevice = waitForOpenGLDevice }; diff --git a/projects/apis/opengl/src/device/glsl_shader.glsl b/projects/apis/opengl/src/device/glsl_shader.glsl new file mode 100644 index 0000000..d241692 --- /dev/null +++ b/projects/apis/opengl/src/device/glsl_shader.glsl @@ -0,0 +1,22 @@ +const char * vertex_shader_source = + "#version 450 core" + "" + "layout(location = 0) in vec2 inPosition;" + "layout(location = 1) in vec2 inTexcoord;" + "" + "out vec2 texcoord;" + "" + "void main() {" + " gl_Position = vec4(inPosition, 0.0, 1.0);" + " texcoord = inTexcoord;" + "}" ; +const char * fragment_shader_source = + "#version 450 core" + "" + "out vec4 FragColor;" + "layout(binding = 0) uniform sampler2D tex;" + "in vec2 texcoord;" + "" + "void main() {" + " FragColor = texture(tex, texcoord);" + "}" ; diff --git a/projects/apis/opengl/src/device/opengl_output_device.c b/projects/apis/opengl/src/device/opengl_output_device.c index a344e2d..93e94b6 100644 --- a/projects/apis/opengl/src/device/opengl_output_device.c +++ b/projects/apis/opengl/src/device/opengl_output_device.c @@ -1,5 +1,43 @@ +#include "glad/glad.h" #include "opengl_output_device.h" +#include "glsl_shader.glsl" +#include "stdlib.h" -gnReturnCode createOpenGLOutputDevice(gnInstanceHandle instance, gnOutputDeviceHandle device, gnOutputDeviceInfo deviceInfo) { return GN_SUCCESS; } +gnReturnCode createOpenGLOutputDevice(gnInstanceHandle instance, gnOutputDeviceHandle device, gnOutputDeviceInfo deviceInfo) { + device->outputDevice = malloc(sizeof(gnPlatformOutputDevice)); + float vertices[] = { + -1.0f, -1.0f, 0.0f, 1.0f, + 1.0f, -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 0.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 0.0f, + }; + glCreateBuffers(1, &device->outputDevice->buffer); + glBindBuffer(GL_ARRAY_BUFFER, device->outputDevice->buffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)(sizeof(float) * 2)); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + unsigned int vertexShader; + vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertex_shader_source, NULL); + glCompileShader(vertexShader); + + unsigned int fragmentShader; + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragment_shader_source, NULL); + glCompileShader(fragmentShader); + + device->outputDevice->shaderProgram = glCreateProgram(); + glAttachShader(device->outputDevice->shaderProgram, vertexShader); + glAttachShader(device->outputDevice->shaderProgram, fragmentShader); + glLinkProgram(device->outputDevice->shaderProgram); + + return GN_SUCCESS; +} void waitForOpenGLDevice(const gnOutputDeviceHandle device) {} void destroyOpenGLOutputDevice(gnOutputDeviceHandle device) {} diff --git a/projects/apis/opengl/src/device/opengl_output_device.h b/projects/apis/opengl/src/device/opengl_output_device.h index 3651dcb..e4d3d1b 100644 --- a/projects/apis/opengl/src/device/opengl_output_device.h +++ b/projects/apis/opengl/src/device/opengl_output_device.h @@ -1,7 +1,9 @@ #pragma once #include -typedef struct gnPlatformOutputDevice_t {} gnPlatformOutputDevice; +typedef struct gnPlatformOutputDevice_t { + unsigned int buffer, shaderProgram; +} gnPlatformOutputDevice; gnReturnCode createOpenGLOutputDevice(gnInstanceHandle instance, gnOutputDeviceHandle device, gnOutputDeviceInfo deviceInfo); void waitForOpenGLDevice(const gnOutputDeviceHandle device);