﻿ Examples > Composite scenes > Clock

Clock.

Let's create an object - a clock.

First, let's construct a face - a flat face stretched on a circle with the radius equal to 10:

SG_CIRCLE cirG;

cirG.center.x = cirG.center.y =  cirG.center.z = 0.0;

cirG.normal.x = cirG.normal.y = 0.0; cirG.normal.z = 1.0;

sgCCircle* cir = sgCreateCircle(cirG);

m_face = sgSurfaces::Face(*cir,NULL,0);

sgDeleteObject(cir);

sgGetScene()->AttachObject(m_face);

m_face->SetAttribute(SG_OA_COLOR,35);

m_face->SetAttribute(SG_OA_LINE_THICKNESS, 1);

SG_VECTOR trVec = {0.0, 0.0, -0.03};

m_face->InitTempMatrix()->Translate(trVec);

m_face->ApplyTempMatrix();

m_face->DestroyTempMatrix();

Then comes the torus edging with the face:

m_face_tor = sgCTorus::Create(10.0, 0.2,36,24);

sgGetScene()->AttachObject(m_face_tor);

m_face_tor->SetAttribute(SG_OA_COLOR,30);

m_face_tor->SetAttribute(SG_OA_LINE_THICKNESS, 1);

Further let's create a hairline which will be located in the place of the hour figures. It will be an object of extrusion with closing to a solid. The twelve o'clock hairline will be colored differently from the rest. The following function creates the extruded contour:

sgCContour*  CreateHairLine()

{

sgCObject*  objcts[4];

SG_ARC arcGeo;

SG_POINT arcP1;

SG_POINT arcP2;

SG_POINT arcP3;

arcP1.x = -0.15;  arcP1.y = 9.0;   arcP1.z = 0.0;

arcP2.x = 0.15;   arcP2.y = 9.0;   arcP2.z = 0.0;

arcP3.x = 0.0;    arcP3.y = 9.2;   arcP3.z = 0.0;

arcGeo.FromTreePoints(arcP1,arcP2,arcP3,false);

objcts[0] = sgCreateArc(arcGeo);

objcts[1] = sgCreateLine(0.15, 9.0, 0.0, 0.15, 8.0, 0.0);

arcP1.x = 0.15;  arcP1.y = 8.0;   arcP1.z = 0.0;

arcP2.x = -0.15;   arcP2.y = 8.0;   arcP2.z = 0.0;

arcP3.x = 0.0;    arcP3.y = 7.8;   arcP3.z = 0.0;

arcGeo.FromTreePoints(arcP1,arcP2,arcP3,false);

objcts[2] = sgCreateArc(arcGeo);

objcts[3] = sgCreateLine(-0.15, 8.0, 0.0, -0.15, 9.0, 0.0);

return sgCContour::CreateContour(objcts,4);

}

And now let's create a solid of extrusion from this contour:

sgCContour* tmpCnt = CreateHairLine();

SG_VECTOR extrVec = {0.0, 0.0, 0.2};

m_hairLines3D[0] = sgKinematic::Extrude((const sgC2DObject&)(*tmpCnt),NULL,0,extrVec,true);

sgDeleteObject(tmpCnt);

sgGetScene()->AttachObject(m_hairLines3D[0]);

m_hairLines3D[0]->SetAttribute(SG_OA_COLOR,12);

m_hairLines3D[0]->SetAttribute(SG_OA_LINE_THICKNESS, 1);

Let's multiply this solid of extrusion on all hour figures:

SG_POINT     rotAxeP = {0.0, 0.0, 0.0};

SG_VECTOR    rotAxeDir = {0.0, 0.0, 1.0};

for (int i=1;i<12;i++)

{

m_hairLines3D[i] = (sgCContour*)(m_hairLines3D[0]->Clone());

m_hairLines3D[i]->InitTempMatrix()->Rotate(rotAxeP,rotAxeDir,i*3.14159265/6.0);

m_hairLines3D[i]->ApplyTempMatrix();

m_hairLines3D[i]->DestroyTempMatrix();

sgGetScene()->AttachObject(m_hairLines3D[i]);

m_hairLines3D[i]->SetAttribute(SG_OA_COLOR,90);

}

