GithubHelp home page GithubHelp logo

adivonslav / glblocks Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 2.92 MB

A voxel engine clone of Minecraft

License: Apache License 2.0

C++ 77.84% C 1.09% Batchfile 0.04% Lua 3.30% GLSL 3.71% Makefile 13.99% Shell 0.04%
game opengl voxel voxel-engine 3d-graphics cpp

glblocks's Introduction

glBlocks

This is a voxel engine I've been working on to advance my knowledge in OpenGL. For this project I used OpenGL 4.6 Core-profile and C++20 (so I could take advantage of the new std::filesystem first and foremost).

Terrain generation

Terrain is generated on the fly and stored as chunk files on disk, theoretically allowing for infinite terrain. Due to type limitations, for now chunk positions cannot pass ±32767 in X or Z position (the max amount of chunks that can be generated and stored is then equal to roughly 16 million), however that can probably be changed by sacrificing some memory usage. Obviously, another limitation is disk space for the chunks, one chunk equals 33KB. As mentioned earlier, the terrain height map is calculcated using 2D Perlin noise and then altered with a custom seed implementation (albeit a primitive one) in order to achieve a level of randomness between generation runs. The chunk files are encoded with this seed value, alongside the chunk structure information, so using the same files it is possible to continue generating seed-appropriate terrain. Chunks are loaded and unloaded into/from memory based on the distance between the chunks and the camera and are also rendered depending on the distance. Using std::future I was able to port all of the logic relating to building chunk vertex data and Perlin generation into separate thread calls, thereby unblocking the main thread.

Lighting

I implemented lighting for the voxels using the Phong lighting method. Ambient lighting is added first, the strength of which depends on the time of day (i.e. the position of the sun/moon). Then, diffuse lighting is calculated based on the light vector and the surface normal of each voxel face. Normals are not passed via buffers to the vertex shader (in order to conserve memory) but are rather calculated by passing information on what face the vertex comprises (if the vertex is on the top face of the voxel, then the corresponding normal vector is just a vector with a Y value of 1). Finally, specular lighting is calculated to simulate reflection of diffuse light based on how the camera views the voxel surface.

Planned features

  • Breaking and placing of blocks
  • Day/night cycle
  • Multithreaded chunk generation and loading
  • Chunk persistence between application runs (stored as files)
  • Fog and lighting effects
  • 3D Perlin noise for ores

The libraries that I used are:

  • GLAD for loading the necessary OpenGL functions
  • stb_image for image loading
  • GLM for mathematical types and operations
  • Dear ImGui for UI overlay elements

Controls

  • WASD - Movement
  • Right click (Hold) - Pan camera
  • Left shift (Hold) - Accelerate movement
  • Left ctrl (Hold) - Downward movement
  • ESC - Open dashboard

Build instructions

Windows

Simply run 'genproj.bat' which will generate the necessary project files for Visual Studio 2022 using Premake5.

Linux

To compile on Linux, you need make, g++ and GLFW installed

For Ubuntu/Debian, run this

sudo apt install make g++ libglfw3 libglfw3-devel

For RHEL-based distros, run this

sudo dnf install make g++ glfw glfw-devel

To compile, simply run the following

make config=release # Release build
make config=debug # Debug build

The binaries will be located in bin/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.