openGL SubTexturing

前端 未结 2 1594
时光取名叫无心
时光取名叫无心 2020-12-05 16:36

I have image data and i want to get a sub image of that to use as an opengl texture.

glGenTextures(1, &m_name);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &am         


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 17:03

    Edit: Use glPixelStorei. You use it to set GL_UNPACK_ROW_LENGTH to the width (in pixels) of the entire image. Then you call glTexImage2D (or whatever), passing it a pointer to the first pixel of the subimage and the width and height of the subimage.

    Don't forget to restore GL_UNPACK_ROW_LENGTH to 0 when you're finished with it.

    Ie:

    glPixelStorei( GL_UNPACK_ROW_LENGTH, img_width );
    char *subimg = (char*)m_data + (sub_x + sub_y*img_width)*4;
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sub_width, sub_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, subimg );
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
    

    Or, if you're allergic to pointer maths:

    glPixelStorei( GL_UNPACK_ROW_LENGTH, img_width );
    glPixelStorei( GL_UNPACK_SKIP_PIXELS, sub_x );
    glPixelStorei( GL_UNPACK_SKIP_ROWS, sub_y );
    
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sub_width, sub_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_data );
    
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
    glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
    glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
    

    Edit2: For the sake of completeness, I should point out that if you're using OpenGL-ES then you don't get GL_UNPACK_ROW_LENGTH. In which case, you could either (a) extract the subimage into a new buffer yourself, or (b)...

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sub_width, sub_height, 0, GL_RGBA, GL_UNSIGNED_BYTES, NULL );
    
    for( int y = 0; y < sub_height; y++ )
    {
        char *row = m_data + ((y + sub_y)*img_width + sub_x) * 4;
        glTexSubImage2D( GL_TEXTURE_2D, 0, 0, y, sub_width, 1, GL_RGBA, GL_UNSIGNED_BYTE, row );
    }
    

提交回复
热议问题