Skip to content

Commit aa14301

Browse files
committed
Proper UVs and mapping
1 parent 429bb41 commit aa14301

File tree

9 files changed

+120
-268
lines changed

9 files changed

+120
-268
lines changed

resources/shader/voxelchunk.frag.glsl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ layout(location = 0) out vec4 color;
55
in vec3 v_position;
66
in vec3 v_normal;
77
in vec2 v_uv;
8+
in float v_blocktype;
89

9-
uniform sampler2DArray textureArray;
10+
uniform sampler2D u_texture;
1011

1112
void main()
1213
{
13-
color = vec4(v_uv, 0.0, 1.0);
14+
// we have a 10x10 texture atlas
15+
// the blocktype represents the index of the texture in the atlas
16+
float atlasSize = 10.0;
17+
float cellX = mod(v_blocktype, atlasSize);
18+
float cellY = floor(v_blocktype / atlasSize);
19+
vec2 uvOffset = vec2(cellX / atlasSize, cellY / atlasSize);
20+
vec2 uvScale = vec2(1.0 / atlasSize, 1.0 / -atlasSize);
21+
22+
vec2 atlasUV = uvScale * v_uv + uvOffset;
23+
color = texture(u_texture, atlasUV);
1424
}

resources/shader/voxelchunk.vert.glsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
layout(location = 0) in vec3 position;
44
layout(location = 1) in vec3 normal;
55
layout(location = 2) in vec2 uv;
6+
layout(location = 3) in float blocktype;
67

78
uniform mat4 model;
89
uniform mat4 view;
@@ -11,11 +12,13 @@ uniform mat4 projection;
1112
out vec3 v_position;
1213
out vec3 v_normal;
1314
out vec2 v_uv;
15+
out float v_blocktype;
1416

1517
void main()
1618
{
1719
v_position = position;
1820
v_normal = normal;
1921
v_uv = uv;
22+
v_blocktype = blocktype;
2023
gl_Position = projection * view * model * vec4(position, 1.0);
2124
}

resources/sprites/textures.png

37.1 KB
Loading

src/Renderer/BackgroundRenderer.php

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/Renderer/ExampleImageRenderer.php

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/System/RenderingSystem.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace App\System;
44

5-
use App\Component\ExampleImage;
6-
use App\Renderer\BackgroundRenderer;
7-
use App\Renderer\ExampleImageRenderer;
85
use App\Voxel\ChunkRenderer;
96
use GL\Math\{GLM, Quat, Vec2, Vec3};
107
use VISU\ECS\EntitiesInterface;
@@ -21,16 +18,6 @@
2118

2219
class RenderingSystem implements SystemInterface
2320
{
24-
/**
25-
* Background renderer
26-
*/
27-
private BackgroundRenderer $backgroundRenderer;
28-
29-
/**
30-
* The example image renderer
31-
*/
32-
private ExampleImageRenderer $exampleImageRenderer;
33-
3421
/**
3522
* Fullscreen Texture Debug Renderer
3623
*/
@@ -49,10 +36,8 @@ public function __construct(
4936
private ShaderCollection $shaders
5037
)
5138
{
52-
$this->backgroundRenderer = new BackgroundRenderer($this->gl, $this->shaders);
53-
$this->exampleImageRenderer = new ExampleImageRenderer($this->gl, $this->shaders);
5439
$this->fullscreenRenderer = new FullscreenTextureRenderer($this->gl);
55-
$this->voxelRenderer = new ChunkRenderer($this->shaders);
40+
$this->voxelRenderer = new ChunkRenderer($this->gl, $this->shaders);
5641
}
5742

5843
/**

0 commit comments

Comments
 (0)