1

I have simple program trying to use PBO to update sub texture data which is dynamic generated. The problem is to get the correct order of data in texture because it gets inverted or not stored in the correct order.

Here is some code for initializing, data input and draw function

//////////////////////////////////////////////////////////////////////////
//Globals
#define TEX_WIDTH 512
#define TEX_HEIGHT 2048

GLuint renderedTexture;
GLuint pbo_id;
GLubyte* pbo_memory = 0;

Initialization:

void initialize() 
{
    int w = 1024; //screen width
    int h = 1024; //screen height

    glViewport( 0, 0, w, h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D(0, w, h, 0);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glGenTextures(1, &renderedTexture);

    glBindTexture(GL_TEXTURE_2D, renderedTexture);

    //Here internal format is RGBA and input format not sure.
    //Idea is to put GLushort as red channel input only.

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH , TEX_HEIGHT, 0, GL_RED, GL_UNSIGNED_SHORT, 0);
    std::cout << "Error = \n" << glGetError();

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glGenBuffers(1, &pbo_id);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_id);

    // Is this the correct initialization of buffer size and access type???
    glBufferData(GL_PIXEL_UNPACK_BUFFER, TEX_WIDTH * TEX_HEIGHT * sizeof(GLushort), 0, GL_STREAM_DRAW);

    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}

and now draw function. I would like to make vertical lines but they are rendered horizontal why?

void display()
{   
    if(pbo_memory == 0)
    {
        // Get pointer to memory
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_id);
        pbo_memory = reinterpret_cast<GLubyte*>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, 
            GL_WRITE_ONLY /*| GL_MAP_FLUSH_EXPLICIT_BIT*/));
        glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
    }
    else
    {
        //make lines 
        /*
            +-------+
            | | | | |
            | | | | |
            | | | | | 2048
            | | | | |
            | | | | |
            | | | | |
            +-------+
               512
                          */
        for (int i = 0; i < TEX_WIDTH; ++i)
        {
            if( i % 64 == 0 && i > 2)
            {
                GLubyte* _data = &pbo_memory[i*TEX_HEIGHT*2];
                GLubyte* _data_prev = &pbo_memory[(i-1) *TEX_HEIGHT*2 ];
                GLubyte* _data_prev1 = &pbo_memory[(i-2) *TEX_HEIGHT*2];
                GLubyte* _data_next = &pbo_memory[(i+1) *TEX_HEIGHT*2];
                GLubyte* _data_next1 = &pbo_memory[(i+2) *TEX_HEIGHT*2];
                for (int j = 0; j< TEX_HEIGHT*2;++j)
                {
                    _data[j] = 255;
                    _data_prev[j] = 255;
                    _data_next[j] = 255;
                    _data_prev1[j] = 255;
                    _data_next1[j] = 255;
                }
            }
            else
                memset( &pbo_memory[i*TEX_HEIGHT*2], 0, TEX_HEIGHT * 2);
        }
    }

    glViewport(0,0,1024,1024);
    glClearColor(0,0,0.5,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D); 

    GLfloat m_texWidth = 256;   //512/2
    GLfloat m_texHeight = 1024; // 2048/2
    GLfloat tw = 1.0;
    GLfloat th = 1.0;

    glBindTexture(GL_TEXTURE_2D, renderedTexture);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_id);

    glTexSubImage2D(GL_TEXTURE_2D, 
            0,
            0,
            0, 
            512, 
            2048 , 
            GL_RED,
            GL_UNSIGNED_SHORT,
            0);

    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

    assert(glGetError() == 0);


    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, th);
        glVertex2f( (GLfloat)0,
            (GLfloat)0 );

        glTexCoord2f(tw, th);
        glVertex2f( (GLfloat)m_texWidth,
            (GLfloat)0 );

        glTexCoord2f(tw, 0.0f);
        glVertex2f( (GLfloat)m_texWidth,
            (GLfloat)m_texHeight);

        glTexCoord2f(0.0f, 0.0f);
        glVertex2f( (GLfloat)0,
            (GLfloat)m_texHeight);
    glEnd();

    // Swap front and back buffers
    glutSwapBuffers();
}

So why my vertical lines become horizontal what can I do to change it so the data is correctly uploaded to graphic card. I am using AMD Radeon.

  • You should write code which generates vertical lines. Because your code is clearly generating horizontal ones. Also, you should stop using a mapped pointer *after you unmap it*. – Nicol Bolas Nov 08 '16 at 13:29

0 Answers0