BilliardsPlayView.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:63k
源码类别:

游戏

开发平台:

Visual C++

  1. // BilliardsPlayView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Billiards.h"
  5. #include <math.h>
  6. #include <gl/gl.h>
  7. #include <gl/glu.h>
  8. #include <gl/glaux.h>
  9. #include <gl/glut.h>
  10. #include "BilliardsPlayView.h"
  11. #include "InitOpenGL.h"
  12. #include "BilliardsDoc.h"
  13. #include "MainFrm.h"
  14. #include "BilliardsFormView.h"
  15. #include <Mmsystem.h>
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. AUX_RGBImageRec *image1,*image2,*image3;
  22. unsigned char *pdata1,*pdata2,*pdata3;
  23. #define ImageWidth 256
  24. #define ImageHeight 256
  25. static GLuint texName[3];
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CBilliardsPlayView
  28. IMPLEMENT_DYNCREATE(CBilliardsPlayView, CView)
  29. void CBilliardsPlayView::texinit(void)
  30. {
  31. // glClearColor(0.8,0.8,0.8,1.0);
  32. glEnable(GL_DEPTH_TEST);
  33. glEnable(GL_AUTO_NORMAL);
  34. glDepthFunc(GL_LESS);
  35. // glFrontFace(GL_CW);
  36. glShadeModel(GL_FLAT);
  37. image1=auxDIBImageLoadA("MOHOGANY.bmp");
  38. pdata1=(BYTE *)malloc(256*256*3*sizeof(BYTE));
  39. if (!pdata1)
  40. exit(0);
  41. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  42. gluScaleImage(GL_RGB,image1->sizeX,image1->sizeY,GL_UNSIGNED_BYTE,image1->data,
  43.            ImageWidth,ImageHeight,GL_UNSIGNED_BYTE,pdata1);
  44. image2=auxDIBImageLoadA("BILLIARDSTABLE.bmp");
  45. pdata2=(BYTE *)malloc(256*256*3*sizeof(BYTE));
  46. if (!pdata2)
  47. exit(0);
  48. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  49. gluScaleImage(GL_RGB,image2->sizeX,image2->sizeY,GL_UNSIGNED_BYTE,image2->data,
  50.            ImageWidth,ImageHeight,GL_UNSIGNED_BYTE,pdata2);
  51. image3=auxDIBImageLoadA("tableface.bmp");
  52. pdata3=(BYTE *)malloc(256*256*3*sizeof(BYTE));
  53. if (!pdata3)
  54. exit(0);
  55. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  56. gluScaleImage(GL_RGB,image3->sizeX,image3->sizeY,GL_UNSIGNED_BYTE,image3->data,
  57.            ImageWidth,ImageHeight,GL_UNSIGNED_BYTE,pdata3);
  58. glGenTextures(3,texName);
  59.   
  60. glBindTexture(GL_TEXTURE_2D,texName[0]);
  61. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  62. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  63. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  64. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  65. glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,ImageWidth,ImageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,pdata1);
  66. glBindTexture(GL_TEXTURE_2D,texName[1]);
  67. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  68. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  69. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  70. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  71. glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,ImageWidth,ImageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,pdata2);
  72. glBindTexture(GL_TEXTURE_2D,texName[2]);
  73. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  74. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  75. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  76. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  77. glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,ImageWidth,ImageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,pdata3);
  78. glEnable(GL_TEXTURE_2D);
  79. }
  80. CBilliardsPlayView::CBilliardsPlayView()
  81. {
  82.     m_fViewHeight = 60.0;
  83. m_fViewNear = 200.0;
  84. m_fViewFar = 400.0;
  85. m_nMouseAct = MOUSE_SELECT;
  86. m_spinAngle = 0.0;
  87. m_spinAxes[0] = m_spinAxes[2] = 0.0; m_spinAxes[1] = 1.0;
  88. m_sEye.origin.x = m_sEye.origin.y = 0; m_sEye.origin.z = 300.0;
  89. m_sEye.center.x = m_sEye.center.y = 0.0; m_sEye.center.z = 0.0;
  90. m_sEye.up.x = m_sEye.up.z = 0.0; m_sEye.up.y = 1.0;
  91. m_sEye.axisX.y = m_sEye.axisX.z = 0.0; m_sEye.axisX.x = 1.0;
  92. m_sEye.axisY.x = m_sEye.axisY.z = 0.0; m_sEye.axisY.y = 1.0;
  93. m_sEye.axisZ.x = m_sEye.axisZ.y = 0.0; m_sEye.axisZ.z = -1.0;
  94. m_vProjectCenter[0] = 0.0f;
  95. m_vProjectCenter[1] = 0.0f;
  96. m_vProjectCenter[2] = (m_fViewNear+m_fViewFar)/2;
  97. int i;
  98. float tmpf;
  99. for (i=0;i<NUM_BALL;i++)
  100. m_bVisible[i]=TRUE;
  101. for (i=0;i<NUM_BALL;i++)
  102. m_fVelocity[i][0]=m_fVelocity[i][1]=m_fVelocity[i][2]=0.0f;
  103. for (i=0;i<NUM_BALL;i++)
  104. m_fPosition[i][1]=0.0f;
  105. m_fPosition[0][0] = - 2.0*TABLE_LENGTH/3.0;
  106. m_fPosition[0][2] = 0.0f;
  107. m_fPosition[1][0] = TABLE_LENGTH/2.5;
  108. m_fPosition[1][2] = 0.0f;
  109. tmpf = 2.0*BALL_RADIUS*0.8661;
  110. m_fPosition[2][0] = m_fPosition[1][0]+tmpf;
  111. m_fPosition[2][2] = BALL_RADIUS;
  112. m_fPosition[3][0] = m_fPosition[2][0]+tmpf;
  113. m_fPosition[3][2] = 2.0*BALL_RADIUS;
  114. m_fPosition[4][0] = m_fPosition[3][0]+tmpf;
  115. m_fPosition[4][2] = 3.0*BALL_RADIUS;
  116. m_fPosition[5][0] = m_fPosition[4][0]+tmpf;
  117. m_fPosition[5][2] = 4.0*BALL_RADIUS;
  118. m_fPosition[6][0] = m_fPosition[4][0];
  119. m_fPosition[6][2] = BALL_RADIUS;
  120. m_fPosition[7][0] = m_fPosition[5][0];
  121. m_fPosition[7][2] = 2.0*BALL_RADIUS;
  122. m_fPosition[8][0] = m_fPosition[3][0];
  123. m_fPosition[8][2] = 0.0f;
  124. m_fPosition[15][0]= m_fPosition[5][0];
  125. m_fPosition[15][2]= 0.0f;
  126. m_fPosition[9][0] = m_fPosition[2][0];
  127. m_fPosition[9][2] = -m_fPosition[2][2];
  128. m_fPosition[10][0]= m_fPosition[3][0];
  129. m_fPosition[10][2]= -m_fPosition[3][2];
  130. m_fPosition[11][0]= m_fPosition[4][0];
  131. m_fPosition[11][2]= -m_fPosition[4][2];
  132. m_fPosition[12][0]= m_fPosition[5][0];
  133. m_fPosition[12][2]= -m_fPosition[5][2];
  134. m_fPosition[13][0]= m_fPosition[6][0];
  135. m_fPosition[13][2]= -m_fPosition[6][2];
  136. m_fPosition[14][0]= m_fPosition[7][0];
  137. m_fPosition[14][2]= -m_fPosition[7][2];
  138. m_fStrength=22.5f;
  139. m_fDirection[0]=1.0f;
  140. m_fDirection[1]=0.0f;
  141. m_fDirection[2]=0.0f;
  142. m_fVelocity[0][0]=5.0f;
  143. m_nSelectObject = -1;
  144. m_bAnimate = FALSE;
  145. m_bCanHit = TRUE;
  146. }
  147. CBilliardsPlayView::~CBilliardsPlayView()
  148. {
  149. }
  150. BEGIN_MESSAGE_MAP(CBilliardsPlayView, CView)
  151. //{{AFX_MSG_MAP(CBilliardsPlayView)
  152. ON_WM_SIZE()
  153. ON_WM_ERASEBKGND()
  154. ON_WM_MOUSEMOVE()
  155. ON_WM_LBUTTONDOWN()
  156. ON_WM_LBUTTONUP()
  157. ON_WM_CREATE()
  158. ON_WM_TIMER()
  159. //}}AFX_MSG_MAP
  160. END_MESSAGE_MAP()
  161. /////////////////////////////////////////////////////////////////////////////
  162. // CBilliardsPlayView drawing
  163. void CBilliardsPlayView::OnDraw(CDC* pDC)
  164. {
  165. CDocument* pDoc = GetDocument();
  166.     
  167.     DrawScene();
  168. // TODO: add draw code here
  169. }
  170. /////////////////////////////////////////////////////////////////////////////
  171. // CBilliardsPlayView diagnostics
  172. #ifdef _DEBUG
  173. void CBilliardsPlayView::AssertValid() const
  174. {
  175. CView::AssertValid();
  176. }
  177. void CBilliardsPlayView::Dump(CDumpContext& dc) const
  178. {
  179. CView::Dump(dc);
  180. }
  181. #endif //_DEBUG
  182. /////////////////////////////////////////////////////////////////////////////
  183. // CBilliardsPlayView message handlers
  184. void CBilliardsPlayView::Init()
  185. {
  186. CDC *pDC = GetDC();
  187. m_hDc = pDC->GetSafeHdc();
  188. SetThePixelFormat(pDC);
  189. m_hRc = wglCreateContext(m_hDc);
  190. wglMakeCurrent(m_hDc,m_hRc);
  191. ReleaseDC(pDC);
  192. GLfloat light0_posi[]={-40.0,0.0,100.0,1.0};
  193. GLfloat light1_posi[]={40.0,0.0,100.0,1.0};
  194. GLfloat light2_posi[]={0.0,100.0,0.0,1.0};
  195. GLfloat white_light[]={1.0,1.0,1.0,1.0};
  196. glClearColor(0.7,0.7,0.7,1.0);
  197. glShadeModel(GL_SMOOTH);
  198. glLightfv(GL_LIGHT0,GL_POSITION,light0_posi);
  199. glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
  200. glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
  201. glLightfv(GL_LIGHT1,GL_POSITION,light1_posi);
  202. glLightfv(GL_LIGHT1,GL_DIFFUSE,white_light);
  203. glLightfv(GL_LIGHT1,GL_SPECULAR,white_light);
  204. glLightfv(GL_LIGHT2,GL_POSITION,light2_posi);
  205. glLightfv(GL_LIGHT2,GL_DIFFUSE,white_light);
  206. glLightfv(GL_LIGHT2,GL_SPECULAR,white_light);
  207. glEnable(GL_DEPTH_TEST);
  208. glEnable(GL_LIGHTING);
  209. glEnable(GL_LIGHT0);
  210. glEnable(GL_LIGHT1);
  211. glEnable(GL_LIGHT2);
  212. glEnable(GL_AUTO_NORMAL);
  213. glMatrixMode(GL_MODELVIEW);
  214. glRotatef(80.0,1.0,0.0,0.0);
  215.  
  216. texinit();
  217. }
  218. void CBilliardsPlayView::DrawPlayground()
  219. {
  220. float tmpf,tmpf1;
  221.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  222.    
  223. glMatrixMode(GL_MODELVIEW);
  224. glPushMatrix();
  225. //    glColor3f(1.0,1.0,1.0);
  226. float ambient0[4] = {0.259459f,0.251553f,0.0800367f,1.0f};
  227. float diffuse0[4] = {1.0f,0.969526f,0.308475f,1.0f};
  228. float specular0[4] = {0.728905f,0.691826f,0.153194f,1.0f};
  229.     float shininess0  = 0.891892f;
  230. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient0);
  231. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse0);
  232. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular0);
  233. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess0);
  234. // glPushMatrix();
  235. // glTranslatef( 0.0f, 0.0f, TABLE_WIDTH - 4.0*BAR_WIDTH);
  236. // auxSolidCylinder(POLE_RADIUS,POLE_HEIGHT);
  237. // glTranslatef( TABLE_LENGTH - 4.0*BAR_WIDTH, 0.0f, 0.0f);
  238. // auxSolidCylinder(POLE_RADIUS,POLE_HEIGHT);
  239. // glTranslatef( 0.0f, 0.0f, -2.0*(TABLE_WIDTH - 4.0*BAR_WIDTH));
  240. // auxSolidCylinder(POLE_RADIUS,POLE_HEIGHT);
  241. // glTranslatef( 4.0*BAR_WIDTH - TABLE_LENGTH, 0.0f, 0.0f);
  242. // auxSolidCylinder(POLE_RADIUS,POLE_HEIGHT);
  243. // glTranslatef( 4.0*BAR_WIDTH - TABLE_LENGTH, 0.0f, 0.0f);
  244. // auxSolidCylinder(POLE_RADIUS,POLE_HEIGHT);
  245. // glTranslatef( 0.0f, 0.0f, 2.0*(TABLE_WIDTH - 4.0*BAR_WIDTH));
  246.  //  auxSolidCylinder(POLE_RADIUS,POLE_HEIGHT);
  247. // glPopMatrix();
  248. //下桌面
  249. float ambient1[4] = {0.0f,0.023506f,0.014165f,1.0f};
  250. float diffuse1[4] = {0.0f,0.019048f,0.020158f,1.0f};
  251. float specular1[4] = {0.0714408f,0.20962f,0.126984f,1.0f};
  252.     float shininess1  = 0.0108108f;
  253. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient1);
  254. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse1);
  255. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular1);
  256. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess1);
  257. glPushMatrix();
  258. // glColor3f(0.0,1.0,0.0);
  259. glBegin(GL_QUAD_STRIP);
  260. glVertex3f(-TABLE_LENGTH-12.0f,0.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  261. glVertex3f(-TABLE_LENGTH-12.0f,0.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  262. glVertex3f(-TABLE_LENGTH-2.0f,0.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  263. glVertex3f(-TABLE_LENGTH-2.0f,0.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  264. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-2.0f);
  265. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+2.0f);
  266. glEnd();
  267. glBegin(GL_QUADS);
  268. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  269. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  270. glVertex3f(-1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  271. glVertex3f(-1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  272. glEnd();
  273. glBegin(GL_QUADS);
  274. glVertex3f(1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-2.0f);
  275. glVertex3f(1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+2.0f);
  276. glVertex3f(-1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+2.0f);
  277. glVertex3f(-1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-2.0f);
  278. glEnd();
  279. glBegin(GL_QUADS);
  280. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  281. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  282. glVertex3f(1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  283. glVertex3f(1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  284. glEnd();
  285. glBegin(GL_QUAD_STRIP);
  286. glVertex3f(TABLE_LENGTH+12.0f,0.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  287. glVertex3f(TABLE_LENGTH+12.0f,0.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  288. glVertex3f(TABLE_LENGTH+2.0f,0.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  289. glVertex3f(TABLE_LENGTH+2.0f,0.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  290. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-2.0f);
  291. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+2.0f);
  292. glEnd();
  293. float ambient2[4] = {0.7f,0.5f,0.5f,1.0f};
  294. float diffuse2[4] = {0.4f,0.3f,0.3f,1.0f};
  295. float specular2[4] = {0.0f,0.0f,0.0f,1.0f};
  296.     float shininess2  = 0.0f;
  297. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient2);
  298. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse2);
  299. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular2);
  300. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess2);
  301. glPushMatrix();
  302. //left
  303. glBegin(GL_QUADS);
  304. glVertex3f(-TABLE_LENGTH-12.0f,0.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  305. glVertex3f(-TABLE_LENGTH-12.0f,0.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  306. glVertex3f(-TABLE_LENGTH-12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  307. glVertex3f(-TABLE_LENGTH-12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  308. glEnd();
  309. //RIGHT
  310. glBegin(GL_QUADS);
  311. glVertex3f(TABLE_LENGTH+12.0f,0.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  312. glVertex3f(TABLE_LENGTH+12.0f,0.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  313. glVertex3f(TABLE_LENGTH+12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  314. glVertex3f(TABLE_LENGTH+12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  315. glEnd();
  316. //LEFT TOP
  317. glBegin(GL_QUADS);
  318. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  319. glVertex3f(-1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  320. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  321. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  322. glEnd();
  323. //LEFT down
  324. glBegin(GL_QUADS);
  325. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  326. glVertex3f(-1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  327. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  328. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  329. glEnd();
  330. //right TOP
  331. glBegin(GL_QUADS);
  332. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  333. glVertex3f(1.5*BALL_RADIUS,0.0f,-TABLE_WIDTH-12.0f);
  334. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  335. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  336. glEnd();
  337. //right down
  338. glBegin(GL_QUADS);
  339. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  340. glVertex3f(1.5*BALL_RADIUS,0.0f,TABLE_WIDTH+12.0f);
  341. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  342. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  343. glEnd();
  344. glPopMatrix();
  345.  
  346. //bar texture
  347. glPushMatrix();
  348. glEnable(GL_TEXTURE_2D);
  349. glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_DECAL);
  350. glBindTexture(GL_TEXTURE_2D,texName[0]);
  351. glBegin(GL_QUADS);
  352. glNormal3f(0.0,1.0,0.0);
  353. glTexCoord2f(0.0,0.0);
  354. glVertex3f(-TABLE_LENGTH-12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  355. glTexCoord2f(0.0,1.0);
  356. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  357. glTexCoord2f(1.0,1.0);
  358. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  359. glTexCoord2f(1.0,0.0);
  360. glVertex3f(-TABLE_LENGTH-12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  361. //glEnd();
  362. //glBegin(GL_QUADS);
  363. glTexCoord2f(0.0,0.0);
  364. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  365. glTexCoord2f(0.0,1.0);
  366. glVertex3f(TABLE_LENGTH+12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  367. glTexCoord2f(1.0,1.0);
  368. glVertex3f(TABLE_LENGTH+12.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  369. glTexCoord2f(1.0,0.0);
  370. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  371. glEnd();
  372. glBegin(GL_QUADS);
  373. glTexCoord2f(0.0,0.0);
  374. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  375. glTexCoord2f(0.0,1.0);
  376. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  377. glTexCoord2f(1.0,1.0);
  378. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  379. glTexCoord2f(1.0,0.0);
  380. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  381.     glEnd();
  382. glBegin(GL_QUADS);
  383. glTexCoord2f(0.0,0.0);
  384. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  385. glTexCoord2f(0.0,1.0);
  386. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  387. glTexCoord2f(1.0,1.0);
  388. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  389. glTexCoord2f(1.0,0.0);
  390. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  391. glEnd();
  392. glBegin(GL_QUADS);
  393. glTexCoord2f(0.0,0.0);
  394. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  395. glTexCoord2f(0.0,1.0);
  396. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  397. glTexCoord2f(1.0,1.0);
  398. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-12.0f);
  399. glTexCoord2f(1.0,0.0);
  400. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  401. glEnd();
  402. glBegin(GL_QUADS);
  403. glTexCoord2f(0.0,0.0);
  404. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  405. glTexCoord2f(0.0,1.0);
  406. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  407. glTexCoord2f(1.0,1.0);
  408. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  409. glTexCoord2f(1.0,0.0);
  410. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+12.0f);
  411. glEnd();
  412. glPopMatrix();
  413. glDisable(GL_TEXTURE_2D);
  414. // face texture
  415. glPushMatrix();
  416. glEnable(GL_TEXTURE_2D);
  417. glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_DECAL);
  418. glBindTexture(GL_TEXTURE_2D,texName[1]);
  419. glBegin(GL_POLYGON);
  420. glTexCoord2f(0.0,0.05);
  421. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT,TABLE_WIDTH-1.5*BALL_RADIUS);
  422. glTexCoord2f(0.0,0.95);
  423. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT,-TABLE_WIDTH+1.5*BALL_RADIUS);
  424. glTexCoord2f(0.05,1.0);
  425. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  426. glTexCoord2f(0.95,1.0);
  427. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  428. glTexCoord2f(1.0,0.95);
  429. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT,-TABLE_WIDTH+1.5*BALL_RADIUS);
  430. glTexCoord2f(1.0,0.05);
  431. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT,TABLE_WIDTH-1.5*BALL_RADIUS);
  432. glTexCoord2f(0.95,0.0);
  433. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  434. glTexCoord2f(0.05,0.0);
  435. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  436. glEnd();
  437. glPopMatrix();
  438. glDisable(GL_TEXTURE_2D);
  439. //edge texture
  440. glPushMatrix();
  441. glEnable(GL_TEXTURE_2D);
  442. glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_DECAL);
  443. glBindTexture(GL_TEXTURE_2D,texName[2]);
  444. //left
  445. glBegin(GL_QUADS);
  446. glTexCoord2f(0.0,0.0);
  447. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  448. glTexCoord2f(0.0,1.0);
  449. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  450. glTexCoord2f(1.0,1.0);
  451. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  452. glTexCoord2f(0.0,0.0);
  453. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  454. glEnd();
  455. glBegin(GL_QUADS);
  456. glTexCoord2f(0.0,0.0);
  457. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  458. glTexCoord2f(0.0,1.0);
  459. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT,-TABLE_WIDTH+1.5*BALL_RADIUS);
  460. glTexCoord2f(1.0,1.0);
  461. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  462. glTexCoord2f(1.0,0.0);
  463. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  464. glEnd();
  465. glBegin(GL_QUADS);
  466. glTexCoord2f(0.0,0.0);
  467. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT,-TABLE_WIDTH+1.5*BALL_RADIUS);
  468. glTexCoord2f(0.0,1.0);
  469. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  470. glTexCoord2f(1.0,1.0);
  471. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  472. glTexCoord2f(1.0,0.0);
  473. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT,TABLE_WIDTH-1.5*BALL_RADIUS);
  474. glEnd();
  475. glBegin(GL_QUADS);
  476. glTexCoord2f(0.0,0.0);
  477. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT,TABLE_WIDTH-1.5*BALL_RADIUS);
  478. glTexCoord2f(0.0,1.0);
  479. glVertex3f(-TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  480. glTexCoord2f(1.0,1.0);
  481. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  482. glTexCoord2f(1.0,0.0);
  483. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  484. glEnd();
  485. glBegin(GL_QUADS);
  486. glTexCoord2f(0.0,0.0);
  487. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  488. glTexCoord2f(0.0,1.0);
  489. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  490. glTexCoord2f(1.0,1.0);
  491. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  492. glTexCoord2f(1.0,0.0);
  493. glVertex3f(-TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  494. glEnd();
  495. //right
  496. glBegin(GL_QUADS);
  497. glTexCoord2f(0.0,0.0);
  498. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  499. glTexCoord2f(0.0,1.0);
  500. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  501. glTexCoord2f(1.0,1.0);
  502. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  503. glTexCoord2f(0.0,0.0);
  504. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  505. glEnd();
  506. glBegin(GL_QUADS);
  507. glTexCoord2f(0.0,0.0);
  508. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH+1.5*BALL_RADIUS);
  509. glTexCoord2f(0.0,1.0);
  510. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT,-TABLE_WIDTH+1.5*BALL_RADIUS);
  511. glTexCoord2f(1.0,1.0);
  512. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  513. glTexCoord2f(1.0,0.0);
  514. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  515. glEnd();
  516. glBegin(GL_QUADS);
  517. glTexCoord2f(0.0,0.0);
  518. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT,-TABLE_WIDTH+1.5*BALL_RADIUS);
  519. glTexCoord2f(0.0,1.0);
  520. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  521. glTexCoord2f(1.0,1.0);
  522. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  523. glTexCoord2f(1.0,0.0);
  524. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT,TABLE_WIDTH-1.5*BALL_RADIUS);
  525. glEnd();
  526. glBegin(GL_QUADS);
  527. glTexCoord2f(0.0,0.0);
  528. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT,TABLE_WIDTH-1.5*BALL_RADIUS);
  529. glTexCoord2f(0.0,1.0);
  530. glVertex3f(TABLE_LENGTH+2.0f,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH-1.5*BALL_RADIUS);
  531. glTexCoord2f(1.0,1.0);
  532. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  533. glTexCoord2f(1.0,0.0);
  534. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  535. glEnd();
  536. glBegin(GL_QUADS);
  537. glTexCoord2f(0.0,0.0);
  538. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  539. glTexCoord2f(0.0,1.0);
  540. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  541. glTexCoord2f(1.0,1.0);
  542. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+1.5*BALL_RADIUS+6.0f);
  543. glTexCoord2f(1.0,0.0);
  544. glVertex3f(TABLE_LENGTH-2.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-1.5*BALL_RADIUS-6.0f);
  545. glEnd();
  546. //left top
  547. glBegin(GL_QUADS);
  548. glTexCoord2f(0.0,0.0);
  549. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  550. glTexCoord2f(0.0,1.0);
  551. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  552. glTexCoord2f(1.0,1.0);
  553. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  554. glTexCoord2f(1.0,0.0);
  555. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  556. glEnd();
  557. glBegin(GL_QUADS);
  558. glTexCoord2f(0.0,0.0);
  559. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  560. glTexCoord2f(0.0,1.0);
  561. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  562. glTexCoord2f(1.0,1.0);
  563. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  564. glTexCoord2f(1.0,0.0);
  565. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  566. glEnd();
  567. glBegin(GL_QUADS);
  568. glTexCoord2f(0.0,0.0);
  569. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  570. glTexCoord2f(0.0,1.0);
  571. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  572. glTexCoord2f(1.0,1.0);
  573. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  574. glTexCoord2f(1.0,0.0);
  575. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  576. glEnd();
  577. glBegin(GL_QUADS);
  578. glTexCoord2f(0.0,0.0);
  579. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  580. glTexCoord2f(0.0,1.0);
  581. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  582. glTexCoord2f(1.0,1.0);
  583. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  584. glTexCoord2f(1.0,0.0);
  585. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  586. glEnd();
  587. glBegin(GL_QUADS);
  588. glTexCoord2f(0.0,0.0);
  589. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  590. glTexCoord2f(0.0,1.0);
  591. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  592. glTexCoord2f(1.0,1.0);
  593. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  594. glTexCoord2f(1.0,0.0);
  595. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  596. glEnd();
  597. //right top
  598. glBegin(GL_QUADS);
  599. glTexCoord2f(0.0,0.0);
  600. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  601. glTexCoord2f(0.0,1.0);
  602. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  603. glTexCoord2f(1.0,1.0);
  604. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  605. glTexCoord2f(1.0,0.0);
  606. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  607. glEnd();
  608. glBegin(GL_QUADS);
  609. glTexCoord2f(0.0,0.0);
  610. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  611. glTexCoord2f(0.0,1.0);
  612. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  613. glTexCoord2f(1.0,1.0);
  614. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  615. glTexCoord2f(1.0,0.0);
  616. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  617. glEnd();
  618. glBegin(GL_QUADS);
  619. glTexCoord2f(0.0,0.0);
  620. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT,-TABLE_WIDTH-2.0f);
  621. glTexCoord2f(0.0,1.0);
  622. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  623. glTexCoord2f(1.0,1.0);
  624. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  625. glTexCoord2f(1.0,0.0);
  626. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  627. glEnd();
  628. glBegin(GL_QUADS);
  629. glTexCoord2f(0.0,0.0);
  630. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  631. glTexCoord2f(0.0,1.0);
  632. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,-TABLE_WIDTH-2.0f);
  633. glTexCoord2f(1.0,1.0);
  634. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  635. glTexCoord2f(1.0,0.0);
  636. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  637. glEnd();
  638. glBegin(GL_QUADS);
  639. glTexCoord2f(0.0,0.0);
  640. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  641. glTexCoord2f(0.0,1.0);
  642. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  643. glTexCoord2f(1.0,1.0);
  644. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,-TABLE_WIDTH+2.0f);
  645. glTexCoord2f(1.0,0.0);
  646. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,-TABLE_WIDTH+2.0f);
  647. glEnd();
  648. //left down
  649. glBegin(GL_QUADS);
  650. glTexCoord2f(0.0,0.0);
  651. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  652. glTexCoord2f(0.0,1.0);
  653. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  654. glTexCoord2f(1.0,1.0);
  655. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  656. glTexCoord2f(1.0,0.0);
  657. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  658. glEnd();
  659. glBegin(GL_QUADS);
  660. glTexCoord2f(0.0,0.0);
  661. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  662. glTexCoord2f(0.0,1.0);
  663. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  664. glTexCoord2f(1.0,1.0);
  665. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  666. glTexCoord2f(1.0,0.0);
  667. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  668. glEnd();
  669. glBegin(GL_QUADS);
  670. glTexCoord2f(0.0,0.0);
  671. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  672. glTexCoord2f(0.0,1.0);
  673. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  674. glTexCoord2f(1.0,1.0);
  675. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  676. glTexCoord2f(1.0,0.0);
  677. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  678. glEnd();
  679. glBegin(GL_QUADS);
  680. glTexCoord2f(0.0,0.0);
  681. glVertex3f(-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  682. glTexCoord2f(0.0,1.0);
  683. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  684. glTexCoord2f(1.0,1.0);
  685. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  686. glTexCoord2f(1.0,0.0);
  687. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  688. glEnd();
  689. glBegin(GL_QUADS);
  690. glTexCoord2f(0.0,0.0);
  691. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  692. glTexCoord2f(0.0,1.0);
  693. glVertex3f(-TABLE_LENGTH+1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  694. glTexCoord2f(1.0,1.0);
  695. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  696. glTexCoord2f(1.0,0.0);
  697. glVertex3f(-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  698. glEnd();
  699. //right down
  700. glBegin(GL_QUADS);
  701. glTexCoord2f(0.0,0.0);
  702. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  703. glTexCoord2f(0.0,1.0);
  704. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  705. glTexCoord2f(1.0,1.0);
  706. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  707. glTexCoord2f(1.0,0.0);
  708. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  709. glEnd();
  710. glBegin(GL_QUADS);
  711. glTexCoord2f(0.0,0.0);
  712. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  713. glTexCoord2f(0.0,1.0);
  714. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  715. glTexCoord2f(1.0,1.0);
  716. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  717. glTexCoord2f(1.0,0.0);
  718. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  719. glEnd();
  720. glBegin(GL_QUADS);
  721. glTexCoord2f(0.0,0.0);
  722. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT,TABLE_WIDTH+2.0f);
  723. glTexCoord2f(0.0,1.0);
  724. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  725. glTexCoord2f(1.0,1.0);
  726. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  727. glTexCoord2f(1.0,0.0);
  728. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  729. glEnd();
  730. glBegin(GL_QUADS);
  731. glTexCoord2f(0.0,0.0);
  732. glVertex3f(1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  733. glTexCoord2f(0.0,1.0);
  734. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  735. glTexCoord2f(1.0,1.0);
  736. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  737. glTexCoord2f(1.0,0.0);
  738. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS,TABLE_HEIGHT+BALL_RADIUS+2.0f,TABLE_WIDTH+2.0f);
  739. glEnd();
  740. glBegin(GL_QUADS);
  741. glTexCoord2f(0.0,0.0);
  742. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  743. glTexCoord2f(0.0,1.0);
  744. glVertex3f(TABLE_LENGTH-1.5*BALL_RADIUS-6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  745. glTexCoord2f(1.0,1.0);
  746. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS-1.0f,TABLE_WIDTH-2.0f);
  747. glTexCoord2f(1.0,0.0);
  748. glVertex3f(1.5*BALL_RADIUS+6.0f,TABLE_HEIGHT+BALL_RADIUS+1.0f,TABLE_WIDTH-2.0f);
  749. glEnd();
  750. glPopMatrix();
  751.     glPopMatrix();
  752.     glFlush();
  753. glFinish();
  754. SwapBuffers(wglGetCurrentDC());
  755. glDisable(GL_TEXTURE_2D);
  756. }
  757. void CBilliardsPlayView::DrawBall()
  758. {
  759. /*glMatrixMode(GL_MODELVIEW);
  760. glPushMatrix();
  761. glTranslatef(0.0f, TABLE_HEIGHT/2.0+BALL_RADIUS, 0.0f);
  762. if (m_bVisible[0])
  763. {
  764. glPushMatrix();
  765. glTranslatef(m_fPosition[0][0],m_fPosition[0][1],m_fPosition[0][2]);
  766. float ambient0[4] = {0.2f,0.2f,0.2f,1.0f};
  767. float diffuse0[4] = {0.9f, 0.9f, 0.9f,1.0f};
  768. float specular0[4] = {0.25f,0.25f,0.25f,1.0f};
  769. float shininess0  = 0.8f;
  770. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient0);
  771. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse0);
  772. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular0);
  773. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess0);
  774. glutSolidSphere(BALL_RADIUS,20,16);
  775. glPopMatrix();
  776. }
  777. float ambient1[4] = {0.2f,0.08f,0.0f,1.0f};
  778. float diffuse1[4] = {0.8f,0.0f,0.0f,1.0f};
  779. float specular1[4] = {0.1f,0.3f,0.1f,1.0f};
  780.     float shininess1  = 0.8f;
  781. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient1);
  782. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse1);
  783. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular1);
  784. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess1);
  785. for (int i = 1; i<8; i++)
  786. if (m_bVisible[i])
  787. {
  788. glPushMatrix();
  789. glTranslatef(m_fPosition[i][0],m_fPosition[i][1],m_fPosition[i][2]);
  790. glutSolidSphere(BALL_RADIUS,20,16);
  791. glPopMatrix();
  792. }
  793. if (m_bVisible[8])
  794. {
  795. glPushMatrix();
  796. glTranslatef(m_fPosition[8][0],m_fPosition[8][1],m_fPosition[8][2]);
  797. float ambient2[4] = {0.0f,0.1f,0.1f,1.0f};
  798. float diffuse2[4] = {0.1f,0.1f,0.1f,1.0f};
  799. float specular2[4] = {0.2f,0.2f,0.2f,1.0f};
  800. float shininess2  = 0.8f;
  801. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient2);
  802. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse2);
  803. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular2);
  804. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess2);
  805. glutSolidSphere(BALL_RADIUS,20,16);
  806. glPopMatrix();
  807. }
  808. float ambient3[4] = {0.09f,0.0f,0.1f,1.0f};
  809. float diffuse3[4] = {0.0f,0.0f,0.8f,1.0f};
  810. float specular3[4] = {0.1f,0.3f,0.1f,1.0f};
  811.     float shininess3  = 0.8f;
  812. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient3);
  813. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse3);
  814. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular3);
  815. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess3);
  816.    for (i = 9; i<16; i++)
  817. if (m_bVisible[i])
  818. {
  819. glPushMatrix();
  820. glTranslatef(m_fPosition[i][0],m_fPosition[i][1],m_fPosition[i][2]);
  821. glutSolidSphere(BALL_RADIUS,20,16);
  822. glPopMatrix();
  823. }
  824. glPopMatrix();*/
  825.     glMatrixMode(GL_MODELVIEW);
  826. glPushMatrix();
  827. glTranslatef(0.0f, TABLE_HEIGHT+BALL_RADIUS, 0.0f);
  828. // GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  829. // GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
  830. // GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
  831. // GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
  832. // GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  833. // GLfloat no_shininess[] = { 0.0 };
  834. // GLfloat low_shininess[] = { 5.0 };
  835. // GLfloat high_shininess[] = { 100.0 };
  836. // GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
  837. //
  838. //     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  839. // glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  840. // glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  841. // glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  842. // glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  843. if (m_bVisible[0])
  844. {
  845. glPushMatrix();
  846. glTranslatef(m_fPosition[0][0],m_fPosition[0][1],m_fPosition[0][2]);
  847. float ambient0[4] = {0.5f,0.5f,0.5f,1.0f};
  848. float diffuse0[4] = {0.4f,0.4f,0.4f,1.0f};
  849. //float specular0[4] = {0.25f,0.25f,0.25f,1.0f};
  850. float specular0[] = {1.0f,1.0f,1.0f,1.0f};
  851. float shininess0 = 100.0f;
  852. //float mat_emission[] = {0.3, 0.2, 0.2, 0.0};
  853. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient0);
  854. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse0);
  855. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular0);
  856. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess0);
  857. //glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  858. glutSolidSphere(BALL_RADIUS,20,16);
  859. glPopMatrix();
  860. }
  861. float ambient1[4] = {0.2f,0.08f,0.0f,1.0f};
  862. float diffuse1[4] = {0.8f,0.0f,0.0f,1.0f};
  863. // float specular1[4] = {0.1f,0.3f,0.1f,1.0f};
  864. //    float shininess1  = 0.8f;
  865. float specular1[] = {1.0f,1.0f,1.0f,1.0f};
  866. float shininess1 = 100.0f;
  867. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient1);
  868. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse1);
  869. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular1);
  870. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess1);
  871. for (int i = 1; i<9; i++)
  872. if (m_bVisible[i])
  873. {
  874. glPushMatrix();
  875. glTranslatef(m_fPosition[i][0],m_fPosition[i][1],m_fPosition[i][2]);
  876. glutSolidSphere(BALL_RADIUS,20,16);
  877. glPopMatrix();
  878. }
  879. // if (m_bVisible[8])
  880. // {
  881. // glPushMatrix();
  882. // glTranslatef(m_fPosition[8][0],m_fPosition[8][1],m_fPosition[8][2]);
  883. // float ambient2[4] = {0.0f,0.1f,0.1f,1.0f};
  884. // float diffuse2[4] = {0.1f,0.1f,0.1f,1.0f};
  885. //// float specular2[4] = {0.2f,0.2f,0.2f,1.0f};
  886. //// float shininess2  = 0.8f;
  887. // float specular2[] = {1.0f,1.0f,1.0f,1.0f};
  888. // float shininess2 = 100.0f;
  889. //
  890. // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient2);
  891. // glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse2);
  892. // glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular2);
  893. // glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess2);
  894. // glutSolidSphere(BALL_RADIUS,20,16);
  895. // glPopMatrix();
  896. // }
  897. float ambient3[4] = {0.09f,0.0f,0.1f,1.0f};
  898. float diffuse3[4] = {0.0f,0.0f,0.8f,1.0f};
  899. // float specular3[4] = {0.1f,0.3f,0.1f,1.0f};
  900. //    float shininess3  = 0.8f;
  901. float specular3[] = {1.0f,1.0f,1.0f,1.0f};
  902. float shininess3 = 100.0f;
  903. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient3);
  904. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse3);
  905. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular3);
  906. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess3);
  907.    for (i = 9; i<16; i++)
  908. if (m_bVisible[i])
  909. {
  910. glPushMatrix();
  911. glTranslatef(m_fPosition[i][0],m_fPosition[i][1],m_fPosition[i][2]);
  912. glutSolidSphere(BALL_RADIUS,20,16);
  913. glPopMatrix();
  914. }
  915. glPopMatrix();
  916. }
  917. int CBilliardsPlayView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  918. {
  919. if (CWnd::OnCreate(lpCreateStruct) == -1)
  920. return -1;
  921.     Init();
  922.    
  923. return 0;
  924. }
  925. BOOL CBilliardsPlayView::SetThePixelFormat(CDC *pDC)
  926. {
  927.     static PIXELFORMATDESCRIPTOR pfd = {
  928. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  929. 1,                              // version number
  930. PFD_DRAW_TO_WINDOW |            // support window
  931. PFD_SUPPORT_OPENGL |            // support OpenGL
  932. PFD_DOUBLEBUFFER,               // double buffered
  933. PFD_TYPE_RGBA,                  // RGBA type
  934. 24,                             // 24-bit color depth
  935. 0, 0, 0, 0, 0, 0,               // color bits ignored
  936. 0,                              // no alpha buffer
  937. 0,                              // shift bit ignored
  938. 0,                              // no accumulation buffer
  939. 0, 0, 0, 0,                     // accum bits ignored
  940. 32,                             // 32-bit z-buffer
  941. 0,                              // no stencil buffer
  942. 0,                              // no auxiliary buffer
  943. PFD_MAIN_PLANE,                 // main layer
  944. 0,                              // reserved
  945. 0, 0, 0                         // layer masks ignored
  946.     };
  947. int pixelformat;
  948.     if((pixelformat = ChoosePixelFormat(m_hDc, &pfd)) == 0)
  949.     {
  950.         MessageBox( "ChoosePixelFormat failed", "Error", MB_OK);
  951.         return FALSE;
  952.     }
  953.     if(SetPixelFormat(m_hDc, pixelformat, &pfd) == FALSE)
  954.     {
  955.         MessageBox( "SetPixelFormat failed", "Error", MB_OK);
  956.         return FALSE;
  957.     }
  958.     CreateRGBPalette(m_hDc);
  959.     return TRUE;
  960. }
  961. BOOL CBilliardsPlayView::PreCreateWindow(CREATESTRUCT& cs) 
  962. {
  963.     cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN ;
  964. cs.dwExStyle |= WS_EX_CLIENTEDGE;
  965. cs.style &= ~WS_BORDER;
  966. cs.lpszClass = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
  967. ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);
  968. if (!CWnd::PreCreateWindow(cs))
  969. return FALSE;
  970. return TRUE;
  971. }
  972. void CBilliardsPlayView::OnSize(UINT nType, int cx, int cy) 
  973. {
  974. CView::OnSize(nType, cx, cy);
  975. Project();
  976. }
  977. void CBilliardsPlayView::Project()
  978. {
  979. RECT rect;
  980. GetClientRect( &rect);
  981. GLsizei nWidth = rect.right;
  982. GLsizei nHeight = rect.bottom;
  983. nHeight = (nHeight == 0) ? 1 : nHeight;
  984. float dAspect = (float)nWidth/(float)nHeight;
  985. m_fViewWidth = m_fViewHeight * dAspect;
  986. glViewport(0,0,nWidth,nHeight);
  987. glMatrixMode(GL_PROJECTION);
  988. glLoadIdentity();
  989. glFrustum(-m_fViewWidth,  m_fViewWidth,
  990.       -m_fViewHeight, m_fViewHeight,
  991.       m_fViewNear,    m_fViewFar );
  992. gluLookAt(m_sEye.origin.x,m_sEye.origin.y,m_sEye.origin.z,
  993.   m_sEye.center.x,m_sEye.center.y,m_sEye.center.z,
  994.   m_sEye.up.x,m_sEye.up.y,m_sEye.up.z);
  995. glMatrixMode(GL_MODELVIEW);
  996. }
  997. BOOL CBilliardsPlayView::OnEraseBkgnd(CDC* pDC) 
  998. {
  999. return TRUE;
  1000. }
  1001. void CBilliardsPlayView::DrawScene()
  1002. {
  1003. int j;
  1004. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1005. glMatrixMode(GL_MODELVIEW);
  1006. glPushMatrix();
  1007. if (m_bAnimate)
  1008. {
  1009. if (Calculate())
  1010. {
  1011. KillTimer(ANIMATE_TIMER);
  1012. m_bAnimate = FALSE;
  1013. if (m_nEnter != 100)
  1014. {
  1015. CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
  1016. CBilliardsPlayView *pView = (CBilliardsPlayView *)(pFrame->m_wndSplitter).GetPane(0,1);
  1017. CBilliardsDoc *pDoc = (CBilliardsDoc *)pView->GetDocument();
  1018. CString strTemp;
  1019. if (m_nEnter <= 0)
  1020. m_bCanHit = !m_bCanHit;
  1021. else
  1022. {
  1023. if (m_bCanHit)
  1024. {
  1025. //strTemp = _T("Hehe, 我进球了");
  1026. //pDoc->SendMsg(strTemp);
  1027. }
  1028. if (pDoc->m_bPlayer == SERVER)
  1029. {
  1030. for (j=8; j>0; j--)
  1031. if (m_bVisible[j])
  1032. break;
  1033. if (j<=0)
  1034. {
  1035. //pDoc->WinGame();
  1036. }
  1037. else if (m_bCanHit && !m_bVisible[8])
  1038. {
  1039. //pDoc->LoseGame();
  1040. }
  1041. else
  1042. {
  1043. for (j=8; j<NUM_BALL; j++)
  1044. if (m_bVisible[j])
  1045. break;
  1046. if (j>=NUM_BALL)
  1047. {
  1048. // pDoc->LoseGame();
  1049. }
  1050. else if (!m_bCanHit && !m_bVisible[8])
  1051. {
  1052. // pDoc->WinGame();
  1053. }
  1054. }
  1055. }
  1056. else if (pDoc->m_bPlayer == CLIENT)
  1057. {
  1058. for (j=8; j<NUM_BALL; j++)
  1059. if (m_bVisible[j])
  1060. break;
  1061. if (j>=NUM_BALL)
  1062. {
  1063. //pDoc->WinGame();
  1064. }
  1065. else if (m_bCanHit && !m_bVisible[8])
  1066. {
  1067. //pDoc->LoseGame();
  1068. }
  1069. else
  1070. {
  1071. for (j=8; j>0; j--)
  1072. if (m_bVisible[j])
  1073. break;
  1074. if (j<=0)
  1075. {
  1076. // pDoc->LoseGame();
  1077. }
  1078. else if (!m_bCanHit && !m_bVisible[8])
  1079. {
  1080. // pDoc->WinGame();
  1081. }
  1082. }
  1083. }
  1084. if (!m_bVisible[0])
  1085. m_bCanHit = !m_bCanHit;
  1086. }
  1087. if (pDoc->m_bPlayer == IDLE)
  1088. m_bCanHit = TRUE;
  1089. else
  1090. {
  1091. if (m_bCanHit)
  1092. {
  1093. //strTemp = _T("1该我击球");
  1094. //pDoc->m_pClientSocket->Send(strTemp,strTemp.GetLength());
  1095. }
  1096. }
  1097. }
  1098. if (!m_bVisible[0]) //母球落袋后重画母球
  1099. {
  1100. m_fPosition[0][1]=m_fPosition[0][2]=0.0f;
  1101. m_fPosition[0][0] = - 2.0*TABLE_LENGTH/3.0;
  1102. m_bVisible[0]=TRUE;
  1103. }
  1104. m_fStrength=22.5f;
  1105. m_fDirection[0]=1.0f;
  1106. m_fDirection[2]=0.0f;
  1107. m_fVelocity[0][0]=5.0f;
  1108. }
  1109. }
  1110. DrawPlayground();
  1111. DrawBall();
  1112. if (!m_bAnimate)
  1113. {
  1114. glPushName(1);
  1115. DrawArrow();
  1116. glPopName();
  1117. }
  1118. glMatrixMode(GL_MODELVIEW);
  1119. glPopMatrix();
  1120. glFlush();
  1121. SwapBuffers(m_hDc); //交换缓冲区
  1122. }
  1123. void CBilliardsPlayView::DrawArrow()
  1124. {
  1125. float angle;
  1126. int mode;
  1127. glGetIntegerv(GL_RENDER_MODE , &mode);
  1128. glDisable(GL_LIGHTING);
  1129. glMatrixMode(GL_MODELVIEW);
  1130. glPushMatrix();
  1131. glTranslatef(0.0f, TABLE_HEIGHT/2.0+2.0*BALL_RADIUS, 0.0f);
  1132. glTranslatef(m_fPosition[0][0],m_fPosition[0][1],m_fPosition[0][2]);
  1133. glColor3f(0.5f,0.5f,0.0f);
  1134. glLineWidth(1.5);
  1135. if (m_fDirection[0]>=0.0)
  1136. angle = asin(m_fDirection[2])/M_PI*180.0;
  1137. else
  1138. angle = 180.0 - asin(m_fDirection[2])/M_PI*180.0;
  1139. glRotatef(-angle,0.0f,1.0f,0.0f);
  1140. glBegin(GL_LINES);
  1141. glVertex3f(0.0f,0.0f,0.0f);
  1142. glVertex3f(m_fStrength,0.0,0.0);
  1143. glEnd();
  1144. glTranslatef(m_fStrength,0.0f,0.0f);
  1145. glBegin(GL_TRIANGLES);
  1146. glVertex3f(-2.0f, 0.0f, 2.0f);
  1147. glVertex3f(-2.0f, 0.0f, -2.0f);
  1148. glVertex3f(3.0f, 0.0f, 0.0f);
  1149. glEnd();
  1150. glPopMatrix();
  1151. glEnable(GL_LIGHTING);
  1152. glLineWidth(1.0);
  1153. }
  1154. int CBilliardsPlayView::Calculate()
  1155. {
  1156. CMainFrame *pView=(CMainFrame *)(AfxGetMainWnd());
  1157. float x1,z1,x2,z2,x3,z3;
  1158. float tmpf,minf;
  1159. BOOL mark[NUM_BALL][NUM_BALL];
  1160. int i,j;
  1161. int k;
  1162. x1 = TABLE_LENGTH - BAR_WIDTH;
  1163. z1 = TABLE_WIDTH - BAR_WIDTH;
  1164. x2 = 0.0;
  1165. z2 = TABLE_WIDTH;
  1166. for (i=0;i<NUM_BALL;i++)
  1167. if (m_bVisible[i])
  1168. {
  1169. x3 = fabs(m_fPosition[i][0]);
  1170. z3 = fabs(m_fPosition[i][2]);
  1171. if (((x1-x3)*(x1-x3)+(z1-z3)*(z1-z3)<2.0*BALL_RADIUS*BALL_RADIUS)||
  1172. ((x2-x3)*(x2-x3)+(z2-z3)*(z2-z3)<2.0*BALL_RADIUS*BALL_RADIUS))//球进洞了
  1173. {
  1174. m_bVisible[i] = FALSE;
  1175. if(pView->m_bSoundGoal)
  1176. sndPlaySound("res\goal.wav",SND_ASYNC);
  1177. if (m_nEnter<i)       //?
  1178. m_nEnter = i;
  1179. }
  1180. }
  1181. for (i=0;i<NUM_BALL;i++)
  1182. for (j=0;j<NUM_BALL;j++)
  1183. mark[i][j]=FALSE;
  1184. for (i=0;i<NUM_BALL;i++)
  1185. if (m_bVisible[i])
  1186. {
  1187. if ((m_fPosition[i][0]>TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS)&&(fabs(m_fPosition[i][2])<TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS))
  1188. {//右壁
  1189. m_fPosition[i][0]=TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS;
  1190. hitx=INFINITY;
  1191.                 hitz=INFINITY;
  1192. if (m_fVelocity[i][0]>0.0){
  1193. m_fVelocity[i][0] = -DECR*m_fVelocity[i][0];
  1194. if(pView->m_bSoundCush)
  1195. sndPlaySound("res\cush.wav",SND_ASYNC);
  1196. }
  1197. }
  1198. else if ((m_fPosition[i][0]<-(TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS))&&(fabs(m_fPosition[i][2])<TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS))
  1199. {//左壁
  1200. m_fPosition[i][0]=-(TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS);
  1201. hitx=INFINITY;
  1202.                 hitz=INFINITY;
  1203. if (m_fVelocity[i][0]<0.0){
  1204. m_fVelocity[i][0] = -DECR*m_fVelocity[i][0];
  1205. if(pView->m_bSoundCush)
  1206. sndPlaySound("res\cush.wav",SND_ASYNC);
  1207. }
  1208. }
  1209. else if ((m_fPosition[i][2]>TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS)&&(fabs(m_fPosition[i][0])>1.2*BALL_RADIUS))
  1210. {//上壁
  1211. m_fPosition[i][2]=TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS;
  1212. hitx=INFINITY;
  1213.                 hitz=INFINITY;
  1214. if (m_fVelocity[i][2]>0.0){
  1215. m_fVelocity[i][2] = -DECR*m_fVelocity[i][2];
  1216. if(pView->m_bSoundCush)
  1217. sndPlaySound("res\cush.wav",SND_ASYNC);
  1218. }
  1219. }
  1220. else if ((m_fPosition[i][2]<-(TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS))&&(fabs(m_fPosition[i][0])>1.2*BALL_RADIUS))
  1221. {//下壁
  1222. m_fPosition[i][2]=-(TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS);
  1223. hitx=INFINITY;
  1224.                 hitz=INFINITY;
  1225. if (m_fVelocity[i][2]<0.0){
  1226. m_fVelocity[i][2] = -DECR*m_fVelocity[i][2];
  1227. if(pView->m_bSoundCush)
  1228. sndPlaySound("res\cush.wav",SND_ASYNC);
  1229. }
  1230. }
  1231. else//不碰壁
  1232. {
  1233. minf = 8*BALL_RADIUS*BALL_RADIUS;
  1234. for (j=0;j<NUM_BALL;j++)
  1235. if (m_bVisible[j]&&(i!=j)&&!mark[i][j])
  1236. {
  1237. tmpf = (m_fPosition[i][0]-m_fPosition[j][0])*(m_fPosition[i][0]-m_fPosition[j][0])+(m_fPosition[i][2]-m_fPosition[j][2])*(m_fPosition[i][2]-m_fPosition[j][2]);
  1238. //两球心距离的平方和
  1239.                         if (tmpf<minf)
  1240. {
  1241. minf=tmpf;
  1242. k=j;
  1243. }
  1244. }
  1245.     if (minf<=4*BALL_RADIUS*BALL_RADIUS)//两球心距离的平方和
  1246.      {
  1247.     float line[3];
  1248.     float t1,t2;
  1249.     float vec1[3],vec2[3],vec3[3],vec4[3];
  1250.     
  1251.     line[0]=m_fPosition[k][0]-m_fPosition[i][0];    //x坐标差
  1252.     line[1]=0.0f;
  1253.     line[2]=m_fPosition[k][2]-m_fPosition[i][2];    //z坐标差
  1254.     t1 = sqrt(DOTPROD3(line,line));                 //长度
  1255.     if (t1>0.0)
  1256.      VEC3_V_OP_S(line,line,/,t1);                //line[i]=line[i]/t1,成单位向量
  1257.     
  1258.     t1=DOTPROD3(m_fVelocity[i],line);               //i号球的速度乘单位向量
  1259.     t2=DOTPROD3(m_fVelocity[k],line);               //k号球的速度乘单位向量
  1260.     VEC3_V_OP_S(vec1,line,*,t1);                    //恢复原line的长度,放入vec1
  1261.     VEC3_V_OP_V(vec1,m_fVelocity[i],-,vec1);        //i号球的x,z方向的分速度-vec1x,z方向分速度
  1262.     VEC3_V_OP_S(vec2,line,*,t2);                    //k号球的速度
  1263.     VEC3_V_OP_V(vec2,m_fVelocity[k],-,vec2);        //
  1264.     if (t1-t2>0.0)
  1265.     {
  1266.      VEC3_V_OP_S(vec3,line,*,t2);
  1267.     VEC3_V_OP_V(m_fVelocity[i],vec1,+,vec3);
  1268.     VEC3_V_OP_S(vec4,line,*,t1);
  1269.     VEC3_V_OP_V(m_fVelocity[k],vec2,+,vec4);
  1270.     mark[i][k]=mark[k][i]=TRUE;
  1271. if(pView->m_bSoundBall)
  1272. sndPlaySound("res\ball2ball.wav",SND_ASYNC);
  1273.     }
  1274. hitx=INFINITY;
  1275. hitz=INFINITY;
  1276.     }
  1277.     }
  1278.      if (fabs(m_fPosition[i][0])>TABLE_LENGTH||fabs(m_fPosition[i][2])>TABLE_WIDTH)
  1279.      {
  1280.     m_bVisible[i]=FALSE;
  1281. if(pView->m_bSoundGoal)
  1282. sndPlaySound("res\goal.wav",SND_ASYNC);
  1283.     if (m_nEnter<i)
  1284.      m_nEnter = i;
  1285.     }
  1286.     }
  1287. for (i=0;i<NUM_BALL;i++)
  1288. if (m_bVisible[i])
  1289. {
  1290. float t1,s1;
  1291. float vec1[3];
  1292. t1 = sqrt(DOTPROD3(m_fVelocity[i],m_fVelocity[i]));
  1293. if (t1>0.0f)
  1294. {
  1295. VEC3_V_OP_S(vec1,m_fVelocity[i],/,t1);
  1296. s1 = t1+ACCELERATION/2.0;
  1297. t1 = t1+ACCELERATION;
  1298. if (t1<-0.1*ACCELERATION)    //速度小于一定值就停下来
  1299. {
  1300. s1=0.0f;
  1301. t1=0.0f;
  1302. }
  1303. VEC3_V_OP_S(m_fVelocity[i],vec1,*,t1);
  1304. VEC3_V_OP_S(vec1,vec1,*,s1);
  1305.                 
  1306.                 if(i==0){
  1307.                     if(DOTPROD3(vec1,vec1)>((m_fPosition[i][0]-hitx)*(m_fPosition[i][0]-hitx)+(m_fPosition[i][2]-hitz)*(m_fPosition[i][2]-hitz))){
  1308.                         m_fPosition[i][0]=hitx;
  1309.                         m_fPosition[i][2]=hitz;
  1310.                         hitx=INFINITY;
  1311.                         hitz=INFINITY;
  1312.                     }
  1313.                     else
  1314.         VEC3_V_OP_V(m_fPosition[i],m_fPosition[i],+,vec1);
  1315.                 }
  1316.                 else
  1317.                     VEC3_V_OP_V(m_fPosition[i],m_fPosition[i],+,vec1);
  1318. x3 = fabs(m_fPosition[i][0]);
  1319. z3 = fabs(m_fPosition[i][2]);
  1320. if (((x1-x3)*(x1-x3)+(z1-z3)*(z1-z3)<BALL_RADIUS*BALL_RADIUS)||
  1321. ((x2-x3)*(x2-x3)+(z2-z3)*(z2-z3)<BALL_RADIUS*BALL_RADIUS))
  1322. ;
  1323. //以下防止球越界过多
  1324.                 else if ((m_fPosition[i][0]>TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS)&&(fabs(m_fPosition[i][2])<TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS))
  1325. {
  1326. m_fPosition[i][0]=TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS+0.01;
  1327.                     
  1328. }
  1329. else if ((m_fPosition[i][0]<-(TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS))&&(fabs(m_fPosition[i][2])<TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS))
  1330. {
  1331. m_fPosition[i][0]=-(TABLE_LENGTH-BAR_WIDTH-BALL_RADIUS)-0.01;
  1332.                     hitx=INFINITY;
  1333.                     hitz=INFINITY;
  1334. }
  1335. else if ((m_fPosition[i][2]>TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS)&&(fabs(m_fPosition[i][0])>1.2*BALL_RADIUS))
  1336. {
  1337. m_fPosition[i][2]=TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS+0.01;
  1338.                     hitx=INFINITY;
  1339.                     hitz=INFINITY;
  1340. }
  1341. else if ((m_fPosition[i][2]<-(TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS))&&(fabs(m_fPosition[i][0])>1.2*BALL_RADIUS))
  1342. {
  1343. m_fPosition[i][2]=-(TABLE_WIDTH-BAR_WIDTH-BALL_RADIUS)-0.01;
  1344.                     hitx=INFINITY;
  1345.                     hitz=INFINITY;
  1346. }
  1347. for (j=0;j<NUM_BALL;j++)
  1348. if (m_bVisible[j]&&(i!=j))
  1349. {
  1350. tmpf = (m_fPosition[i][0]-m_fPosition[j][0])*(m_fPosition[i][0]-m_fPosition[j][0])+(m_fPosition[i][2]-m_fPosition[j][2])*(m_fPosition[i][2]-m_fPosition[j][2]);
  1351. if (tmpf<4*BALL_RADIUS*BALL_RADIUS)
  1352. break;
  1353. }
  1354. if (j<NUM_BALL)     //出现两球距离小于直径的情况
  1355. {
  1356. VEC3_V_OP_S(vec1,vec1,/,2.0);
  1357. VEC3_V_OP_V(m_fPosition[i],m_fPosition[i],-,vec1); 
  1358. }
  1359. }
  1360. }
  1361. for (i=0;i<NUM_BALL;i++)
  1362. if (m_bVisible[i])
  1363. if ((m_fVelocity[i][0]!=0.0)||(m_fVelocity[i][2]!=0.0))
  1364. break;
  1365. if (i<NUM_BALL)
  1366. return 0;
  1367. return 1;
  1368. }
  1369. void CBilliardsPlayView::MouseSpinGlobal(UINT nFlags, int x, int y, int init)
  1370. {
  1371. float centerX, centerY;
  1372. float radius;
  1373. float w, h;  // window
  1374.     RECT rect;
  1375.     GetClientRect( &rect);
  1376.     w =(float)rect.right;
  1377. h =(float)rect.bottom;
  1378.     h = (h == 0.0f) ? 1.0f : h;
  1379.     centerX = w / 2.0f;
  1380. centerY = h / 2.0f;
  1381. radius  = (w > h) ? centerY : centerX ;
  1382. if (init)
  1383. {
  1384. Trackball( 0, radius, centerX, centerY, (float)x, (float)(h-y), 
  1385.            m_spinAxes[0], m_spinAxes[1], m_spinAxes[2], m_spinAngle);
  1386. }
  1387. else if ( nFlags & MK_LBUTTON )
  1388. {
  1389.     Trackball(1, radius, centerX, centerY, (float)x, (float)(h-y), 
  1390.       m_spinAxes[0], m_spinAxes[1], m_spinAxes[2], m_spinAngle);
  1391. SpinGlobal(m_spinAxes, m_spinAngle);
  1392. }
  1393. }
  1394. void CBilliardsPlayView::SpinGlobal(float axes[], float angle)
  1395. {
  1396. EyeRotate(m_vProjectCenter, axes, angle, m_sEye );
  1397. DrawScene(); 
  1398. }
  1399. void CBilliardsPlayView::MouseZoom(UINT nFlags, int x, int y)
  1400. {
  1401. float w, h;
  1402. float scale;
  1403. static float oldY, dy ;
  1404. RECT rect;
  1405. GetClientRect( &rect);
  1406. w =(float)rect.right;
  1407. h =(float)rect.bottom;
  1408. h = (h == 0.0f) ? 1.0f : h;
  1409. if( m_bLButtonDown && (nFlags & MK_LBUTTON))
  1410. {
  1411. dy=oldY-y;
  1412. scale=1.0f+dy/h;
  1413. Zoom(scale);
  1414. oldY=(float)y;
  1415. }
  1416. else
  1417. {
  1418. oldY=(float)y;
  1419. }
  1420. }
  1421. void CBilliardsPlayView::Zoom(float scale)
  1422. {
  1423. double a[1][2];
  1424. a[0][0] = 1.0;
  1425. a[0][1] = 2.0;
  1426. double *p;
  1427. p = (double *)a;
  1428. if(scale<0.001) return;
  1429. m_fViewHeight/=scale;
  1430. Project();
  1431. DrawScene();
  1432. }
  1433. void CBilliardsPlayView::MouseTranslate(UINT nFlags, int x, int y)
  1434. {
  1435. float w, h;
  1436. static float oldX, oldY, dx, dy, d[3] ;
  1437. RECT rect;
  1438. GetClientRect( &rect);
  1439. w =(float)rect.right;
  1440. h =(float)rect.bottom;
  1441. h = (h == 0.0f) ? 1.0f : h;
  1442. if( m_bLButtonDown && (nFlags & MK_LBUTTON))
  1443. {
  1444. dx=x-oldX;
  1445. dy=oldY-y;
  1446. d[0]=m_fViewWidth*dx/w*0.8f;
  1447. d[1]=m_fViewHeight*dy/h*0.8f;           
  1448. d[2]=0.0f;
  1449. Translate(d);   
  1450. oldX=(float)x;
  1451. oldY=(float)y;
  1452. }
  1453. else
  1454. {
  1455. oldX=(float)x;
  1456. oldY=(float)y;
  1457. }
  1458. }
  1459. void CBilliardsPlayView::Translate(float d[])
  1460. {
  1461. EyeTranslate(d, m_sEye);
  1462. DrawScene();
  1463. }
  1464. void CBilliardsPlayView::OnMouseMove(UINT nFlags, CPoint point) 
  1465. {
  1466.     CBilliardsFormView *pView=(CBilliardsFormView *)(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));
  1467.     if ( nFlags & MK_LBUTTON )
  1468. {
  1469. if (m_nMouseAct == MOUSE_SPIN)
  1470. {
  1471. MouseSpinGlobal(nFlags, point.x, point.y, 0);
  1472. }
  1473. else if (m_nMouseAct == MOUSE_ZOOM)
  1474. {
  1475. MouseZoom(nFlags, point.x, point.y);
  1476. }
  1477. else if (m_nMouseAct == MOUSE_TRANSLATE)
  1478. {
  1479. MouseTranslate(nFlags, point.x, point.y);
  1480. }
  1481. else if (m_nMouseAct == MOUSE_SELECT)
  1482. {
  1483. if (m_nSelectObject == 1)
  1484. {
  1485. int x,y;
  1486.                 int j;
  1487.                 //float slope;
  1488.                 //float a,b,c;//斜率
  1489.                 //float tempx,tempx1,tempx2,tempz,tempz1,tempz2,templength;
  1490.                 
  1491.                 float length=INFINITY;  //当无穷大用;
  1492. GLdouble modelMatrix[16];
  1493. GLdouble projMatrix[16];
  1494. GLint viewport[4];
  1495. RECT rect;
  1496. GLdouble dx,dy,dz;
  1497. GLdouble sx,sy,sz;
  1498. GLdouble ox,oy,oz;
  1499. float tmpf;
  1500. x = point.x;
  1501. y = point.y;
  1502.                
  1503.                 glMatrixMode(GL_MODELVIEW);
  1504. glPushMatrix();
  1505. glTranslatef(0.0f, TABLE_HEIGHT/2.0+2.0*BALL_RADIUS, 0.0f);
  1506. glTranslatef(m_fPosition[0][0],m_fPosition[0][1],m_fPosition[0][2]);
  1507. GetClientRect(&rect);
  1508. y = rect.bottom-y;
  1509. glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
  1510. glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
  1511. glGetIntegerv(GL_VIEWPORT,viewport);
  1512. gluProject(m_fDirection[0],0.0f,m_fDirection[2],
  1513. modelMatrix,projMatrix,viewport,
  1514. &sx,&sy,&sz);
  1515. gluProject(0.0f,0.0f,0.0f,
  1516. modelMatrix,projMatrix,viewport,
  1517. &ox,&oy,&oz);
  1518. tmpf =  sqrt(((float)x-ox)*((float)x-ox)+((float)y-oy)*((float)y-oy))/
  1519. sqrt((sx-ox)*(sx-ox)+(sy-oy)*(sy-oy));
  1520. gluProject(tmpf*m_fDirection[0],0.0f,tmpf*m_fDirection[2],
  1521. modelMatrix,projMatrix,viewport,
  1522. &sx,&sy,&sz);
  1523. gluUnProject((float)x,(float)y,sz,
  1524. modelMatrix,projMatrix,viewport,
  1525. &dx,&dy,&dz);
  1526. m_fDirection[0] = dx;
  1527. m_fDirection[2] = dz;
  1528. m_fStrength = tmpf = sqrt(dx*dx+dz*dz);
  1529.                 /*
  1530.  if(dx!=0){
  1531.                     slope=dz/dx;
  1532.                     for (j=0; j<NUM_BALL; j++)
  1533.                         if (m_bVisible[j]){
  1534.                             b=-(2.0*m_fPosition[j][0]+2.0*m_fPosition[0][0]*slope-(2.0*m_fPosition[j][2]*slope));
  1535.                             a=slope;
  1536.                             c=m_fPosition[j][0]*m_fPosition[j][0]*slope*slope-2.0*m_fPosition[0][2]*m_fPosition[j][2]*slope+m_fPosition[j][2]*m_fPosition[j][2]-4.0*BALL_RADIUS*BALL_RADIUS;
  1537.                             if(b*b-4*a*c>0){
  1538.                                 tempx2=(-b-sqrt(b*b-4*a*c))/(2*a);
  1539.                                 tempx1=(-b+sqrt(b*b-4*a*c))/(2*a);
  1540.                                 tempx=fabs(tempx1-m_fPosition[0][0])-fabs(tempx2-m_fPosition[0][0])<0?tempx1:tempx2;
  1541.                                 tempz=slope*(tempx-m_fPosition[0][0])+m_fPosition[0][2];
  1542.                                 templength=sqrt((tempx-m_fPosition[0][0])*(tempx-m_fPosition[0][0])+(tempz-m_fPosition[0][2])*(tempz-m_fPosition[0][2]));
  1543.                                 if(length>templength){
  1544.                                     length=templength;
  1545.                                     hitx=tempx;
  1546.                                     hitz=tempz;
  1547.                                 }
  1548.                             }
  1549.                         }
  1550.                 }
  1551.                 else{
  1552.                     for (j=0; j<NUM_BALL; j++)
  1553.                         if (m_bVisible[j]){
  1554.                             if(fabs(m_fPosition[j][2]-m_fPosition[0][2])<2*BALL_RADIUS){
  1555.                                 tempz1=m_fPosition[j][2]-sqrt(4*BALL_RADIUS*BALL_RADIUS-(m_fPosition[0][0]-m_fPosition[j][0])*(m_fPosition[0][0]-m_fPosition[j][0]));
  1556.                                 tempz2=m_fPosition[j][2]+sqrt(4*BALL_RADIUS*BALL_RADIUS-(m_fPosition[0][0]-m_fPosition[j][0])*(m_fPosition[0][0]-m_fPosition[j][0]));
  1557.                                 tempz=fabs(tempz1-m_fPosition[0][2])-fabs(tempz2-m_fPosition[0][2])<0?tempz1:tempz2;
  1558.                                 templength=fabs(m_fPosition[j][2]-m_fPosition[0][2]);
  1559.                                 if(length>templength){
  1560.                                     length=templength;
  1561.                                     hitx=m_fPosition[0][0];
  1562.                                     hitz=tempz;
  1563.                                 }
  1564.                             }
  1565.                         }
  1566.                 }
  1567.                 */
  1568.                 VEC3_V_OP_S(m_fDirection,m_fDirection,/,tmpf);
  1569. if (tmpf<16.0)
  1570. tmpf = 16.0;
  1571. else if (tmpf>30.0)
  1572. tmpf = 30.0;
  1573. tmpf = (tmpf-15.0)*2.0/3.0;
  1574. VEC3_V_OP_S(m_fVelocity[0],m_fDirection,*,tmpf);
  1575.                 pView->m_ctlProgressPower.SetPos(DOTPROD3(m_fVelocity[0],m_fVelocity[0]));
  1576. glMatrixMode(GL_MODELVIEW);
  1577. glPopMatrix();
  1578. DrawScene();
  1579. }
  1580. }
  1581. }
  1582. CView::OnMouseMove(nFlags, point);
  1583. }
  1584. void CBilliardsPlayView::OnLButtonDown(UINT nFlags, CPoint point) 
  1585. {
  1586.     m_spinAngle = 0.0f;
  1587. if (m_nMouseAct == MOUSE_SPIN)
  1588. {
  1589. MouseSpinGlobal(nFlags, point.x, point.y, 1);
  1590. }
  1591. else if (m_nMouseAct == MOUSE_ZOOM)
  1592. {
  1593. MouseZoom(nFlags, point.x, point.y);
  1594. }
  1595. else if (m_nMouseAct == MOUSE_TRANSLATE)
  1596. {
  1597. MouseTranslate(nFlags, point.x, point.y);
  1598. }
  1599. else if (m_nMouseAct == MOUSE_SELECT)
  1600. {
  1601. if (m_bCanHit)
  1602. {
  1603. int hit = MouseSelect(point.x, point.y);
  1604. if (hit>0)
  1605. {
  1606. GLuint name[3];
  1607. m_nSelectObject = GetSelectObjFront(hit, name);
  1608. }
  1609. }
  1610. }
  1611.     
  1612. CView::OnLButtonDown(nFlags, point);
  1613. /*
  1614.     MSG msg;
  1615. while(::PeekMessage( &msg, m_hWnd,
  1616. WM_MOUSEMOVE, WM_MOUSEMOVE,
  1617. PM_REMOVE))
  1618. ;
  1619. */
  1620. m_bLButtonDown = TRUE;
  1621. }
  1622. void CBilliardsPlayView::OnLButtonUp(UINT nFlags, CPoint point) 
  1623. {
  1624.     m_bLButtonDown = FALSE;
  1625. m_nSelectObject = -1;
  1626. /*
  1627. MSG msg;
  1628. while(::PeekMessage( &msg, m_hWnd,
  1629. WM_MOUSEMOVE, WM_MOUSEMOVE,
  1630. PM_REMOVE))
  1631. ;
  1632. */
  1633. CView::OnLButtonUp(nFlags, point);
  1634. }
  1635. int CBilliardsPlayView::MouseSelect(int x, int y)
  1636. {
  1637. RECT rect;
  1638. float winH;
  1639. GLint viewport[4];
  1640. glSelectBuffer(100, m_SelectBuffer);
  1641. glRenderMode(GL_SELECT);
  1642. glInitNames();
  1643. glMatrixMode(GL_PROJECTION);
  1644. glPushMatrix();
  1645. glLoadIdentity();
  1646. GetClientRect( &rect);
  1647. winH =(float)rect.bottom;
  1648. winH =(winH == 0.0f) ? 1.0f : winH;
  1649. glGetIntegerv(GL_VIEWPORT,viewport);
  1650. gluPickMatrix(x, winH - y, 5, 5, viewport);
  1651. glFrustum(-m_fViewWidth,  m_fViewWidth,
  1652.       -m_fViewHeight, m_fViewHeight,
  1653.       m_fViewNear,    m_fViewFar );
  1654. gluLookAt(m_sEye.origin.x,m_sEye.origin.y,m_sEye.origin.z,
  1655.   m_sEye.center.x,m_sEye.center.y,m_sEye.center.z,
  1656.   m_sEye.up.x,m_sEye.up.y,m_sEye.up.z);
  1657. glMatrixMode(GL_MODELVIEW);
  1658. glPushMatrix();
  1659. DrawScene();
  1660. glMatrixMode(GL_MODELVIEW);
  1661. glPopMatrix();
  1662.   
  1663. glMatrixMode(GL_PROJECTION);
  1664. glPopMatrix();
  1665. glMatrixMode(GL_MODELVIEW);
  1666. int selecthit = glRenderMode(GL_RENDER);
  1667. return selecthit;
  1668. }
  1669. int CBilliardsPlayView::GetSelectObjFront(int selecthits, unsigned int *name)
  1670. {
  1671. int i,j,result;
  1672. GLuint mindepth;
  1673. if (selecthits<=0)
  1674. return -1;
  1675. result=0;
  1676. j=0;
  1677. mindepth=0;
  1678. mindepth=~mindepth;
  1679. for (i=0;i<selecthits;i++)
  1680. {
  1681. if (m_SelectBuffer[j]<=0)
  1682. {
  1683. j=j+3+m_SelectBuffer[j];
  1684. continue;
  1685. }
  1686. if (m_SelectBuffer[j+1]<mindepth)
  1687. {
  1688. mindepth=m_SelectBuffer[j+1];
  1689. result=j;
  1690. }
  1691. j=j+3+m_SelectBuffer[j];
  1692. }
  1693. if (m_SelectBuffer[result]<=0)
  1694. return -1;
  1695. for (i=0;i<m_SelectBuffer[result];i++)
  1696. name[i]=m_SelectBuffer[result+i+3];
  1697. return m_SelectBuffer[result];
  1698. }
  1699. void CBilliardsPlayView::HitBall()
  1700. {
  1701. if (!m_bAnimate)
  1702. {
  1703. m_nEnter = 100;
  1704. CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
  1705. CBilliardsPlayView *pView = (CBilliardsPlayView *)(pFrame->m_wndSplitter).GetPane(0,1);
  1706. CBilliardsDoc *pDoc = (CBilliardsDoc *)pView->GetDocument();
  1707. if (pDoc->m_bPlayer!=IDLE)
  1708. {
  1709. m_nEnter = -1;
  1710. if (m_bCanHit)
  1711.                 pDoc->SendVelocity(m_fVelocity[0]);
  1712.         }
  1713. m_bAnimate = TRUE;
  1714. SetTimer(ANIMATE_TIMER,5,NULL);
  1715. }
  1716. }
  1717. void CBilliardsPlayView::OnTimer(UINT nIDEvent) 
  1718. {
  1719.     CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
  1720. CBilliardsPlayView *pView = (CBilliardsPlayView *)(pFrame->m_wndSplitter).GetPane(0,1);
  1721.     if (nIDEvent == ANIMATE_TIMER){
  1722.    DrawScene();
  1723. //Invalidate();
  1724.         pView->GetDlgItem(IDC_BUTTON_SHOOT)->EnableWindow(m_bCanHit&&!m_bAnimate);
  1725.     }
  1726. CView::OnTimer(nIDEvent);
  1727. }