Exploring other Cinder 3D primitives

Now let's try out the different 3D primitives that Cinder supplies it's own functions for. We just tried to draw a different kind of cube, so let's continue with something equally classic, a sphere. Replace the gl::drawColorCube() function call with the following line:

gl::drawSphere( Vec3f::zero(), 100 );

The first parameter of this function defines the center of the sphere and the second defines the radius. There is a third (optional) parameter int that controls the number of segments the sphere is made of. The default value is 12, but you might want to change it to a higher value to increase the smoothness of the sphere.

Compile and run the project. You should see an image similar to the following screenshot:

Exploring other Cinder 3D primitives

The problem here is that it does not look quite 3D. As you might have already guessed, the problem is that there is no light. To add light to the scene, add the following lines in the setup() method implementation:

glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );

These are OpenGL functions that are responsible for turning the light on. The first one enables light as such, the second is turning on the first light or GL_LIGHT0. There are ways of positioning and changing it's parameters, but we won't dig into this as it is outside the scope of this book.

Compile and run the project to see the sphere with lights turned on. You should get a result that is similar to the following screenshot:

Exploring other Cinder 3D primitives

Let's try to draw something else instead of a sphere. How about a cylinder? Replace the drawSphere() function call with the following line:

gl::drawCylinder(50, 50, 100);

This will draw an open-ended cylinder on the screen. The first parameter defines the width of the cylinder base, the second parameter defines the width of the cylinder top. The third parameter defines the cylinder height.

After we compile and run the application, you will notice that the cylinder is not rotating around it's center like the cube or sphere we drew before. That is because of the way it is drawn – from the bottom up instead of both – up and down. To change that, we can draw another cylinder, with a negative height or change the translate() values. For the sake of simplicity, let's draw another cylinder. Replace the cylinder drawing code to this:

gl::drawCylinder(50, 50, 50);
gl::drawCylinder(50, 50, -50);

We can see the same cylinder but now it rotates around its center. Another thing that we might want to add are the closed ends of the cylinder. We have to use 2D shapes again to accomplish this task. Add the following lines of code directly after the drawCylinder() function calls:

gl::rotate( Vec3f(-90,0,0) );
gl::translate( Vec3f(0,0,50) );
gl::drawSolidCircle(Vec2f(0,0), 50, 12);
gl::translate( Vec3f(0,0,-50) );
gl::rotate( Vec3f(180,0,0) );
gl::translate( Vec3f(0,0,50) );

gl::drawSolidCircle(Vec2f(0,0), 50, 12);
Not too easy, right? There is no drawSolidCircle() function that would draw a circle based on 3D coordinates yet, but this is a kind of shorthand anyway so we don't have to write pure OpenGL. What these lines of code do is that they move the scene or canvas in appropriate position and rotation relative to the already drawn cylinder to draw the top and the bottom of it.

Let's change the code a bit, so we see another thing that is possible with the drawCylinder() function that is drawing a cone or even a pyramid:

gl::drawCylinder(50, 0, 50, 4);
gl::drawCylinder(50, 0, -50, 4);

Delete or comment out the drawSolidCircle() part and change the top values of the drawCylinder() functions to 0 and add a fourth value (and set it to 4 or more if you want to draw a cone) that represents the slice count of the cylinder.

After compiling and running the application, a pyramid is not quite what we see. Let's call it a diamond to describe the shape more correctly. In the following screenshot you can see an image strip with the gl::drawCylinder() adjustments we just did:

Exploring other Cinder 3D primitives

Another basic shape worth introducing is the torus. Cinder has a built-in function for drawing this and this is how the function looks:

gl::drawTorus(100, 20);

Add the preceding line of code after the cylinder drawing function calls. The first parameter defines the outer radius (the circle responsible for the base shape of the torus), the second defines the inner radius or the radius if the circle that is used for creating the volume of the torus.

To make things look even more straightedged, let's add the third and fourth parameter that are responsible for the longitude and latitude segment count of the torus:

gl::drawTorus(100, 20, 8, 8);

If we compile and run the application now, we should see an image similar to one of these:

Exploring other Cinder 3D primitives
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.84.169