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

Last change on this file since 2720 was 2096, checked in by ldelgass, 14 years ago

Normalize line endings, set eol-style to native on *.cpp, *.h files

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1
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 "Trace.h"
21#include <assert.h>
22#include "ScreenSnapper.h"
23
24ScreenSnapper::ScreenSnapper(int w, int h, GLuint type,
25                             int channel_per_pixel)
26{
27    width = w;
28    height = h;
29
30    //only allow two types
31    assert(type == GL_FLOAT || type == GL_UNSIGNED_BYTE);
32    data_type = type;
33 
34    //only allow RGB or RGBA
35    assert(channel_per_pixel == 3 || channel_per_pixel == 4);
36    n_channels_per_pixel = channel_per_pixel;
37
38    if(type == GL_FLOAT)
39        data = new float[w*h*channel_per_pixel];
40    else if(type == GL_UNSIGNED_BYTE)
41        data = new unsigned char[w*h*channel_per_pixel];
42 
43    assert(data!=0);
44    reset(0);   //reset data
45}
46
47ScreenSnapper::~ScreenSnapper()
48{
49    if(data_type == GL_FLOAT)
50        delete[] (float*)data;
51    else if(data_type == GL_UNSIGNED_BYTE)
52        delete[] (unsigned char*)data;
53}
54
55void
56ScreenSnapper::reset(char c)
57{
58    unsigned int elemSize;
59    switch (data_type) {
60    case GL_FLOAT:
61        elemSize = sizeof(float);
62        break;
63
64    case GL_UNSIGNED_BYTE:
65        elemSize = sizeof(unsigned char);
66        break;
67
68    default:
69        assert(0);
70        break;
71    }
72    unsigned int size;
73    size = elemSize * width * height * n_channels_per_pixel;
74    memset(data, size, c);
75}
76
77void
78ScreenSnapper::capture()
79{
80    if(data_type == GL_FLOAT){
81        if(n_channels_per_pixel==3)
82            glReadPixels(0, 0, width, height, GL_RGB, GL_FLOAT, data);
83        else if(n_channels_per_pixel==4)
84            glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, data);
85    }
86    else if(data_type == GL_UNSIGNED_BYTE){
87        if(n_channels_per_pixel==3)
88            glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
89        else if(n_channels_per_pixel==4)
90            glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
91    }
92    assert(glGetError()==0);
93}
94
95void
96ScreenSnapper::print()
97{
98    for(int i=0; i<width*height; i++){
99        if(data_type == GL_FLOAT){
100            if(n_channels_per_pixel==3)
101                TRACE("(%f %f %f) ", ((float*)data)[3*i],
102                       ((float*)data)[3*i+1], ((float*)data)[3*i+2]);
103            else if(n_channels_per_pixel==4)
104                TRACE("(%f %f %f %f) ", ((float*)data)[4*i],
105                      ((float*)data)[4*i+1],
106                      ((float*)data)[4*i+2],
107                       ((float*)data)[4*i+3]);
108        }
109        else if(data_type == GL_UNSIGNED_BYTE){
110            if(n_channels_per_pixel==3)
111                TRACE("(%d %d %d) ",
112                        ((unsigned char*)data)[3*i],
113                        ((unsigned char*)data)[3*i+1],
114                       ((unsigned char*)data)[3*i+2]);
115            else if(n_channels_per_pixel==4)
116                TRACE("(%d %d %d %d) ",
117                        ((unsigned char*)data)[4*i],
118                        ((unsigned char*)data)[4*i+1],
119                        ((unsigned char*)data)[4*i+2],
120                       ((unsigned char*)data)[4*i+3]);
121        }
122    }
123}
124
Note: See TracBrowser for help on using the repository browser.