// 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 /* */ using namespace Tellusim; /* */ int32_t main(int32_t argc, char **argv) { DECLARE_WINDOW // create window String title = String::format("%s Tellusim::ParallaxCube", window.getPlatformName()); if(!window.create(title) || !window.setHidden(false)) return 1; // structures struct CommonParameters { Matrix4x4f projection; Matrix4x4f modelview; Matrix4x4f transform; Vector4f icamera; Vector4f ilight; Vector4f camera; Vector4f light; }; // create device Device device(window); if(!device) return 1; // create target Target target = device.createTarget(window); // create pipeline Pipeline pipeline = device.createPipeline(); pipeline.setUniformMask(0, Shader::MaskVertex); pipeline.setSamplerMask(0, Shader::MaskFragment); pipeline.setTextureMask(0, Shader::MaskFragment); pipeline.addAttribute(Pipeline::AttributePosition, FormatRGBf32, 0, sizeof(float32_t) * 0, sizeof(float32_t) * 8); pipeline.setColorFormat(window.getColorFormat()); pipeline.setDepthFormat(window.getDepthFormat()); pipeline.setDepthFunc(Pipeline::DepthFuncLessEqual); pipeline.setCullMode(target.isFlipped() ? Pipeline::CullModeBack : Pipeline::CullModeFront); 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; // create sampler Sampler sampler = device.createSampler(Sampler::FilterTrilinear, Sampler::WrapModeClamp); if(!sampler) return 1; // create texture Texture texture = device.loadTexture("height.ktx", Texture::FlagMipmaps); if(!texture) return 1; // create sphere geometry #include "main_sphere.h" Buffer sphere_vertex_buffer = device.createBuffer(Buffer::FlagVertex, sphere_vertices, sizeof(float32_t) * num_sphere_vertices); Buffer sphere_index_buffer = device.createBuffer(Buffer::FlagIndex, sphere_indices, sizeof(uint32_t) * num_sphere_indices); if(!sphere_vertex_buffer || !sphere_index_buffer) return 1; // 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); command.setSampler(0, sampler); command.setTexture(0, texture); // set buffers command.setVertexBuffer(0, sphere_vertex_buffer); command.setIndexBuffer(FormatRu32, sphere_index_buffer); // set common parameters CommonParameters common_parameters; common_parameters.camera = Vector4f(3.0f, 3.0f, 1.0f, 0.0f); common_parameters.light = Vector4f(3.0f, 3.0f, 6.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::rotateZ(time * 16.0f) * Matrix4x4f::rotateY(time * 4.0f) * Matrix4x4f::scale(4.0f, 4.0f, 4.0f); common_parameters.icamera = inverse(common_parameters.transform) * common_parameters.camera; common_parameters.ilight = inverse(common_parameters.transform) * common_parameters.light; if(target.isFlipped()) common_parameters.projection = Matrix4x4f::scale(1.0f, -1.0f, 1.0f) * common_parameters.projection; command.setUniform(0, common_parameters); // draw sphere command.drawElements(num_sphere_indices); } target.end(); if(!window.present()) return false; if(!device.check()) return false; return true; }); // finish context window.finish(); return 0; }