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

时间:2015-02-06 02:23:37

标签: c++ opengl

我们获得了一些作业代码。我在确定为什么get在.h文件中并且set在.cpp文件中时遇到了一些麻烦。当我在我的纹理类中调用theTexMap.GetRgbPixel(x, y, r, g, b);时,它无法在.h文件中找到GetRgbPixel函数(显然)。可能需要将get添加到RgbImage.cpp中?有人有时间解释一下吗?看起来它完全是在.h中为getter构建的,所以我不确定为什么我必须添加这个函数。当很多内容已经在.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);
}

1 个答案:

答案 0 :(得分:1)

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

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

你会想做这样的事情:

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

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