将opengl纹理保存到Byte数组中

时间:2016-04-15 11:52:46

标签: c++ opengl mfc

我在MFC中使用opengl绘制了一个圆圈,现在我想读取该纹理并将其存储在字节数组中。怎么做? 我绘制了圆形并尝试使用下面的代码存储纹理,但它不起作用

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

1 个答案:

答案 0 :(得分:0)

您正在绘制窗口,而不是纹理。 glGetTexLevelParameterfv返回绑定纹理的高度和宽度,顺便说一下,你甚至没有加载任何东西。打赌他们都是0。

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

相关问题