C++ openGL - 获取和设置像素的函数

C++ openGL - get and set functions of pixels

我们得到了一些作业代码。我无法确定为什么 get 在 .h 文件中而 set 在 .cpp 文件中。当我在纹理 class 中调用 theTexMap.GetRgbPixel(x, y, r, g, b); 时,它无法在 .h 文件中找到 GetRgbPixel 函数(很明显)。大概需要把get加到RgbImage.cpp?有人有时间解释一下吗?看起来它完全内置于 getter 的 .h 中,所以我不确定为什么我必须添加此功能。当 .h 中已经有很多 GetRgbPixel 函数时,我需要添加到什么程度?

/*********************************************************************
 * SetRgbPixel routines allow changing the contents of the RgbImage. *
 *********************************************************************/

void RgbImage::SetRgbPixelf( long row, long col, double red, double green, double blue )
{
    SetRgbPixelc( row, col, doubleToUnsignedChar(red), 
                            doubleToUnsignedChar(green),
                            doubleToUnsignedChar(blue) );
}

void RgbImage::SetRgbPixelc( long row, long col,
                   unsigned char red, unsigned char green, unsigned char blue )
{
    assert ( row<NumRows && col<NumCols );
    unsigned char* thePixel = GetRgbPixel( row, col );
    *(thePixel++) = red;
    *(thePixel++) = green;
    *(thePixel) = blue;
}

RbgImage.h 文件代码。

// Returned value points to three "unsigned char" values for R,G,B
inline const unsigned char* RgbImage::GetRgbPixel( long row, long col ) const
{
    assert ( row<NumRows && col<NumCols );
    const unsigned char* ret = ImagePtr;
    long i = row*GetNumBytesPerRow() + 3*col;
    ret += i;
    return ret;
}

inline unsigned char* RgbImage::GetRgbPixel( long row, long col ) 
{
    assert ( row<NumRows && col<NumCols );
    unsigned char* ret = ImagePtr;
    long i = row*GetNumBytesPerRow() + 3*col;
    ret += i;
    return ret;
}

inline void RgbImage::GetRgbPixel( long row, long col, float* red, float* green, float* blue ) const
{
    assert ( row<NumRows && col<NumCols );
    const unsigned char* thePixel = GetRgbPixel( row, col );
    const float f = 1.0f/255.0f;
    *red = f*(float)(*(thePixel++));
    *green = f*(float)(*(thePixel++));
    *blue = f*(float)(*thePixel);
}

inline void RgbImage::GetRgbPixel( long row, long col, double* red, double* green, double* blue ) const
{
    assert ( row<NumRows && col<NumCols );
    const unsigned char* thePixel = GetRgbPixel( row, col );
    const double f = 1.0/255.0;
    *red = f*(double)(*(thePixel++));
    *green = f*(double)(*(thePixel++));
    *blue = f*(double)(*thePixel);
}

内联函数应该在 header 中定义,所以这不是问题所在。 inline 告诉编译器无论在哪里调用函数,它都会用函数中的实际代码替换对函数的调用。它可以用作速度优化,但 inline 实际上只是对编译器的提示。最终由编译器决定它会做什么。

您的问题可能是 GetRgbPixel 获取 redgreenblue 参数的指针,因为这是 return值。

你会想做这样的事情:

float r, g, b;
theTexMap.GetRgbPixel(x, y, &r, &g, &b);

& 运算符获取变量的地址,将其转换为指针并允许 GetRgbPixel 函数 return 这些参数的值。