Skip to content

Steph04m/3D-Cube-Simulation-with-SFML

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 

Repository files navigation

3D Cube Simulation with SFML

This project consists in an implementation in C++ of a Colored 3D Cube using the SFML library.

Characteristics of the Project

The Language: Why C++

The main reasons that I chose C++ over other programming languages are two:

  • Absolute OOP (@BoredDam). I could have used C and GTK as a graphics library since it offers some good abstractions to manage the window, the buttons and etc., but since C++ offers great and fast Data Structures like Vector and it was actually easier to write, I choose C++. Also, before implementing it in C++, I've used Processing to give me an idea on what I needed to write to make the 3D Cube and since Processing is OOP I thought it would be easier to translate some of the code to another OOP languange like C++.
  • The SFML library. It's a quite easy library to learn with a lot of useful classes ready to use, and while I'm far from being an expert, I was able to learn the fundamentals in few days. That said, I've encountered a big problem while working on this project.

The Problem: SFML is a 2D library

Exactly, it's a 2D library. Creating a 3D Cube and making it rotatable was the real challenge. How I managed to do it:

  • The structure of the Cube was created using Vector and some alterations of it included in the SFML library, like Vector3f to create the 24 vertexes. To store them I've used the VertexArray class (included in the SFML library) and divided the vertexes to create the Cube's faces. It wasn't easy but this was by far the easiest part.
  • To rotate the Cube I created multiple methods, some of them to make the rotation itself and some others to display the Cube correctly.
  • Since SFML is a 2D library, it lacks a Z-buffer, so simulating depth was a big challenge. To solve the problem I've used the Painter's Algorithm.

Simulating Depth: How the Painter's Algorithm works and why I chose it

I chose the Painter's Algorithm over other algorithms for one main reason:

Simplicity. The Painter's Algorithm's simplicity was a big reason. The algorithm works on a polygon-by-polygon basis. It starts by sorting the polygons by their depth, from the farthest one to the nearest one and after sorting them it draws them.

This algorithm has some isssues and it's not fast since it renders every object in the scene, even the non-visible ones, but for a 3D Cube it gets the job done.

Features and Commands

The program features an FPS counter in the bottom left corner and some basic commands to rotate the Cube.

  • WASD or the Arrow-keys to rotate the Cube.
  • ESC to close the program.

There's also a Framerate Limiter set to 144 FPS max. You can disable it by commenting the line in the RenderSim.cpp file (line 71).

How to Compile and Run the Simulation

Installing SFML library

(Fedora)

sudo dnf install SFML SFML-devel

(Debian, Ubuntu, Mint)

sudo apt-get install libsfml-dev

Compiling and Running the Source Code

g++ main.cpp RenderSim.cpp Cubo.cpp -lsfml-graphics -lsfml-window -lsfml-system -o main

Keep in mind that you have to keep all .cpp and .hpp files and the folder files_used in the same directory.

./main

You can use -O optimization flags to get better performances, but unless you have a 25 years old PC, you won't need them.

Planned Changes

  • Instead of using squares to render the Cube, I also plan to create a version with triangles.
  • No other changes are planned.

About

Implementation in C++ of a Colored 3D Cube using the SFML library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages