1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library;
|
21
|
|
22
|
import android.opengl.GLES20;
|
23
|
import android.opengl.Matrix;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
public class DistortedRenderTarget
|
28
|
{
|
29
|
private static final int TEXTURE_FAILED_TO_CREATE = -1;
|
30
|
private static final int TEXTURE_NOT_CREATED_YET = -2;
|
31
|
|
32
|
private int mFramebufferID, mTextureID;
|
33
|
private float mX, mY;
|
34
|
private float mFOV;
|
35
|
|
36
|
int mWidth,mHeight,mDepth,mDistance;
|
37
|
float[] mProjectionMatrix;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
private void createProjection()
|
42
|
{
|
43
|
float ratio = (float) mWidth / mHeight;
|
44
|
float left =-ratio; //
|
45
|
float right = ratio; // Create a new perspective projection matrix.
|
46
|
float bottom = -1.0f; //
|
47
|
float top = 1.0f; // any change to those values will have serious consequences!
|
48
|
float near, far;
|
49
|
|
50
|
if( mFOV>0.0f ) // perspective projection
|
51
|
{
|
52
|
near= (float)(top / Math.tan(mFOV*Math.PI/360));
|
53
|
mDistance = (int)(mHeight*near/(top-bottom));
|
54
|
far = 2*mDistance-near;
|
55
|
|
56
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
57
|
}
|
58
|
else // parallel projection
|
59
|
{
|
60
|
near= (float)(top / Math.tan(Math.PI/360));
|
61
|
mDistance = (int)(mHeight*near/(top-bottom));
|
62
|
far = 2*mDistance-near;
|
63
|
|
64
|
Matrix.orthoM(mProjectionMatrix, 0, -mWidth/2, mWidth/2,-mHeight/2, mHeight/2, near, far);
|
65
|
}
|
66
|
|
67
|
mDepth = (int)((far-near)/2);
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
// must be called form a thread holding OpenGL Context
|
72
|
|
73
|
private boolean createFBO()
|
74
|
{
|
75
|
int[] textureIds = new int[1];
|
76
|
GLES20.glGenTextures(1, textureIds, 0);
|
77
|
mTextureID = textureIds[0];
|
78
|
int[] mFBORenderToTexture = new int[1];
|
79
|
|
80
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
|
81
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
|
82
|
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
|
83
|
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
|
84
|
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
|
85
|
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
|
86
|
|
87
|
GLES20.glGenFramebuffers(1, mFBORenderToTexture, 0);
|
88
|
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBORenderToTexture[0]);
|
89
|
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mTextureID, 0);
|
90
|
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
|
91
|
|
92
|
if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
|
93
|
{
|
94
|
android.util.Log.e("RenderTarget", "failed to create framebuffer, error="+status);
|
95
|
|
96
|
GLES20.glDeleteTextures(1, textureIds, 0);
|
97
|
GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
|
98
|
mFramebufferID = 0;
|
99
|
mTextureID = TEXTURE_FAILED_TO_CREATE;
|
100
|
return false;
|
101
|
}
|
102
|
|
103
|
mFramebufferID = mFBORenderToTexture[0];
|
104
|
|
105
|
return true;
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
private void deleteFBO()
|
111
|
{
|
112
|
int[] textureIds = new int[1];
|
113
|
int[] mFBORenderToTexture = new int[1];
|
114
|
|
115
|
textureIds[0] = mTextureID;
|
116
|
mFBORenderToTexture[0] = mFramebufferID;
|
117
|
|
118
|
GLES20.glDeleteTextures(1, textureIds, 0);
|
119
|
GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
|
120
|
|
121
|
mFramebufferID = 0;
|
122
|
mTextureID = TEXTURE_NOT_CREATED_YET;
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
// PUBLIC API
|
127
|
|
128
|
public DistortedRenderTarget(int width, int height)
|
129
|
{
|
130
|
mProjectionMatrix = new float[16];
|
131
|
|
132
|
mHeight = height;
|
133
|
mWidth = width;
|
134
|
mFramebufferID = 0;
|
135
|
mTextureID = TEXTURE_NOT_CREATED_YET;
|
136
|
mFOV = 6.0f;
|
137
|
mX = 0.0f;
|
138
|
mY = 0.0f;
|
139
|
|
140
|
createProjection();
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
public void release()
|
146
|
{
|
147
|
if( mTextureID>=0 ) deleteFBO();
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
public void setProjection(float FOV, float x, float y)
|
153
|
{
|
154
|
mFOV = FOV;
|
155
|
mX = x;
|
156
|
mY = y;
|
157
|
|
158
|
createProjection();
|
159
|
}
|
160
|
|
161
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162
|
|
163
|
public void resize(int width, int height)
|
164
|
{
|
165
|
mWidth = width;
|
166
|
mHeight= height;
|
167
|
|
168
|
createProjection();
|
169
|
|
170
|
if( mTextureID>0 ) deleteFBO();
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public int getFBO()
|
176
|
{
|
177
|
return mFramebufferID;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
public int getTexture()
|
183
|
{
|
184
|
return mTextureID;
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
// set this as Render Target. Must be called from a thread holding OpenGL context.
|
189
|
|
190
|
public void use()
|
191
|
{
|
192
|
if( mTextureID==TEXTURE_NOT_CREATED_YET ) createFBO();
|
193
|
|
194
|
|
195
|
}
|
196
|
}
|