Skip to content

Commit

Permalink
feat(samples): port gl.quad core sample to web
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Jun 13, 2024
1 parent 6e7ab8b commit 9eacfdc
Showing 1 changed file with 137 additions and 114 deletions.
251 changes: 137 additions & 114 deletions core/samples/gl/quad/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <cstdio>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include <glm/gtc/matrix_transform.hpp>

#include <cubos/core/gl/render_device.hpp>
Expand All @@ -8,13 +12,74 @@

using namespace cubos::core;

int main()
struct State
{
io::Window window;
gl::ShaderPipeline shaderPipeline;
gl::VertexArray vertexArray;
gl::IndexBuffer indexBuffer;
gl::ShaderBindingPoint textureBP, constantBufferBP;
gl::ConstantBuffer constantBuffer;
gl::Sampler sampler;
gl::Texture2D texture;
gl::RasterState rasterState;

float t;
};

void update(void* data)
{
auto window = io::openWindow();
auto& renderDevice = window->renderDevice();
auto* state = static_cast<State*>(data);

auto sz = state->window->framebufferSize();
auto& renderDevice = state->window->renderDevice();

renderDevice.setViewport(0, 0, static_cast<int>(sz.x), static_cast<int>(sz.y));
renderDevice.clearColor(0.894F, 0.592F, 0.141F, 0.0F);

renderDevice.setShaderPipeline(state->shaderPipeline);
renderDevice.setVertexArray(state->vertexArray);
renderDevice.setIndexBuffer(state->indexBuffer);

state->textureBP->bind(state->texture);
state->textureBP->bind(state->sampler);
state->constantBufferBP->bind(state->constantBuffer);

auto mvp = glm::perspective(glm::radians(70.0F), float(sz.x) / float(sz.y), 0.1F, 1000.0F) *
glm::lookAt(glm::vec3{0.0F, 0.0F, -5.0F}, glm::vec3{0.0F, 0.0F, 0.0F}, glm::vec3{0.0F, 1.0F, 0.0F}) *
glm::translate(glm::mat4(1.0F), glm::vec3{-2.0F, 0.0F, 0.0F}) *
glm::rotate(glm::mat4(1.0F), state->t, glm::vec3{0.0F, 1.0F, 0.0F});
state->constantBuffer->fill(&mvp, sizeof(mvp));

renderDevice.setRasterState(nullptr);
renderDevice.drawTrianglesIndexed(0, 6);

mvp = glm::perspective(glm::radians(70.0F), float(sz.x) / float(sz.y), 0.1F, 1000.0F) *
glm::lookAt(glm::vec3{0.0F, 0.0F, -5.0F}, glm::vec3{0.0F, 0.0F, 0.0F}, glm::vec3{0.0F, 1.0F, 0.0F}) *
glm::translate(glm::mat4(1.0F), glm::vec3{2.0F, 0.0F, 0.0F}) *
glm::rotate(glm::mat4(1.0F), state->t, glm::vec3{0.0F, 1.0F, 0.0F});
state->constantBuffer->fill(&mvp, sizeof(mvp));

renderDevice.setRasterState(state->rasterState);
renderDevice.drawTrianglesIndexed(0, 6);

state->window->swapBuffers();
while (state->window->pollEvent().has_value())
{
auto vs = renderDevice.createShaderStage(gl::Stage::Vertex, R"(
; // Do nothing with events.
}

state->t += 0.01F;
}

int main()
{
auto state = State{};

state.window = io::openWindow();
auto& renderDevice = state.window->renderDevice();

auto vs = renderDevice.createShaderStage(gl::Stage::Vertex, R"(
in vec2 position;
in vec2 uv;
Expand All @@ -32,7 +97,7 @@ int main()
}
)");

auto ps = renderDevice.createShaderStage(gl::Stage::Pixel, R"(
auto ps = renderDevice.createShaderStage(gl::Stage::Pixel, R"(
in vec2 fragUV;
out vec4 color;
Expand All @@ -44,115 +109,73 @@ int main()
}
)");

auto pp = renderDevice.createShaderPipeline(vs, ps);

float verts[] = {
-0.5F, -0.5F, 0.0F, 0.0F, -0.5F, +0.5F, 0.0F, 1.0F, +0.5F, +0.5F, 1.0F, 1.0F, +0.5F, -0.5F, 1.0F, 0.0F,
};

auto vb = renderDevice.createVertexBuffer(sizeof(verts), verts, gl::Usage::Static);

unsigned int indices[] = {
0, 1, 2, 2, 3, 0,
};

auto ib = renderDevice.createIndexBuffer(sizeof(indices), indices, gl::IndexFormat::UInt, gl::Usage::Static);

auto cb = renderDevice.createConstantBuffer(sizeof(glm::mat4), nullptr, gl::Usage::Dynamic);
auto* cbBP = pp->getBindingPoint("MVP");

float textureData[] = {
0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F,
};

gl::Texture2DDesc textureDesc;
textureDesc.data[0] = textureData;
textureDesc.format = gl::TextureFormat::RGB32Float;
textureDesc.width = 2;
textureDesc.height = 2;
auto texture = renderDevice.createTexture2D(textureDesc);
auto* textureBP = pp->getBindingPoint("colorTexture");

gl::SamplerDesc samplerDesc;
samplerDesc.minFilter = gl::TextureFilter::Linear;
samplerDesc.magFilter = gl::TextureFilter::Linear;
samplerDesc.addressU = gl::AddressMode::Mirror;
samplerDesc.addressV = gl::AddressMode::Mirror;
auto sampler = renderDevice.createSampler(samplerDesc);

gl::VertexArrayDesc vaDesc;
vaDesc.elementCount = 2;
vaDesc.elements[0].name = "position";
vaDesc.elements[0].type = gl::Type::Float;
vaDesc.elements[0].size = 2;
vaDesc.elements[0].buffer.index = 0;
vaDesc.elements[0].buffer.offset = 0;
vaDesc.elements[0].buffer.stride = 4 * sizeof(float);
vaDesc.elements[1].name = "uv";
vaDesc.elements[1].type = gl::Type::Float;
vaDesc.elements[1].size = 2;
vaDesc.elements[1].buffer.index = 0;
vaDesc.elements[1].buffer.offset = 2 * sizeof(float);
vaDesc.elements[1].buffer.stride = 4 * sizeof(float);
vaDesc.buffers[0] = vb;
vaDesc.shaderPipeline = pp;
auto va = renderDevice.createVertexArray(vaDesc);

gl::RasterStateDesc rsDesc;
auto rs = renderDevice.createRasterState(rsDesc);

float t = 0.0F;

while (!window->shouldClose())
{
auto sz = window->framebufferSize();
renderDevice.setViewport(0, 0, static_cast<int>(sz.x), static_cast<int>(sz.y));

renderDevice.clearColor(0.894F, 0.592F, 0.141F, 0.0F);

renderDevice.setShaderPipeline(pp);
renderDevice.setVertexArray(va);
renderDevice.setIndexBuffer(ib);

textureBP->bind(texture);
textureBP->bind(sampler);
cbBP->bind(cb);

{
auto& mvp = *(glm::mat4*)cb->map();
mvp = glm::perspective(glm::radians(70.0F), float(sz.x) / float(sz.y), 0.1F, 1000.0F) *
glm::lookAt(glm::vec3{0.0F, 0.0F, -5.0F}, glm::vec3{0.0F, 0.0F, 0.0F},
glm::vec3{0.0F, 1.0F, 0.0F}) *
glm::translate(glm::mat4(1.0F), glm::vec3{-2.0F, 0.0F, 0.0F}) *
glm::rotate(glm::mat4(1.0F), t, glm::vec3{0.0F, 1.0F, 0.0F});
cb->unmap();
}

renderDevice.setRasterState(nullptr);
renderDevice.drawTrianglesIndexed(0, 6);

{
auto& mvp = *(glm::mat4*)cb->map();
mvp = glm::perspective(glm::radians(70.0F), float(sz.x) / float(sz.y), 0.1F, 1000.0F) *
glm::lookAt(glm::vec3{0.0F, 0.0F, -5.0F}, glm::vec3{0.0F, 0.0F, 0.0F},
glm::vec3{0.0F, 1.0F, 0.0F}) *
glm::translate(glm::mat4(1.0F), glm::vec3{2.0F, 0.0F, 0.0F}) *
glm::rotate(glm::mat4(1.0F), t, glm::vec3{0.0F, 1.0F, 0.0F});
cb->unmap();
}

renderDevice.setRasterState(rs);
renderDevice.drawTrianglesIndexed(0, 6);

window->swapBuffers();
while (window->pollEvent().has_value())
{
; // Do nothing with events.
}

t += 0.01F;
}
state.shaderPipeline = renderDevice.createShaderPipeline(vs, ps);

float verts[] = {
-0.5F, -0.5F, 0.0F, 0.0F, -0.5F, +0.5F, 0.0F, 1.0F, +0.5F, +0.5F, 1.0F, 1.0F, +0.5F, -0.5F, 1.0F, 0.0F,
};

auto vb = renderDevice.createVertexBuffer(sizeof(verts), verts, gl::Usage::Static);

unsigned int indices[] = {
0, 1, 2, 2, 3, 0,
};

state.indexBuffer =
renderDevice.createIndexBuffer(sizeof(indices), indices, gl::IndexFormat::UInt, gl::Usage::Static);

state.constantBuffer = renderDevice.createConstantBuffer(sizeof(glm::mat4), nullptr, gl::Usage::Dynamic);
state.constantBufferBP = state.shaderPipeline->getBindingPoint("MVP");

float textureData[] = {
0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F,
};

gl::Texture2DDesc textureDesc;
textureDesc.data[0] = textureData;
textureDesc.format = gl::TextureFormat::RGB32Float;
textureDesc.width = 2;
textureDesc.height = 2;
state.texture = renderDevice.createTexture2D(textureDesc);
state.textureBP = state.shaderPipeline->getBindingPoint("colorTexture");

gl::SamplerDesc samplerDesc;
samplerDesc.minFilter = gl::TextureFilter::Linear;
samplerDesc.magFilter = gl::TextureFilter::Linear;
samplerDesc.addressU = gl::AddressMode::Mirror;
samplerDesc.addressV = gl::AddressMode::Mirror;
state.sampler = renderDevice.createSampler(samplerDesc);

gl::VertexArrayDesc vaDesc;
vaDesc.elementCount = 2;
vaDesc.elements[0].name = "position";
vaDesc.elements[0].type = gl::Type::Float;
vaDesc.elements[0].size = 2;
vaDesc.elements[0].buffer.index = 0;
vaDesc.elements[0].buffer.offset = 0;
vaDesc.elements[0].buffer.stride = 4 * sizeof(float);
vaDesc.elements[1].name = "uv";
vaDesc.elements[1].type = gl::Type::Float;
vaDesc.elements[1].size = 2;
vaDesc.elements[1].buffer.index = 0;
vaDesc.elements[1].buffer.offset = 2 * sizeof(float);
vaDesc.elements[1].buffer.stride = 4 * sizeof(float);
vaDesc.buffers[0] = vb;
vaDesc.shaderPipeline = state.shaderPipeline;
state.vertexArray = renderDevice.createVertexArray(vaDesc);

gl::RasterStateDesc rsDesc;
state.rasterState = renderDevice.createRasterState(rsDesc);

state.t = 0.0F;

#ifndef __EMSCRIPTEN__
while (!state.window->shouldClose())
{
}
#else
emscripten_set_main_loop_arg(update, &state, 0, 1);
#endif

return 0;
}
}

0 comments on commit 9eacfdc

Please sign in to comment.