summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-02-23 16:47:24 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-02-23 16:47:24 +0100
commit5664fd34a2f0317a276249762512d1a479fd7585 (patch)
tree38425c03b171145f4e7774a20f975dc7389f6c28
parent767f69d60c87a039c6ab0c3bf9ba4ed171b28da4 (diff)
flush
-rw-r--r--atlas.cpp47
-rw-r--r--atlas.hpp9
-rw-r--r--main.cpp61
-rw-r--r--shaders/tile-shader.frag5
-rw-r--r--shaders/tile-shader.vert2
5 files changed, 88 insertions, 36 deletions
diff --git a/atlas.cpp b/atlas.cpp
index d6b6a0c2..4528393f 100644
--- a/atlas.cpp
+++ b/atlas.cpp
@@ -37,7 +37,9 @@ std::array<Vector2, 4> atlas_texture::texcoords_for_id(int id_) const
}};
}
-std::array<Vector3, 4> atlas_texture::floor_quad(Vector3 center, Vector2 size)
+using vertex_array_type = atlas_texture::vertex_array_type;
+
+vertex_array_type atlas_texture::floor_quad(Vector3 center, Vector2 size)
{
float x = size[0]*.5f, y = size[1]*.5f;
return {{
@@ -48,14 +50,47 @@ std::array<Vector3, 4> atlas_texture::floor_quad(Vector3 center, Vector2 size)
}};
}
-std::array<Vector3, 4> atlas_texture::wall_quad(Vector3 center, Vector3 size)
+vertex_array_type atlas_texture::wall_quad_N(Vector3 center, Vector3 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
+ return {{
+ { x + center[0], y + center[1], center[2] },
+ { x + center[0], y + center[1], z + center[2] },
+ {-x + center[0], y + center[1], center[2] },
+ {-x + center[0], y + center[1], z + center[2] },
+ }};
+}
+
+vertex_array_type atlas_texture::wall_quad_W(Vector3 center, Vector3 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
+ return {{
+ {-x + center[0], y + center[1], center[2] },
+ {-x + center[0], y + center[1], z + center[2] },
+ {-x + center[0], -y + center[1], center[2] },
+ {-x + center[0], -y + center[1], z + center[2] },
+ }};
+}
+
+vertex_array_type atlas_texture::wall_quad_S(Vector3 center, Vector3 size)
+{
+ float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
+ return {{
+ { x + center[0], -y + center[1], center[2] },
+ { x + center[0], -y + center[1], z + center[2] },
+ {-x + center[0], -y + center[1], center[2] },
+ {-x + center[0], -y + center[1], z + center[2] },
+ }};
+}
+
+vertex_array_type atlas_texture::wall_quad_E(Vector3 center, Vector3 size)
{
float x = size[0]*.5f, y = size[1]*.5f, z = size[2];
return {{
- { x + center[0], -y + center[1], + center[2] },
- { x + center[0], -y + center[1], z+ center[2] },
- {-x + center[0], y + center[1], + center[2] },
- {-x + center[0], y + center[1], z+ center[2] },
+ { x + center[0], -y + center[1], center[2] },
+ { x + center[0], -y + center[1], z + center[2] },
+ { x + center[0], y + center[1], center[2] },
+ { x + center[0], y + center[1], z + center[2] },
}};
}
diff --git a/atlas.hpp b/atlas.hpp
index 43c858de..d2fddff2 100644
--- a/atlas.hpp
+++ b/atlas.hpp
@@ -9,10 +9,15 @@ namespace Magnum::Examples {
struct atlas_texture final
{
+ using vertex_array_type = std::array<Vector3, 4>;
+
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<Vector3, 4> wall_quad(Vector3 center, Vector3 size);
+ static vertex_array_type floor_quad(Vector3 center, Vector2 size);
+ static vertex_array_type wall_quad_W(Vector3 center, Vector3 size);
+ static vertex_array_type wall_quad_S(Vector3 center, Vector3 size);
+ static vertex_array_type wall_quad_E(Vector3 center, Vector3 size);
+ static vertex_array_type wall_quad_N(Vector3 center, Vector3 size);
static std::array<UnsignedShort, 6> indices(int N);
GL::Texture2D& texture() { return tex_; }
constexpr int size() const { return dims_.product(); }
diff --git a/main.cpp b/main.cpp
index 57cd789f..1e37a01c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -51,7 +51,7 @@ struct application final : Platform::Application
loader.tile_atlas("../share/game/images/metal1.tga", {1, 1});
//loader.tile_atlas("../share/game/images/floor1.tga", {4, 4});
std::shared_ptr<atlas_texture> atlas2 =
- loader.tile_atlas("../share/game/images/tiles2.tga", {8, 5});
+ loader.tile_atlas("../share/game/images/metal2.tga", {4, 4});
std::uint64_t time_ticks = 0, time_freq = SDL_GetPerformanceFrequency();
Vector3 camera_offset;
@@ -76,11 +76,12 @@ float application::projection_size_ratio()
glm::mat<4, 4, double> application::make_view(Vector3 offset) {
using vec3 = glm::vec<3, double>;
- auto m = glm::scale(glm::mat<4, 4, double>{1}, { 1., .6, 1. });
- //auto m = glm::mat<4, 4, double>{1};
+ using mat4 = glm::mat<4, 4, double>;
+ mat4 m{1};
+ m = glm::scale(glm::mat<4, 4, double>{1}, { 1., .6, 1. });
m = glm::translate(m, { (double)offset[0], (double)-offset[1], (double)offset[2] });
- m = glm::rotate(m, glm::radians(std::asin(1./std::sqrt(2))), vec3(1, 0, 0));
- m = glm::rotate(m, glm::radians(45.), vec3(0, 0, 1));
+ m = glm::rotate(m, glm::radians(-std::atan(1./std::sqrt(2))), vec3(0, 1, 0));
+ m = glm::rotate(m, glm::radians(-45.), vec3(0, 0, 1));
return m;
}
@@ -117,6 +118,8 @@ application::application(const Arguments& arguments):
auto sz = Vector2{100, 100} * ratio;
{
+ vertices.clear();
+ indices.clear();
int k = 0;
for (int j = -2; j <= 2; j++)
for (int i = -2; i <= 2; i++)
@@ -142,33 +145,43 @@ application::application(const Arguments& arguments):
indices.clear();
{
- auto positions = atlas2->wall_quad({}, Vector3(sz[0], sz[1], sz[1]));
- //auto positions = atlas->floor_quad({(float)(sz[0]*0), (float)(sz[1]*0), sz[1]*2}, sz);
- auto texcoords = atlas->texcoords_for_id(0);
- auto indices_ = atlas->indices(0);
- for (unsigned x = 0; x < 4; x++)
+ atlas_texture::vertex_array_type walls[] = {
+ atlas2->wall_quad_W({}, Vector3(sz[0], sz[1], sz[1]*2)),
+ atlas2->wall_quad_S({}, Vector3(sz[0], sz[1], sz[1]*2)),
+ atlas2->wall_quad_N({}, Vector3(sz[0], sz[1], sz[1]*2)),
+ atlas2->wall_quad_E({}, Vector3(sz[0], sz[1], sz[1]*2)),
+ };
+
+ int k = 0;
+ for (const auto& positions : walls)
{
- Utility::Debug{} << "wall" << x << positions[x];
- vertices.push_back({ positions[x], texcoords[x] });
+ auto texcoords = atlas2->texcoords_for_id(k);
+ auto indices_ = atlas2->indices(k);
+ for (unsigned x = 0; x < 4; x++)
+ vertices.push_back({ positions[x], texcoords[x] });
+ for (auto x : indices_)
+ indices.push_back(x);
+ k++;
}
- for (auto x : indices_)
- indices.push_back(x);
- _mesh2.setCount((int)indices.size())
- .addVertexBuffer(GL::Buffer{vertices}, 0,
- tile_shader::Position{}, tile_shader::TextureCoordinates{})
- .setIndexBuffer(GL::Buffer{indices}, 0, GL::MeshIndexType::UnsignedShort);
+ //auto positions = atlas->floor_quad({(float)(sz[0]*0), (float)(sz[1]*0), sz[1]*2}, sz);
}
+ _mesh2.setCount((int)indices.size())
+ .addVertexBuffer(GL::Buffer{vertices}, 0,
+ tile_shader::Position{}, tile_shader::TextureCoordinates{})
+ .setIndexBuffer(GL::Buffer{indices}, 0, GL::MeshIndexType::UnsignedShort);
+
(void)get_dt();
}
void application::drawEvent() {
- GL::defaultFramebuffer.clear(GL::FramebufferClear::Color | GL::FramebufferClear::Depth);
+ GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);
- GL::Renderer::setDepthMask(true);
- GL::Renderer::setDepthFunction(GL::Renderer::DepthFunction::LessOrEqual);
- GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
+ //GL::defaultFramebuffer.clear(GL::FramebufferClear::Depth);
+ //GL::Renderer::setDepthMask(true);
+ //GL::Renderer::setDepthFunction(GL::Renderer::DepthFunction::LessOrEqual);
+ //GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
{
float dt = get_dt();
@@ -199,7 +212,7 @@ void application::drawEvent() {
void application::update(float dt)
{
- constexpr float pixels_per_second = 10;
+ constexpr float pixels_per_second = 100;
if (keys[(int)key::camera_up])
camera_offset += Vector3(0, -1, 0) * dt * pixels_per_second;
else if (keys[(int)key::camera_down])
@@ -215,7 +228,7 @@ void application::update(float dt)
void application::do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated)
{
- using Mods = KeyEvent::Modifiers;
+ //using Mods = KeyEvent::Modifiers;
(void)m;
(void)repeated;
diff --git a/shaders/tile-shader.frag b/shaders/tile-shader.frag
index 2d3de0b2..790399e8 100644
--- a/shaders/tile-shader.frag
+++ b/shaders/tile-shader.frag
@@ -1,4 +1,3 @@
-uniform vec3 color = vec3(1.0, 1.0, 1.0);
layout(location = 2) uniform sampler2D textureData;
layout(location = 1) uniform float y_scale;
@@ -8,7 +7,7 @@ in float interpolated_frag_depth;
out vec4 fragmentColor;
void main() {
- fragmentColor.rgb = color*texture(textureData, interpolatedTextureCoordinates).rgb;
+ fragmentColor.rgb = texture(textureData, interpolatedTextureCoordinates).rgb;
fragmentColor.a = 1.0;
- gl_FragDepth = -interpolated_frag_depth * y_scale;
+ gl_FragDepth = interpolated_frag_depth * y_scale;
}
diff --git a/shaders/tile-shader.vert b/shaders/tile-shader.vert
index cc90bae5..10b17f38 100644
--- a/shaders/tile-shader.vert
+++ b/shaders/tile-shader.vert
@@ -12,5 +12,5 @@ void main() {
interpolatedTextureCoordinates = textureCoordinates;
gl_Position = projection * vec4(position.xy, 0, position.w) + vec4(0, position.z * y_scale, 0, 0);
- interpolated_frag_depth = position.z;
+ interpolated_frag_depth = -position.z;
}