FrameBufferGL.cc

Go to the documentation of this file.
00001 #include <gra/framebuffer/FrameBufferGL.h>
00002 #include <gra/camera/CameraGL.h>
00003 
00004 using namespace gra;
00005 
00006 void FrameBufferGL::setWidth(unsigned w) 
00007 {
00008     FrameBuffer::setWidth(w);
00009     GLint val[4];
00010     glGetIntegerv(GL_VIEWPORT, val);
00011     glViewport(val[0], val[1], w, val[3]);
00012 }
00013 
00014 void FrameBufferGL::setHeight(unsigned h)
00015 {
00016     FrameBuffer::setHeight(h);
00017     GLint val[4];
00018     glGetIntegerv(GL_VIEWPORT, val);
00019     glViewport(val[0], val[1], val[2], h);
00020 }
00021 
00022 void FrameBufferGL::setColor(const Camera&  camera,
00023                              unsigned       x,
00024                              unsigned       y,
00025                              float          depth,
00026                              const Color3f& color)
00027 {
00028     //cerr << "Setting pixel (" << x << "," << y << "," << depth << "):\t";
00029     //cerr << color.x << " " << color.y << " " << color.z << endl;
00030 
00031     GLint     v[4];
00032     GLboolean validRP;
00033     GLfloat   c[3] = { color.x, color.y, color.z };
00034 
00035     /*
00036      * Compute position of pixel on the camera's projection plane with
00037      * respect to projection plane size and position.
00038      */
00039     const Projection& proj = camera.getProjection();
00040     float newX =
00041         (float)(x+1) / (float)_width * proj.getPlaneWidth() +
00042         proj.getLeftPlane();
00043     float newY =
00044         (float)(y+1) / (float)_height * proj.getPlaneHeight() +
00045         proj.getBottomPlane();
00046 
00047     glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &validRP);
00048     if (validRP) glGetIntegerv(GL_CURRENT_RASTER_POSITION, v);
00049 
00050     glMatrixMode(GL_MODELVIEW);
00051     glPushMatrix();
00052     glLoadIdentity();
00053     glRasterPos3f(newX, newY, depth);
00054     glDrawPixels(1, 1, GL_RGB, GL_FLOAT, (void*) c);
00055     if (validRP) glRasterPos4iv(v);
00056     glPopMatrix();
00057 }
00058 
00059 void FrameBufferGL::setColor(const Camera&  camera,
00060                              unsigned       x,
00061                              unsigned       y,
00062                              const Color3f& color)
00063 {
00064     //cerr << "Setting pixel (" << x << "," << y << "):\t";
00065     //cerr << color.x << " " << color.y << " " << color.z << endl;
00066 
00067     GLint     v[4];
00068     GLboolean validRP;
00069     GLfloat   c[3] = { color.x, color.y, color.z };
00070 
00071     /*
00072      * Compute position of pixel on the camera's projection plane with
00073      * respect to projection plane size and position.
00074      */
00075     const Projection& proj = camera.getProjection();
00076     float newX =
00077         (float)(x+1) / (float)_width * proj.getPlaneWidth() +
00078         proj.getLeftPlane();
00079     float newY =
00080         (float)(y+1) / (float)_height * proj.getPlaneHeight() +
00081         proj.getBottomPlane();
00082 
00083     /*
00084      * Ensure that the OpenGL projection matrix is set, store OpenGL state
00085      */
00086     glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &validRP);
00087     if (validRP) glGetIntegerv(GL_CURRENT_RASTER_POSITION, v);
00088     glMatrixMode(GL_PROJECTION);
00089     glPushMatrix();
00090     glMatrixMode(GL_MODELVIEW);
00091     glPushMatrix();
00092     CameraGL cgl(camera);
00093 
00094     /*
00095      * OpenGL camera could change transformation
00096      */
00097     glMatrixMode(GL_MODELVIEW);
00098     glLoadIdentity();
00099 
00100     /*
00101      * Set pixel color
00102      */
00103     glRasterPos2f(newX, newY);
00104     glDrawPixels(1, 1, GL_RGB, GL_FLOAT, (void*) c);
00105 
00106     /*
00107      * Restore OpenGL state
00108      */
00109     if (validRP) glRasterPos4iv(v);
00110     glPopMatrix();
00111     glMatrixMode(GL_PROJECTION);
00112     glPopMatrix();
00113 }
00114 
00115 void FrameBufferGL::setColors(const Camera& camera,
00116                               unsigned      x,
00117                               unsigned      y,
00118                               float         depth,
00119                               const Color3f colors[],
00120                               unsigned      size)
00121 {
00122     fprintf(stderr,"FrameBufferGL::setColors(): To do");
00123     // to do
00124 }
00125 
00126 void FrameBufferGL::setColors(const Camera& camera,
00127                               unsigned      x,
00128                               unsigned      y,
00129                               const Color3f colors[],
00130                               unsigned      size)
00131 {
00132     GLint     v[4];
00133     GLboolean validRP;
00134     GLfloat * c = new GLfloat [3 * size];
00135 
00136     for (unsigned i = 0; i < size; i++) {
00137         c[3 * i + 0] = colors[i].x;
00138         c[3 * i + 1] = colors[i].y;
00139         c[3 * i + 2] = colors[i].z;
00140         //fprintf(stderr,"%f %f %f\n",colors[i].x,colors[i].y,colors[i].z);
00141     }
00142 
00143     /*
00144      * Ensure that the OpenGL projection matrix is set, store OpenGL state
00145      */
00146     glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &validRP);
00147     if (validRP) glGetIntegerv(GL_CURRENT_RASTER_POSITION, v);
00148     glMatrixMode(GL_PROJECTION);
00149     glPushMatrix();
00150     glMatrixMode(GL_MODELVIEW);
00151     glPushMatrix();
00152     CameraGL cgl(camera);
00153 
00154     /*
00155      * OpenGL camera could change transformation
00156      */
00157     glMatrixMode(GL_MODELVIEW);
00158     glLoadIdentity();
00159 
00160     glPixelZoom(1, -1);
00161 
00162     const Projection& proj = camera.getProjection();
00163     float xpos, ypos;
00164     unsigned firstSegment = _width - x;
00165     unsigned block = 0;
00166     unsigned lastSegment = 0;
00167 
00168     if (firstSegment == _width)
00169         firstSegment = 0;
00170     if (firstSegment > size)
00171         firstSegment = size;
00172     else {
00173         block = (unsigned) ((size - firstSegment) / _width);
00174         lastSegment = size - firstSegment - block * _width;
00175     }
00176     
00177     //cerr << firstSegment << "," << block << "," << lastSegment << endl;
00178 
00179     Vector2f ps = camera.getPixelSize(_width, _height);
00180     ps.scale(.5);
00181 
00182     /*
00183      * write first line segment
00184      */
00185     if (firstSegment) {
00186         xpos =
00187             (float)(x+1) / (float)_width * proj.getPlaneWidth() +
00188             proj.getLeftPlane();
00189         ypos =
00190             (float)(y+1) / (float)_height * proj.getPlaneHeight() +
00191             proj.getBottomPlane();
00192 
00193         glRasterPos3f(xpos, ypos, proj.getNearPlane());
00194         glDrawPixels(firstSegment, 1, GL_RGB, GL_FLOAT, c);
00195     }
00196 
00197     /*
00198      * write block of entire lines
00199      */
00200     if (block) {
00201         xpos =
00202             proj.getPlaneWidth() / (float)_width +
00203             proj.getLeftPlane();
00204         ypos =
00205             (float)(y) / (float)_height * proj.getPlaneHeight() +
00206             proj.getBottomPlane();
00207 
00208         glRasterPos3f(xpos, ypos, proj.getNearPlane());
00209         glDrawPixels(_width+1, block, GL_RGB, GL_FLOAT, c + 3 * firstSegment);
00210     }
00211 
00212     /*
00213      * write last line segment
00214      */
00215     if (lastSegment) {
00216         xpos =
00217             (float)(1.0) / (float)_width * proj.getPlaneWidth() +
00218             proj.getLeftPlane();
00219         ypos =
00220             (float)(y - block - ((firstSegment) ? 2 : 1)) / (float)_height * proj.getPlaneHeight() +
00221             proj.getBottomPlane();
00222         
00223         glRasterPos3f(xpos, ypos, proj.getNearPlane());
00224         glDrawPixels(lastSegment, 1, GL_RGB, GL_FLOAT, c + 3 * firstSegment + 3 * _width * block);
00225     }
00226     
00227     /*
00228      * Restore OpenGL state
00229      */
00230     if (validRP) glRasterPos4iv(v);
00231     glPopMatrix();
00232     glMatrixMode(GL_PROJECTION);
00233     glPopMatrix();
00234     glPixelZoom(1, 1);
00235 
00236     delete [] c;
00237 }
00238 
00239 Color3f FrameBufferGL::getColor(unsigned x, unsigned y) const
00240 {
00241     /*
00242      * We need either the camera that made the snapshot (see setColor()),
00243      * or to remember stored colors separately, which is memory consuming.
00244      */
00245     cerr << "FrameBufferGL::getColor(): unimplemented" << endl;
00246     return Color3f();
00247 }
00248 
00249 void FrameBufferGL::clearColor(const Color3f& color)
00250 {
00251     glClearColor(color.x, color.y, color.z, 1.0);
00252     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00253 }
00254 
00255 
00256 #if 0
00257 int FrameBufferGL::init()
00258 {
00259     glMatrixMode(GL_PROJECTION);
00260     glLoadIdentity();
00261     glOrtho(0, getWidth(), 0, getHeight(), -1, 1);
00262 
00263     glDisable(GL_LIGHTING);
00264 
00265     glMatrixMode(GL_MODELVIEW);
00266 
00267     return 1;
00268 }
00269 
00270 #endif // 0

Generated on Tue Nov 21 15:11:42 2006 for gra by  doxygen 1.4.6