source: trunk/packages/vizservers/nanovis/ScreenSnapper.cpp @ 2892

Last change on this file since 2892 was 2870, checked in by ldelgass, 12 years ago

remove global.h header. Move global Cg context handle into NvShader? as a
static member. Move LoadCgSourceProgram? to NvShader?.cpp, but make shader
classes use the methods NvShader::loadVertex/FragmentProgram instead.
There are still some users of LoadCgSourceProgram? left that need the context
handle.

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 * ScreenSnapper.h: ScreenSnapper class. It captures the render result
5 *                      and stores it in an array of chars or floats
6 *                      depending on chosen format.
7 *
8 * ======================================================================
9 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
10 *           Purdue Rendering and Perceptualization Lab (PURPL)
11 *
12 *  Copyright (c) 2004-2006  Purdue Research Foundation
13 *
14 *  See the file "license.terms" for information on usage and
15 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 * ======================================================================
17 */
18
19#include <memory.h>
20#include <assert.h>
21
22#include "ScreenSnapper.h"
23#include "Trace.h"
24
25ScreenSnapper::ScreenSnapper(int w, int h, GLuint type,
26                             int channelsPerPixel)
27{
28    _width = w;
29    _height = h;
30
31    //only allow two types
32    assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
33    _dataType = type;
34 
35    //only allow RGB or RGBA
36    assert(channelsPerPixel == 3 || channelsPerPixel == 4);
37    _numChannelsPerPixel = channelsPerPixel;
38
39    if (type == GL_FLOAT)
40        _data = new float[w*h*channelsPerPixel];
41    else if (type == GL_UNSIGNED_BYTE)
42        _data = new unsigned char[w*h*channelsPerPixel];
43 
44    assert(_data != 0);
45    reset(0);   //reset data
46}
47
48ScreenSnapper::~ScreenSnapper()
49{
50    if (_dataType == GL_FLOAT)
51        delete[] (float *)_data;
52    else if(_dataType == GL_UNSIGNED_BYTE)
53        delete[] (unsigned char*)_data;
54}
55
56void
57ScreenSnapper::reset(char c)
58{
59    unsigned int elemSize;
60    switch (_dataType) {
61    case GL_FLOAT:
62        elemSize = sizeof(float);
63        break;
64    case GL_UNSIGNED_BYTE:
65        elemSize = sizeof(unsigned char);
66        break;
67    default:
68        assert(0);
69        break;
70    }
71    unsigned int size;
72    size = elemSize * _width * _height * _numChannelsPerPixel;
73    memset(_data, size, c);
74}
75
76void
77ScreenSnapper::capture()
78{
79    if (_dataType == GL_FLOAT) {
80        if (_numChannelsPerPixel == 3)
81            glReadPixels(0, 0, _width, _height, GL_RGB, GL_FLOAT, _data);
82        else if (_numChannelsPerPixel == 4)
83            glReadPixels(0, 0, _width, _height, GL_RGBA, GL_FLOAT, _data);
84    } else if (_dataType == GL_UNSIGNED_BYTE) {
85        if (_numChannelsPerPixel == 3)
86            glReadPixels(0, 0, _width, _height, GL_RGB, GL_UNSIGNED_BYTE, _data);
87        else if (_numChannelsPerPixel == 4)
88            glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, _data);
89    }
90    assert(glGetError() == 0);
91}
92
93void
94ScreenSnapper::print()
95{
96    for (int i = 0; i < _width*_height; i++) {
97        if (_dataType == GL_FLOAT) {
98            if (_numChannelsPerPixel == 3) {
99                TRACE("(%f %f %f) ", ((float *)_data)[3*i],
100                      ((float*)_data)[3*i+1], ((float *)_data)[3*i+2]);
101            } else if (_numChannelsPerPixel == 4) {
102                TRACE("(%f %f %f %f) ", ((float *)_data)[4*i],
103                      ((float *)_data)[4*i+1],
104                      ((float *)_data)[4*i+2],
105                      ((float *)_data)[4*i+3]);
106            }
107        } else if (_dataType == GL_UNSIGNED_BYTE) {
108            if (_numChannelsPerPixel == 3) {
109                TRACE("(%d %d %d) ",
110                      ((unsigned char *)_data)[3*i],
111                      ((unsigned char *)_data)[3*i+1],
112                      ((unsigned char *)_data)[3*i+2]);
113            } else if (_numChannelsPerPixel == 4) {
114                TRACE("(%d %d %d %d) ",
115                      ((unsigned char *)_data)[4*i],
116                      ((unsigned char *)_data)[4*i+1],
117                      ((unsigned char *)_data)[4*i+2],
118                      ((unsigned char *)_data)[4*i+3]);
119            }
120        }
121    }
122}
123
Note: See TracBrowser for help on using the repository browser.