summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--.gitmodules3
-rw-r--r--CMakeLists.txt64
-rw-r--r--TexturedQuadExample.cpp95
-rw-r--r--TexturedQuadShader.cpp61
-rw-r--r--TexturedQuadShader.h63
-rw-r--r--chunk.cpp59
-rw-r--r--chunk.hpp28
-rw-r--r--images/tiles.tgabin0 -> 960018 bytes
-rw-r--r--old/matrix-test.cxx14
-rw-r--r--resources.conf7
-rw-r--r--shaders/TexturedQuadShader.frag40
-rw-r--r--shaders/TexturedQuadShader.vert39
13 files changed, 478 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..277d5b27
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+/build*/
+/.clang-tidy
+/.clang-format
+/.idea/
+/userconfig-*.cmake
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 00000000..86f6dea8
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "glm"]
+ path = glm
+ url = https://github.com/g-truc/glm.git
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 00000000..0dedbd3f
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,64 @@
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
+cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
+project(game)
+
+set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
+
+set(CMAKE_CXX_STANDARD 20)
+set(CMAKE_CXX_STANDARD_DEFAULT 20)
+set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
+set(CMAKE_CXX_EXTENSIONS FALSE)
+
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_C_STANDARD_DEFAULT 11)
+set(CMAKE_C_STANDARD_REQUIRED TRUE)
+set(CMAKE_C_EXTENSIONS FALSE)
+
+set(CMAKE_C_VISIBILITY_PRESET hidden)
+set(CMAKE_CXX_VISIBILITY_PRESET hidden)
+set(CMAKE_C_LINKER_PREFERENCE_PROPAGATES OFF)
+set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES ON)
+set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
+
+if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR CMAKE_INSTALL_PREFIX STREQUAL "")
+ set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "" FORCE)
+endif()
+
+set(_userconfig "userconfig-${CMAKE_CXX_COMPILER_ID}.cmake")
+if(EXISTS "${CMAKE_SOURCE_DIR}/${_userconfig}")
+ include("${CMAKE_SOURCE_DIR}/${_userconfig}")
+else()
+ message(STATUS "user config '${_userconfig}' not found")
+endif()
+
+if(MSVC)
+ add_compile_options(-EHsc)
+endif()
+
+if(WIN32)
+ add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_USE_MATH_DEFINES=1 -DNOMINMAX -DWIN32_LEAN_AND_MEAN)
+endif()
+
+include_directories("glm")
+
+find_package(SDL2 QUIET)
+find_package(Corrade REQUIRED)
+find_package(Magnum REQUIRED GL Shaders WglContext Sdl2Application Trade)
+
+set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS OFF)
+set_directory_properties(PROPERTIES CORRADE_CXX_STANDARD 20)
+set_directory_properties(PROPERTIES INTERFACE_CORRADE_CXX_STANDARD 20)
+
+corrade_add_resource(TexturedQuad_RESOURCES resources.conf)
+
+file(GLOB sources "*.cpp" CONFIGURE_ARGS)
+add_executable(magnum-triangle WIN32 "${sources}" "${TexturedQuad_RESOURCES}")
+target_link_libraries(magnum-triangle PRIVATE
+ Magnum::Application
+ Magnum::GL
+ Magnum::WglContext
+ Magnum::Magnum
+ Magnum::Shaders
+ Magnum::Trade)
+
+install(TARGETS DESTINATION .)
diff --git a/TexturedQuadExample.cpp b/TexturedQuadExample.cpp
new file mode 100644
index 00000000..ec166d6e
--- /dev/null
+++ b/TexturedQuadExample.cpp
@@ -0,0 +1,95 @@
+#include "chunk.hpp"
+
+#include <Corrade/Containers/Optional.h>
+#include <Corrade/Containers/ArrayViewStl.h>
+#include <Corrade/PluginManager/Manager.h>
+#include <Corrade/Utility/Resource.h>
+#include <Magnum/ImageView.h>
+#include <Magnum/GL/Buffer.h>
+#include <Magnum/GL/DefaultFramebuffer.h>
+#include <Magnum/GL/Mesh.h>
+#include <Magnum/GL/Texture.h>
+#include <Magnum/GL/TextureFormat.h>
+#include <Magnum/Platform/Sdl2Application.h>
+#include <Magnum/Trade/AbstractImporter.h>
+#include <Magnum/Trade/ImageData.h>
+
+#include "TexturedQuadShader.h"
+
+namespace Magnum::Examples {
+
+struct TexturedQuadExample: Platform::Application {
+ explicit TexturedQuadExample(const Arguments& arguments);
+ void drawEvent() override;
+
+ const Utility::Resource rs{"texturedquad-data"};
+ PluginManager::Manager<Trade::AbstractImporter> plugins;
+ Containers::Pointer<Trade::AbstractImporter> tga_importer =
+ plugins.loadAndInstantiate("TgaImporter");
+
+ GL::Mesh _mesh;
+ TexturedQuadShader _shader;
+ atlas_texture atlas = make_atlas("images/tiles.tga", {8, 4});
+
+ atlas_texture make_atlas(const std::string& file, Vector2i dims)
+ {
+ if(!tga_importer || !tga_importer->openFile(file))
+ std::exit(1);
+
+ Containers::Optional<Trade::ImageData2D> image = tga_importer->image2D(0);
+ CORRADE_INTERNAL_ASSERT(image);
+
+ return atlas_texture{*image, dims};
+ }
+};
+
+TexturedQuadExample::TexturedQuadExample(const Arguments& arguments):
+ Platform::Application{arguments, Configuration{}
+ .setTitle("Magnum Textured Quad Example")
+ .setSize({512, 512})}
+{
+ struct QuadVertex {
+ Vector3 position;
+ Vector2 textureCoordinates;
+ };
+ QuadVertex vertices[4];
+ auto positions = atlas.floor_quad({}, {2, 2});
+ auto texcoords = atlas.texcoords_for_id(2);
+ auto indices = atlas.indices(0);
+
+ for (unsigned i = 0; i < std::size(vertices); i++)
+ vertices[i] = { positions[i], texcoords[i] };
+
+ _mesh.setCount(std::size(indices))
+ .addVertexBuffer(GL::Buffer{vertices}, 0,
+ TexturedQuadShader::Position{},
+ TexturedQuadShader::TextureCoordinates{})
+ .setIndexBuffer(GL::Buffer{indices}, 0,
+ GL::MeshIndexType::UnsignedShort);
+}
+
+void TexturedQuadExample::drawEvent() {
+ GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);
+
+ using namespace Math::Literals;
+
+ _shader
+ .setColor(0xffffff_rgbf)
+ .bindTexture(atlas.texture())
+ .draw(_mesh);
+
+ swapBuffers();
+}
+
+} // namespace Magnum::Examples
+
+MAGNUM_APPLICATION_MAIN(Magnum::Examples::TexturedQuadExample);
+
+#include <windows.h>
+
+#if defined _MSC_VER
+int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int /* nCmdShow */)
+{
+ return main(__argc, __argv);
+}
+#endif
diff --git a/TexturedQuadShader.cpp b/TexturedQuadShader.cpp
new file mode 100644
index 00000000..c6d91c4b
--- /dev/null
+++ b/TexturedQuadShader.cpp
@@ -0,0 +1,61 @@
+/*
+ This file is part of Magnum.
+
+ Original authors — credit is appreciated but not required:
+
+ 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
+ 2020, 2021, 2022 — Vladimír Vondruš <mosra@centrum.cz>
+
+ This is free and unencumbered software released into the public domain.
+
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute
+ this software, either in source code form or as a compiled binary, for any
+ purpose, commercial or non-commercial, and by any means.
+
+ In jurisdictions that recognize copyright laws, the author or authors of
+ this software dedicate any and all copyright interest in the software to
+ the public domain. We make this dedication for the benefit of the public
+ at large and to the detriment of our heirs and successors. We intend this
+ dedication to be an overt act of relinquishment in perpetuity of all
+ present and future rights to this software under copyright law.
+
+ 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 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 "TexturedQuadShader.h"
+
+#include <Corrade/Containers/Reference.h>
+#include <Corrade/Utility/Resource.h>
+#include <Magnum/GL/Context.h>
+#include <Magnum/GL/Shader.h>
+#include <Magnum/GL/Version.h>
+
+namespace Magnum::Examples {
+
+TexturedQuadShader::TexturedQuadShader() {
+ MAGNUM_ASSERT_GL_VERSION_SUPPORTED(GL::Version::GL330);
+
+ const Utility::Resource rs{"texturedquad-data"};
+
+ GL::Shader vert{GL::Version::GL330, GL::Shader::Type::Vertex};
+ GL::Shader frag{GL::Version::GL330, GL::Shader::Type::Fragment};
+
+ vert.addSource(rs.get("shaders/TexturedQuadShader.vert"));
+ frag.addSource(rs.get("shaders/TexturedQuadShader.frag"));
+
+ CORRADE_INTERNAL_ASSERT_OUTPUT(GL::Shader::compile({vert, frag}));
+
+ attachShaders({vert, frag});
+
+ CORRADE_INTERNAL_ASSERT_OUTPUT(link());
+
+ _colorUniform = uniformLocation("color");
+ setUniform(uniformLocation("textureData"), TextureUnit);
+}
+
+} // namespace Magnum::Examples
diff --git a/TexturedQuadShader.h b/TexturedQuadShader.h
new file mode 100644
index 00000000..de7b5c04
--- /dev/null
+++ b/TexturedQuadShader.h
@@ -0,0 +1,63 @@
+#ifndef Magnum_Examples_TexturedQuad_TexturedQuadShader_h
+#define Magnum_Examples_TexturedQuad_TexturedQuadShader_h
+/*
+ This file is part of Magnum.
+
+ Original authors — credit is appreciated but not required:
+
+ 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
+ 2020, 2021, 2022 — Vladimír Vondruš <mosra@centrum.cz>
+
+ This is free and unencumbered software released into the public domain.
+
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute
+ this software, either in source code form or as a compiled binary, for any
+ purpose, commercial or non-commercial, and by any means.
+
+ In jurisdictions that recognize copyright laws, the author or authors of
+ this software dedicate any and all copyright interest in the software to
+ the public domain. We make this dedication for the benefit of the public
+ at large and to the detriment of our heirs and successors. We intend this
+ dedication to be an overt act of relinquishment in perpetuity of all
+ present and future rights to this software under copyright law.
+
+ 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 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 <Magnum/GL/AbstractShaderProgram.h>
+#include <Magnum/GL/Texture.h>
+#include <Magnum/Math/Color.h>
+
+namespace Magnum::Examples {
+
+class TexturedQuadShader: public GL::AbstractShaderProgram {
+public:
+ typedef GL::Attribute<0, Vector3> Position;
+ typedef GL::Attribute<1, Vector2> TextureCoordinates;
+
+ explicit TexturedQuadShader();
+
+ TexturedQuadShader& setColor(const Color3& color) {
+ setUniform(_colorUniform, color);
+ return *this;
+ }
+
+ TexturedQuadShader& bindTexture(GL::Texture2D& texture) {
+ texture.bind(TextureUnit);
+ return *this;
+ }
+
+private:
+ enum: Int { TextureUnit = 0 };
+
+ Int _colorUniform;
+};
+
+} // namespace Magnum::Examples
+
+#endif
diff --git a/chunk.cpp b/chunk.cpp
new file mode 100644
index 00000000..d0a29a29
--- /dev/null
+++ b/chunk.cpp
@@ -0,0 +1,59 @@
+#include "chunk.hpp"
+#include <Magnum/GL/TextureFormat.h>
+#include <Magnum/ImageView.h>
+
+namespace Magnum::Examples {
+
+atlas_texture::atlas_texture(const Trade::ImageData2D& image, Vector2i dims) :
+ size_{image.size()},
+ dims_{dims},
+ tile_size_{size_ / dims}
+{
+ CORRADE_INTERNAL_ASSERT(dims_[0] > 0 && dims_[1] > 0);
+ CORRADE_INTERNAL_ASSERT(tile_size_ * dims_ == size_);
+ CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2i{});
+ tex_.setWrapping(GL::SamplerWrapping::ClampToEdge)
+ .setMagnificationFilter(GL::SamplerFilter::Nearest)
+ .setMinificationFilter(GL::SamplerFilter::Linear)
+ .setStorage(1, GL::textureFormat(image.format()), image.size())
+ .setSubImage(0, {}, image);
+}
+
+std::array<Vector2, 4> atlas_texture::texcoords_for_id(int id_) const
+{
+ CORRADE_INTERNAL_ASSERT(id_ >= 0 && id_ < dims_.product());
+ constexpr Vector2 _05 = { 0.5f, 0.5f };
+ constexpr Vector2i _1 = { 1, 1 };
+ Vector2i id = { id_ % dims_[1], id_ / dims_[1] };
+ auto p0 = (Vector2(id * tile_size_) + _05) / Vector2(size_);
+ auto p1 = (Vector2((id + _1) * tile_size_) + _05) / Vector2(size_);
+ return {{
+ { p1[0], p1[1] }, // bottom right
+ { p1[0], p0[1] }, // top right
+ { p0[0], p1[1] }, // bottom left
+ { p0[0], p0[1] } // top left
+ }};
+}
+
+std::array<Vector3, 4> atlas_texture::floor_quad(Vector3 center, Vector2 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f;
+ return {{
+ { x + center[0], -y + center[1], 0},
+ { x + center[0], y + center[1], 0},
+ {-x + center[0], -y + center[1], 0},
+ {-x + center[0], y + center[1], 0},
+ }};
+}
+
+std::array<UnsignedShort, 6> atlas_texture::indices(int N)
+{
+ CORRADE_INTERNAL_ASSERT(N >= 0);
+ using u16 = UnsignedShort;
+ return { /* 3--1 1 */
+ (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */
+ (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */
+ }; /* 2 2--0 */
+}
+
+} // namespace Magnum::Examples
diff --git a/chunk.hpp b/chunk.hpp
new file mode 100644
index 00000000..690fdb91
--- /dev/null
+++ b/chunk.hpp
@@ -0,0 +1,28 @@
+#pragma once
+#include <Magnum/Magnum.h>
+#include <Magnum/GL/Texture.h>
+#include <Magnum/Math/Vector.h>
+#include <Magnum/Math/Vector2.h>
+#include <Magnum/Trade/ImageData.h>
+#include <tuple>
+#include <array>
+
+namespace Magnum::Examples {
+using Vector2i = Math::Vector<2, int>;
+
+struct atlas_texture final
+{
+ atlas_texture(const Trade::ImageData2D& img, Vector2i dims);
+ std::array<Vector2, 4> texcoords_for_id(int id) const;
+ static std::array<Vector3, 4> floor_quad(Vector3 center, Vector2 size);
+ static std::array<UnsignedShort, 6> indices(int N);
+ GL::Texture2D& texture() { return tex_; }
+
+ atlas_texture(const atlas_texture&) = delete;
+ atlas_texture& operator=(const atlas_texture&) = delete;
+private:
+ GL::Texture2D tex_;
+ Vector2i size_, dims_, tile_size_;
+};
+
+} // namespace Magnum::Examples
diff --git a/images/tiles.tga b/images/tiles.tga
new file mode 100644
index 00000000..2e520f2a
--- /dev/null
+++ b/images/tiles.tga
Binary files differ
diff --git a/old/matrix-test.cxx b/old/matrix-test.cxx
new file mode 100644
index 00000000..ee3ed43c
--- /dev/null
+++ b/old/matrix-test.cxx
@@ -0,0 +1,14 @@
+#include <glm/glm.hpp>
+#include <glm/ext/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale
+#include <cstdio>
+int main(void)
+{
+ glm::mat4 m(1);
+ m = glm::rotate(m, glm::radians(-45.f), glm::vec3(1.0f, 0.0f, 0.0f));
+ m = glm::rotate(m, glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
+ m = glm::rotate(m, glm::radians(-45.0f), glm::vec3(0.0f, 0.0f, 1.0f));
+ m = glm::scale(m, { 0.68, 1, 1 });
+ for (int j = 0; j < 4; j++)
+ printf("%f, %f, %f, %f,\n", m[j][0], m[j][1], m[j][2], m[j][3]);
+ return 0;
+}
diff --git a/resources.conf b/resources.conf
new file mode 100644
index 00000000..43cf92f1
--- /dev/null
+++ b/resources.conf
@@ -0,0 +1,7 @@
+group=texturedquad-data
+
+[file]
+filename=shaders/TexturedQuadShader.frag
+
+[file]
+filename=shaders/TexturedQuadShader.vert
diff --git a/shaders/TexturedQuadShader.frag b/shaders/TexturedQuadShader.frag
new file mode 100644
index 00000000..06d647f8
--- /dev/null
+++ b/shaders/TexturedQuadShader.frag
@@ -0,0 +1,40 @@
+/*
+ This file is part of Magnum.
+
+ Original authors — credit is appreciated but not required:
+
+ 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
+ 2020, 2021, 2022 — Vladimír Vondruš <mosra@centrum.cz>
+
+ This is free and unencumbered software released into the public domain.
+
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute
+ this software, either in source code form or as a compiled binary, for any
+ purpose, commercial or non-commercial, and by any means.
+
+ In jurisdictions that recognize copyright laws, the author or authors of
+ this software dedicate any and all copyright interest in the software to
+ the public domain. We make this dedication for the benefit of the public
+ at large and to the detriment of our heirs and successors. We intend this
+ dedication to be an overt act of relinquishment in perpetuity of all
+ present and future rights to this software under copyright law.
+
+ 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 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.
+*/
+
+uniform vec3 color = vec3(1.0, 1.0, 1.0);
+uniform sampler2D textureData;
+
+in vec2 interpolatedTextureCoordinates;
+
+out vec4 fragmentColor;
+
+void main() {
+ fragmentColor.rgb = color*texture(textureData, interpolatedTextureCoordinates).rgb;
+ fragmentColor.a = 1.0;
+}
diff --git a/shaders/TexturedQuadShader.vert b/shaders/TexturedQuadShader.vert
new file mode 100644
index 00000000..2fd94f8f
--- /dev/null
+++ b/shaders/TexturedQuadShader.vert
@@ -0,0 +1,39 @@
+/*
+ This file is part of Magnum.
+
+ Original authors — credit is appreciated but not required:
+
+ 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
+ 2020, 2021, 2022 — Vladimír Vondruš <mosra@centrum.cz>
+
+ This is free and unencumbered software released into the public domain.
+
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute
+ this software, either in source code form or as a compiled binary, for any
+ purpose, commercial or non-commercial, and by any means.
+
+ In jurisdictions that recognize copyright laws, the author or authors of
+ this software dedicate any and all copyright interest in the software to
+ the public domain. We make this dedication for the benefit of the public
+ at large and to the detriment of our heirs and successors. We intend this
+ dedication to be an overt act of relinquishment in perpetuity of all
+ present and future rights to this software under copyright law.
+
+ 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 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.
+*/
+
+layout(location = 0) in vec4 position;
+layout(location = 1) in vec2 textureCoordinates;
+
+out vec2 interpolatedTextureCoordinates;
+
+void main() {
+ interpolatedTextureCoordinates = textureCoordinates;
+
+ gl_Position = position;
+}