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,h
eight,GL_R
GB,GL_FLOA
T,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,
- capricious
Start Free Trial