
Опытный
 
Профиль
Группа: Участник
Сообщений: 379
Регистрация: 9.11.2005
Репутация: 1 Всего: 31
|
Написано кривовато, но вот... | Код | #include<math.h> #include<windows.h> #include<GL\gl.h> #include<GL\glu.h> #include<GL\glaux.h> #pragma comment(lib,"opengl32.lib") #pragma comment(lib,"glu32.lib") #pragma comment(lib,"glaux.lib") const double basl=0.4;/////////////////////// const double basw=0.4;// const double bash=0.1;// const double bl=0.2;//ñòóïíè const double bw=0.2;// const double bh=0.05;/////////////////////// const double basbl=1.5;////////////////////// const double basbw=0.1;// const double basbh=0.4;//íîãè const double bassl=1.5;// const double bassw=0.1;// const double bassh=0.4;/////////////////////// const double bodywidth=0.9;//////////////////// const double bodylength=1;// const double bodysmallheight=1.5;//òóëîâèùå const double bodybigheight=2;//////////////////// const double hl=0.5;// const double hw=0.5;// const double hh=0.5;//ãîëîâà const double hsl=1;// class CLocation { private: double x,y,z;//êîîðäèíàòû public: CLocation(double ix,double iy,double iz):x(ix),y(iy),z(iz)//êîíñòðóêòîð {} double GetX(){return x;}//âîçâðàùàåò êîîðäèíàòó x double GetY(){return y;}//âîçâðàùàåò êîîðäèíàòó y double GetZ(){return z;}//âîçâðàùàåò êîîðäèíàòó z void SetX(double ix){x=ix;}//óñòàíàâëèâàåò çíà÷åíèå êîîðäèíàòû x void SetY(double iy){y=iy;}//óñòàíàâëèâàåò çíà÷åíèå êîîðäèíàòû y void SetZ(double iz){z=iz;}//óñòàíàâëèâàåò çíà÷åíèå êîîðäèíàòû z virtual void Draw()=0;//÷èñòàÿ âèðòóàëüíàÿ ôóíêöèÿ ~CLocation(){} }; ////////////////////////////////////////////////////////////////////////////////////////////////////// class CBox:public CLocation { private: double length,width,height,alpha; public: CBox(double ix,double iy,double iz,double il,double iw,double ih,double ia): CLocation(ix,iy,iz),length(il),width(iw),height(ih),alpha(ia) {} double GetLength(){return length;}//âîçâðàùàåò äëèíó double GetWidth(){return width;}//âîçâðàùàåò øèðèíó double GetHeight(){return height;}//âîçâðàùàåò âûñîòó double GetAlpha(){return alpha;}//âîçâðàùàåò óãîë void SetAlpha(double ia){alpha=ia;}//óñòàíàâëèâàåò óãîë void Draw();//ðèñóåò ïàðàëëåëèïèïåä ~CBox(){} }; void CBox::Draw() { glPushMatrix();//ñîõðàíåíèå ìàòðèöû glColor3d(1,1,1); glTranslated(GetX(),GetY(),GetZ());//ïåðåíîñ ñèñòåìû êîîðäèíàò glRotated(alpha,0,1,0);//ïîâîðîò ñèñòåìû êîîðäèíàò auxSolidBox(length,width,height); glPopMatrix();//âîññòàíîâëåíèå } /////////////////////////////////////////////////////////////////////////////////// class CFoot:public CLocation { private: CBox basis,b1,b2,b3,b4; public: CFoot(double ix,double iy,double iz):CLocation(ix,iy,iz), basis(0,0,0,basl,basw,bash,0),b1((basl+bl)/2,0,-(bash-bh)/2,bl,bw,bh,0), b2(0,(basw+bw)/2,-(bash-bh)/2,bl,bw,bh,0),b3(-(basl+bl)/2,0,0-(bash-bh)/2,bl,bw,bh,0), b4(0,-(basw+bw)/2,-(bash-bh)/2,bl,bw,bh,0) {} void Draw(); ~CFoot(){} }; void CFoot::Draw() { basis.Draw(); b1.Draw(); b2.Draw(); b3.Draw(); b4.Draw(); } //////////////////////////////////////////////////////////////////////////////////////////// class CLeg:public CLocation { private: double alpha; CBox bup,bdown; public: CLeg(double ix,double iy,double iz,double ia):CLocation(ix,iy,iz), bup(0,0,0,basbl,basbw,basbh,ia),bdown(0,0,0,bassl,bassw,bassh,90),alpha(ia) {} double GetAlpha(){return alpha;} void SetAlpha(double ia) {alpha=ia; bup.SetAlpha(ia); } void Draw(); ~CLeg(){} }; void CLeg::Draw() { glPushMatrix(); glTranslated(GetX()+basbl*cos(3.14*alpha/180)/2,GetY(),GetZ()-basbl*sin(3.14*alpha/180)/2); bup.Draw(); glTranslated(basbl*cos(3.14*alpha/180)/2,0,-basbl*sin(3.14*alpha/180)/2-basbl/2); bdown.Draw(); glPopMatrix(); } /////////////////////////////////////////////////////////////////////////////////////////////////// class CFullLeg:public CLocation { private: CLeg l; CFoot f; public: CFullLeg(double ix,double iy,double iz,double ia):CLocation(ix,iy,iz), l(ix,iy,iz,ia),f(ix,iy,iz) {} void SetAlphaTest(double ia){l.SetAlpha(ia);} double GetAlphaTest(){return l.GetAlpha();} void Draw(); ~CFullLeg(){} }; void CFullLeg::Draw() { glPushMatrix(); l.Draw(); glTranslated(l.GetX()+basbl*cos(3.14*l.GetAlpha()/180),l.GetY(),l.GetZ()-basbl*sin(3.14*l.GetAlpha()/180)-basbl); f.Draw(); glPopMatrix(); } ///////////////////////////////////////////////////////////////////////////////////////////// class CBody:public CLocation { private: CBox base,bb,bf; public: CBody(double ix,double iy,double iz):CLocation(ix,iy,iz), base(0,0,0,bodylength,bodywidth,bodybigheight,0),bb(0,0,0,bodylength,bodywidth,bodysmallheight,0),bf(0,0,0,bodylength,bodywidth,bodysmallheight,0) {} void Draw(); ~CBody(){} }; void CBody::Draw() { glPushMatrix(); glTranslated(GetX(),GetY(),GetZ()); base.Draw(); glTranslated(-(base.GetLength()+bb.GetLength())/2,0,-(base.GetHeight()-bb.GetHeight())/2); bb.Draw(); glTranslated((base.GetLength()+bb.GetLength()+base.GetLength()+bf.GetLength())/2,0,0); bf.Draw(); glPopMatrix(); } ///////////////////////////////////////////////////////////////////////////////////////////// class CHead:public CLocation { private: CBox base,b1,b2,b3,b4; public: CHead(double ix,double iy,double iz):CLocation(ix,iy,iz), base(0,0,0,hl,hw,hh,0), b1(0,0,0,hsl,0.1,0.1,0), b2(0,0,0,hsl,0.1,0.1,0), b3(0,0,0,hsl,0.1,0.1,0), b4(0,0,0,hsl,0.1,0.1,0) {} void Draw(); ~CHead(){} }; void CHead::Draw() { glPushMatrix(); glTranslated(GetX(),GetY(),GetZ()); base.Draw(); //auxSolidSphere(0.5); glPushMatrix(); glTranslated(hsl/2,hw/2,hh/2); b1.Draw(); glPopMatrix(); glPushMatrix(); glTranslated(hsl/2,-hw/2,-hh/2); b2.Draw(); glPopMatrix(); glPushMatrix(); glTranslated(hsl/2,hw/2,-hh/2); b3.Draw(); glPopMatrix(); glPushMatrix(); glTranslated(hsl/2,-hw/2,hh/2); b4.Draw(); glPopMatrix(); glPopMatrix(); } /////////////////////////////////////////////////////////////////////////////////////////////////////// class CCuber:public CLocation { private: CBody b;//òåëî CFullLeg l1,l2,l3,l4;//íîãè CBox neck;//øåÿ CHead h;//ãîëîâà int step,leg,neckstep; public: CCuber(double ix,double iy,double iz):CLocation(ix,iy,iz),b(0,0,0), l1(0,0,0,90),l2(0,0,0,90),l3(0,0,0,90),l4(0,0,0,90),neck(0,0,0,1,0.5,0.5,0),h(0,0,0),step(-1),leg(1),neckstep(-1) {} void Draw(); ~CCuber(){} }; void CCuber::Draw() { if(leg==1) { l1.SetAlphaTest(l1.GetAlphaTest()+step); if(l1.GetAlphaTest()>90) { l1.SetAlphaTest(90); step=-1; leg=2; } if(l1.GetAlphaTest()<45)step=-step; } if(leg==2) { l2.SetAlphaTest(l2.GetAlphaTest()+step); if(l2.GetAlphaTest()>90) { l2.SetAlphaTest(90); step=-1; leg=3; } if(l2.GetAlphaTest()<45)step=-step; } if(leg==3) { l3.SetAlphaTest(l3.GetAlphaTest()+step); if(l3.GetAlphaTest()>90) { l3.SetAlphaTest(90); step=-1; leg=4; } if(l3.GetAlphaTest()<45)step=-step; } if(leg==4) { l4.SetAlphaTest(l4.GetAlphaTest()+step); if(l4.GetAlphaTest()>90) { l4.SetAlphaTest(90); step=-1; leg=1; } if(l4.GetAlphaTest()<45)step=-step; } if(step>0)SetX(GetX()+0.005); glPushMatrix(); glTranslated(GetX(),GetY(),GetZ()); glPushMatrix(); glTranslated(3*bodylength/2+cos(3.14*neck.GetAlpha()/180)/2 ,0,-sin(3.14*neck.GetAlpha()/180)/2); neck.SetAlpha(neck.GetAlpha()+neckstep); if(neck.GetAlpha()>0||neck.GetAlpha()<-45)neckstep=-neckstep; neck.Draw(); glTranslated(cos(3.14*neck.GetAlpha()/180)/2,0,-sin(3.14*neck.GetAlpha()/180)/2); h.Draw(); glPopMatrix(); b.Draw(); glTranslated(bodylength,bodywidth/2-basbw/2,-bodysmallheight/2); l1.Draw(); //lf glTranslated(-2*bodylength,0,0); l2.Draw();//lb glTranslated(2*bodylength,-bodywidth+basbw,0); l3.Draw();//rf glTranslated(-2*bodylength,0,0); l4.Draw();//rb glPopMatrix(); } /////////////////////////////////////////////// CCuber Cuber1(-6,0,0),Cuber2(-4,3,0),Cuber3(2,1,0); void CALLBACK resize(int width,int height) { glViewport(0,0,width,height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(-10,10, -10,10, -10,30); gluLookAt(3,3,3, 0,0,0, 0,0,1 ); glMatrixMode( GL_MODELVIEW ); } void CALLBACK display(void) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );//î÷èñòêà ýêðàíà glColor3d(0,1,0); glPushMatrix(); glTranslated(0,0,-4); auxSolidBox(20,20,0.001);//ðèñóåì ïëîñêîñòü glPopMatrix(); Cuber1.Draw(); // Cuber2.Draw(); //ïðîðèñîâêà 3-õ Cuber'îâ Cuber3.Draw(); // auxSwapBuffers(); }
void main() { //èíèöèàëèçàöèÿ îêíà è îêîííûõ ôóíêöèé auxInitPosition((GetSystemMetrics(SM_CXFULLSCREEN)-640)/2, (GetSystemMetrics(SM_CYFULLSCREEN)-480)/2, 640,480 ); auxInitDisplayMode( AUX_RGB | AUX_DEPTH | AUX_DOUBLE ); auxInitWindow( "Cuber" ); auxIdleFunc(display); auxReshapeFunc(resize); glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); auxMainLoop(display); }
|
|