// MIT License // // Copyright (C) 2018-2023, Tellusim Technologies Inc. https://tellusim.com/ // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include #include #include #include #include #include #include #include /* */ using namespace Tellusim; /* */ int32_t main(int32_t argc, char **argv) { DECLARE_WINDOW // create window String title = String::format("%s Tellusim::Refine", window.getPlatformName()); if(!window.create(title) || !window.setHidden(false)) return 1; // structures struct Vertex { float32_t position[3]; float32_t normal[3]; }; struct CommonParameters { Matrix4x4f projection; Matrix4x4f modelview; Matrix4x4f transform; Vector4f camera; }; // create device Device device(window); if(!device) return 1; // create pipeline Pipeline pipeline = device.createPipeline(); pipeline.setUniformMask(0, Shader::MaskVertex); pipeline.addAttribute(Pipeline::AttributePosition, FormatRGBf32, 0, offsetof(Vertex, position), sizeof(Vertex)); pipeline.addAttribute(Pipeline::AttributeNormal, FormatRGBf32, 0, offsetof(Vertex, normal), sizeof(Vertex)); pipeline.setColorFormat(window.getColorFormat()); pipeline.setDepthFormat(window.getDepthFormat()); pipeline.setDepthFunc(Pipeline::DepthFuncLessEqual); if(!pipeline.loadShaderGLSL(Shader::TypeVertex, "main.shader", "VERTEX_SHADER=1")) return 1; if(!pipeline.loadShaderGLSL(Shader::TypeFragment, "main.shader", "FRAGMENT_SHADER=1")) return 1; if(!pipeline.create()) return 1; // load mesh Mesh src_mesh; if(!src_mesh.load("model.fbx")) return 1; // create model MeshModel src_model; if(!src_model.create(device, pipeline, src_mesh)) return 1; // subdiv mesh Mesh subdiv_mesh; uint64_t subdiv_begin = Time::current(); MeshRefine::subdiv(subdiv_mesh, src_mesh, 4); uint64_t subdiv_end = Time::current(); // create normals subdiv_mesh.createNormals(true); // subdiv model MeshModel subdiv_model; if(!subdiv_model.create(device, pipeline, subdiv_mesh)) return 1; TS_LOGF(Message, " Subdiv: %s\n", String::fromTime(subdiv_end - subdiv_begin).get()); TS_LOGF(Message, "Vertices: %u -> %u\n", src_model.getNumVertices(), subdiv_model.getNumVertices()); TS_LOGF(Message, " Indices: %u -> %u\n", src_model.getNumIndices(), subdiv_model.getNumIndices()); // save mesh //subdiv_mesh.save("model_subdiv.glb"); // create target Target target = device.createTarget(window); // main loop DECLARE_GLOBAL window.run([&]() -> bool { DECLARE_COMMON Window::update(); if(!window.render()) return false; // window title if(fps > 0.0f) window.setTitle(String::format("%s %.1f FPS", title.get(), fps)); // window target target.setClearColor(0.2f, 0.2f, 0.2f, 1.0f); target.begin(); { // create command list Command command = device.createCommand(target); // set pipeline command.setPipeline(pipeline); // set common parameters CommonParameters common_parameters; common_parameters.camera = Vector4f(2.0f, 2.0f, 1.0f, 0.0f); common_parameters.projection = Matrix4x4f::perspective(60.0f, (float32_t)window.getWidth() / window.getHeight(), 0.1f, 1000.0f); common_parameters.modelview = Matrix4x4f::lookAt(Vector3f(common_parameters.camera), Vector3f(0.0f, 0.0f, 0.0f), Vector3f(0.0f, 0.0f, 1.0f)); common_parameters.transform = Matrix4x4f::rotateX(time * 8.0f) * Matrix4x4f::rotateZ(time * 16.0f); if(target.isFlipped()) common_parameters.projection = Matrix4x4f::scale(1.0f, -1.0f, 1.0f) * common_parameters.projection; command.setUniform(0, common_parameters); // draw model if(window.getKeyboardKey('1')) { src_model.setBuffers(command); src_model.draw(command); } else { subdiv_model.setBuffers(command); subdiv_model.draw(command); } } target.end(); if(!window.present()) return false; if(!device.check()) return false; return true; }); // finish context window.finish(); return 0; }