File: rfx_rendertarget.cpp

package info (click to toggle)
meshlab 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (181 lines) | stat: -rw-r--r-- 5,966 bytes parent folder | download | duplicates (5)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
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
/****************************************************************************
* MeshLab                                                           o o     *
* A versatile mesh processing toolbox                             o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005-2008                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program 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 General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#include "rfx_rendertarget.h"

RfxRenderTarget::RfxRenderTarget(const QString& rtName)
{
	name = rtName;
	width = 0;
	height = 0;
	fbo = 0;
	depTex = 0;
	colTex = 0;
	mipmaps = false;
	vportdim = false;
	initOk = false;
}

RfxRenderTarget::~RfxRenderTarget()
{
	if (initOk) {
		glDeleteFramebuffersEXT(1, &fbo);
		glDeleteRenderbuffersEXT(1, &depTex);
		glDeleteTextures(1, &colTex);
	}
}

void RfxRenderTarget::SetClear(int pass, float depthClear, float *colorClear)
{
	passOptions[pass].clearMask = 0;
	passOptions[pass].colorClear = false;
	passOptions[pass].depthClear = false;

	// valid depth clear values are [0, 1]
	// -1.0 disable depth clear
	if (depthClear != -1.0) {
		passOptions[pass].depthClear = true;

		passOptions[pass].depthClearVal = depthClear;
		passOptions[pass].clearMask |= GL_DEPTH_BUFFER_BIT;
	}

	// color should be a 4-elements array.
	// NULL disable color clear
	if (colorClear != NULL) {
		passOptions[pass].colorClear = true;

		passOptions[pass].colorClearVal[0] = colorClear[0];
		passOptions[pass].colorClearVal[1] = colorClear[1];
		passOptions[pass].colorClearVal[2] = colorClear[2];
		passOptions[pass].colorClearVal[3] = colorClear[3];
		passOptions[pass].clearMask |= GL_COLOR_BUFFER_BIT;
	}
}

bool RfxRenderTarget::Setup(int pass)
{
	if (!GLEW_EXT_framebuffer_object) {
		qWarning("FBO not supported!");
		return false;
	}

	if (initOk)
		return true;

	glGenFramebuffersEXT(1, &fbo);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

	// if "use viewport dimensions" at this point we have a gl context
	if (vportdim) {
		GLfloat dims[4];
		glGetFloatv(GL_VIEWPORT, dims);
		width = (int)dims[2];
		height = (int)dims[3];
	}

	// depth buffer
	glGenRenderbuffersEXT(1, &depTex);
	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depTex);
	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depTex);

	// color buffer
	glGenTextures(1, &colTex);
	glBindTexture(GL_TEXTURE_2D, colTex);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colTex, 0);

	// set texture state based on the first uniform that will use RT
	QList<int> k = passStates.keys();
	for (int i = 0; i < k.size(); ++i) {
		if (k.at(i) > pass) {
			foreach (RfxState *s, passStates.value(i))
				s->SetEnvironment(GL_TEXTURE_2D);
		}
	}

	initOk = (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	return initOk;
}

void RfxRenderTarget::Bind(int pass)
{
	assert(initOk);

	bool colClear = passOptions.value(pass).colorClear;
	bool depClear = passOptions.value(pass).depthClear;

	if (colClear) {
		const float *cols = passOptions.value(pass).colorClearVal;
		glClearColor(cols[0], cols[1], cols[2], cols[3]);
	}

	if (depClear)
		glClearDepth(passOptions.value(pass).depthClearVal);

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

	glPushAttrib(GL_VIEWPORT_BIT);
	glViewport(0, 0, width, height);

	if (colClear || depClear)
		glClear(passOptions.value(pass).clearMask);
}

void RfxRenderTarget::Unbind()
{
	if (!initOk)
		return;

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glPopAttrib();
}

QImage RfxRenderTarget::GetQImage()
{
	if (!initOk)
		return QImage();

	QImage img(width, height, QImage::Format_RGB32);

	unsigned char *tempBuf = new unsigned char[width * height * 3];
	unsigned char *tempBufPtr = tempBuf;
	glBindTexture(GL_TEXTURE_2D, colTex);
	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, tempBufPtr);
	for (int i = 0; i < height; ++i) {
		QRgb *scanLine = (QRgb*)img.scanLine(i);
		for (int j = 0; j < width; ++j) {
			scanLine[j] = qRgb(tempBufPtr[0], tempBufPtr[1], tempBufPtr[2]);
			tempBufPtr += 3;
		}
	}
	delete[] tempBuf;

	return img.mirrored();
}