C++ glReadPixels() “数据”参数用法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8841422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:12:05  来源:igfitidea点击:

glReadPixels() "data" argument usage?

c++openglgraphics

提问by CustardBun

I'm trying to use glReadPixels to get color data from an image. I'm supposed to be using glReadPixels but I can't seem to figure it out. It's part of a much larger project, but right now all I want is to know how to properly use this.

我正在尝试使用 glReadPixels 从图像中获取颜色数据。我应该使用 glReadPixels 但我似乎无法弄清楚。这是一个更大项目的一部分,但现在我只想知道如何正确使用它。

I looked it up and got something like this:

我查了一下,得到了这样的东西:

    void glReadPixels(GLint x, 
       GLint y, 
       GLsizei width, 
       GLsizei height, 
       GLenum format, 
       GLenum type, 
       GLvoid* data);

But I'm not sure what I should be putting in as that last argument, and when I do, how I would even use it. Help would really be appreciated! (ie: a simple example of how to use it, or how to get the color)

但我不确定我应该把什么作为最后一个论点,当我这样做时,我什至会如何使用它。帮助将不胜感激!(即:如何使用它或如何获取颜色的简单示例)

回答by datenwolf

datatakes a pointer to some buffer you prepared for glReadPixels to put the data into. Like this:

data获取一个指向您为 glReadPixels 准备的缓冲区的指针,以将数据放入其中。像这样:

switch(format) {
case GL_BGR:
case GL_RGB:
    components = 3; break;

case GL_BGRA:
case GL_RGBA:
    components = 4; break;

case GL_ALPHA:
case GL_LUMINANCE:
    components = 1; break;
}

GLubyte *data = malloc(components * width * height);
if( data ) {
    glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, data);
}

回答by genpfault

Usage example:

用法示例:

#include <GL/glut.h>
#include <iostream>

int mx = 0, my = 0;
void display()
{
    glClearColor( 0, 0, 0, 1 );
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -10, 10, -10, 10, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glScalef( 5, 5, 5 );
    glBegin( GL_TRIANGLES );
    glColor3ub( 255, 0, 0 );
    glVertex2f( -1, -1 );
    glColor3ub( 0, 255, 0 );
    glVertex2f( 1, -1 );
    glColor3ub( 0, 0, 255 );
    glVertex2f( 0, 1 );
    glEnd();

    // 4 bytes per pixel (RGBA), 1x1 bitmap
    unsigned char pixels[ 1 * 1 * 4 ] = { 0 };
    glReadPixels( mx, my, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels );
    std::cout << "r: " << static_cast< int >( pixels[ 0 ] ) << '\n';
    std::cout << "g: " << static_cast< int >( pixels[ 1 ] ) << '\n';
    std::cout << "b: " << static_cast< int >( pixels[ 2 ] ) << '\n';
    std::cout << "a: " << static_cast< int >( pixels[ 3 ] ) << '\n' << std::endl;

    glutSwapBuffers();
}

void mouse( int x, int y )
{
    mx = x;
    my = glutGet( GLUT_WINDOW_HEIGHT ) - y;
    glutPostRedisplay();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 800, 600 );
    glutCreateWindow( "glReadPixels() example" );
    glutDisplayFunc( display );
    glutPassiveMotionFunc( mouse );
    glutMainLoop();
    return 0;
}

Use the mouse to get the RGBA value for a pixel.

使用鼠标获取像素的 RGBA 值。

回答by Tim

datais the pointer to the pixel data you're trying to read. Take a look at some example code, and look a few lines abovethat call to find out how they're initializing it. Usually it will just be an allocation of size something like x * y * depth. You'd pass it in as &data. Try reading a 1x1 pixel block of known color and see what kind of information it gives back.

data是指向您尝试读取的像素数据的指针。查看一些示例代码,并查看该调用上方的几行以了解它们如何初始化它。通常它只是大小的分配,例如x * y * depth. 你会把它作为&data. 尝试读取已知颜色的 1x1 像素块,看看它返回什么样的信息。