将opengl纹理保存为字节数组 [英] Save opengl texture into a byte array

查看:60
本文介绍了将opengl纹理保存为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MFC中使用opengl绘制了一个圆圈,现在我想读取该纹理并将其存储在字节数组中。怎么做?



我尝试了什么:



我绘制圆圈并尝试使用下面的代码存储纹理,但它不起作用



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?

What I have tried:

I drew the circle and tried to store the texture using below code, but it doesnt work

void OverlayDisplayDlg::OnBnClickedCircleRadio()
{
    GLfloat glRadius = 0.5f;

    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( 1,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();
    glDisable( GL_TEXTURE_2D);
    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 );
    glReadBuffer( GL_BACK );
    glReadPixels( 0, 0, m_glTextureWidth, m_glTextureHeight, GL_RGBA, GL_UNSIGNED_BYTE, m_pbyImageData1 );
    SwapBuffers( m_hDeviceContextDC );
}





m_pbyImageData1是一个字节数组。请帮帮我..



m_pbyImageData1 is a byte array . Please help me..

推荐答案

glGetTexLevelParameterfv< =返回一个浮动值:-)



你想要glReadPixels的像素那么整数版请...所以glGetTexLevelParameteriv。



这就是为什么你必须转换为int并且如果你已经调试了尺寸那么一直很小。使用你正在使用的调试器。



接下来要小心乘法,你可以乘以大整数,使其向上转换为长整数

glGetTexLevelParameterfv <= That returns a float value :-)

You want the pixels for glReadPixels so integer version please ... so glGetTexLevelParameteriv.

Thats why you were having to cast to int and if you had of debugged the sizes would have been tiny. Use you debugger that is what it is there for.

Next be careful with the multiply you can be multiplying big integers so upcast to a long
m_pbyImageData1 = new BYTE[ 4 * (long)m_glTextureWidth * (long)m_glTextureHeight ];



你真的想要GL_BACK吗?



完成后不要忘记释放内存分配


Do you really want GL_BACK??

Dont forget to free the memory allocation when you are done


这就是你可以解决的代码所做的事情。我认为你要么使用该代码得到垃圾或空白区域。



glEnable(GL_TEXTURE_2D)和禁用已禁用我试过快速搜索和最常见的回答是即使实施也没有做任何事情。从未使用它,也不知道它做了什么。



This is what you code does a far as I can work out .. I would think you either get junk or a blank area using that code.

glEnable( GL_TEXTURE_2D ) and disable is deprecated and I tried a quick search and the most common answer was it does nothing even if implemented. Never used it and no idea what it does.

BYTE* pbyNewImageData = NULL;
pbyNewImageData = new BYTE[ CIRCLEWIDTH * CIRCLEHEIGHT * 3];  // Allocate buffer 

// The allocated buffer contains zeros or random rubbish depending on mode
 
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Yep clear GL buffers

glEnable( GL_TEXTURE_2D );  // ** I have no idea what this does

glGenTextures( 1, &m_glTexture[1] );                   // Generate a texture
glBindTexture( GL_TEXTURE_2D, m_glTexture[1] );        // Bind that texture to a 2D
glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );   // Use the lights/materials
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

// Next line puts zeros or random image buffer data to texture .. UMMM Okay 
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 
   CIRCLEWIDTH, CIRCLEHEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, pbyNewImageData );

glEnable( GL_TEXTURE_2D );  // ** I have no idea what this does

glBindTexture( GL_TEXTURE_2D, m_glTexture[1] );  // Bind the texture again :-) 

// Standard draw the texture onto faces
glBegin( GL_QUADS );
glTexCoord2d( 0.0,0.0 );
glVertex2f( -0.8f, -0.8f );
glTexCoord2d( 1.0,0.0 );
glVertex2f( 0.8f, -0.8f );
glTexCoord2d( 1.0,1.0 );
glVertex2f( 0.8f, 0.8f );
glTexCoord2d( 0.0,1.0 );
glVertex2f( -0.8f, 0.8f );
glEnd();

SwapBuffers( m_hDeviceContextDC );  // Resultant image to Dc probably screen

glDisable( GL_TEXTURE_2D );         // ** No idea what this does

delete[] pbyNewImageData;   // Delete our array that had zeros or junk


这篇关于将opengl纹理保存为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