Skip to content

Commit

Permalink
CHUNK RENDERING!!!!!!!!!!
Browse files Browse the repository at this point in the history
Without block textures 2333
  • Loading branch information
bridgekat committed Aug 3, 2016
1 parent 29c42ba commit 6934313
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 144 deletions.
100 changes: 95 additions & 5 deletions src/client/chunkrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,105 @@ void ChunkRenderer::buildVertexArray()
}
else
{
Vec3i::for_range(0, ChunkSize,[&](const Vec3i& pos)
Vec3i::for_range(0, ChunkSize, [&](const Vec3i& pos)
{
Vec3i worldpos = m_chunk.getPos() + pos;
if (pos.x == 0 || pos.x == ChunkSize - 1||
pos.x == 0 || pos.x == ChunkSize - 1||
pos.x == 0 || pos.x == ChunkSize - 1)
//Vec3i worldpos = m_chunk.getPos() + pos;

BlockData curr = m_chunk.getBlock(pos);
BlockData neighbors[6] =
{
pos.x == ChunkSize - 1 ? BlockData(0, 15, 0) : m_chunk.getBlock(Vec3i(pos.x + 1, pos.y, pos.z)),
pos.x == 0 ? BlockData(0, 15, 0) : m_chunk.getBlock(Vec3i(pos.x - 1, pos.y, pos.z)),
pos.y == ChunkSize - 1 ? BlockData(0, 15, 0) : m_chunk.getBlock(Vec3i(pos.x, pos.y + 1, pos.z)),
pos.y == 0 ? BlockData(0, 15, 0) : m_chunk.getBlock(Vec3i(pos.x, pos.y - 1, pos.z)),
pos.z == ChunkSize - 1 ? BlockData(0, 15, 0) : m_chunk.getBlock(Vec3i(pos.x, pos.y, pos.z + 1)),
pos.z == 0 ? BlockData(0, 15, 0) : m_chunk.getBlock(Vec3i(pos.x, pos.y, pos.z - 1)),
};

// Right
if (adjacentTest(curr, neighbors[0]))
{
va.setColor({ 0.5f, 0.5f, 0.5f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f });
va.setTexture({ 0.0f, 1.0f });
va.addVertex({ pos.x + 0.5f, pos.y - 0.5f, pos.z + 0.5f });
va.setTexture({ 1.0f, 1.0f });
va.addVertex({ pos.x + 0.5f, pos.y - 0.5f, pos.z - 0.5f });
va.setTexture({ 1.0f, 0.0f });
va.addVertex({ pos.x + 0.5f, pos.y + 0.5f, pos.z - 0.5f });
}

// Left
if (adjacentTest(curr, neighbors[1]))
{
va.setColor({ 0.5f, 0.5f, 0.5f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ pos.x - 0.5f, pos.y + 0.5f, pos.z - 0.5f });
va.setTexture({ 0.0f, 1.0f });
va.addVertex({ pos.x - 0.5f, pos.y - 0.5f, pos.z - 0.5f });
va.setTexture({ 1.0f, 1.0f });
va.addVertex({ pos.x - 0.5f, pos.y - 0.5f, pos.z + 0.5f });
va.setTexture({ 1.0f, 0.0f });
va.addVertex({ pos.x - 0.5f, pos.y + 0.5f, pos.z + 0.5f });
}

// Top
if (adjacentTest(curr, neighbors[2]))
{
va.setColor({ 1.0f, 1.0f, 1.0f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ pos.x - 0.5f, pos.y + 0.5f, pos.z - 0.5f });
va.setTexture({ 0.0f, 1.0f });
va.addVertex({ pos.x - 0.5f, pos.y + 0.5f, pos.z + 0.5f });
va.setTexture({ 1.0f, 1.0f });
va.addVertex({ pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f });
va.setTexture({ 1.0f, 0.0f });
va.addVertex({ pos.x + 0.5f, pos.y + 0.5f, pos.z - 0.5f });
}

// Bottom
if (adjacentTest(curr, neighbors[3]))
{
va.setColor({ 1.0f, 1.0f, 1.0f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ pos.x - 0.5f, pos.y - 0.5f, pos.z + 0.5f });
va.setTexture({ 0.0f, 1.0f });
va.addVertex({ pos.x - 0.5f, pos.y - 0.5f, pos.z - 0.5f });
va.setTexture({ 1.0f, 1.0f });
va.addVertex({ pos.x + 0.5f, pos.y - 0.5f, pos.z - 0.5f });
va.setTexture({ 1.0f, 0.0f });
va.addVertex({ pos.x + 0.5f, pos.y - 0.5f, pos.z + 0.5f });
}

// Front
if (adjacentTest(curr, neighbors[4]))
{
va.setColor({ 0.7f, 0.7f, 0.7f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ pos.x - 0.5f, pos.y + 0.5f, pos.z + 0.5f });
va.setTexture({ 0.0f, 1.0f });
va.addVertex({ pos.x - 0.5f, pos.y - 0.5f, pos.z + 0.5f });
va.setTexture({ 1.0f, 1.0f });
va.addVertex({ pos.x + 0.5f, pos.y - 0.5f, pos.z + 0.5f });
va.setTexture({ 1.0f, 0.0f });
va.addVertex({ pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f });
}

// Back
if (adjacentTest(curr, neighbors[5]))
{
va.setColor({ 0.7f, 0.7f, 0.7f });
va.setTexture({ 0.0f, 0.0f });
va.addVertex({ pos.x + 0.5f, pos.y + 0.5f, pos.z - 0.5f });
va.setTexture({ 0.0f, 1.0f });
va.addVertex({ pos.x + 0.5f, pos.y - 0.5f, pos.z - 0.5f });
va.setTexture({ 1.0f, 1.0f });
va.addVertex({ pos.x - 0.5f, pos.y - 0.5f, pos.z - 0.5f });
va.setTexture({ 1.0f, 0.0f });
va.addVertex({ pos.x - 0.5f, pos.y + 0.5f, pos.z - 0.5f });
}
});
}
m_buffer = VertexBuffer(va);
}
7 changes: 7 additions & 0 deletions src/client/chunkrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class ChunkRenderer
static VertexArray va;
// Merge face rendering
static bool mergeFace;

