-
Notifications
You must be signed in to change notification settings - Fork 12
Using 3D Shapes
Now that you have learned about TSGL's 3D aspects, let us put them to good use!
Linux/Mac/WSL/Cygwin users: Start off by creating a folder and name it "Tutorial12". Create a file inside of Tutorial12 and name it "shapes3D.cpp". Then, navigate to the cloned TSGL folder and into the genericMakefile folder. Copy the Makefile into Tutorial12 and change the "TARGET" line so that "program" is now "shapes3D".
All platforms: Follow the steps in the Building Programs page on how to compile and run the program (this is a single-file program).
Let us again draw a Cube on a Canvas using an outside function!
#include <tsgl.h>
using namespace tsgl;
void Shapes3D(Canvas& c) {
Cube *cube = new Cube(0, 0, 0, 100, 0, 0, 0, BLUE);
cube->setRotationPoint(0,0,50);
c.add(cb);
while (c.isOpen()) {
c.sleep();
cube->changeYawBy(1);
cube->changePitchBy(1);
}
delete cube;
}
int main() {
Canvas c(0, 0, 700, 700, "Hello World!", GRAY, nullptr, FRAME);
c.run(Shapes3D);
}
This creates a really cool animation, but next we will add more 3D Shapes and put them in a vector. From there we can animate every shape a little differently using its index in that vector.
#include <tsgl.h>
#include <vector>
using namespace tsgl;
void Shapes3D(Canvas& c) {
std::vector<Shape*> shapes3D;
Cube *cube = new Cube(0, 0, 0, 100, 0, 0, 20, BLUE);
cube->setIsOutlined(true);
cube->setOutlineColor(BLACK);
cube->setRotationPoint(0,0,50);
c.add(cube);
Cone *cone = new Cone(250, 150, 0, 75, 35, 25, 0, 0, MAGENTA);
cone->setIsOutlined(true);
cone->setOutlineColor(BLACK);
cone->setRotationPoint(0,0,0);
c.add(cone);
Prism *prism = new Prism(-200, -100, -50, 6, 100, 35, 20, 0, 20, CYAN);
prism->setIsOutlined(true);
prism->setOutlineColor(BLACK);
prism->setRotationPoint(0,0,0);
c.add(prism);
Pyramid *pyramid = new Pyramid(-150, 150, 50, 12, 100, 100, 15, 0, 15, LIME);
pyramid->setIsOutlined(true);
pyramid->setOutlineColor(BLACK);
pyramid->setRotationPoint(0,0,0);
c.add(pyramid);
shapes3D.push_back(cube);
shapes3D.push_back(cone);
shapes3D.push_back(prism);
shapes3D.push_back(pyramid);
while (c.isOpen()) {
for(unsigned i = 0; i < shapes3D.size(); i++) {
c.sleep();
shapes3D[i]->changeYawBy(i+1);
}
}
delete cube;
delete cone;
delete prism;
delete pyramid;
}
int main() {
Canvas c(0, 0, 700, 700, "Hello World!", GRAY, nullptr, FRAME);
c.run(Shapes3D);
}
Now you can see some more of TSGL's 3D shapes! Along with this each shape is rotating at a different speed based off of its index in the vector that they have been added to. Putting different shapes that you have created in a vector is one way that you can manage them among others. Finally, in this tutorial we will add Threads so that each one of your shapes is managed by a separate Thread.
#include <tsgl.h>
#include <vector>
using namespace tsgl;
void Shapes3D(Canvas& c) {
std::vector<Shape*> shapes3D;
Cube *cube = new Cube(0, 0, 0, 100, 0, 0, 20, BLUE);
cube->setIsOutlined(true);
cube->setOutlineColor(BLACK);
cube->setRotationPoint(0,0,50);
c.add(cube);
Cone *cone = new Cone(250, 150, 0, 75, 35, 25, 0, 0, MAGENTA);
cone->setIsOutlined(true);
cone->setOutlineColor(BLACK);
cone->setRotationPoint(0,0,0);
c.add(cone);
Prism *prism = new Prism(-200, -100, -50, 6, 100, 35, 20, 0, 20, CYAN);
prism->setIsOutlined(true);
prism->setOutlineColor(BLACK);
prism->setRotationPoint(0,0,0);
c.add(prism);
Pyramid *pyramid = new Pyramid(-150, 150, 50, 12, 100, 100, 15, 0, 15, LIME);
pyramid->setIsOutlined(true);
pyramid->setOutlineColor(BLACK);
pyramid->setRotationPoint(0,0,0);
c.add(pyramid);
shapes3D.push_back(cube);
shapes3D.push_back(cone);
shapes3D.push_back(prism);
shapes3D.push_back(pyramid);
while (c.isOpen()) {
#pragma omp parallel
{
#pragma omp for
for(unsigned i = 0; i < shapes3D.size(); i++) {
c.sleep();
shapes3D[i]->changeYawBy(i+1);
}
}
}
delete cube;
delete cone;
delete prism;
delete pyramid;
}
int main() {
omp_set_num_threads(4);
Canvas c(0, 0, 700, 700, "Hello World!", GRAY, nullptr, FRAME);
c.run(Shapes3D);
}
It's as easy as that! Just add three lines of code and you have created a separate Thread that controls each of your shapes. Along with this you can definitely see the speed up that this allows for your animation as you have 4 Threads controlling your shapes instead of only 1 Thread for all the shapes.
That concludes this tutorial!
Finally, there is a Camera object that TSGL provides which is talked about in Using Camera!
2D
- Using Background
- Using Canvas
- Using Shapes
- Using Text and Images
- Using Colors
- Animation Loops
- Using Functions
- Using Keyboard And Mouse
- Command-line arguments
- Bringing It All Together
3D