1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "client/rendering/renderer/WorldRenderer.h"
- WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resources/textures.png"),
- normalTexture("resources/normal.png") {
- for(uint x = 0; x < World::WORLD_SIZE; x++) {
- for(uint y = 0; y < World::WORLD_SIZE; y++) {
- for(uint z = 0; z < World::WORLD_SIZE; z++) {
- if(world.getBlock(x, y, z).getId() != 0) {
- addCube(x, y, z,
- isAir(x, y - 1, z), isAir(x, y + 1, z),
- isAir(x - 1, y, z), isAir(x + 1, y, z),
- isAir(x, y, z + 1), isAir(x, y, z - 1));
- }
- }
- }
- }
- mesh.build();
- }
- void WorldRenderer::render(float lag, Renderer& renderer) const {
- (void) lag;
- (void) renderer;
- texture.bind(0);
- normalTexture.bind(2);
- //mesh.draw();
- }
- bool WorldRenderer::isAir(uint x, uint y, uint z) const {
- return world.getBlock(x, y, z).getId() == 0;
- }
- void WorldRenderer::addCube(float x, float y, float z, bool bottom, bool top, bool left, bool right, bool front, bool back) {
- Vector3 v000(x, y, z);
- Vector3 v001(x, y, z + 1);
- Vector3 v010(x, y + 1, z);
- Vector3 v011(x, y + 1, z + 1);
- Vector3 v100(x + 1, y, z);
- Vector3 v101(x + 1, y, z + 1);
- Vector3 v110(x + 1, y + 1, z);
- Vector3 v111(x + 1, y + 1, z + 1);
- Vector2 t1(0.1875f, 0.0f);
- Vector2 t2(0.25f, 0.0f);
- Vector2 t3(0.25f, 0.0625f);
- Vector2 t4(0.1875f, 0.0625f);
- if(bottom) {
- Vector2 tb(0.125f, 0.0625f);
- mesh.add(Triangle(Vertex(v000, Vector2(0.125f, 0.0f)), Vertex(v100, t1), Vertex(v001, tb)));
- mesh.add(Triangle(Vertex(v100, t1), Vertex(v101, t4), Vertex(v001, tb)));
- }
- if(top) {
- Vector2 tt(0.3125f, 0.0f);
- mesh.add(Triangle(Vertex(v010, t2), Vertex(v011, t3), Vertex(v110, tt)));
- mesh.add(Triangle(Vertex(v110, tt), Vertex(v011, t3), Vertex(v111, Vector2(0.3125f, 0.0625f))));
- }
- if(left) {
- mesh.add(Triangle(Vertex(v000, t4), Vertex(v001, t3), Vertex(v010, t1)));
- mesh.add(Triangle(Vertex(v001, t3), Vertex(v011, t2), Vertex(v010, t1)));
- }
- if(right) {
- mesh.add(Triangle(Vertex(v100, t3), Vertex(v110, t2), Vertex(v101, t4)));
- mesh.add(Triangle(Vertex(v101, t4), Vertex(v110, t2), Vertex(v111, t1)));
- }
- if(front) {
- mesh.add(Triangle(Vertex(v001, t4), Vertex(v101, t3), Vertex(v011, t1)));
- mesh.add(Triangle(Vertex(v111, t2), Vertex(v011, t1), Vertex(v101, t3)));
- }
- if(back) {
- mesh.add(Triangle(Vertex(v000, t3), Vertex(v010, t2), Vertex(v100, t4)));
- mesh.add(Triangle(Vertex(v110, t1), Vertex(v100, t4), Vertex(v010, t2)));
- }
- }
|