在Mac中读取文件

纽荷比

我是openGL的新手,正在从事体积渲染项目。因此,该项目首先需要从文件中读取帧,然后构造2D纹理。在示例代码中,程序员使用该CFile.read()函数来执行此操作。但是,我在MacPro上工作,看来我们只能在Windows下使用CFile。因此,请告诉我该怎么做才能成功读取图像数据。

PS:示例代码基于3D纹理,而我的代码基于2D纹理。因此可能会有一些差异。但是,您应该只关注此处读取图像数据的函数,而不是生成纹理的语句。

示例代码:

bool CRawDataProcessor::ReadFile( LPCTSTR lpDataFile_i, int nWidth_i, int nHeight_i, int nSlices_i )
{
    CFile Medfile;
    if( !Medfile.Open( lpDataFile_i ,CFile::modeRead ))
    {
        return false;
    }

    // File has only image data. The dimension of the data should be known.
    m_uImageCount = nSlices_i;
    m_uImageWidth = nWidth_i;
    m_uImageHeight = nHeight_i;

    // Holds the luminance buffer.
    char* chBuffer = new char[ m_uImageWidth*m_uImageHeight*m_uImageCount ];
    if( !chBuffer )
    {
        return false;
    }
    // Holds the RGBA buffer.
    char* pRGBABuffer = new char[ m_uImageWidth*m_uImageHeight*m_uImageCount*4 ];
    if( !pRGBABuffer )
    {
        return false;
    }

    Medfile.Read( chBuffer, m_uImageWidth*m_uImageHeight*m_uImageCount );

    // Convert the data to RGBA data.
    // Here we are simply putting the same value to R, G, B and A channels.
    // Usually for raw data, the alpha value will be constructed by a threshold value  given by the user.

    for( int nIndx = 0; nIndx < m_uImageWidth*m_uImageHeight*m_uImageCount; ++nIndx )
    {
        pRGBABuffer[nIndx*4] = chBuffer[nIndx];
        pRGBABuffer[nIndx*4+1] = chBuffer[nIndx];
        pRGBABuffer[nIndx*4+2] = chBuffer[nIndx];
        pRGBABuffer[nIndx*4+3] = chBuffer[nIndx];
    }

    // If this function is getting called again for another data file.
    // Deleting and creating texture is not a good idea, 
    // we can use the glTexSubImage3D for better performance for such scenario.
    // I am not using that now :-)
    if( 0 != m_nTexId )
    {
        glDeleteTextures( 1, (GLuint*)&m_nTexId );
    }
    glGenTextures( 1, (GLuint*)&m_nTexId );

    glBindTexture( GL_TEXTURE_3D, m_nTexId );
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

    glTexImage3D( GL_TEXTURE_3D, 0, GL_RGBA, m_uImageWidth, m_uImageHeight, m_uImageCount, 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, pRGBABuffer );
    glBindTexture( GL_TEXTURE_3D, 0 );

    delete[] chBuffer;
    delete[] pRGBABuffer;
    return true;
}

我在这里尝试使用fstream,但是它不起作用。我的代码:

bool InitTextures2D( const char* filePath )
{
    std::fstream myFile;
    myFile.open( filePath, std::ios::out | std::ios::binary );

    m_uImageCount = 109;
    m_uImageWidth = 256;
    m_uImageHeight = 256;

    // Holds the texuture IDs.
    m_puTextureIDs = new int[m_uImageCount];

    // Holds the luminance buffer.
    char* chBuffer = new char[256 * 256];
    glGenTextures( m_uImageCount, (GLuint*)m_puTextureIDs );

    // Read each frames and construct the texture.
    for( int nIndx = 0; nIndx < m_uImageCount; ++nIndx )
    {
        // Read the frame.
        myFile.read(chBuffer, m_uImageWidth*m_uImageHeight);

        // Set the properties of the texture.
        glBindTexture( GL_TEXTURE_2D, m_puTextureIDs[nIndx] );
        glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

        glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, m_uImageWidth, m_uImageHeight, 0,
                      GL_LUMINANCE, GL_UNSIGNED_BYTE,(GLvoid *) chBuffer );
        glBindTexture( GL_TEXTURE_2D, 0 );
    }

    delete[] chBuffer;
    return true;
}
奥德什

您打开文件进行写入:“ std :: ios :: out ”,然后从中读取..那将行不通。

改变:

myFile.open(filePath, std::ios::out | std::ios::binary);

myFile.open(filePath, std::ios::in | std::ios::binary);

请注意,如果您执行了先前编写的代码,则会打开该文件进行写入。哪个文件被截断了,现在大概是0个字节。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章