bool adjacentTest(BlockData a, BlockData b) const
{
if (a.getID() == 0) return false;
if (m_world.getBlockTypes().getType(b.getID()).isOpaque()) return false;
return true;
}
};

#endif // !CHUNKRENDERER_H_
96 changes: 19 additions & 77 deletions src/client/gameview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@
#include <logger.h>
#include "texture.h"

GameView::GameView(UI::Core::Window* win) :UI::Controls::GLContext()
GameView::GameView(UI::Core::Window* win) :UI::Controls::GLContext(),
m_world("", m_plugins, m_blocks), m_cpa(8), m_worldLoader(m_world, m_cpa), m_chunk(Vec3i(0, 0, 0)), m_chunkRenderer(m_world, m_chunk)
{
PluginAPI::Blocks = &m_blocks;
PluginAPI::Plugins = &m_plugins;
m_plugins.loadPlugins("./");

keyFunc.connect([this](int scancode, UI::Core::ButtonAction)
{
onKey(scancode);
});
win->renderdelegate.push_back([this, win]() { init(win); });

//m_worldLoader.setLoadRange(4);
//m_worldLoader.sortChunkLoadUnloadList(Vec3i(0, 0, 0));

// Test chunk build
ChunkLoader(m_chunk).build(15);
BlockData block = m_chunk.getBlock(Vec3i(0, 0, 0));
infostream << "Block at (0,0,0) = {ID: " << block.getID() << ", brightness: " << block.getBrightness() << ", state: " << block.getState() << "}";
debugstream << "Full information:";
m_blocks.showInfo(block.getID());
}

