簡體   English   中英

如何使用glkit,opengl es2繪制數千個正方形?

[英]How do I draw thousands of squares with glkit, opengl es2?

我正在嘗試在屏幕上繪制20萬個正方形。 或基本上是很多正方形。 我相信我只是在打很多抽獎電話,這正在削弱應用程序的性能。 正方形僅在按下按鈕時更新,因此不必一定每幀都更新一次。

這是我現在擁有的代碼:

- (void)glkViewControllerUpdate:(GLKViewController *)controller
{

//static float transY = 0.0f;
//float y = sinf(transY)/2.0f;
//transY += 0.175f;

GLKMatrix4 modelview = GLKMatrix4MakeTranslation(0, 0, -5.f);
effect.transform.modelviewMatrix = modelview;

//GLfloat ratio = self.view.bounds.size.width/self.view.bounds.size.height;
GLKMatrix4 projection = GLKMatrix4MakeOrtho(0, 768, 1024, 0, 0.1f, 20.0f);    
effect.transform.projectionMatrix = projection;
_isOpenGLViewReady = YES;
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
if(_model.updateView && _isOpenGLViewReady)
{

    glClear(GL_COLOR_BUFFER_BIT);
    [effect prepareToDraw];
    int pixelSize = _model.pixelSize;
    if(!_model.isReady)
        return;
    //NSLog(@"UPDATING: %d, %d", _model.rows, _model.columns);
    for(int i = 0; i < _model.rows; i++)
    {
        for(int ii = 0; ii < _model.columns; ii++)
        {
            ColorModel *color = [_model getColorAtRow:i andColumn:ii];
            CGRect rect = CGRectMake(ii * pixelSize, i*pixelSize, pixelSize, pixelSize);
            //[self drawRectWithRect:rect withColor:c];
            GLubyte squareColors[] = {
                color.red, color.green, color.blue, 255,
                color.red, color.green, color.blue, 255,
                color.red, color.green, color.blue, 255,
                color.red, color.green, color.blue, 255
            };

            // NSLog(@"Drawing color with red: %d", color.red);


            int xVal = rect.origin.x;
            int yVal = rect.origin.y;
            int width = rect.size.width;
            int height = rect.size.height;
            GLfloat squareVertices[] = {
                xVal, yVal, 1,
                xVal + width, yVal, 1,
                xVal,  yVal + height, 1,
                xVal + width,  yVal + height, 1
            };    

            glEnableVertexAttribArray(GLKVertexAttribPosition);
            glEnableVertexAttribArray(GLKVertexAttribColor);

            glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, squareVertices);
            glVertexAttribPointer(GLKVertexAttribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, squareColors);

            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

            glDisableVertexAttribArray(GLKVertexAttribPosition);
            glDisableVertexAttribArray(GLKVertexAttribColor);


        }
    }   
    _model.updateView = YES;
}

首先,您真的需要繪制20萬個正方形嗎? 您的視口總共只有786,000像素。 您可能能夠減少繪制對象的數量,而不會顯着影響場景的整體質量。

也就是說,如果這些是較小的正方形,則可以將其繪制為像素大小足以覆蓋正方形區域的點。 這將需要在頂點着色器中將gl_PointSize設置為適當的像素寬度。 然后,您可以生成坐標並將其全部發送為GL_POINTS,以一次進行繪制。 這將消除三角形額外幾何圖形的開銷以及您在此處使用的各個繪圖調用。

即使您不使用點,仍然要先計算所需的所有三角形幾何體,然后在一次繪制調用中將所有幾何體發送出去,這仍然是一個好主意。 這將大大減少您的OpenGL ES API調用開銷。

您可能要考慮的另一件事是使用頂點緩沖區對象來存儲此幾何。 如果幾何是靜態的,則可以避免在每個繪制的框架上發送它,或者僅更新已更改的部分。 即使您只是每幀更改一次數據,我相信使用VBO進行動態幾何處理在現代iOS設備上也具有性能優勢。

您不能嘗試以某種方式對其進行優化嗎? 我對圖形類型的東西並不十分熟悉,但是我想如果您要繪制200,000個正方形,那么所有這些形狀實際上都可見的可能性似乎很小。 您是否可以為mySquare類添加某種isVisible標簽,該標簽確定您要繪制的正方形是否實際可見? 然后,顯而易見的下一步是修改繪圖功能,以便在看不見正方形的情況下不要繪制它。

還是您正在要求某人嘗試改進當前的代碼,因為如果您的性能與您所說的一樣糟糕,那么我認為對上述代碼進行少量更改不會解決您的問題。 您將不得不重新考慮自己的繪圖方式。

看起來您的代碼實際想要執行的操作是拍攝_model.rows × _model.columns 2D圖像並將其按_model.pixelSize放大_model.pixelSize 如果-[ColorModel getColorAtRow:andColumn:]一次從一個顏色值數組中檢索3個字節,那么您可能要考慮將該顏色值數組作為GL_RGB / GL_UNSIGNED_BYTE數據GL_RGB OpenGL紋理中,並讓GPU放大一次顯示所有像素。

另外,如果按比例放大ColorModel的內容是使用OpenGL ES和GLKit的唯一原因,則最好將顏色值包裝到CGImage中,並允許UIKit和Core Animation為您繪制圖形。 ColorModel的顏色值多久更新一次?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM