Advertisement

02.11.2005 at 02:18AM PST, ID: 21310907
[x]
Attachment Details

A simple question on how to use glReadPixels()

Asked by capricious in OpenGL Graphics & Game Programming

Tags: glreadpixels

Hi,

I'm a newbie in OpenGL so hope someone can really help!

I have written a short program to draw a magenta square. It occupies a pixel area. I then attempted to read the color buffer to see how it works, but it gave me values that do not correspond to magenta's RGB code (ie. 1,0,1).
Can anyone explain where I went wrong?

/***** My Program ******/

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

void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}


void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glEnable (GL_DEPTH_TEST);
   glColor3f (1.0, 1.0, 1.0);
   glLoadIdentity (); /* clear the matrix to the identity matrix */
   
   glColor3f(1.0, 0.0, 1.0);       //set color to be magenta.
   int p1 [] = {0, 0};          //using array.
   int p2 [] = {1, 0};
   int p3 [] = {1, 1};
   int p4 [] = {0, 1};

   glBegin (GL_QUADS);
      glVertex2iv(p1);         //specify point positions.
      glVertex2iv(p2);
      glVertex2iv(p3);
      glVertex2iv(p4);
   glEnd ();
 
   /* read color buffer */
   int width=2, height=2;
   float pixels [3*width*height];
   glReadBuffer(GL_FRONT_LEFT);
   glReadPixels(-1,-1,width,height,GL_RGB,GL_FLOAT,pixels);
   std::cout << "----" << std::endl;
   for (int i=0; i<3*width*height; i++){
       std::cout << pixels[i] << std::endl;
   }
   
   glDisable (GL_DEPTH_TEST);
   glFlush ();
}


void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_MODELVIEW);   /* current matrix specifies the model matrix */
}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}


/****** My Results Obtained from MinGW command prompt *******/
1.26117e-044
3.20173e-039
3.21383e-039
1.04836e-033
0
1
3.2031e-039
1.#QNAN
3.20173e-039
0
0
0



thanks,
- capriciousStart Free Trial
 
Loading Advertisement...
 
[+][-]02.11.2005 at 07:44AM PST, ID: 13286759

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]02.11.2005 at 08:20AM PST, ID: 13287208

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.11.2005 at 01:46PM PST, ID: 13290477

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.11.2005 at 01:49PM PST, ID: 13290538

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.11.2005 at 10:43PM PST, ID: 13292663

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.12.2005 at 07:11AM PST, ID: 13293827

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.12.2005 at 02:42PM PST, ID: 13295541

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: OpenGL Graphics & Game Programming
Tags: glreadpixels
Sign Up Now!
Solution Provided By: davebytes
Participating Experts: 3
Solution Grade: B
 
 
[+][-]02.23.2005 at 03:31AM PST, ID: 13381016

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.23.2005 at 03:35AM PST, ID: 13381037

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32