forked from realthunder/solvespace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgloffscreen.cpp
More file actions
86 lines (69 loc) · 2.72 KB
/
gloffscreen.cpp
File metadata and controls
86 lines (69 loc) · 2.72 KB
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
26
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//-----------------------------------------------------------------------------
// Offscreen rendering in OpenGL using framebuffer objects.
//
// Copyright 2015 <[email protected]>
//-----------------------------------------------------------------------------
#ifdef __APPLE__
#include <OpenGL/GL.h>
#else
#include <GL/glew.h>
#endif
#include "gloffscreen.h"
#include "solvespace.h"
GLOffscreen::GLOffscreen() : _pixels(NULL), _pixels_inv(NULL), _width(0), _height(0) {
#ifndef __APPLE__
if(glewInit() != GLEW_OK)
oops();
#endif
if(!GL_EXT_framebuffer_object)
oops();
glGenFramebuffersEXT(1, &_framebuffer);
glGenRenderbuffersEXT(1, &_color_renderbuffer);
glGenRenderbuffersEXT(1, &_depth_renderbuffer);
}
GLOffscreen::~GLOffscreen() {
delete[] _pixels;
delete[] _pixels_inv;
glDeleteRenderbuffersEXT(1, &_depth_renderbuffer);
glDeleteRenderbuffersEXT(1, &_color_renderbuffer);
glDeleteFramebuffersEXT(1, &_framebuffer);
}
bool GLOffscreen::begin(int width, int height) {
if(_width != width || _height != height) {
delete[] _pixels;
delete[] _pixels_inv;
_pixels = new uint32_t[width * height * 4];
_pixels_inv = new uint32_t[width * height * 4];
_width = width;
_height = height;
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _framebuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _color_renderbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, _width, _height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, _color_renderbuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depth_renderbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, _width, _height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, _depth_renderbuffer);
if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT)
return true;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
return false;
}
uint8_t *GLOffscreen::end(bool flip) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
glReadPixels(0, 0, _width, _height,
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, _pixels_inv);
#else
glReadPixels(0, 0, _width, _height,
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, _pixels_inv);
#endif
if(flip) {
/* in OpenGL coordinates, bottom is zero Y */
for(int i = 0; i < _height; i++)
memcpy(&_pixels[_width * i], &_pixels_inv[_width * (_height - i - 1)], _width * 4);
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
return (uint8_t*) (flip ? _pixels : _pixels_inv);
}