Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

c++ - glReadPixels() "data" argument usage?

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.

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...