Then let's create minute hairlines - just line segments:

m_hairLines2D[0] = sgCreateLine(0.0, 8.0, 0.0, 0.0, 9.0, 0.0);

sgGetScene()->AttachObject(m_hairLines2D[0]);

for (int i=1;i<60;i++)

{

m_hairLines2D[i] = (sgCContour*)(m_hairLines2D[0]->Clone());

m_hairLines2D[i]->InitTempMatrix()->Rotate(rotAxeP,rotAxeDir,i*3.14159265/30.0);

m_hairLines2D[i]->ApplyTempMatrix();

m_hairLines2D[i]->DestroyTempMatrix();

sgGetScene()->AttachObject(m_hairLines2D[i]);

m_hairLines2D[i]->SetAttribute(SG_OA_COLOR,0);

}

Let's put a sphere into the center of the clock where we will fix the hands:

m_clock_center = sgCreateSphere(0.2,36,36);

sgGetScene()->AttachObject(m_clock_center);

m_clock_center->SetAttribute(SG_OA_COLOR,3);

And finally we'll create three hands with various rotation angles:

The hour hand is a flat face with a hole:

sgCObject* CClockScene::CreateHourHand()

{

sgCObject*  ob_buff[4];

ob_buff[0] = sgCreateLine(0.0, -0.1, 0.0, 0.5, 0.5, 0.0);

ob_buff[1] = sgCreateLine(0.5, 0.5, 0.0, 0.0, 6.0, 0.0);

ob_buff[2] = sgCreateLine(0.0, 6.0, 0.0, -0.5, 0.5, 0.0);

ob_buff[3] = sgCreateLine(-0.5, 0.5, 0.0, 0.0, -0.1, 0.0);

sgC2DObject* cont1 = sgCContour::CreateContour(ob_buff,4);

ob_buff[0] = sgCreateLine(0.0, 0.6, 0.0, 0.2, 0.8, 0.0);

ob_buff[1] = sgCreateLine(0.2, 0.8, 0.0, 0.0, 5.0, 0.0);

ob_buff[2] = sgCreateLine(0.0, 5.0, 0.0, -0.2, 0.8, 0.0);

ob_buff[3] = sgCreateLine(-0.2, 0.8, 0.0, 0.0, 0.6, 0.0);

sgC2DObject* cont2 = sgCContour::CreateContour(ob_buff,4);

sgCObject* res = sgSurfaces::Face(*cont1,(const sgC2DObject**)(&cont2),1);

sgDeleteObject(cont1);

sgDeleteObject(cont2);

return res;

}

The minute hand is a flat face with a hole:

sgCObject* CClockScene::CreateMinuteHand()

{

sgCObject*  ob_buff[4];

ob_buff[0] = sgCreateLine(0.0, -0.1, 0.0, 0.5, 0.5, 0.0);

ob_buff[1] = sgCreateLine(0.5, 0.5, 0.0, 0.0, 9.0, 0.0);

ob_buff[2] = sgCreateLine(0.0, 9.0, 0.0, -0.5, 0.5, 0.0);

ob_buff[3] = sgCreateLine(-0.5, 0.5, 0.0, 0.0, -0.1, 0.0);

sgC2DObject* cont1 = sgCContour::CreateContour(ob_buff,4);

ob_buff[0] = sgCreateLine(0.0, 1.6, 0.0, 0.2, 1.8, 0.0);

ob_buff[1] = sgCreateLine(0.2, 1.8, 0.0, 0.0, 8.0, 0.0);

ob_buff[2] = sgCreateLine(0.0, 8.0, 0.0, -0.2, 1.8, 0.0);

ob_buff[3] = sgCreateLine(-0.2, 1.8, 0.0, 0.0, 1.6, 0.0);

sgC2DObject* cont2 = sgCContour::CreateContour(ob_buff,4);

sgCObject* res = sgSurfaces::Face(*cont1,(const sgC2DObject**)(&cont2),1);

sgDeleteObject(cont1);

sgDeleteObject(cont2);

return res;

}

The second hand is a line segment:

sgCObject* CClockScene::CreateSecondHand()

{

return sgCreateLine(0.0, 0.0, 0.0, 0.0, 9.0, 0.0);

}