本文介绍了我需要将bin文件转换为opengl纹理并将其显示在mfc中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想将bin文件转换为opengl纹理并在MFC中显示它。我使用下面的代码。我尝试了glTexParameterf而不是glTexParameteri,GL_LINEAR而不是GL_NEAREST并且使用了GL_REPEAT而不是GL_CLAMP但没有用。 我尝试了什么: 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.推荐答案 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 =新字符[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); b $ b // glMatrixMode(GL_MODELVIEW); //选择Modelview矩阵 // glLoadIdentity(); glEnable(GL_TEXTURE_2D); glGenTextures(1,&glTexture); glBindTexture(GL_TEXTURE_2D,glTexture); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); //选择调制以混合纹理和颜色进行着色 //当纹理区域很小时,双线性过滤最近的mipmap 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_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; 返回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(这);; 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.0 f,0.0f); glClearDepth(0.0f); OnBnClickedOk(); } 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();} 这篇关于我需要将bin文件转换为opengl纹理并将其显示在mfc中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 19:56