OpenGL PBO更新纹理错误宽度高度数据

时间:2016-11-08 13:11:02

标签: c++ opengl textures pbo

我有一个简单的程序试图使用PBO来更新动态生成的子纹理数据。问题是在纹理中获得正确的数据顺序,因为它被反转或没有以正确的顺序存储。

这是一些初始化,数据输入和绘制功能的代码

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

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

初​​始化:

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);
}

现在绘制功能。 我想制作垂直线,但为什么它们呈现水平?

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();
}

为什么我的垂直线变为水平线我可以做些什么来改变它,以便将数据正确上传到图形卡。我正在使用AMD Radeon。

0 个答案:

没有答案