?? billiardsplayview.cpp
字號(hào):
centerX = w / 2.0f;
centerY = h / 2.0f;
radius = (w > h) ? centerY : centerX ;
if (init)
{
Trackball( 0, radius, centerX, centerY, (float)x, (float)(h-y),
m_spinAxes[0], m_spinAxes[1], m_spinAxes[2], m_spinAngle);
}
else if ( nFlags & MK_LBUTTON )
{
Trackball(1, radius, centerX, centerY, (float)x, (float)(h-y),
m_spinAxes[0], m_spinAxes[1], m_spinAxes[2], m_spinAngle);
SpinGlobal(m_spinAxes, m_spinAngle);
}
}
void CBilliardsPlayView::SpinGlobal(float axes[], float angle)
{
EyeRotate(m_vProjectCenter, axes, angle, m_sEye );
DrawScene();
}
void CBilliardsPlayView::MouseZoom(UINT nFlags, int x, int y)
{
float w, h;
float scale;
static float oldY, dy ;
RECT rect;
GetClientRect( &rect);
w =(float)rect.right;
h =(float)rect.bottom;
h = (h == 0.0f) ? 1.0f : h;
if( m_bLButtonDown && (nFlags & MK_LBUTTON))
{
dy=oldY-y;
scale=1.0f+dy/h;
Zoom(scale);
oldY=(float)y;
}
else
{
oldY=(float)y;
}
}
void CBilliardsPlayView::Zoom(float scale)
{
double a[1][2];
a[0][0] = 1.0;
a[0][1] = 2.0;
double *p;
p = (double *)a;
if(scale<0.001) return;
m_fViewHeight/=scale;
Project();
DrawScene();
}
void CBilliardsPlayView::MouseTranslate(UINT nFlags, int x, int y)
{
float w, h;
static float oldX, oldY, dx, dy, d[3] ;
RECT rect;
GetClientRect( &rect);
w =(float)rect.right;
h =(float)rect.bottom;
h = (h == 0.0f) ? 1.0f : h;
if( m_bLButtonDown && (nFlags & MK_LBUTTON))
{
dx=x-oldX;
dy=oldY-y;
d[0]=m_fViewWidth*dx/w*0.8f;
d[1]=m_fViewHeight*dy/h*0.8f;
d[2]=0.0f;
Translate(d);
oldX=(float)x;
oldY=(float)y;
}
else
{
oldX=(float)x;
oldY=(float)y;
}
}
void CBilliardsPlayView::Translate(float d[])
{
EyeTranslate(d, m_sEye);
DrawScene();
}
void CBilliardsPlayView::OnMouseMove(UINT nFlags, CPoint point)
{
CBilliardsFormView *pView=(CBilliardsFormView *)(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,1));
if ( nFlags & MK_LBUTTON )
{
if (m_nMouseAct == MOUSE_SPIN)
{
MouseSpinGlobal(nFlags, point.x, point.y, 0);
}
else if (m_nMouseAct == MOUSE_ZOOM)
{
MouseZoom(nFlags, point.x, point.y);
}
else if (m_nMouseAct == MOUSE_TRANSLATE)
{
MouseTranslate(nFlags, point.x, point.y);
}
else if (m_nMouseAct == MOUSE_SELECT)
{
if (m_nSelectObject == 1)
{
int x,y;
int j;
//float slope;
//float a,b,c;//斜率
//float tempx,tempx1,tempx2,tempz,tempz1,tempz2,templength;
float length=INFINITY; //當(dāng)無窮大用;
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];
RECT rect;
GLdouble dx,dy,dz;
GLdouble sx,sy,sz;
GLdouble ox,oy,oz;
float tmpf;
x = point.x;
y = point.y;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0f, TABLE_HEIGHT/2.0+2.0*BALL_RADIUS, 0.0f);
glTranslatef(m_fPosition[0][0],m_fPosition[0][1],m_fPosition[0][2]);
GetClientRect(&rect);
y = rect.bottom-y;
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
glGetIntegerv(GL_VIEWPORT,viewport);
gluProject(m_fDirection[0],0.0f,m_fDirection[2],
modelMatrix,projMatrix,viewport,
&sx,&sy,&sz);
gluProject(0.0f,0.0f,0.0f,
modelMatrix,projMatrix,viewport,
&ox,&oy,&oz);
tmpf = sqrt(((float)x-ox)*((float)x-ox)+((float)y-oy)*((float)y-oy))/
sqrt((sx-ox)*(sx-ox)+(sy-oy)*(sy-oy));
gluProject(tmpf*m_fDirection[0],0.0f,tmpf*m_fDirection[2],
modelMatrix,projMatrix,viewport,
&sx,&sy,&sz);
gluUnProject((float)x,(float)y,sz,
modelMatrix,projMatrix,viewport,
&dx,&dy,&dz);
m_fDirection[0] = dx;
m_fDirection[2] = dz;
m_fStrength = tmpf = sqrt(dx*dx+dz*dz);
/*
if(dx!=0){
slope=dz/dx;
for (j=0; j<NUM_BALL; j++)
if (m_bVisible[j]){
b=-(2.0*m_fPosition[j][0]+2.0*m_fPosition[0][0]*slope-(2.0*m_fPosition[j][2]*slope));
a=slope;
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;
if(b*b-4*a*c>0){
tempx2=(-b-sqrt(b*b-4*a*c))/(2*a);
tempx1=(-b+sqrt(b*b-4*a*c))/(2*a);
tempx=fabs(tempx1-m_fPosition[0][0])-fabs(tempx2-m_fPosition[0][0])<0?tempx1:tempx2;
tempz=slope*(tempx-m_fPosition[0][0])+m_fPosition[0][2];
templength=sqrt((tempx-m_fPosition[0][0])*(tempx-m_fPosition[0][0])+(tempz-m_fPosition[0][2])*(tempz-m_fPosition[0][2]));
if(length>templength){
length=templength;
hitx=tempx;
hitz=tempz;
}
}
}
}
else{
for (j=0; j<NUM_BALL; j++)
if (m_bVisible[j]){
if(fabs(m_fPosition[j][2]-m_fPosition[0][2])<2*BALL_RADIUS){
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]));
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]));
tempz=fabs(tempz1-m_fPosition[0][2])-fabs(tempz2-m_fPosition[0][2])<0?tempz1:tempz2;
templength=fabs(m_fPosition[j][2]-m_fPosition[0][2]);
if(length>templength){
length=templength;
hitx=m_fPosition[0][0];
hitz=tempz;
}
}
}
}
*/
VEC3_V_OP_S(m_fDirection,m_fDirection,/,tmpf);
if (tmpf<16.0)
tmpf = 16.0;
else if (tmpf>30.0)
tmpf = 30.0;
tmpf = (tmpf-15.0)*2.0/3.0;
VEC3_V_OP_S(m_fVelocity[0],m_fDirection,*,tmpf);
pView->m_ctlProgressPower.SetPos(DOTPROD3(m_fVelocity[0],m_fVelocity[0]));
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
DrawScene();
}
}
}
CView::OnMouseMove(nFlags, point);
}
void CBilliardsPlayView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_spinAngle = 0.0f;
if (m_nMouseAct == MOUSE_SPIN)
{
MouseSpinGlobal(nFlags, point.x, point.y, 1);
}
else if (m_nMouseAct == MOUSE_ZOOM)
{
MouseZoom(nFlags, point.x, point.y);
}
else if (m_nMouseAct == MOUSE_TRANSLATE)
{
MouseTranslate(nFlags, point.x, point.y);
}
else if (m_nMouseAct == MOUSE_SELECT)
{
if (m_bCanHit)
{
int hit = MouseSelect(point.x, point.y);
if (hit>0)
{
GLuint name[3];
m_nSelectObject = GetSelectObjFront(hit, name);
}
}
}
CView::OnLButtonDown(nFlags, point);
/*
MSG msg;
while(::PeekMessage( &msg, m_hWnd,
WM_MOUSEMOVE, WM_MOUSEMOVE,
PM_REMOVE))
;
*/
m_bLButtonDown = TRUE;
}
void CBilliardsPlayView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bLButtonDown = FALSE;
m_nSelectObject = -1;
/*
MSG msg;
while(::PeekMessage( &msg, m_hWnd,
WM_MOUSEMOVE, WM_MOUSEMOVE,
PM_REMOVE))
;
*/
CView::OnLButtonUp(nFlags, point);
}
int CBilliardsPlayView::MouseSelect(int x, int y)
{
RECT rect;
float winH;
GLint viewport[4];
glSelectBuffer(100, m_SelectBuffer);
glRenderMode(GL_SELECT);
glInitNames();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
GetClientRect( &rect);
winH =(float)rect.bottom;
winH =(winH == 0.0f) ? 1.0f : winH;
glGetIntegerv(GL_VIEWPORT,viewport);
gluPickMatrix(x, winH - y, 5, 5, viewport);
glFrustum(-m_fViewWidth, m_fViewWidth,
-m_fViewHeight, m_fViewHeight,
m_fViewNear, m_fViewFar );
gluLookAt(m_sEye.origin.x,m_sEye.origin.y,m_sEye.origin.z,
m_sEye.center.x,m_sEye.center.y,m_sEye.center.z,
m_sEye.up.x,m_sEye.up.y,m_sEye.up.z);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
DrawScene();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
int selecthit = glRenderMode(GL_RENDER);
return selecthit;
}
int CBilliardsPlayView::GetSelectObjFront(int selecthits, unsigned int *name)
{
int i,j,result;
GLuint mindepth;
if (selecthits<=0)
return -1;
result=0;
j=0;
mindepth=0;
mindepth=~mindepth;
for (i=0;i<selecthits;i++)
{
if (m_SelectBuffer[j]<=0)
{
j=j+3+m_SelectBuffer[j];
continue;
}
if (m_SelectBuffer[j+1]<mindepth)
{
mindepth=m_SelectBuffer[j+1];
result=j;
}
j=j+3+m_SelectBuffer[j];
}
if (m_SelectBuffer[result]<=0)
return -1;
for (i=0;i<m_SelectBuffer[result];i++)
name[i]=m_SelectBuffer[result+i+3];
return m_SelectBuffer[result];
}
void CBilliardsPlayView::HitBall()
{
if (!m_bAnimate)
{
m_nEnter = 100;
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CBilliardsPlayView *pView = (CBilliardsPlayView *)(pFrame->m_wndSplitter).GetPane(0,1);
CBilliardsDoc *pDoc = (CBilliardsDoc *)pView->GetDocument();
if (pDoc->m_bPlayer!=IDLE)
{
m_nEnter = -1;
if (m_bCanHit)
pDoc->SendVelocity(m_fVelocity[0]);
}
m_bAnimate = TRUE;
SetTimer(ANIMATE_TIMER,5,NULL);
}
}
void CBilliardsPlayView::OnTimer(UINT nIDEvent)
{
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CBilliardsPlayView *pView = (CBilliardsPlayView *)(pFrame->m_wndSplitter).GetPane(0,1);
if (nIDEvent == ANIMATE_TIMER){
DrawScene();
//Invalidate();
pView->GetDlgItem(IDC_BUTTON_SHOOT)->EnableWindow(m_bCanHit&&!m_bAnimate);
}
CView::OnTimer(nIDEvent);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -