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

C++ openGL - get and set functions of pixels

本文关键字:函数 像素 设置 获取 C++openGL-      更新时间:2023-10-16

我们得到了一些赋值代码。我在确定get在.h文件中而set在.cpp文件中的原因时遇到了一些问题。当我在纹理类中调用theTexMap.GetRgbPixel(x, y, r, g, b);时,它无法在.h文件中找到GetRgbPixel函数(显然)。可能需要将get添加到RgbImage.cpp中?有人有时间解释吗?看起来这一切都是为getter完全内置在.h中的,所以我不确定为什么要添加这个函数。当很多GetRgbPixel函数已经在.h中时,我需要在多大程度上添加它?

/*********************************************************************
 * 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);
}

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

您的问题可能是GetRgbPixel接受redgreenblue参数的指针,因为这是它返回值的地方。

你会想做这样的事情:

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

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