UIScrollView 中的平滑缩放 - 如何在 OpenGL ES 1.1 中实现?

发布于 2024-11-13 16:48:48 字数 6680 浏览 0 评论 0原文

我有两个 UIScrollView,它们应该同步滚动和重绘。 另外,我还有 EAGLView 视图(在另一个视图上),其中绘制了这两个 UIScrollView 的内容。 EAGLView 视图具有 UIScrollView 视图数组,并为每个视图激活绘图功能。

[self BeginRedrawWindow];
for (unsigned int nView = 0; nView < nCountViews; nView++)
{
curView = &(views[nView]);
rcViewFrame = [curView->m_pDrawView GetFrameRect];
rcClipRect = CGRectMake(rcViewFrame.origin.x - rcOwnFrame.origin.x, 
rcViewFrame.origin.y - rcOwnFrame.origin.y, 
rcViewFrame.size.width, 
rcViewFrame.size.height);
curView->m_DrawContext.BeginDrawing(rcClipRect.origin.x, rcClipRect.origin.y); //just reset some params
curView->m_DrawContext.SetClipRect(&rcClipRect);
[curView->m_pDrawView DrawInToDeviceContext:&(curView->m_DrawContext)];
}
[self EndRedrawWindow];

其中

- (void)BeginRedrawWindow
{
glViewport(0, 0, m_nBackingWidth, m_nBackingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, (GLfloat)m_nBackingWidth, 0.0f, (GLfloat)m_nBackingHeight, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Clears the view with black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:m_context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_viewFramebuffer);
glClear(GL_COLOR_BUFFER_BIT);
//correct matrix pos
glTranslatef(0, (CGFloat)m_nBackingHeight, 0);
}

- (void)EndRedrawWindow
{
ImgSize curImgSize = _skinMgr.GetCurrentImgSize();
glBindTexture(GL_TEXTURE_2D, m_cellsTextureArr[curImgSize]);
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
// Set a blending function to use
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Enable blending
glEnable(GL_BLEND);
GLDrawItem* curView;
for (unsigned int nView = 0; nView < nCountViews; nView++)
{
curView = &(views[nView]);
[self ApplyContextData:&(curView->m_DrawContext)];
}

glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_viewRenderbuffer);
[m_context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

- (void)ApplyContextData:(GLDrawContext*)ctx
{
CGRect clipRect;
BOOL bEnableClipRect = ctx->GetClipRect(&clipRect);
//set clip rect
if (bEnableClipRect)
{
glEnable(GL_SCISSOR_TEST);
glScissor(clipRect.origin.x, m_nBackingHeight - clipRect.origin.y - clipRect.size.height, clipRect.size.width, clipRect.size.height);
}
const unsigned int nCountItems = ctx->GetCountCells();
if (nCountItems > 0)
{
const GLfloat* verticesBack = ctx->GetVertices_Back();
const GLfloat* texcoordsBack = ctx->GetTexcoords_Back();
glVertexPointer(2, GL_FLOAT, 0, verticesBack);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoordsBack);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, POINTS_PER_CELL * nCountItems);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
//disable clip rect
if (bEnableClipRect)
{
glDisable(GL_SCISSOR_TEST);
}
}

UIScrollView 的绘图函数大致如下所示:

//go throuth lines
for (int nCellY = firstDrawCell.y; nCellY < m_nCountRows; nCellY++)
{
//go throuth cell by x axe
for (int nCellX = firstDrawCell.x; nCellX < m_nCountCols; nCellX++)
{
CCell* pCurCell = m_pPuzzleBoard->GetCell(eMainCells, nCellX, nCellY);
bool bSelected = false;
//
if (m_bPressedOnCell)
{
if ((m_PressedCell.x == nCellX) || (m_PressedCell.y == nCellY))
bSelected = true;
}
ImgCellType cellType = _skinMgr.GetCellTypeFromState(pCurCell->m_eState, bSelected);
pDrawCtx->AddNewCell(cellType, curDrawPoint.x, curDrawPoint.y);
//for separator
if (((nCellX + 1) < m_nCountCols) && ((nCellX + 1) % m_GridStep.cx == 0))
{
curDrawPoint.x += m_nGridSize;
}
curDrawPoint.x += m_cellSize.width;
//
if (curDrawPoint.x >= rect.size.width)
break;
} 
//for separator
if (((nCellY + 1) < m_nCountRows) && ((nCellY + 1) % m_GridStep.cy == 0))
{
curDrawPoint.y += m_nGridSize;
}
curDrawPoint.y += m_cellSize.height;
curDrawPoint.x = firstDrawCellPoint.x;
//
if (curDrawPoint.y >= rect.size.height)
break;
}

