Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to convert a bin file into opengl texture and display it in MFC. I used the below code. I tried glTexParameterf instead of glTexParameteri,GL_LINEAR instead of GL_NEAREST and used GL_REPEAT instead of GL_CLAMP but no use.

What I have tried:

char* chBinBuffer = NULL;
    BYTE* Bitmap_Data = NULL;
   
    chBinBuffer = new char[1280 *690 * 4];
	Bitmap_Data = new BYTE[1280 *690 * 4 ];

    TCHAR tchFileName[MAX_PATH];
    swprintf_s(tchFileName, _T("D:\\Bin Files\\InputData_1280_690.bin"));
    FILE* pFile = NULL;
    _wfopen_s(&pFile, tchFileName, _T("rb"));
    
    if(NULL != pFile)
    {
        fread(Bitmap_Data, 1280 *690 * 4 * sizeof( unsigned char ), 1, pFile);
        fclose(pFile);
    }

    GLuint glTexture;
    
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &glTexture);
    glBindTexture(GL_TEXTURE_2D, glTexture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);    // select modulate to mix texture with color for shading
   // when texture area is small, bilinear filter the closest mipmap
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_NEAREST );
    // when texture area is large, bilinear filter the original
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

    // the texture wraps over at the edges (repeat)
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

   //  // build our texture mipmaps
   /* gluBuild2DMipmaps( GL_TEXTURE_2D, 3, 1280, 690,
                       GL_RGBA, GL_UNSIGNED_BYTE, Bitmap_Data );*/

    glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA8, 1280, 690, 0, GL_RGBA, GL_UNSIGNED_BYTE, Bitmap_Data);
    //glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 1280, 690, GL_RGBA, GL_UNSIGNED_BYTE, Bitmap_Data );
    delete [] chBinBuffer;
	delete[] Bitmap_Data;
    //glDeleteTextures( 1, &glTexture );


But i didnt get any output. Please help me.
Posted
Updated 6-Apr-16 2:01am
v3
Comments
phil.o 5-Apr-16 7:54am    
"A bin file" refers to nothing specific, except the extension of the file. What is stored in binary format inside it can be anything, of which I'm afraid no-one has a clue.
Member 10536430 5-Apr-16 8:12am    
actually it is a image and i need to get that image from bin file and display as texture. I have converted this bin file to bmp and displayed it on MFC. but now i need to display it as texture and i'm not getting it.
Jochen Arndt 5-Apr-16 8:38am    
I don't know much about OpenGL. But it seems that you should call glEnable() and glTexEnvf() after the texture has been loaded into memory and then draw something.
See for example http://www.glprogramming.com/red/chapter09.html.
Member 10536430 5-Apr-16 8:49am    
I tried it but no change, still not getting any output.
Jochen Arndt 5-Apr-16 8:59am    
You should use the Reply button on the top right of a comment. Then the poster gets an email notification.

Regarding the output:
You must draw something. See the examples from my link.

1 solution

void CDisplayTextureDlg::OnBnClickedOk()
{
GLuint glTexture;
glTexture = LoadTexture();
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, glTexture );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0);
glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0);
glVertex2d(1.0,0.0);
glTexCoord2d(1.0,1.0);
glVertex2d(1.0,1.0);
glTexCoord2d(0.0,1.0);
glVertex2d(0.0,1.0);
glEnd();
SwapBuffers(m_hDeviceContext);

// CDialogEx::OnOK();
}


GLuint CDisplayTextureDlg :: LoadTexture()
{
char* pchBinBuffer = NULL;
BYTE* bData = NULL;

pchBinBuffer = new char[nWIDTH *nHEIGHT * nPIXEL];
bData = new BYTE[nWIDTH *nHEIGHT * nPIXEL ];

TCHAR tchFileName[MAX_PATH];
swprintf_s(tchFileName, _T("D:\\Bin Files\\InputData_1280_690.bin"));
FILE* pFile = NULL;
_wfopen_s(&pFile, tchFileName, _T("rb"));

if(NULL != pFile)
{
fread(bData, nWIDTH *nHEIGHT * nPIXEL , 1, pFile);
// memset( bData, 255, 1280 *690 * 3 );
fclose(pFile);
}

GLuint glTexture ;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// gluPerspective(30.0f,(GLfloat)640/(GLfloat)480,0.3f,200.0f);

// glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
// glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &glTexture);
glBindTexture(GL_TEXTURE_2D, glTexture);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // select modulate to mix texture with color for shading
// when texture area is small, bilinear filter the closest mipmap
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST );
// when texture area is large, bilinear filter the original
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

// the texture wraps over at the edges (repeat)
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
// gluBuild2DMipmaps( GL_TEXTURE_2D, 1, nWIDTH, nHEIGHT, GL_RGBA8, GL_UNSIGNED_BYTE, bData );
glTexImage2D(GL_TEXTURE_2D,0, 1 , nWIDTH, nHEIGHT, 0, GL_LUMINANCE , GL_UNSIGNED_BYTE, bData);

delete [] pchBinBuffer;
delete[] bData;

return glTexture;
}

void CDisplayTextureDlg::Initialize(void)
{
PIXELFORMATDESCRIPTOR pfd ;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.cColorBits = 8;
pfd.cDepthBits = 16;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER ;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.nVersion = 1;

CClientDC dc(this);
m_hDeviceContext = dc.GetSafeHdc();
int nPixelFormat = ChoosePixelFormat(m_hDeviceContext, &pfd);
SetPixelFormat(m_hDeviceContext,nPixelFormat, &pfd);
m_hRenderContext = wglCreateContext(m_hDeviceContext);
wglMakeCurrent(m_hDeviceContext, m_hRenderContext);
glClearColor(2.0f, 2.0f, 2.0f, 0.0f);
glClearDepth(0.0f);
OnBnClickedOk();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900