In this recipe, we will learn how to draw the following 3D geometric shapes:
Include the necessary header to draw in OpenGL using Cinder commands and statements. Add the following code to the top of your source file:
#include "cinder/gl/gl.h" #include "cinder/Camera.h" using namespace ci;
We will create several geometric primitives using Cinder's methods for drawing in 3D.
Vec3f mCubePos, mCubeSize; Vec3f mSphereCenter; float mSphereRadius; Vec3f mLineBegin, mLineEnd; Vec3f mTorusPos; float mTorusOuterRadius, mTorusInnerRadius; Vec3f mCylinderPos; float mCylinderBaseRadius, mCylinderTopRadius, mCylinderHeight;
setup
method:mCubePos = Vec3f( 100.0f, 300.0f, 100.0f ); mCubeSize = Vec3f( 100.0f, 100.0f, 100.0f ); mSphereCenter = Vec3f( 500, 250, 0.0f ); mSphereRadius = 100.0f; mLineBegin = Vec3f( 200, 0, 200 ); mLineEnd = Vec3f( 500, 500, -200 ); mTorusPos = Vec3f( 300.0f, 100.0f, 0.0f ); mTorusOuterRadius = 100.0f; mTorusInnerRadius = 20.0f; mCylinderPos = Vec3f( 500.0f, 0.0f, -200.0f ); mCylinderBaseRadius = 50.0f; mCylinderTopRadius = 80.0f; mCylinderHeight = 100.0f;
ci::CameraPersp
object:CameraPerspmCamera;
setup
method:mCamera = CameraPersp( getWindowWidth(), getWindowHeight(), 60.0f );
update
method, we will make the camera rotate around our scene. Add the following code in the update
method:Vec2f windowCenter = getWindowCenter(); floatcameraAngle = getElapsedSeconds(); floatcameraDist = 450.0f; float x = sinf( cameraAngle ) * cameraDist + windowCenter.x; float z = cosf( cameraAngle ) * cameraDist; mCamera.setEyePoint( Vec3f( x, windowCenter.y, z ) ); mCamera.lookAt( Vec3f( windowCenter.x, windowCenter.y, 0.0f ) );
draw
method, we will clear the background with black and use mCamera
to define the window's matrices. We will also enable OpenGL to read and write to the depth buffers. Add the following code in the draw
method:gl::clear( Color::black() ); gl::setMatrices( mCamera ); gl::enableDepthRead(); gl::enableDepthWrite();
gl::color( Color::white() ); gl::drawCube( mCubePos, mCubeSize ); gl::color( Color::black() ); gl::drawStrokedCube( mCubePos, mCubeSize );
mSphereCenter
and mSphereRadius
as the sphere's position and radius, and the number of segments as 30
.gl::color( Color::white() ); gl::drawSphere( mSphereCenter, mSphereRadius, 30 );
mLineBegin
and ends at mLineEnd
:gl::drawLine( mLineBegin, mLineEnd );
Torus
at the coordinates of the origin [0,0]
. So, we will have to translate it to the desired position at mTorusPos
. We will be using mTorusOuterRadius
and mTorusInnerRadius
to define the shape's inner and outer sizes:gl::pushMatrices(); gl::translate( mTorusPos ); gl::drawTorus( mTorusOutterRadius, mTorusInnerRadius ); gl::popMatrices();
[0,0]
, so we will have to translate it to the position defined in mCylinderPosition
. We will also be using mCylinderBaseRadius
and mCylinderTopRadius
, to set the cylinder's bottom and top sizes and mCylinderHeight
, to set its height:gl::pushMatrices(); gl::translate( mCylinderPos ); gl::drawCylinder( mCylinderBaseRadius, mCylinderTopRadius, mCylinderHeight ); gl::popMatrices();
3.145.125.205