Exemplo n.º 1
0
void image_impl::render(int pWindowId, int pX, int pY, int pViewPortWidth, int pViewPortHeight)
{
    float xscale = 1.f;
    float yscale = 1.f;
    if (mKeepARatio) {
        if (mWidth > mHeight) {
            float trgtH = pViewPortWidth * float(mHeight)/float(mWidth);
            float trgtW = trgtH * float(mWidth)/float(mHeight);
            xscale = trgtW/pViewPortWidth;
            yscale = trgtH/pViewPortHeight;
        } else {
            float trgtW = pViewPortHeight * float(mWidth)/float(mHeight);
            float trgtH = trgtW * float(mHeight)/float(mWidth);
            xscale = trgtW/pViewPortWidth;
            yscale = trgtH/pViewPortHeight;
        }
    }
    glm::mat4 strans = glm::scale(glm::mat4(1.0f), glm::vec3(xscale, yscale, 1));

    glUseProgram(mProgram);
    // get uniform locations
    int mat_loc = glGetUniformLocation(mProgram, "matrix");
    int tex_loc = glGetUniformLocation(mProgram, "tex");
    int chn_loc = glGetUniformLocation(mProgram, "isGrayScale");
    int cml_loc = glGetUniformLocation(mProgram, "cmaplen");
    int ubo_idx = glGetUniformBlockIndex(mProgram, "ColorMap");

    glUniform1i(chn_loc, mFormat==1);
    // load texture from PBO
    glActiveTexture(GL_TEXTURE0);
    glUniform1i(tex_loc, 0);
    glBindTexture(GL_TEXTURE_2D, mTex);
    // bind PBO to load data into texture
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPBO);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mWidth, mHeight, mGLformat, mGLType, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    glUniformMatrix4fv(mat_loc, 1, GL_FALSE, glm::value_ptr(strans));

    glUniform1f(cml_loc, (GLfloat)mUBOSize);
    glBindBufferBase(GL_UNIFORM_BUFFER, 0, mColorMapUBO);
    glUniformBlockBinding(mProgram, ubo_idx, 0);

    CheckGL("Before render");

    // Draw to screen
    bindResources(pWindowId);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    unbindResources();

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

    // ubind the shader program
    glUseProgram(0);
    CheckGL("After render");
}
Exemplo n.º 2
0
void image_impl::render(const void* pWnd, int pX, int pY, int pViewPortWidth, int pViewPortHeight)
{
    static const float matrix[16] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f};

    glUseProgram(mProgram);
    // get uniform locations
    int mat_loc = glGetUniformLocation(mProgram, "matrix");
    int tex_loc = glGetUniformLocation(mProgram, "tex");
    int chn_loc = glGetUniformLocation(mProgram, "isGrayScale");
    int cml_loc = glGetUniformLocation(mProgram, "cmaplen");
    int ubo_idx = glGetUniformBlockIndex(mProgram, "ColorMap");

    glUniform1i(chn_loc, mFormat==1);
    // load texture from PBO
    glActiveTexture(GL_TEXTURE0);
    glUniform1i(tex_loc, 0);
    glBindTexture(GL_TEXTURE_2D, mTex);
    // bind PBO to load data into texture
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPBO);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mWidth, mHeight,
                    mGLformat, mDataType, 0);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    glUniformMatrix4fv(mat_loc, 1, GL_FALSE, matrix);

    glUniform1f(cml_loc, (GLfloat)mUBOSize);
    glBindBufferBase(GL_UNIFORM_BUFFER, 0, mColorMapUBO);
    glUniformBlockBinding(mProgram, ubo_idx, 0);

    CheckGL("Before render");

    // Draw to screen
    bindResources(pWnd);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    unbindResources();

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

    // ubind the shader program
    glUseProgram(0);
    CheckGL("After render");
}
Exemplo n.º 3
0
void hist_impl::render(const void* pWnd, int pX, int pY, int pVPW, int pVPH)
{
    float w = float(pVPW - (leftMargin()+rightMargin()+tickSize()));
    float h = float(pVPH - (bottomMargin()+topMargin()+tickSize()));
    float offset_x = (2.0f * (leftMargin()+tickSize()) + (w - pVPW)) / pVPW;
    float offset_y = (2.0f * (bottomMargin()+tickSize()) + (h - pVPH)) / pVPH;
    float scale_x = w / pVPW;
    float scale_y = h / pVPH;

    CheckGL("Begin Histogram::render");
    /* Enavle scissor test to discard anything drawn beyond viewport.
     * Set scissor rectangle to clip fragments outside of viewport */
    glScissor(pX+leftMargin()+tickSize(), pY+bottomMargin()+tickSize(),
            pVPW - (leftMargin()+rightMargin()+tickSize()),
            pVPH - (bottomMargin()+topMargin()+tickSize()));
    glEnable(GL_SCISSOR_TEST);

    glm::mat4 trans = glm::translate(glm::scale(glm::mat4(1),
                                                glm::vec3(scale_x, scale_y, 1)),
                                     glm::vec3(offset_x, offset_y, 0));

    glUseProgram(mHistBarProgram);
    glUniformMatrix4fv(mHistBarMatIndex, 1, GL_FALSE, glm::value_ptr(trans));
    glUniform4fv(mHistBarColorIndex, 1, mBarColor);
    glUniform1f(mHistBarNBinsIndex, (GLfloat)mNBins);
    glUniform1f(mHistBarYMaxIndex, ymax());

    /* render a rectangle for each bin. Same
     * rectangle is scaled and translated accordingly
     * for each bin. This is done by OpenGL feature of
     * instanced rendering */
    bindResources(pWnd);
    glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, mNBins);
    unbindResources();

    glUseProgram(0);
    /* Stop clipping */
    glDisable(GL_SCISSOR_TEST);

    renderChart(pWnd, pX, pY, pVPW, pVPH);
    CheckGL("End Histogram::render");
}
Exemplo n.º 4
0
void AbstractChart2D::renderChart(const void* pWnd, int pX, int pY, int pVPW, int pVPH)
{
    float w = float(pVPW - (mLeftMargin + mRightMargin + mTickSize));
    float h = float(pVPH - (mTopMargin + mBottomMargin + mTickSize));
    float offset_x = (1.9f * (mLeftMargin+mTickSize) + (w - pVPW)) / pVPW;
    float offset_y = (1.9f * (mBottomMargin+mTickSize) + (h - pVPH)) / pVPH;
    float scale_x = w / pVPW;
    float scale_y = h / pVPH;

    CheckGL("Begin Chart::render");

    bindResources(pWnd);

    /* bind the plotting shader program  */
    glUseProgram(mBorderProgram);

    /* set uniform attributes of shader
     * for drawing the plot borders */
    glm::mat4 trans = glm::scale(glm::translate(glm::mat4(1),
                                                glm::vec3(offset_x, offset_y, 0)),
                                 glm::vec3(scale_x, scale_y, 1));
    glUniformMatrix4fv(mBorderMatIndex, 1, GL_FALSE, glm::value_ptr(trans));
    glUniform4fv(mBorderColorIndex, 1, WHITE);

    /* Draw borders */
    glDrawArrays(GL_LINE_LOOP, 0, 4);

    /* reset shader program binding */
    glUseProgram(0);

    /* bind the sprite shader program to
     * draw ticks on x and y axes */
    glPointSize((GLfloat)mTickSize);

    glUseProgram(mSpriteProgram);
    glUniform4fv(mSpriteTickcolorIndex, 1, WHITE);
    glUniformMatrix4fv(mSpriteMatIndex, 1, GL_FALSE, glm::value_ptr(trans));
    /* Draw tick marks on y axis */
    glUniform1i(mSpriteTickaxisIndex, 1);
    glDrawArrays(GL_POINTS, 4, mTickCount);
    /* Draw tick marks on x axis */
    glUniform1i(mSpriteTickaxisIndex, 0);
    glDrawArrays(GL_POINTS, 4+mTickCount, mTickCount);

    glUseProgram(0);
    glPointSize(1);
    unbindResources();

    auto &fonter = getChartFont();
    fonter->setOthro2D(int(w), int(h));

    float pos[2];
    /* render tick marker texts for y axis */
    for (StringIter it = mYText.begin(); it!=mYText.end(); ++it) {
        int idx = int(it - mYText.begin());
        glm::vec4 res = trans * glm::vec4(mTickTextX[idx], mTickTextY[idx], 0, 1);
        /* convert text position from [-1,1] range to
         * [0, 1) range and then offset horizontally
         * to compensate for margins and ticksize */
        pos[0] = w*(res.x+1.0f)/2.0f;
        pos[1] = h*(res.y+1.0f)/2.0f;
        pos[0] -= (pVPW-w)*0.60f;
        fonter->render(pWnd, pos, WHITE, it->c_str(), CHART2D_FONT_SIZE);
    }
    /* render tick marker texts for x axis */
    for (StringIter it = mXText.begin(); it!=mXText.end(); ++it) {
        int idx = int(it - mXText.begin());
        /* mTickCount offset is needed while reading point coordinates for
         * x axis tick marks */
        glm::vec4 res = trans * glm::vec4(mTickTextX[idx+mTickCount], mTickTextY[idx+mTickCount], 0, 1);
        /* convert text position from [-1,1] range to
         * [0, 1) range and then offset vertically
         * to compensate for margins and ticksize */
        pos[0] = w*(res.x+1.0f)/2.0f;
        pos[1] = h*(res.y+1.0f)/2.0f;
        pos[1] -= (pVPH-h)*0.34f;
        /* offset horizontally based on text size to align
         * text center with tick mark position */
        pos[0] -= (CHART2D_FONT_SIZE*(it->length()-2)/2.0f);
        fonter->render(pWnd, pos, WHITE, it->c_str(), CHART2D_FONT_SIZE);
    }
    /* render chart axes titles */
    if (!mYTitle.empty()) {
        glm::vec4 res = trans * glm::vec4(-1.0f, 0.0f, 0.0f, 1.0f);
        pos[0] = w*(res.x+1.0f)/2.0f;
        pos[1] = h*(res.y+1.0f)/2.0f;
        pos[0] -= (pVPW-w)*0.76;
        fonter->render(pWnd, pos, WHITE, mYTitle.c_str(), CHART2D_FONT_SIZE, true);
    }
    if (!mXTitle.empty()) {
        glm::vec4 res = trans * glm::vec4(0.0f, -1.0f, 0.0f, 1.0f);
        pos[0] = w*(res.x+1.0f)/2.0f;
        pos[1] = h*(res.y+1.0f)/2.0f;
        pos[1] -= (pVPH-h)*0.66;
        fonter->render(pWnd, pos, WHITE, mXTitle.c_str(), CHART2D_FONT_SIZE);
    }

    CheckGL("End Chart::render");
}
Exemplo n.º 5
0
void font_impl::render(int pWindowId,
                       const float pPos[], const float pColor[], const char* pText,
                       int pFontSize, bool pIsVertical)
{
    if(!mIsFontLoaded) {
        std::cerr<<"No font was loaded!, hence skipping text rendering."<<std::endl;
        return;
    }

    glDisable(GL_DEPTH_TEST);
    glDepthFunc(GL_ALWAYS);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glUseProgram(mProgram);
    GLuint pmat_loc = glGetUniformLocation(mProgram, "projectionMatrix");
    GLuint mvmat_loc = glGetUniformLocation(mProgram, "modelViewMatrix");
    GLuint tex_loc = glGetUniformLocation(mProgram, "tex");
    GLuint col_loc = glGetUniformLocation(mProgram, "textColor");

    glm::mat4 projMat = glm::ortho(0.0f, float(mWidth), 0.0f, float(mHeight));

    glUniformMatrix4fv(pmat_loc, 1, GL_FALSE, (GLfloat*)&projMat);
    glUniform4fv(col_loc, 1, pColor);

    int loc_x = int(pPos[0]);
    int loc_y = int(pPos[1]);
    if(pFontSize == -1)
        pFontSize = mLoadedPixelSize;
    float scale_factor = float(pFontSize) / float(mLoadedPixelSize);

    bindResources(pWindowId);
    CheckGL("After Font::render bind resources");
    glActiveTexture(GL_TEXTURE0);
    glUniform1i(tex_loc, 0);
    glBindSampler(0, mSampler);

    std::string str(pText);
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        char currChar = *it;

        if(currChar == '\n') {
            // if it is new line, move location to next line
            loc_x = int(pPos[0]);
            loc_y -= mNewLine * pFontSize / mLoadedPixelSize;
        } else if (currChar <= '~' && currChar >= ' ') {
            // regular characters are rendered as textured quad
            int idx = int(currChar) - START_CHAR;
            loc_x += mBearingX[idx] * pFontSize / mLoadedPixelSize;

            glBindTexture(GL_TEXTURE_2D, mCharTextures[idx]);

            /* rotate by 90 degress if we need
             * to render the characters vertically */
            glm::mat4 modelView = glm::translate(glm::mat4(1.0f),
                    glm::vec3(float(loc_x), float(loc_y), 0.0f));

            modelView = glm::scale(modelView, glm::vec3(scale_factor));
            glUniformMatrix4fv(mvmat_loc, 1, GL_FALSE, (GLfloat*)&modelView);

            // Draw letter
            glDrawArrays(GL_TRIANGLE_STRIP, idx*4, 4);

            loc_x += (mAdvX[idx] - mBearingX[idx]) * pFontSize / mLoadedPixelSize;
        }
        /* if the text needs to be rendered vertically,
         * move the pen cursor to next line after each
         * character render mandatorily */
        if (pIsVertical) {
            loc_x = int(pPos[0]);
            loc_y -= mNewLine * pFontSize / mLoadedPixelSize;
        }
    }

    unbindResources();

    glUseProgram(0);
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    CheckGL("After Font::render ");
}