void BakedOpRenderer::startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) {
    LOG_ALWAYS_FATAL_IF(mRenderTarget.frameBufferId != 0, "primary framebufferId must be 0");
    mRenderState.bindFramebuffer(0);
    setViewport(width, height);

    if (!mOpaque) {
        clearColorBuffer(repaintRect);
    }

    mRenderState.debugOverdraw(true, true);
}
void BakedOpRenderer::startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) {
    LOG_ALWAYS_FATAL_IF(mRenderTarget.offscreenBuffer, "already has layer...");

    // subtract repaintRect from region, since it will be regenerated
    if (repaintRect.contains(0, 0,
                offscreenBuffer->viewportWidth, offscreenBuffer->viewportHeight)) {
        // repaint full layer, so throw away entire region
        offscreenBuffer->region.clear();
    } else {
        offscreenBuffer->region.subtractSelf(android::Rect(repaintRect.left, repaintRect.top,
                repaintRect.right, repaintRect.bottom));
    }

    mRenderTarget.offscreenBuffer = offscreenBuffer;
    mRenderTarget.offscreenBuffer->hasRenderedSinceRepaint = false;

    // create and bind framebuffer
    mRenderTarget.frameBufferId = mRenderState.createFramebuffer();
    mRenderState.bindFramebuffer(mRenderTarget.frameBufferId);

    // attach the texture to the FBO
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
            offscreenBuffer->texture.id(), 0);
    GL_CHECKPOINT(LOW);

    int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    LOG_ALWAYS_FATAL_IF(status != GL_FRAMEBUFFER_COMPLETE,
            "framebuffer incomplete, status %d, textureId %d, size %dx%d",
            status,
            offscreenBuffer->texture.id(),
            offscreenBuffer->texture.width(),
            offscreenBuffer->texture.height());

    // Change the viewport & ortho projection
    setViewport(offscreenBuffer->viewportWidth, offscreenBuffer->viewportHeight);

    clearColorBuffer(repaintRect);
}
示例#3
0
文件: main.c 项目: jpdoyle/GRender
int main(void) {
    unsigned ticksPerFrame = 1000/FRAMES_PER_SECOND;
    SDL_Surface* screen;
    Context* ct;

    if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        fprintf(stderr,"SDL init failed");
        return -1;
    }
    if(!(screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,
                                   SCREEN_BPP,SDL_SWSURFACE))) {
        fprintf(stderr,"SDL video init failed");
        return -1;
    }
    if(!(ct = createContext(SCREEN_WIDTH,SCREEN_HEIGHT))) {
        fprintf(stderr,"Context init failed");
        return -1;
    }

    ct->depthEnabled = 1;
    ct->cullBackFace = 1;
    ct->frontFace    = WINDING_CCW;

    ct->vertShader = &shadeVertex;
    ct->fragShader = &shadeFragment;

    mat44Perspective(matStackTop(ct->matrices[MATRIX_PROJECTION]),
                     FOVY,SCREEN_WIDTH/(float)SCREEN_HEIGHT,1,30);

    printf("Projection:\n");
    mat44Print(stdout,matStackTop(ct->matrices[MATRIX_PROJECTION]));

    mat44Ident(matStackTop(ct->matrices[MATRIX_MODELVIEW]));

    Mat44 translate;
    mat44Translate(translate,0,0,-4);

    Mat44 rotateVert;
    mat44Ident(rotateVert);

    Mat44 tetraTransform;
    mat44Ident(tetraTransform);

    float tetraHorizRot = 0,
          tetraVertRot  = 0;

    VertexArray* varr = createVertArray(0,NULL);
    varr->locs      = &verts[0].loc;
    varr->locStep   = sizeof(CustomVert);
    varr->colors    = &verts[0].color;
    varr->colorStep = sizeof(CustomVert);

    Color4 black = { 0,0,0,0 };

    CustomVert* gridVerts;
    unsigned*   gridIndices;
    unsigned    gridNumEdges;
    makeGrid(&gridVerts,&gridIndices,&gridNumEdges,
             -6,-6,6,6,12,12);

    VertexArray* gridArr = createVertArray(0,NULL);

    gridArr->locs      = &gridVerts[0].loc;
    gridArr->locStep   = sizeof(CustomVert);
    gridArr->colors    = &gridVerts[0].color;
    gridArr->colorStep = sizeof(CustomVert);

    Mat44 gridTransform;
    mat44Translate(gridTransform,0,-5,0);

    SDL_WM_SetCaption(TITLE,NULL);

    char title[256];
    float fps = 0;
    float fpsLerp = 0.2;

    int numViewports = 1;

    int mouseCaptured = 0;
    int running = 1;
    int debugged = 0;
    while(running) {
        Uint32 startTicks = SDL_GetTicks();

        int vertRot  = 0,
            horizRot = 0;

        SDL_Event event;
        while(SDL_PollEvent(&event)) {
            switch(event.type) {
                case SDL_QUIT:
                    running = 0;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym) {
                    case SDLK_ESCAPE:
                        mouseCaptured = !mouseCaptured;
                        SDL_ShowCursor(mouseCaptured ? SDL_DISABLE :
                                                       SDL_ENABLE);
                        break;
                    case SDLK_p:
                        debugged = 0;
                        break;
                    case SDLK_BACKQUOTE:
                        if(numViewports == 1) {
                            numViewports = 2;
                        } else {
                            numViewports = 1;
                        }
                        mat44Perspective(matStackTop(ct->matrices[MATRIX_PROJECTION]),
                                         FOVY,SCREEN_WIDTH/(float)numViewports/SCREEN_HEIGHT,1,30);
                        break;
                    default:
                        break;
                    }
                    break;
                case SDL_MOUSEMOTION:
                    if(mouseCaptured) {
                        SDL_WarpMouse(SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
                        horizRot = -event.motion.xrel,
                        vertRot  = event.motion.yrel;
                    }
                    break;
                default:
                    break;
            }
        }

        if(!debugged) {
            printf("\nDebugging!\n\n");
        }

        clearColorBuffer(ct);
        fillDepthBuffer(ct,1);
        /* fillBuffers(ct,black,1); */
        
        Uint8* keys = SDL_GetKeyState(NULL);

        if(!mouseCaptured) {
            vertRot = horizRot = 0;
            if(keys[SDLK_UP]) {
                vertRot += 1;
            }
            if(keys[SDLK_DOWN]) {
                vertRot -= 1;
            }

            if(keys[SDLK_RIGHT]) {
                horizRot += 1;
            }
            if(keys[SDLK_LEFT]) {
                horizRot -= 1;
            }
        }

        int xDelta = 0,
            yDelta = 0,
            zDelta = 0;
        if(keys[SDLK_w]) { 
            zDelta -= 1;
        }
        if(keys[SDLK_s]) {
            zDelta += 1;
        }
        if(keys[SDLK_a]) { 
            xDelta -= 1;
        }
        if(keys[SDLK_d]) {
            xDelta += 1;
        }
        if(keys[SDLK_RSHIFT] || keys[SDLK_LSHIFT]) {
            yDelta -= 1;
        }
        if(keys[SDLK_SPACE]) {
            yDelta += 1;
        }

        Mat44 tmp;

        if(xDelta || yDelta || zDelta) {
            float xDiff = xDelta*MOVEMENT_SPEED*SECONDS_PER_FRAME,
                  yDiff = yDelta*MOVEMENT_SPEED*SECONDS_PER_FRAME,
                  zDiff = zDelta*MOVEMENT_SPEED*SECONDS_PER_FRAME;
            mat44Translate(tmp,-xDiff,-yDiff,-zDiff);
            mat44Mult(translate,tmp,translate);
        }

        if(vertRot) {
            float vertDiff = vertRot*(mouseCaptured ?
                                       DEGREES_PER_PIXEL :
                                       ROTATION_SPEED*SECONDS_PER_FRAME);
            mat44Rotate(tmp,vertDiff,-1,0,0);
            mat44Mult(rotateVert,tmp,rotateVert);
        }
        if(horizRot) {
            float horizDiff = horizRot*(mouseCaptured ?
                                       DEGREES_PER_PIXEL :
                                       ROTATION_SPEED*SECONDS_PER_FRAME);
            mat44Rotate(tmp,horizDiff,0,1,0);
            mat44Mult(translate,tmp,translate);
        }

        tetraHorizRot += TETRA_HORIZ_ROTATION_SPEED*SECONDS_PER_FRAME;
        tetraVertRot  += TETRA_VERT_ROTATION_SPEED *SECONDS_PER_FRAME;

        mat44Rotate(tetraTransform,tetraHorizRot,0,1,0);

        mat44Rotate(tmp,tetraHorizRot,0,1,0);
        Vec4 xAxis = { 1,0,0,1 };
        mat44MultVec4(xAxis,tmp,xAxis);
        mat44Rotate(tmp,tetraVertRot,xAxis[0],xAxis[1],xAxis[2]);
        mat44Mult(tetraTransform,tetraTransform,tmp);


        ct->viewport.width = ct->_width/numViewports;

        unsigned i;
        for(i=0;i<numViewports;++i) {
            ct->viewport.x = ct->_width-ct->viewport.width*(i+1);

            matStackPush(ct->matrices[MATRIX_MODELVIEW]);

                if(i == 0) {
                    matStackMult(ct->matrices[MATRIX_MODELVIEW],rotateVert);
                    matStackMult(ct->matrices[MATRIX_MODELVIEW],translate);
                } else {
                    mat44Translate(tmp,0,0,-5);
                    matStackMult(ct->matrices[MATRIX_MODELVIEW],tmp);
                }

                //drawShapeIndexed(ct,SHAPE_TRIANGLE,4,varr,indices);
                matStackPush(ct->matrices[MATRIX_MODELVIEW]);

                    matStackMult(ct->matrices[MATRIX_MODELVIEW],gridTransform);
                    
                    drawShapeIndexed(ct,SHAPE_LINE,gridNumEdges,gridArr,gridIndices);

                matStackPop(ct->matrices[MATRIX_MODELVIEW]);
    
                matStackPush(ct->matrices[MATRIX_MODELVIEW]);
    
                    matStackMult(ct->matrices[MATRIX_MODELVIEW],tetraTransform);
    
                    drawShapeIndexed(ct,SHAPE_TRIANGLE,4,varr,indices);

                matStackPop(ct->matrices[MATRIX_MODELVIEW]);
                
            matStackPop(ct->matrices[MATRIX_MODELVIEW]);
            
            debugged = 1;
        }

        SDL_BlitSurface(ct->surface,NULL,screen,NULL);
        SDL_Flip(screen);

        unsigned elapsedTime = SDL_GetTicks()-startTicks;
        /* printf("%d ms/frame\n",elapsedTime); */
        unsigned currFps = 1000/elapsedTime;
        if(currFps > FRAMES_PER_SECOND) {
            currFps = FRAMES_PER_SECOND;
        }

        fps = fpsLerp*currFps+(1-fpsLerp)*fps;
        sprintf(title,"%s %u/%u ms%s",TITLE,elapsedTime,ticksPerFrame,
                                      elapsedTime > ticksPerFrame ? "!!":"");
        SDL_WM_SetCaption(title,NULL);

        if(elapsedTime < ticksPerFrame) {
            SDL_Delay(ticksPerFrame-elapsedTime);
        }
    }

    free(gridVerts);
    free(gridIndices);

    freeVertArray(gridArr);
    freeVertArray(varr);

    freeContext(ct);
    return 0;
}