BOOL GLDrawContext:: AddNewCell(ImgCellType cellType, float posX, float posY)
{
ImgElDimensionsF cellDimensions = _skinMgr.GL_GetCurrerntCellDimensions(cellType);
ImgTextureInfoF imgCellInfo = m_pCellTextureInfo[cellType];
if (m_nCurAddCrossCell >= m_nMaxCountCrossCell)
return FALSE;
const int idx = m_nCurAddCrossCell * ELEMENTS_PER_CELL;
//*-------
//|  3--4
//|  |  |
//|  1--2
//*
/* 1X */
m_spriteVerticesCross[idx + 0] = posX;
/* 1Y */
m_spriteVerticesCross[idx + 1] =  -posY - cellDimensions.fHeight;
/* 2X */
m_spriteVerticesCross[idx + 2] = posX + cellDimensions.fWidth;
/* 2Y */
m_spriteVerticesCross[idx + 3] = -posY - cellDimensions.fHeight;
/* 3X */
m_spriteVerticesCross[idx + 4] = posX;
/* 3Y */
m_spriteVerticesCross[idx + 5] = -posY;
/* 2X */
m_spriteVerticesCross[idx + 6] = posX + cellDimensions.fWidth;
/* 2Y */
m_spriteVerticesCross[idx + 7] = -posY - cellDimensions.fHeight;
/* 3X */
m_spriteVerticesCross[idx + 8] = posX;
/* 3Y */
m_spriteVerticesCross[idx + 9] = -posY;
/* 4X */
m_spriteVerticesCross[idx + 10] = posX + cellDimensions.fWidth;
/* 4Y */
m_spriteVerticesCross[idx + 11] = -posY;
//1--2
//|  |
//3--4
//
/* 1X */
m_spriteTexcoordsCross[idx + 0] = imgCellInfo.fX;
/* 1Y */
m_spriteTexcoordsCross[idx + 1] = imgCellInfo.fY + imgCellInfo.fHeight;
/* 2X */
m_spriteTexcoordsCross[idx + 2] = imgCellInfo.fX + imgCellInfo.fWidth;
/* 2Y */
m_spriteTexcoordsCross[idx + 3] = imgCellInfo.fY + imgCellInfo.fHeight;
/* 3X */
m_spriteTexcoordsCross[idx + 4] = imgCellInfo.fX;
/* 3Y */
m_spriteTexcoordsCross[idx + 5] = imgCellInfo.fY;
/* 2X */
m_spriteTexcoordsCross[idx + 6] = imgCellInfo.fX + imgCellInfo.fWidth;
/* 2Y */
m_spriteTexcoordsCross[idx + 7] = imgCellInfo.fY + imgCellInfo.fHeight;
/* 3X */
m_spriteTexcoordsCross[idx + 8] = imgCellInfo.fX;
/* 3Y */
m_spriteTexcoordsCross[idx + 9] = imgCellInfo.fY; 
/* 4X */
m_spriteTexcoordsCross[idx + 10] = imgCellInfo.fX + imgCellInfo.fWidth;
/* 4Y */
m_spriteTexcoordsCross[idx + 11] = imgCellInfo.fY;
//
return TRUE;
}

每个 UIScrollView 的内容由单元格组成(如棋盘)。有四个单元格数组,每个数组包含指定大小的单元格。缩放时,选择具有最佳绘图尺寸的纹理,并使用 OpenGL 1.1 进行绘制。所有四个纹理的初始化在绘图窗口初始化时进行。 内容的缩放发生在这些窗口中。缩放值是通过计算出的新距离(由手指通过(移动或会聚))乘以常数系数来形成的:

fNewZoom += fDistance * fCoef;

fDistance 在减小时可以为负值。 消息处理时调用重绘的绘图函数:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

缩放不是逐渐的,而是急剧的。不知怎的,绘图速度非常慢(大约 6-7 fps)。我应该如何进行正确的变焦?如何优化绘图,例如在 iPhone/iPad 填字游戏程序中?使用 OpenGL 1.1 绘制 500 – 600 个小方块真的是非常资源密集型的工作吗?我做错了什么?请帮助我解决这个问题。

I have two UIScrollView, which should be scrolled and redrawn synchronously.
Also I have got EAGLView view (over the other view), where the content of these two UIScrollView is drawn.
EAGLView view has the array of UIScrollView views and activates the drawing function for each of them.

[self BeginRedrawWindow];
for (unsigned int nView = 0; nView < nCountViews; nView++)
{
curView = &(views[nView]);
rcViewFrame = [curView->m_pDrawView GetFrameRect];
rcClipRect = CGRectMake(rcViewFrame.origin.x - rcOwnFrame.origin.x, 
rcViewFrame.origin.y - rcOwnFrame.origin.y, 
rcViewFrame.size.width, 
rcViewFrame.size.height);
curView->m_DrawContext.BeginDrawing(rcClipRect.origin.x, rcClipRect.origin.y); //just reset some params
curView->m_DrawContext.SetClipRect(&rcClipRect);
[curView->m_pDrawView DrawInToDeviceContext:&(curView->m_DrawContext)];
}
[self EndRedrawWindow];

where

- (void)BeginRedrawWindow
{
glViewport(0, 0, m_nBackingWidth, m_nBackingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, (GLfloat)m_nBackingWidth, 0.0f, (GLfloat)m_nBackingHeight, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Clears the view with black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:m_context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_viewFramebuffer);
glClear(GL_COLOR_BUFFER_BIT);
//correct matrix pos
glTranslatef(0, (CGFloat)m_nBackingHeight, 0);
}

- (void)EndRedrawWindow
{
ImgSize curImgSize = _skinMgr.GetCurrentImgSize();
glBindTexture(GL_TEXTURE_2D, m_cellsTextureArr[curImgSize]);
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
// Set a blending function to use
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Enable blending
glEnable(GL_BLEND);
GLDrawItem* curView;
for (unsigned int nView = 0; nView < nCountViews; nView++)
{
curView = &(views[nView]);
[self ApplyContextData:&(curView->m_DrawContext)];
}

glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_viewRenderbuffer);
[m_context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

- (void)ApplyContextData:(GLDrawContext*)ctx
{
CGRect clipRect;
BOOL bEnableClipRect = ctx->GetClipRect(&clipRect);
//set clip rect
if (bEnableClipRect)
{
glEnable(GL_SCISSOR_TEST);
glScissor(clipRect.origin.x, m_nBackingHeight - clipRect.origin.y - clipRect.size.height, clipRect.size.width, clipRect.size.height);
}
const unsigned int nCountItems = ctx->GetCountCells();
if (nCountItems > 0)
{
const GLfloat* verticesBack = ctx->GetVertices_Back();
const GLfloat* texcoordsBack = ctx->GetTexcoords_Back();
glVertexPointer(2, GL_FLOAT, 0, verticesBack);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoordsBack);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, POINTS_PER_CELL * nCountItems);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
//disable clip rect
if (bEnableClipRect)
{
glDisable(GL_SCISSOR_TEST);
}
}

The drawing function for UIScrollView looks approximately like the following:

//go throuth lines
for (int nCellY = firstDrawCell.y; nCellY < m_nCountRows; nCellY++)
{
//go throuth cell by x axe
for (int nCellX = firstDrawCell.x; nCellX < m_nCountCols; nCellX++)
{
CCell* pCurCell = m_pPuzzleBoard->GetCell(eMainCells, nCellX, nCellY);
bool bSelected = false;
//
if (m_bPressedOnCell)
{
if ((m_PressedCell.x == nCellX) || (m_PressedCell.y == nCellY))
bSelected = true;
}
ImgCellType cellType = _skinMgr.GetCellTypeFromState(pCurCell->m_eState, bSelected);
pDrawCtx->AddNewCell(cellType, curDrawPoint.x, curDrawPoint.y);
//for separator
if (((nCellX + 1) < m_nCountCols) && ((nCellX + 1) % m_GridStep.cx == 0))
{
curDrawPoint.x += m_nGridSize;
}
curDrawPoint.x += m_cellSize.width;
//
if (curDrawPoint.x >= rect.size.width)
break;
} 
//for separator
if (((nCellY + 1) < m_nCountRows) && ((nCellY + 1) % m_GridStep.cy == 0))
{
curDrawPoint.y += m_nGridSize;
}
curDrawPoint.y += m_cellSize.height;
curDrawPoint.x = firstDrawCellPoint.x;
//
if (curDrawPoint.y >= rect.size.height)
break;
}

BOOL GLDrawContext:: AddNewCell(ImgCellType cellType, float posX, float posY)
{
ImgElDimensionsF cellDimensions = _skinMgr.GL_GetCurrerntCellDimensions(cellType);
ImgTextureInfoF imgCellInfo = m_pCellTextureInfo[cellType];
if (m_nCurAddCrossCell >= m_nMaxCountCrossCell)
return FALSE;
const int idx = m_nCurAddCrossCell * ELEMENTS_PER_CELL;
//*-------
//|  3--4
//|  |  |
//|  1--2
//*
/* 1X */
m_spriteVerticesCross[idx + 0] = posX;
/* 1Y */
m_spriteVerticesCross[idx + 1] =  -posY - cellDimensions.fHeight;
/* 2X */
m_spriteVerticesCross[idx + 2] = posX + cellDimensions.fWidth;
/* 2Y */
m_spriteVerticesCross[idx + 3] = -posY - cellDimensions.fHeight;
/* 3X */
m_spriteVerticesCross[idx + 4] = posX;
/* 3Y */
m_spriteVerticesCross[idx + 5] = -posY;
/* 2X */
m_spriteVerticesCross[idx + 6] = posX + cellDimensions.fWidth;
/* 2Y */
m_spriteVerticesCross[idx + 7] = -posY - cellDimensions.fHeight;
/* 3X */
m_spriteVerticesCross[idx + 8] = posX;
/* 3Y */
m_spriteVerticesCross[idx + 9] = -posY;
/* 4X */
m_spriteVerticesCross[idx + 10] = posX + cellDimensions.fWidth;
/* 4Y */
m_spriteVerticesCross[idx + 11] = -posY;
//1--2
//|  |
//3--4
//
/* 1X */
m_spriteTexcoordsCross[idx + 0] = imgCellInfo.fX;
/* 1Y */
m_spriteTexcoordsCross[idx + 1] = imgCellInfo.fY + imgCellInfo.fHeight;
/* 2X */
m_spriteTexcoordsCross[idx + 2] = imgCellInfo.fX + imgCellInfo.fWidth;
/* 2Y */
m_spriteTexcoordsCross[idx + 3] = imgCellInfo.fY + imgCellInfo.fHeight;
/* 3X */
m_spriteTexcoordsCross[idx + 4] = imgCellInfo.fX;
/* 3Y */
m_spriteTexcoordsCross[idx + 5] = imgCellInfo.fY;
/* 2X */
m_spriteTexcoordsCross[idx + 6] = imgCellInfo.fX + imgCellInfo.fWidth;
/* 2Y */
m_spriteTexcoordsCross[idx + 7] = imgCellInfo.fY + imgCellInfo.fHeight;
/* 3X */
m_spriteTexcoordsCross[idx + 8] = imgCellInfo.fX;
/* 3Y */
m_spriteTexcoordsCross[idx + 9] = imgCellInfo.fY; 
/* 4X */
m_spriteTexcoordsCross[idx + 10] = imgCellInfo.fX + imgCellInfo.fWidth;
/* 4Y */
m_spriteTexcoordsCross[idx + 11] = imgCellInfo.fY;
//
return TRUE;
}

Content of each UIScrollView consists of cells (like chessboard). There are four arrays of cells, each array contains cells with specified size. While zooming, the sells with the most optimal size for drawing are chosen and these are drawn with the OpenGL 1.1.The initialization of all four textures takes place at the beginning while initialization of drawing windows.
Zoom of the content takes place in these windows. Zoom value is formed by adding of calculated new distance (which is passed by fingers (moving apart or converging)) multiplied on a constant coefficient:

fNewZoom += fDistance * fCoef;

fDistance can have negative value while decreasing.
The drawing function of redrawing is called while message processing:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

Zoom is made not gradually, but sharply. Somehow drawing is very slow (about 6-7 fps). How should I do the right zoom? How can I optimize drawing, for example, like in Crosswords for iPhone/iPad program ? Is drawing of 500 – 600 small squares with the OpenGL 1.1 is really very resource-intensive job? What am I doing wrong? Please, help me to cope with this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文