MainWindow::MainWindow(int width, int height, const string& title) : UI::Core::Window(title, width, height, 200, 200)
Expand Down Expand Up @@ -63,7 +78,6 @@ void GameView::init(UI::Core::Window*)
{
Renderer::init();

// Example for Texture
texture = Texture::loadTextureRGBA("./Res/test.png");
UI::GameUtils::setSwapInterval(0);
VertexArray cubeArray(3000000, VertexFormat(2, 3, 0, 3));
Expand All @@ -73,80 +87,8 @@ void GameView::init(UI::Core::Window*)
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

for (int x = -25; x < 25; x++)
{
for (int y = -25; y < 25; y++)
{
for (int z = -25; z < 25; z++)
{
// Front
cubeArray.setColor({ 0.7f, 0.7f, 0.7f });
cubeArray.setTexture({ 0.0f, 0.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f + 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 0.0f, 1.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f - 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 1.0f, 1.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f - 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 1.0f, 0.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f + 0.5f, z*2.0f + 0.5f });
// Back
cubeArray.setColor({ 0.7f, 0.7f, 0.7f });
cubeArray.setTexture({ 0.0f, 0.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f + 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 0.0f, 1.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f - 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 1.0f, 1.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f - 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 1.0f, 0.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f + 0.5f, z*2.0f - 0.5f });
// Top
cubeArray.setColor({ 1.0f, 1.0f, 1.0f });
cubeArray.setTexture({ 0.0f, 0.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f + 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 0.0f, 1.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f + 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 1.0f, 1.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f + 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 1.0f, 0.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f + 0.5f, z*2.0f - 0.5f });
// Bottom
cubeArray.setColor({ 1.0f, 1.0f, 1.0f });
cubeArray.setTexture({ 0.0f, 0.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f - 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 0.0f, 1.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f - 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 1.0f, 1.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f - 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 1.0f, 0.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f - 0.5f, z*2.0f + 0.5f });
// Right
cubeArray.setColor({ 0.5f, 0.5f, 0.5f });
cubeArray.setTexture({ 0.0f, 0.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f + 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 0.0f, 1.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f - 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 1.0f, 1.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f - 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 1.0f, 0.0f });
cubeArray.addVertex({ x*2.0f + 0.5f, y*2.0f + 0.5f, z*2.0f - 0.5f });
// Left
cubeArray.setColor({ 0.5f, 0.5f, 0.5f });
cubeArray.setTexture({ 0.0f, 0.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f + 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 0.0f, 1.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f - 0.5f, z*2.0f - 0.5f });
cubeArray.setTexture({ 1.0f, 1.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f - 0.5f, z*2.0f + 0.5f });
cubeArray.setTexture({ 1.0f, 0.0f });
cubeArray.addVertex({ x*2.0f - 0.5f, y*2.0f + 0.5f, z*2.0f + 0.5f });
}
}
if ((x + 26)*2 % 20 == 0) infostream << "Building vertax array: " << (x + 26)*2 << "%";
}

infostream << "Generating VBO...";
cube = VertexBuffer(cubeArray);
infostream << "Complete!";
// Test chunk render
m_chunkRenderer.buildVertexArray();

onRenderF = [this]()
{
Expand All @@ -169,7 +111,7 @@ void GameView::doRender()
Renderer::rotate(trans.x, Vec3f(1.0f, 0.0f, 0.0f));
Renderer::rotate(trans.y, Vec3f(0.0f, 1.0f, 0.0f));

cube.render();
m_chunkRenderer.render();

glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
Expand Down
34 changes: 33 additions & 1 deletion src/client/gameview.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,51 @@
#include "renderer.h"
#include "pages.h"

#include <world.h>
#include <logger.h>
#include <pluginapi.h>
#include <blockmanager.h>
#include <worldmanager.h>
#include <pluginmanager.h>
#include "../server/worldloader.h"
#include "chunkrenderer.h"

class GameView : public UI::Controls::GLContext
{
public:
GameView(UI::Core::Window*);
~GameView()
{
m_plugins.unloadPlugins();
}
void doRender();
void onResize(size_t w, size_t h) override;
void init(UI::Core::Window* win);
void onKey(int key);

private:
int windowWidth = 852, windowHeight = 480;
Vec3f trans = {0.0f, 0.0f, -100.0f};
Vec3f transSpeed = {0.0f, 0.0f, 0.0f};
VertexBuffer cube;

// Blocks
BlockManager m_blocks;
// Loaded plugins
PluginManager m_plugins;
/*
// Loaded worlds
WorldManager m_worlds;
*/
// Current world
World m_world;
// CPA
ChunkPointerArray m_cpa;
// Loading test
WorldLoader m_worldLoader;
// Chunk test
Chunk m_chunk;
// Rendering test
ChunkRenderer m_chunkRenderer;
};

class MainWindow : public UI::Core::Window
Expand Down
19 changes: 1 addition & 18 deletions src/client/neworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
#include <pluginapi.h>

Application::Application(int width, int height, const string& title, const string& path) :
m_width(width), m_height(height), m_title(title), m_world("", m_plugins), m_cpa(8), m_worldLoader(m_world, m_cpa)
m_width(width), m_height(height), m_title(title)
{
PluginAPI::Blocks = &m_blocks;
PluginAPI::Plugins = &m_plugins;
m_plugins.loadPlugins(path);
}

void Application::beforeLaunch()
Expand All @@ -46,7 +43,6 @@ void Application::beforeLaunch()
#undef HOOK

Texture::init();

UI::Logger::init("./Logs");
UI::Font::service.addSearchPaths({ "./Res/Fonts" });
UI::Globalization::Service::getInstance().setBasePath("./Res/Langs/");
Expand All @@ -58,18 +54,6 @@ void Application::beforeLaunch()
UI::Theme::SystemTheme.ControlOnPressBrush = UIMakeSolidColorBrush(UI::Base::Color(0.2 * 0.8, 0.2 * 0.8, 0.2 * 0.8, 0.9));
UI::Theme::SystemTheme.DefaultFont = UI::Font::service.getRenderer("SourceHanSansCN-Normal", 17, UI::Base::Color(1.0, 1.0, 1.0, 1.0));
//std::thread serverThread(networkThread);

//m_worldLoader.setLoadRange(4);
//m_worldLoader.sortChunkLoadUnloadList(Vec3i(0, 0, 0));

// Testing chunk
Chunk chunk(Vec3i(0, 0, 0));
ChunkLoader loader(chunk);
loader.build(15);
BlockData block = chunk.getBlock(Vec3i(0, 0, 0));
infostream << "Block at (0,0,0) = {ID: " << block.getID() << ", brightness: " << block.getBrightness() << ", state: " << block.getState() << "}";
debugstream << "Full information:";
m_blocks.showInfo(block.getID());
}

void Application::afterLaunch()
Expand All @@ -84,7 +68,6 @@ void Application::onTerminate()
infostream << "Terminating...";
UI::Logger::service.dump();
Texture::uninit();
m_plugins.unloadPlugins();
//serverThread.join();
disconnect();
}
27 changes: 3 additions & 24 deletions src/client/neworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,11 @@

#include <common.h>
#include "network.h"
#include <thread>
#include <string>
using std::string;
#include <uilib.h>
#include <world.h>
#include <logger.h>
#include <pluginapi.h>
#include <blockmanager.h>
#include <worldmanager.h>
#include <pluginmanager.h>
#include "../server/worldloader.h"

class Application :public UI::Core::Application
class Application : public UI::Core::Application
{
public:
Application(int width, int height, const string& title, const string& path);
Expand All @@ -43,21 +37,6 @@ class Application :public UI::Core::Application
private:
int m_width, m_height;
string m_title;

// Blocks
BlockManager m_blocks;
// Loaded plugins
PluginManager m_plugins;
/*
// Loaded worlds
WorldManager m_worlds;
*/
// Current world
World m_world;
// CPA
ChunkPointerArray m_cpa;
// Loading test
WorldLoader m_worldLoader;
};

#endif // !NEWORLD_H_
Loading

2 comments on commit 6934313

@jacky8399
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

咋不在BlockType裏加一個bool shouldSideBeRendered(int side)來判定是否應該cullface

@bridgekat
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacky8399 adjacentTest是根据BlockType的属性进行测试的

Please sign in to comment.