Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created May 30, 2021 21:09
Show Gist options
  • Save untodesu/cb5a4e5bef3d2d0568fbc2c2f036cfaa to your computer and use it in GitHub Desktop.
Save untodesu/cb5a4e5bef3d2d0568fbc2c2f036cfaa to your computer and use it in GitHub Desktop.
/*
* main.cc
* Created: 2021-05-31, 01:14:45.
* Copyright (C) 2021, Kirill GPRB.
*/
#include <GLFW/glfw3.h>
#include <rena/renderdevice.hh>
#include <stdio.h>
static GLFWwindow *g_window = NULL;
static void onGlfwError(int code, const char *message)
{
printf("glfw error %d: %s\n", code, message);
}
static void onLog(const char *message)
{
printf("%s\n", message);
}
static void onSwap(int interval)
{
static int s_interval = -1;
if(s_interval != interval) {
glfwSwapInterval(interval);
s_interval = interval;
}
glfwSwapBuffers(g_window);
}
struct Vertex2D {
float position[2];
float texcoord[2];
};
int main()
{
glfwSetErrorCallback(onGlfwError);
if(!glfwInit()) {
printf("glfwInit failed\n");
return 1;
}
if(!IRenderDevice::isAPISupported(GraphicsAPI::OPENGL_4_6)) {
printf("GL46 is not supported for some reason...\n");
glfwTerminate();
return 1;
}
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
g_window = glfwCreateWindow(800, 600, "rena test", nullptr, nullptr);
if(!g_window) {
printf("glfwCreateWindow failed\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(g_window);
IRenderDevice *device = IRenderDevice::create(GraphicsAPI::OPENGL_4_6, glfwGetProcAddress, onSwap);
if(!device) {
printf("IRenderDevice::create failed\n");
glfwTerminate();
return 1;
}
device->enableFeature(DeviceFeature::DEBUG_LOGGING, true);
device->enableFeature(DeviceFeature::SHADER_BYTECODE, false);
device->setLogFunc(onLog);
VertexAttribute attributes[2];
// position
attributes[0].offset = offsetof(Vertex2D, position);
attributes[0].type = FieldType::FLOAT;
attributes[0].count = 2;
// texcoord
attributes[1].offset = offsetof(Vertex2D, texcoord);
attributes[1].type = FieldType::FLOAT;
attributes[1].count = 2;
device->setVertexFormat(sizeof(Vertex2D), 2, attributes);
const Vertex2D quad_v[4] = {
{ { -0.5f, -0.5f }, { 0.0f, 1.0f } },
{ { -0.5f, 0.5f }, { 0.0f, 0.0f } },
{ { 0.5f, 0.5f }, { 1.0f, 0.0f } },
{ { 0.5f, -0.5f }, { 1.0f, 1.0f } }
};
const unsigned int quad_i[6] = {
0, 1, 2,
2, 3, 0
};
const char *vs_src =
"#version 450\n"
"out gl_PerVertex { vec4 gl_Position; };\n"
"layout(location = 0) in vec2 position;\n"
"layout(location = 1) in vec2 texcoord;\n"
"layout(location = 0) out vec2 o_texcoord;\n"
"void main() {\n"
"gl_Position = vec4(position, 0.0, 1.0);\n"
"o_texcoord = texcoord; }\n";
const char *ps_src =
"#version 450\n"
"layout(location = 0) in vec2 texcoord;\n"
"layout(location = 0) out vec4 target;\n"
"layout(binding = 0) uniform sampler2D color;\n"
"void main() {\n"
"target = texture(color, texcoord); }\n";
IShader *vs = nullptr;
if(!device->createVertexShader(&vs, 0, vs_src) || !vs) {
printf("vs failed\n");
IRenderDevice::destroy(device);
glfwTerminate();
return 1;
}
IShader *ps = nullptr;
if(!device->createPixelShader(&ps, 0, ps_src) || !ps) {
printf("ps failed\n");
IRenderDevice::destroy(device);
glfwTerminate();
return 1;
}
IFramebuffer *fbo;
device->createFramebuffer(&fbo);
ITexture *color;
device->createTexture(&color, 800, 600, ImageFormat::RGBA_8U);
device->setFramebufferColor(fbo, 0, color);
unsigned char noise[12288];
for(int i = 0; i < 12288; i++)
noise[i] = rand();
ITexture *noisetex;
device->createTexture(&noisetex, 64, 64, ImageFormat::RGBA_16F);
device->writeTexture(noisetex, 0, 0, 64, 64, ImageFormat::RGB_8U, noise);
while(!glfwWindowShouldClose(g_window)) {
device->beginFrame();
device->setVertexShader(vs);
device->setPixelShader(ps);
device->setVertexData(sizeof(quad_v), quad_v);
device->setIndexData(sizeof(quad_i), quad_i);
device->setTexture(noisetex, 0);
device->setFramebuffer(fbo);
device->clearColor(0.0f, 0.0f, 0.1f);
device->clear(true, false, false);
device->drawIndexed(6, 1);
device->setTexture(color, 0);
device->setFramebuffer(nullptr);
device->clearColor(0.0f, 0.1f, 0.0f);
device->clear(true, false, false);
device->drawIndexed(6, 1);
device->endFrame();
device->swapBuffers(1);
glfwPollEvents();
}
device->destroyShader(ps);
device->destroyShader(vs);
IRenderDevice::destroy(device);
glfwDestroyWindow(g_window);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment