简体   繁体   中英

Save opengl Texture into Byte array

I drew a circle using opengl in MFC, and now i want to read that texture and store it in a byte array. How to do that? I drew the circle and tried to store the texture using below code, but it doesnt work

GLfloat glRadius = 0.5f;

GetDlgItem( IDC_SAVE_BUTTON )->EnableWindow( TRUE );        // To make button Enabled
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glGenTextures( 2, &m_glTexture[1] );
glBindTexture( GL_TEXTURE_2D, m_glTexture[1] );
glBegin(GL_TRIANGLE_FAN);
glColor3f( 0,1,0 );
glVertex2d( 0, 0 );
int nSegments = 100;
GLfloat glAngle ;

for( int nIndex = 0 ;nIndex <= nSegments; nIndex++ )
{
    glAngle = ( nIndex* 2.0f * PI )/ nSegments;
    glVertex2d( cos( glAngle ) * glRadius , sin( glAngle ) * glRadius );
}
glEnd();
glGetTexLevelParameterfv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &m_glTextureWidth );
glGetTexLevelParameterfv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &m_glTextureHeight );
m_pbyImageData1 = new BYTE[ 4 * ( int )m_glTextureWidth * ( int )m_glTextureHeight ];
//glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pbyImageData1 );
glReadPixels( 0, 0, m_glTextureWidth, m_glTextureHeight, GL_RGBA, GL_UNSIGNED_BYTE, m_pbyImageData1 );
glDisable( GL_TEXTURE_2D);
SwapBuffers( m_hDeviceContextDC );

You are drawing to the window, not a texture. glGetTexLevelParameterfv return the height and width of the bound texture which you by the way have not even loaded with anything. Bet they are both 0.

Call: glReadPixels( 0, 0, window_width, window_height, GL_RGBA, GL_UNSIGNED_BYTE, m_pbyImageData1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM