1. 程式人生 > >MFC+OpenGL 怎麼計算投影區域在視窗中的大小

MFC+OpenGL 怎麼計算投影區域在視窗中的大小

    我用的是正交投影gluOrtho2D以紋理貼圖的方式顯示一張BMP點陣圖,當我滾動滑鼠滾輪時點陣圖會縮小放大;我要求的是紋理貼圖在視窗中的大小,如紅色框大小:

    



    這是我顯示圖片的程式碼:

//貼圖的矩形大小與圖片比例一致
#define MAPWIDTH    20.0
#define MAPHIGHT    GLfloat(20.0 / (1920.0 / 1080.0))
//背景圖
void CDrawView::DrawImage()
{
	glPushMatrix();
	glLoadIdentity();

	glScalef(mScale_, mScale_, 1);    //橫縱座標縮放量
	glTranslatef(mTranslateX_ * mVcWidthRatio_, mTranslateY_ * mVcHightRatio_, 0);    //橫縱座標平移量


	//紋理影象的座標範圍是從(0,0)到(1,1),左下角為(0,0),右上角為(1,1)
	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glTexCoord2d(0.0, 0.0);
	glVertex2d(-MAPWIDTH, -MAPHIGHT);

	glTexCoord2d(1.0, 0.0);
	glVertex2d(MAPWIDTH,  -MAPHIGHT);

	glTexCoord2d(1.0, 1.0);
	glVertex2d(MAPWIDTH,   MAPHIGHT);

	glTexCoord2d(0.0, 1.0);
	glVertex2d(-MAPWIDTH,  MAPHIGHT);
	glEnd();
	glDisable(GL_TEXTURE_2D);

	glPopMatrix();
}

    這是OnSize()函式程式碼:

mViewHalfWidth_ = 12;
mViewHalfHight_ = 12;
void CDrawView::OnSize(UINT nType, int cx, int cy)
{
	CScrollView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	//以下兩行保證視窗大小變化後圖像中心點不發生變化
    mTranslateX_ *= (GLfloat)(cx * 1.0 / mClientWidth_);
    mTranslateY_ *= (GLfloat)(cy * 1.0 / mClientHight_);

    mClientWidth_ = cx;
    mClientHight_ = cy;
    if (mClientHight_==0)		// Prevent A Divide By Zero By 防止除以0
    {
        mClientHight_=1;		// Making Height Equal One
    }
    if (mClientWidth_==0)		// Prevent A Divide By Zero By
    {
        mClientWidth_=1;		// Making Height Equal One
    }

	mVcWidthRatio_ = (GLfloat)(mViewHalfWidth_ * 2 / mClientWidth_);//視窗比(視景體/視窗)
	mVcHightRatio_ = (GLfloat)(mViewHalfHight_ * 2 / mClientHight_);

	wglMakeCurrent(mhDC_, mhGLContext_);
	glViewport(0, 0, cx, cy);				// Reset The Current Viewport
	glMatrixMode(GL_PROJECTION);			// Select The Projection Matrix 選擇投影矩陣
	glLoadIdentity();						// Reset The Projection Matrix

	//改變視窗大小時,使檢視等比縮放  
	//正交投影
	GLdouble glScale = (GLdouble)cx / cy;
	if (cx <= cy)
		gluOrtho2D(-mViewHalfWidth_, mViewHalfWidth_, -mViewHalfHight_ / glScale, mViewHalfHight_ / glScale);
	else
		gluOrtho2D(-mViewHalfWidth_ * glScale, mViewHalfWidth_ * glScale, -mViewHalfHight_, mViewHalfHight_);

	glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
	glLoadIdentity();									   // Reset The Modelview Matrix

	CurrentGLpoint(CPoint(), TRUE);
}
    請問我該怎麼求在視窗中投影的大小(圖片的大小、紅框大小),隨著縮放量改變而改變