Commit 6c1f7225 authored by Johannes Braun's avatar Johannes Braun
Browse files

Update executables and external libraries

parent ee726d00
/*
* This example should give you an insight on the abstractions of OpenGL objects.
*/
#include <GLFW/glfw3.h>
#include <dino/input/Window.h>
#include <dino/opengl/Buffer.h>
#include <dino/opengl/Shader.h>
#include <dino/opengl/Texture.h>
#include <dino/opengl/VertexArray.h>
#include <glm/ext.hpp>
#include <iostream>
#include <memory>
#include <random>
// Some pre-defining of structures and glsl code needed later.
using Idx = uint8_t;
struct Vtx
{
glm::vec3 pos {};
glm::u8vec4 col {0, 0, 0, 255};
glm::vec2 uv {};
};
constexpr const char* glslHeader = R"(
#version 430 core
)";
constexpr const char* vertexShader = R"(
layout(location=0) in vec3 pos;
layout(location=1) in vec3 col;
layout(location=2) in vec2 uv;
layout(location=0) out vec3 out_col;
layout(location=1) out vec2 out_uv;
layout(binding=0) uniform Mat { mat4 mvp; };
void main() {
gl_Position = mvp * vec4(pos, 1);
out_col = col;
out_uv = uv;
})";
constexpr const char* fragmentShader = R"(
layout(location=0) in vec3 col;
layout(location=1) in vec2 uv;
layout(location=0) out vec4 out_col;
layout(binding=0) uniform sampler2D tex;
void main() {
out_col = vec4(col, 1) * texture(tex, uv);
})";
std::unique_ptr<dino::Texture> msaaColor;
GLuint fbo;
int main()
{
// Create your window. It will use an OpenGL 4.5 core context, but you can override this by
// passing dino::Window::Hints with GLFW window hint-value pairs as a last parameter to
// make_shared. ImGui will also be automatically initialized.
dino::Window::Hints hints = {{GLFW_OPENGL_DEBUG_CONTEXT, true}, {GLFW_SAMPLES, 8}};
const auto window = std::make_shared<dino::Window>(1280, 720, "Example 01: OpenGL", hints);
// Create a program with simple shaders to draw something to the screen.
// Instead of passing strings, you can also pass the result of dino::ShaderFile::load()
// inbetween.
// Example:
// p.attachNew(GL_VERTEX_SHADER, {glslHeader, dino::ShaderFile::load("myFile.glsl")});
dino::Program program;
program.attachNew(GL_VERTEX_SHADER, {glslHeader, vertexShader});
program.attachNew(GL_FRAGMENT_SHADER, {glslHeader, fragmentShader});
// In the vertex shader, we have a model-view-projection matrix in a uniform buffer. So let's
// create one.
// Make the buffer storage dynamic so we can re-assign it when rendering, i.e. to rotate the
// object.
const glm::mat4 mv = glm::perspectiveFov(glm::radians(70.f), 1280.f, 720.f, 0.1f, 100.f) *
glm::lookAt(glm::vec3(0, 0, 4), glm::vec3(0), glm::vec3(0, 1, 0));
dino::Buffer<glm::mat4> mvpBuffer(mv, GL_DYNAMIC_STORAGE_BIT);
// We can now define a quad we want to render.
const std::vector<Vtx> vertices = {
{glm::vec3(-1, -1, 0), glm::u8vec4(128, 18, 44, 255), glm::vec2(0, 0)},
{glm::vec3(1, -1, 0), glm::u8vec4(41, 78, 200, 255), glm::vec2(1, 0)},
{glm::vec3(1, 1, 0), glm::u8vec4(80, 122, 0, 255), glm::vec2(1, 1)},
{glm::vec3(-1, 1, 0), glm::u8vec4(0, 254, 78, 255), glm::vec2(0, 1)}};
dino::Buffer<Vtx> vbo(vertices);
dino::Buffer<Idx> ibo({0, 1, 2, 0, 2, 3});
// Create and fill a random texture to map onto the quad.
std::mt19937 gen;
std::uniform_real_distribution<float> dist(0.f, 1.f);
std::vector<float> rnds(128 * 128 * 4);
for(int x = 0; x < 128; ++x)
for(int y = 0; y < 128; ++y)
for(int c = 0; c < 4; ++c)
rnds[x * 128 * 4 + y * 4 + c] = glm::perlin(glm::vec3 {x, y, c} / 25.f);
dino::Texture texture(GL_TEXTURE_2D, GL_RGBA8, glm::ivec2(128, 128));
texture.assign2D(GL_RGBA, GL_FLOAT, rnds.data());
texture.set({{GL_TEXTURE_MIN_FILTER, GL_LINEAR}, {GL_TEXTURE_MAG_FILTER, GL_NEAREST}});
texture.generateMipmaps();
// Create a vertex array and assign our buffers to it.
// As a vertex buffer binding, use the binding 0 for both attributes.
dino::VertexArray vao;
// ... Positions: attr 0; 3-component non-normalized float vector ...
vao.format(0, 3, GL_FLOAT, false, offsetof(Vtx, pos));
// ... Color: attr 1; 4-component normalized ubyte vector ...
vao.format(1, 4, GL_UNSIGNED_BYTE, true, offsetof(Vtx, col));
// ... UVs: attr 2; 2-component non-normalized float vector ...
vao.format(2, 2, GL_FLOAT, false, offsetof(Vtx, uv));
// ... and bind buffers.
vao.binding(0, 0);
vao.binding(1, 0);
vao.binding(2, 0);
vao.setVertexBuffer(vbo, 0);
vao.setElementBuffer(ibo);
// For rendering, we can use a multisampled framebuffer with the same sample count as the window
// surface.
msaaColor = std::make_unique<dino::Texture>(
GL_TEXTURE_2D_MULTISAMPLE, GL_RGBA16F, glm::ivec2(1280, 720), dino::Samples::X8);
glfwSetFramebufferSizeCallback(*window, [](GLFWwindow*, int w, int h) {
glViewport(0, 0, w, h);
msaaColor->resize(
GL_TEXTURE_2D_MULTISAMPLE, GL_RGBA16F, glm::ivec2(w, h), dino::Samples::X8);
if(glNamedFramebufferTexture)
glNamedFramebufferTexture(fbo, GL_COLOR_ATTACHMENT0, msaaColor->id(), 0);
else
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, msaaColor->id(), 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
});
// TODO: Need for FBOs & Renderbuffers?
// Just use a renderbuffer for depth/stencil
GLuint depthRenderbuffer;
if(glCreateRenderbuffers)
{
glCreateRenderbuffers(1, &depthRenderbuffer);
glNamedRenderbufferStorageMultisample(depthRenderbuffer, 8, GL_DEPTH24_STENCIL8, 1280, 720);
}
else
{
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH24_STENCIL8, 1280, 720);
}
// Create an FBO and attach texture and renderbuffers. Set color attachment 0 as render target.
if(glCreateFramebuffers)
{
glCreateFramebuffers(1, &fbo);
glNamedFramebufferTexture(fbo, GL_COLOR_ATTACHMENT0, msaaColor->id(), 0);
glNamedFramebufferRenderbuffer(
fbo, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
glNamedFramebufferDrawBuffer(fbo, GL_COLOR_ATTACHMENT0);
}
else
{
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, msaaColor->id(), 0);
glFramebufferRenderbuffer(
GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
glClearColor(0.7f, 0.8f, 1.f, 1.f);
// Simple window loop. update() returns false as soon as the window should be closed.
// If the window is minimized (or otherwise has a framebuffer size of 0x0), update()
// blocks until resized back to a valid size. (glfwPollEvents() will still be called)
while(window->update())
{
// For the sake of completeness, add a simple ImGui window.
static float rotationSpeed = 20.f;
ImGui::Begin("Example 01: OpenGL");
ImGui::Text("This is a simple ImGui window.");
ImGui::DragFloat("Rotation speed", &rotationSpeed, 0.1f, -360.f, 360.f);
if(ImGui::Button("Close"))
window->close();
ImGui::End();
// Let's rotate the quad by modifying the mvp buffer data.
mvpBuffer.assign(
mv * glm::rotate(glm::radians(rotationSpeed * static_cast<float>(glfwGetTime())),
glm::vec3(0, 0, 1)));
// Bind all needed objects and render the quad.
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// Clear the framebuffer to blue'ish.
glClear(GL_COLOR_BUFFER_BIT);
program.use();
vao.bind();
texture.bind(0);
mvpBuffer.bind(GL_UNIFORM_BUFFER, 0);
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(ibo.size()), GL_UNSIGNED_BYTE, nullptr);
int w, h;
glfwGetFramebufferSize(*window, &w, &h);
// Copy fbo contents to screen.
if(glBlitNamedFramebuffer)
{
glBlitNamedFramebuffer(fbo, 0, 0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
else
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
}
return 0;
}
/*
* This executable shows the creation of a distance field, as well as saving it to a json file
* afterwards.
*/
#include <dino/modifiers/Simple.h>
#include <dino/operators/Blend.h>
#include <dino/operators/Simple.h>
#include <dino/primitives/Simple.h>
#include <fstream>
#include <glm/ext.hpp>
#include <iostream>
#include <memory>
#include <optional>
#include <dino/parameters/Arithmetic.h>
#include <dino/parameters/Constants.h>
#include <dino/parameters/Trigonometric.h>
#include <dino/parameters/Vector.h>
#include <dino/parameters/Matrix.h>
namespace dino::ext
{
} // namespace dino::ext
int main()
{
const auto f0 = dino::Param::create("f0", glm::vec3(1, 1, 1));
const auto f1 = dino::Param::create("f1", glm::vec3(2, 2, 2));
const auto f2 = dino::Param::create("f2", glm::vec3(3, 3, 3));
const auto f4 = dino::Param::create("f4", glm::vec3(1.2, 0.2, 0.7));
const auto fi = dino::Param::create("fi", 8);
const auto ff1 = dino::Param::create("f1", 8.f);
const auto ff2 = dino::Param::create("f2", 2.f);
const auto ps = dino::Param::create("ps", glm::vec4(0, 0, 0, 1));
const auto mt = dino::Param::create("mat", glm::translate(glm::vec3(1, 2, 8)));
const auto ps1 = dino::Param::create("ps1", glm::vec4(0.2f, 1.3f, 5.3f, 1));
const auto operation = ((f0 + f1) / f2) % f4;
// Create a cube, a sphere and a torus, which will later be blended together.
const auto cube = dino::PrBox::create(glm::vec3(2, 4, 3));
const auto sphere = dino::PrSphere::create();
const auto torus = dino::PrTorus::create(1.f, 0.2f);
// We don't want both objects to stay in the center, so let's move the sphere a bit to the left.
const auto transform = dino::ModTransform::create(glm::translate(glm::vec3(-2.f, 0.f, 0.f)));
transform->matrix.replace(operation);
sphere->setModifier(transform);
// The object should use a global object transformation transforming the whole thing.
// It should be assigned to the lowest levels in the object tree (cube, torus and
// sphere-transform).
const auto objectTransform =
dino::ModTransform::create(glm::translate(glm::vec3(-1.f, 2.f, 1.f)));
transform->setModifier(objectTransform);
cube->extents.replace(operation);
cube->setModifier(objectTransform);
torus->setModifier(objectTransform);
// Let's also make a shared parameter which links the sphere "radius" and the torus
// "distanceToTube" to one single parameter, which is easier to control.
const auto radius = dino::Param::create("newRadius", 0.8f);
sphere->radius.replace(radius);
torus->circleRadius.replace(radius);
// Now add two polynomial blending operations, one for cube+sphere and one for the final
// blending of the result of that with the torus.
const auto blendCubeSphere = dino::OpBlendPolynomial::create(0.5f);
blendCubeSphere->apply(cube, sphere);
const auto blendFinal = dino::OpBlendPolynomial::create(0.2f);
blendFinal->apply(blendCubeSphere, torus);
// For now, let's just save the object to a json file.
// You can also set a breakpoint in your debugger to first view the contents (code, data, etc.)
// of "object".
const nlohmann::json json1 = blendFinal->serialize();
{
std::ofstream output("example_00_fields.json");
output << std::setw(4) << json1;
}
const auto bf = dino::Function::deserialize(json1);
const nlohmann::json json2 = bf->serialize();
{
std::ofstream output("example_00_fields_pp.json");
output << std::setw(4) << json2;
}
return 0;
}
/*
* This executable shows the creation of a distance field, as well as saving it to a json file
* afterwards.
*/
#include <dino/Dino.h>
#include <fstream>
#include <glm/ext.hpp>
#include <iostream>
#include <memory>
#include <optional>
int main()
{
const auto f0 = dino::Param::create("f0", glm::vec3(1, 1, 1));
const auto f1 = dino::Param::create("f1", glm::vec3(2, 2, 2));
const auto f2 = dino::Param::create("f2", glm::vec3(3, 3, 3));
const auto f4 = dino::Param::create("f4", glm::vec3(1.2, 0.2, 0.7));
const auto fi = dino::Param::create("fi", 8);
const auto ff1 = dino::Param::create("f1", 8.f);
const auto ff2 = dino::Param::create("f2", 2.f);
const auto ps = dino::Param::create("ps", glm::vec4(0, 0, 0, 1));
const auto mt = dino::Param::create("mat", glm::translate(glm::vec3(1, 2, 8)));
const auto ps1 = dino::Param::create("ps1", glm::vec4(0.2f, 1.3f, 5.3f, 1));
const auto operation = ((f0 + f1) / f2) % f4;
// Create a cube, a sphere and a torus, which will later be blended together.
const auto cube = dino::PrBox::create(glm::vec3(2, 4, 3));
const auto sphere = dino::PrSphere::create();
const auto torus = dino::PrTorus::create(1.f, 0.2f);
// We don't want both objects to stay in the center, so let's move the sphere a bit to the left.
const auto transform = dino::ModTransform::create(glm::translate(glm::vec3(-2.f, 0.f, 0.f)));
transform->matrix.replace(operation);
sphere->setModifier(transform);
// The object should use a global object transformation transforming the whole thing.
// It should be assigned to the lowest levels in the object tree (cube, torus and
// sphere-transform).
const auto objectTransform =
dino::ModTransform::create(glm::translate(glm::vec3(-1.f, 2.f, 1.f)));
transform->setModifier(objectTransform);
cube->extents.replace(operation);
cube->setModifier(objectTransform);
torus->setModifier(objectTransform);
// Let's also make a shared parameter which links the sphere "radius" and the torus
// "distanceToTube" to one single parameter, which is easier to control.
const auto radius = dino::Param::create("newRadius", 0.8f);
sphere->radius.replace(radius);
torus->circleRadius.replace(radius);
// Now add two polynomial blending operations, one for cube+sphere and one for the final
// blending of the result of that with the torus.
const auto blendCubeSphere = dino::OpBlendPolynomial::create(0.5f);
blendCubeSphere->apply(cube, sphere);
const auto blendFinal = dino::OpBlendPolynomial::create(0.2f);
blendFinal->apply(blendCubeSphere, torus);
// For now, let's just save the object to a json file.
// You can also set a breakpoint in your debugger to first view the contents (code, data, etc.)
// of "object".
const nlohmann::json json1 = blendFinal->serialize();
{
std::ofstream output("example_00_fields.json");
output << std::setw(4) << json1;
}
const auto bf = dino::Function::deserialize(json1);
const nlohmann::json json2 = bf->serialize();
{
std::ofstream output("example_00_fields_pp.json");
output << std::setw(4) << json2;
}
return 0;
}
#include <dino/Modifier.h>
#include <dino/Operator.h>
#include <dino/Primitive.h>
#include <dino/modifiers/Simple.h>
#include <dino/opengl/Buffer.h>
#include <dino/opengl/Shader.h>
#include <dino/operators/Blend.h>
#include <dino/operators/Simple.h>
#include <dino/primitives/FieldDiscretizer.h>
#include <dino/primitives/Fractal.h>
#include <dino/primitives/Simple.h>
#include <dino/rendering/RendererDeferred.h>
#include <dino/Camera.h>
#include <imgui/imgui_glfw.h>
#include <fstream>
#include <functional>
#include <glm/ext.hpp>
#include <iostream>
#include <GLFW/glfw3.h>
#include <dino/input/Window.h>
#include <glad/glad.h>
#include <glm/glm.hpp>
std::shared_ptr<dino::Window> window;
int main()
{
/************************************** Win/GUI/GL **************************************/
dino::Window::Hints hints = {{GLFW_SAMPLES, 8}};
window = std::make_shared<dino::Window>(1280, 720, "Dino", hints);
auto sphere = dino::PrSphere::create(1.0f);
auto tr_sphere = dino::ModTransform::create(glm::translate(glm::mat4(1.f), glm::vec3(1.f)));
sphere->setModifier(tr_sphere);
auto d_sphere = dino::PrFieldDiscretizer::create(sphere, glm::uvec3{100, 100, 100});
d_sphere->setUpdateEveryFrame(true);
/**************************************** Serialize *****************************************/
// serialize DDF
nlohmann::json test = d_sphere->serialize();
std::ofstream output("jsontest2.json");
output << std::setw(4) << test;
output.close();
// deserialize and load DDF from json
std::ifstream in("jsontest2.json");
nlohmann::json json2;
in >> json2;
in.close();
auto d_sphere_deserialized = dino::Function::deserialize(json2)->as<dino::PrFieldDiscretizer>();
auto tr_sphere_deserialized = d_sphere_deserialized->getField<dino::Primitive>()
->getModifier()
->as<dino::ModTransform>();
/**************************************** Callbacks ****************************************/
glfwSetKeyCallback(*window, [](GLFWwindow*, int key, int sc, int ac, int md) {
if(key == GLFW_KEY_F5 && ac == GLFW_PRESS)
dino::Program::reloadAll();
if(key == GLFW_KEY_ESCAPE && ac == GLFW_PRESS)
window->close();
});
/************************************** Build Buffers **************************************/
dino::Camera camera(glm::vec3(0, 0, 10));
glm::mat4 projection = glm::perspectiveFov(glm::radians(70.f), 1280.f, 720.f, 0.01f, 1000.f);
std::vector<std::shared_ptr<dino::RenderLayer>> deferredPipeline =
dino::RenderLayer::createRenderPipeline(
dino::RenderLayer::LIGHTING_SHADOWMAPS /*dino::RenderLayer::LIGHTING_SHADOW*/);
auto renderer =
std::make_unique<dino::RendererDeferred>(deferredPipeline, glm::ivec2(1280, 720));
renderer->add(d_sphere_deserialized);
/**************************************** Renderloop ****************************************/
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glm::vec4 cc(0.4, 0.5, 0.6, 1.f);
float depth = 1.f;
glClearColor(cc.r, cc.g, cc.b, cc.a);
glClearDepth(depth);
double last = glfwGetTime();
while(window->update())
{
double delta = glfwGetTime() - last;
last = glfwGetTime();
/************************************** GUI ****************************************/
ImGui::Begin("Settings");
ImGui::Value("Frametime: ", static_cast<float>(delta));
ImGui::End();
/************************************** Clear ****************************************/
int windowWidth;
int windowHeight;
glfwGetFramebufferSize(*window, &windowWidth, &windowHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, windowWidth, windowHeight);
/************************************** Animate ****************************************/
tr_sphere_deserialized->matrix = glm::translate(
glm::mat4(1.f), glm::vec3(float(glm::cos(last)), float(glm::sin(last)), 0.f));
/************************************** Update ****************************************/
camera.update(*window);
projection = glm::perspectiveFov(glm::radians(70.f),
static_cast<float>(windowWidth),
static_cast<float>(windowHeight),
0.01f,
1000.f);
/************************************** Draw ****************************************/
renderer->render(camera.view(), projection);
}
return 0;
}
#include <dino/Dino.h>
#include <fstream>
#include <functional>
#include <imgui/imgui_internal.h>
#include <iostream>
#include <random>
......
#include <dino/Modifier.h>
#include <dino/Operator.h>
#include <dino/opengl/Buffer.h>
#include <dino/opengl/Shader.h>
#include <dino/operators/Simple.h>
#include <dino/primitives/MeshDiscretizer.h>
#include <dino/primitives/Simple.h>
#include <dino/rendering/DistanceField.h>
#include <dino/rendering/RendererDeferred.h>
#include <dino/Camera.h>
#include <imgui/imgui_glfw.h>
#include <functional>
#include <glm/ext.hpp>
#include <GLFW/glfw3.h>
#include <dino/input/Window.h>
#include <glad/glad.h>
#include <fstream>
#include <iostream>
std::shared_ptr<dino::Window> window;
int main()
{
/************************************** Win/GUI/GL **************************************/
dino::Window::Hints hints = {{GLFW_SAMPLES, 8}, {GLFW_OPENGL_DEBUG_CONTEXT, 1}};
window = std::make_shared<dino::Window>(1280, 720, "Dino", hints);
/************************************** Object *******************************************/
auto object =
dino::PrMeshDiscretizer::create(dino::RESOURCES_PATH / "bunny.fbx",
dino::PrMeshDiscretizer::Generator::GPU_SIGNED_APPROX,
128);
// serialize DDF
nlohmann::json test;
test = object->serialize();
std::ofstream output("jsontest.json");
output << std::setw(4) << test;
output.close();
// deserialize and load DDF from json
std::ifstream in("jsontest.json");
nlohmann::json json2;
in >> json2;
in.close();
auto y = dino::Function::deserialize(json2)->as<dino::PrMeshDiscretizer>();
auto displace = y; // y->makeIsoDisplace(dino::PrMeshDiscretizer::ISO_LEVEL_OPTIMAL);
/**************************************** Callbacks ****************************************/
glfwSetKeyCallback(*window, [](GLFWwindow*, int key, int sc, int ac, int md) {
if(key == GLFW_KEY_F5 && ac == GLFW_PRESS)
dino::Program::reloadAll();
if(key == GLFW_KEY_ESCAPE && ac == GLFW_PRESS)
window->close();
});
/************************************** Build Buffers **************************************/
dino::Camera camera(glm::vec3(0, 0, 10));
glm::mat4 projection = glm::perspectiveFov(glm::radians(70.f), 1280.f, 720.f, 0.01f, 1000.f);