简体   繁体   English

将opengl纹理保存到字节数组

[英]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. 我在MFC中使用opengl画了一个圆,现在我想读取该纹理并将其存储在字节数组中。 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. glGetTexLevelParameterfv返回绑定纹理的高度和宽度,顺便说一句,您甚至没有加载任何东西。 Bet they are both 0. 打赌他们都是0。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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