Skip to content

Instantly share code, notes, and snippets.

@piusayowale
Created November 2, 2020 13:48
Show Gist options
  • Save piusayowale/eaf3b362352f7a8417877d87e92118ac to your computer and use it in GitHub Desktop.
Save piusayowale/eaf3b362352f7a8417877d87e92118ac to your computer and use it in GitHub Desktop.
Basic Filament with SDL windows
#include <iostream>
#include <filament/Engine.h>
#include <filament/SwapChain.h>
#include <filament/Renderer.h>
#include <filament/Material.h>
#include <filament/MaterialInstance.h>
#include <filament/Camera.h>
#include <filament/Skybox.h>
#include <filament/View.h>
#include <filament/Scene.h>
#include <filament/RenderableManager.h>
#include <filament/Skybox.h>
#include <utils/EntityManager.h>
#include <filament/VertexBuffer.h>
#include <filament/IndexBuffer.h>
#include <filament/Viewport.h>
#include <filament/TransformManager.h>
#include <math/scalar.h>
#include <math/mat4.h>
#include <cmath>
#include <SDL2/SDL.h>
#include "NativeWindowHelper.h"
#include "resources.h"
#define M_PI 3.14159265358979323846264338327950288
struct Vertex {
filament::math::float3 position;
uint32_t color;
};
static const Vertex TRIANGLE_VERTICES[3] = {
{{1, 0, 0.0f}, 0xffff0000u},
{{cos(M_PI * 2 / 3), sin(M_PI * 2 / 3), 0.0f}, 0xff00ff00u},
{{cos(M_PI * 4 / 3), sin(M_PI * 4 / 3), 0.0f}, 0xff0000ffu},
};
static constexpr uint16_t TRIANGLE_INDICES[3] = { 0, 1, 2 };
using namespace filament;
using namespace utils;
struct App {
VertexBuffer* vb;
IndexBuffer* ib;
Material* mat;
Camera* cam;
Entity camera;
Skybox* skybox;
Entity renderable;
};
VertexBuffer* vb;
IndexBuffer* ib;
Material* mat;
Entity renderable;
int main(int argc, char** argv)
{
std::cout << "Hello World!\n";
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
{
printf("Error: %s\n", SDL_GetError());
return -1;
}
// Setup window
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, &current);
SDL_Window* window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glcontext);
//SDL_GL_SetSwapInterval(1); // Enable vsync
filament::Engine* engine = filament::Engine::create(backend::Backend::OPENGL);
filament::SwapChain* swapchain = engine->createSwapChain(getNativeWindow(window));
// Main loop
filament::Renderer* renderer = engine->createRenderer();
Scene* scene = engine->createScene();
Skybox* sbox = Skybox::Builder().color({ 10.1, 0.125, 0.25, 1.0 }).build(*engine);
scene->setSkybox(sbox);
View* view = engine->createView();
Viewport vp{ 0, 0, 1280, 720 };
view->setViewport(vp);
App app;
app.skybox = Skybox::Builder().color({ 0.1, 0.125, 0.25, 1.0 }).build(*engine);
scene->setSkybox(app.skybox);
view->setPostProcessingEnabled(false);
static_assert(sizeof(Vertex) == 16, "Strange vertex size.");
app.vb = VertexBuffer::Builder()
.vertexCount(4)
.bufferCount(1)
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3, 0, 16)
.attribute(VertexAttribute::COLOR, 0, VertexBuffer::AttributeType::UBYTE4, 12, 16)
.normalized(VertexAttribute::COLOR)
.build(*engine);
app.vb->setBufferAt(*engine, 0,
VertexBuffer::BufferDescriptor(TRIANGLE_VERTICES, 3 * 16, nullptr));
app.ib = IndexBuffer::Builder()
.indexCount(3)
.bufferType(IndexBuffer::IndexType::USHORT)
.build(*engine);
app.ib->setBuffer(*engine,
IndexBuffer::BufferDescriptor(TRIANGLE_INDICES, 6, nullptr));
app.mat = Material::Builder()
.package(RESOURCES_BAKEDCOLOR_DATA, RESOURCES_BAKEDCOLOR_SIZE)
.build(*engine);
app.renderable = EntityManager::get().create();
RenderableManager::Builder(1)
.boundingBox({ { -1, -1, -1 }, { 1, 1, 1 } })
.material(0, app.mat->getDefaultInstance())
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.vb, app.ib, 0, 3)
.culling(false)
.receiveShadows(false)
.castShadows(false)
.build(*engine, app.renderable);
scene->addEntity(app.renderable);
app.camera = utils::EntityManager::get().create();
app.cam = engine->createCamera(app.camera);
view->setCamera(app.cam);
view->setScene(scene);
constexpr float ZOOM = 1.5f;
const uint32_t w = view->getViewport().width;
const uint32_t h = view->getViewport().height;
std::cout << h << " " << " " << w << "\n";
const float aspect = (float)w / h;
app.cam->setProjection(Camera::Projection::ORTHO,
-aspect * ZOOM, aspect * ZOOM,
-ZOOM, ZOOM, 0, 1);
std::cout << scene->getRenderableCount() << "\n";
bool done = false;
while (!done)
{
if (renderer->beginFrame(swapchain)) {
std::cout << "Rendering" << " " << renderer->getUserTime() << "\n";
renderer->render(view);
renderer->endFrame();
}
SDL_Event event;
if (SDL_PollEvent(&event) == 1) {
if (event.type == SDL_QUIT) {
done = true;
}
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDL_KeyCode::SDLK_UP) {
}
}
}
//SDL_GL_SwapWindow(window);
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment