Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: some problems with glReadPixels

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    12

    some problems with glReadPixels

    #include <GL/gl.h>
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <iostream>
    #include "stack.h"

    #define FILL_COLOR 100
    #define BORDER_COLOR 0

    using namespace std;
    void DrawRegion();
    void myInit(void);
    void myDisplay(void);
    void RegionFill(void);

    int main(int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 150);
    glutCreateWindow("my first attempt");

    glutDisplayFunc(myDisplay);
    //glutReshapeFunc(myReshape);
    //glutMouseFunc(myMouse);
    //glutKeyboardFunc(myKeyboard);
    myInit();
    glutMainLoop();

    return 0;
    }
    void myInit(void)
    {
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glPointSize(1.0);//
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 480.0);
    }
    void myDisplay(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    DrawRegion();
    RegionFill();
    glFlush();
    }
    void RegionFill(void)
    {
    GLubyte Pixel;
    glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);//
    int ret = glGetError();
    cerr << ret;
    Point seed = {300, 180};
    Stack stack;
    stack.Push(seed);
    glColor3ub(100, 0, 0);
    while(!stack.IsEmpty())
    {
    Point point;
    stack.Pop(point);
    glBegin(GL_POINTS);
    glVertex2i(point.x, point.y);
    glEnd();
    GLint xi = point.x + 1;
    GLubyte iPixel = 0;
    glReadPixels(50, 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);//
    while(iPixel != BORDER_COLOR)//向右填充
    {
    glBegin(GL_POINTS);
    glVertex2i(xi, point.y);
    glEnd();
    xi++;
    glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);

    }
    GLint xRight = xi--;//记录最右点
    xi = point.x - 1;
    glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
    while(int(iPixel) != BORDER_COLOR)
    {
    glBegin(GL_POINTS);
    glVertex2i(xi, point.y);
    glEnd();
    xi--;
    glReadPixels(xi, point.y, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
    }
    //GLint xLeft = xi++;//记录最左点
    for(int i = -1; i <= 1; i += 2)
    {
    while(xi <= xRight)
    {
    glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
    while(iPixel != BORDER_COLOR && iPixel != FILL_COLOR && xi <= xRight)
    {
    xi++;
    glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
    }
    Point tmpPoint = {xi - 1, point.y + i};
    stack.Push(tmpPoint);
    while((iPixel == BORDER_COLOR || iPixel == FILL_COLOR) && xi <= xRight)
    {
    xi++;
    glReadPixels(xi, point.y + i, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &iPixel);
    }
    }
    }
    }
    }
    void DrawRegion(void)
    {
    glColor3ub(0, 0, 0);
    glBegin(GL_LINE_LOOP);
    glVertex2i(50, 50);
    glVertex2i(180, 300);
    glVertex2i(300, 200);
    glVertex2i(450, 320);
    glVertex2i(350, 80);
    glVertex2i(250, 150);
    glVertex2i(150, 150);
    glVertex2i(50, 50);
    glEnd();
    GLubyte Pixel;
    glReadPixels(50 , 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &Pixel);
    glFlush();
    cout << int(Pixel) << " ";
    }

    This is my program. I run it step by step with gdb. The first glReadPixels in DrawRegion returns a right result of Pixel(0).
    But when it returns from the DrawRegion() and get into the RegionFill(), the glReadPixels at the first line of RegionFill returns a wrong result(11.Why the glReadPixels() in two functions executes one after another returns the different results.

    I run the program on T23 with ubuntu. Thanks.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2004
    Location
    San Diego, CA, USA
    Posts
    122

    Re: some problems with glReadPixels

    The two glReadPixels calls are separated by glFlush. It is often necessary to call glFlush before glReadPixels, so I have more trust for the glReadPixels call after glFlush, even if the result is not what you expected.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    12

    Re: some problems with glReadPixels

    Quote Originally Posted by James W. Walker
    The two glReadPixels calls are separated by glFlush. It is often necessary to call glFlush before glReadPixels, so I have more trust for the glReadPixels call after glFlush, even if the result is not what you expected.
    Thanks for your reply. I call the glReadPixels after glFlush and get the same result. I run the program on my computer always get strange results. I doubt whether it has relation to my computer's environment.
    For example, I run the program with gdb step by step and the cout in DrawRegion() puts out 0. But when I run it in terminal, it puts out 255 or other results but not 0.

  4. #4
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: some problems with glReadPixels

    James W. Walker is right, this is not logic to call glFlush after a glReadPixels command. You should call as James said, glFlush before calling glReadPixels, but IMO, this not sufficient, you need to call glFlush then glFinish and finally, glReadPixels. This way, you are sure that all commands prior to glReadPixels are executed and finished!

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    12

    Re: some problems with glReadPixels

    For this reason, OpenGL provides the command glFlush(), which forces the client to send the network packet even though it might not be full. Where there is no network and all commands are truly executed immediately on the server, glFlush() might have no effect. However, if you're writing a program that you want to work properly both with and without a network, include a call to glFlush() at the end of each frame or scene. Note that glFlush() doesn't wait for the drawing to complete - it just forces the drawing to begin execution, thereby guaranteeing that all previous commands execute in finite time even if no further rendering commands are executed.

    If glFlush() isn't sufficient for you, try glFinish(). This command flushes the network as glFlush() does and then waits for notification from the graphics hardware or network indicating that the drawing is complete in the framebuffer

    Thanks for your reply.These paragraphs copy from <OpenGL Programming Guide>.According to it, glFlush and glFinish are only used with network?

    I also try to change the program calling glFinish before glReadPixels. But it have no change to the result. I get the same result as before.

  6. #6

    Re: some problems with glReadPixels

    Neither glFlush nor glFinish is needed before a glReadPixels. The read will implicitly finish all commands before copying the framebuffer contents.

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    12

    Re: some problems with glReadPixels

    I discover a strange case.
    I comment the glReadPixels in DrawRegion() as below,
    //GLubyte tPixel;
    //glReadPixels(50 , 50, 1, 1, GL_RED, GL_UNSIGNED_BYTE, &tPixel);
    //cout << int(Pixel) << " ";

    Then, the glReadPixels in RegionFill return right result. It is so strange.

  8. #8
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    12

    Re: some problems with glReadPixels

    Quote Originally Posted by eile
    Neither glFlush nor glFinish is needed before a glReadPixels. The read will implicitly finish all commands before copying the framebuffer contents.
    Thanks. Now, I already understand this. But it can not solve my problem. Do you have any suggestions for the problem?

  9. #9
    Senior Member Regular Contributor
    Join Date
    Nov 2004
    Location
    San Diego, CA, USA
    Posts
    122

    Re: some problems with glReadPixels

    Quote Originally Posted by eile
    Neither glFlush nor glFinish is needed before a glReadPixels. The read will implicitly finish all commands before copying the framebuffer contents.
    That's the theory, but in practice I have seen cases where glFlush is needed, maybe even glFinish. It may depend on the card and driver.

  10. #10
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: some problems with glReadPixels

    peng530, that is hard to say, your code is quite complex. Try to write a more simple example and see if the problem still occurs. You can also try to simplify your code commenting then uncommenting parts of the code that you consider correct, checking if opengl throws any error. I could looking to your code when I would have time if you can provide the full code.

Page 1 of 2 12 LastLast

Similar Threads

  1. about glReadPixels
    By franz1999 in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 03-31-2005, 03:05 AM
  2. Replies: 8
    Last Post: 12-08-2003, 04:37 AM
  3. glReadPixels
    By mphanke in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-04-2002, 08:24 AM
  4. glReadPixels problems again
    By Zoran Zivkovic in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 05-10-2001, 08:48 PM
  5. Problems with glReadPixels???
    By Zoran Zivkovic in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 02-17-2000, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean