glcanvas.cpp 6.51 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**********************************************************************
 *                                                                    *
 * tgt - Tiny Graphics Toolbox                                        *
 *                                                                    *
 * Copyright (C) 2006-2011 Visualization and Computer Graphics Group, *
 * Department of Computer Science, University of Muenster, Germany.   *
 * <http://viscg.uni-muenster.de>                                     *
 *                                                                    *
 * This file is part of the tgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

#include "tgt/glcanvas.h"
26
#include "tgt/glcontext.h"
schultezub's avatar
schultezub committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "tgt/camera.h"
#include "tgt/painter.h"

#include <cstdlib>
#include <stdio.h>

namespace tgt {

GLCanvas::GLCanvas(const std::string& title,
                   const ivec2& size,
                   const GLCanvas::Buffers buffers )
    : title_(title),
      size_(size),
      buffers_(buffers),
      rgbaSize_(0, 0, 0, 0),
      depthSize_(0),
      stencilSize_(0),
      doubleBuffered_((buffers & DOUBLE_BUFFER) != 0),
      stereoViewing_(false),
      fullscreen_(false),
      autoFlush_(true),
      initialized_(false),
      camera_(0),
      painter_(0) ,
      eventHandler_(new EventHandler())
{}

GLCanvas::~GLCanvas() {
55
    eventHandler_->clearEventListeners();
schultezub's avatar
schultezub committed
56
57
58
59
60
61
62
63
64
65
    delete eventHandler_;
}

void GLCanvas::sizeChanged(const ivec2& size) {
    size_ = size;
    if (painter_)
        painter_->sizeChanged(size);
}

void GLCanvas::paint() {
66
    GLContextScopedLock lock(getContext());
schultezub's avatar
schultezub committed
67
    if (painter_)
68
        painter_->repaint();
schultezub's avatar
schultezub committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    if (autoFlush_) {
        if (doubleBuffered_) {
            swap();
        } else {
            glFlush();
        }
    }
}

void GLCanvas::init() {
    if (!initialized_)
        initialized_ = true;
}

void GLCanvas::setPainter(Painter* p, bool initP) {
    painter_ = p;
    painter_->setCanvas(this);
    if (initP)
        initPainter();
}

Painter* GLCanvas::getPainter() const {
    return painter_;
}

void GLCanvas::initPainter() {
    if (painter_) {
96
97
98
99
        {
            GLContextScopedLock lock(getContext());
            painter_->init();
        }
schultezub's avatar
schultezub committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        painter_->sizeChanged(size_);
    }
}

EventHandler* GLCanvas::getEventHandler() const {
    return eventHandler_;
}
void GLCanvas::setEventHandler(EventHandler* handler){
    delete eventHandler_;
    eventHandler_ = handler;
}

bool GLCanvas::getAutoFlush() {
 return autoFlush_;
}

bool GLCanvas::takeScreenshot(std::string fname) {
    //taken from gamedev.net forum:
    FILE *pFile;               // The file pointer.
    unsigned char uselessChar; // used for useless char.
    short int uselessInt;      // used for useless int.
    unsigned char imageType;   // Type of image we are saving.
    int index;                 // used with the for loop.
    unsigned char bits;    // Bit depth.
    long Size;                 // Size of the picture.
    int colorMode;
    unsigned char tempColors;
    short int width = getWidth();
    short int height = getHeight();

    // Open file for output.
    pFile = fopen(fname.c_str(), "wb");

   // Check if the file opened or not.
    if (!pFile) {
        fclose(pFile);
        return false;
    }

    unsigned char *image = new unsigned char[getWidth()*getHeight()*3];
    //read image from gl:
    glReadPixels( 0, 0, getWidth(), getHeight(), GL_RGB, GL_UNSIGNED_BYTE, image );
    // Set the image type, the color mode, and the bit depth.
    imageType = 2; colorMode = 3; bits = 24;

    // Set these two to 0.
    uselessChar = 0; uselessInt = 0;

    // Write useless data.
    size_t written = 0;
    written += fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
    written += fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);

    // Now image type.
    written += fwrite(&imageType, sizeof(unsigned char), 1, pFile);

    // Write useless data.
    written += fwrite(&uselessInt, sizeof(short int), 1, pFile);
    written += fwrite(&uselessInt, sizeof(short int), 1, pFile);
    written += fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
    written += fwrite(&uselessInt, sizeof(short int), 1, pFile);
    written += fwrite(&uselessInt, sizeof(short int), 1, pFile);

    // Write the size that you want.
    written += fwrite(&width, sizeof(short int), 1, pFile);
    written += fwrite(&height, sizeof(short int), 1, pFile);
    written += fwrite(&bits, sizeof(unsigned char), 1, pFile);

    // Write useless data.
    written += fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);

    // Get image size.
    Size = width * height * colorMode;

    // Now switch image from RGB to BGR.
    for (index = 0; index < Size; index += colorMode){
        tempColors = image[index];
        image[index] = image[index + 2];
        image[index + 2] = tempColors;
    }

    // Finally write the image.
    written += fwrite(image, sizeof(unsigned char), Size, pFile);

    // close the file.
    fclose(pFile);
    delete[] image;

    return (written > 0);
}

const ivec4& GLCanvas::getRgbaSize() const {
    return rgbaSize_;
}

int GLCanvas::getDepthSize() const {
    return depthSize_;
}

int GLCanvas::getStencilSize() const {
    return stencilSize_;
}

ivec2 GLCanvas::getSize() const {
    return size_;
}

int GLCanvas::getWidth() const {
    return size_.x;
}

int GLCanvas::getHeight() const {
    return size_.y;
}

GLCanvas::Buffers GLCanvas::getBuffers() const {
    return buffers_;
}

void GLCanvas::setCamera(Camera* cam) {
    camera_ = cam;
}

Camera* GLCanvas::getCamera() const {
    return camera_;
}

} // namespace