Skip to content

Commit

Permalink
World rendering!
Browse files Browse the repository at this point in the history
NOTICE: Chunks are currently generated at the client side because chunk transmission is not finished
  • Loading branch information
bridgekat committed Oct 15, 2016
1 parent 291df7d commit a58a5a4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
7 changes: 0 additions & 7 deletions src/client/chunkclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ void ChunkClient::buildVertexArray()
va.addVertex({ pos.x + 0.0f, pos.y + 1.0f, pos.z + 0.0f });
}
});

va.setColor({ 1.0f, 1.0f, 1.0f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ 0.0f, 0.0f, 0.0f });
va.addVertex({ 0.0f, 0.0f, 16.0f });
va.addVertex({ 16.0f, 0.0f, 16.0f });
va.addVertex({ 16.0f, 0.0f, 0.0f });
}
m_buffer = VertexBuffer(va);
m_renderBuilt = true;
Expand Down
13 changes: 11 additions & 2 deletions src/client/chunkclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef CHUNKCLIENT_H_
#define CHUNKCLIENT_H_

#include <boost/core/noncopyable.hpp>
#include <chunk.h>
#include <world.h>
#include "renderer.h"
Expand All @@ -30,10 +29,20 @@ class ChunkClient : public Chunk
public:
ChunkClient(const Vec3i& position, World& world) : Chunk(position), m_world(world)
{
// TEMP CODE
// Generate terrain at client side to test rendering
if (position.y <= 0)
{
Vec3i::for_range(0, ChunkSize, [&](const Vec3i& curr)
{
setBlock(curr, BlockData(1, 0, 0));
});
}
// END TEMP CODE
}

// Is render built
bool isRenderBuilt()
bool isRenderBuilt() const
{
return m_renderBuilt;
}
Expand Down
10 changes: 10 additions & 0 deletions src/client/gamescene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ GameScene::GameScene(UI::Core::Window* win, BlockManager& bm, PluginManager& pm)
).get<void NWAPICALL(int, char**)>("main")(sizeof(argv)/sizeof(argv[0]), argv);
});

// TEMP CODE
// Load some chunks at client side to test rendering
m_world.setRenderDistance(4);
m_player.setPosition(Vec3d(-16.0, 32.0, 32.0));
m_player.setRotation(Vec3d(-45.0, -22.5, 0.0));
Vec3i::for_range(-6, 6, [&](const Vec3i& pos)
{
m_world.addChunk(pos);
});
// END TEMP CODE

// FIXME: if the server spends too much time starting, the network thread won't be able to connect to the server.
m_connection.connect();
Expand Down Expand Up @@ -97,12 +105,14 @@ void GameScene::doRender()
// To show the world coordinates
glDisable(GL_CULL_FACE);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
/*
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 32.0f);
glVertex3f(32.0f, 0.0f, 32.0f);
glVertex3f(32.0f, 0.0f, 0.0f);
glEnd();
*/
// X
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
Expand Down
10 changes: 7 additions & 3 deletions src/client/worldclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ Chunk* WorldClient::addChunk(const Vec3i& chunkPos)

void WorldClient::renderUpdate(const Vec3i& position)
{
int pr = 0;
size_t pr = 0;
for (size_t i = 0; i < getChunkCount(); i++)
{
ChunkClient* p = static_cast<ChunkClient*>(getChunkPtr(i));

// In render range, pending to render
if (position.chebyshevDistance(p->getPosition()) <= m_renderDist)
{
if (!p->isRenderBuilt()/* || p->isEmpty()*/)
if (p->isRenderBuilt()/* || p->isEmpty()*/)
continue;

// Get chunk center pos
Expand Down Expand Up @@ -92,14 +92,16 @@ void WorldClient::renderUpdate(const Vec3i& position)
}
}

// debugstream << pr;

for (size_t i = 0; i < pr; i++)
{
m_chunkRenderList[i].first->buildVertexArray();
}

}

int WorldClient::render(const Vec3i& position) const
size_t WorldClient::render(const Vec3i& position) const
{
size_t renderedChunks = 0;
for (size_t i = 0; i < getChunkCount(); i++)
Expand All @@ -109,7 +111,9 @@ int WorldClient::render(const Vec3i& position) const
continue;
if (position.chebyshevDistance(p->getPosition()) > m_renderDist)
continue;
Renderer::translate(p->getPosition() * ChunkSize);
p->render();
Renderer::translate(-p->getPosition() * ChunkSize);
renderedChunks++;
}
return renderedChunks;
Expand Down
4 changes: 2 additions & 2 deletions src/client/worldclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class WorldClient : public World
void renderUpdate(const Vec3i& position);

// Render all chunks
int render(const Vec3i& position) const;
size_t render(const Vec3i& position) const;

private:
// Render distance
int m_renderDist;
int m_renderDist = 0;
// Render build list
std::pair<ChunkClient*, int> m_chunkRenderList[MaxChunkRenderCount];

Expand Down

0 comments on commit a58a5a4

Please sign in to comment.