-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenGL2DTextureRegion.java
265 lines (203 loc) · 8.86 KB
/
OpenGL2DTextureRegion.java
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.jukusoft.rpg.graphic.opengl.image;
import com.jukusoft.rpg.core.logger.GameLogger;
import com.jukusoft.rpg.core.math.Vector3f;
import com.jukusoft.rpg.core.utils.ArrayUtils;
import com.jukusoft.rpg.graphic.opengl.mesh.DrawableObject;
import com.jukusoft.rpg.graphic.opengl.mesh.Material;
import com.jukusoft.rpg.graphic.opengl.mesh.Mesh;
import com.jukusoft.rpg.graphic.opengl.texture.OpenGL2DTexture;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Justin on 10.12.2016.
*/
public class OpenGL2DTextureRegion extends DrawableObject {
/**
* some constants
*/
private static final float ZPOS = 0.0f;
private static final int VERTICES_PER_QUAD = 4;
protected float width = 0;
protected float height = 0;
protected OpenGL2DTexture texture = null;
/**
* some interesting links:
* - http://www.meandmark.com/tilingpart1.html
* - http://gamedev.stackexchange.com/questions/30362/drawing-lots-of-tiles-with-opengl-the-modern-way
* - https://www.opengl.org/discussion_boards/showthread.php/172751-Approaches-for-2D-tile-rendering
*/
/**
* viewport of image
*/
protected ImageRegionInfo imageRegionInfo = null;
protected float maxWidthOfTexture = 0f;
protected float maxHeightOfTexture = 0f;
/**
* instance of list, so we dont need to create an new list, if texture coordinates will be updated
*/
protected List<Float> cachedTextCoords = new ArrayList();
/**
* default constructor
*/
public OpenGL2DTextureRegion (final float x, final float y, final float startX, final float startY, final float width, final float height, OpenGL2DTexture texture) {
this.imageRegionInfo = new ImageRegionInfo(startX, startY, width, height);
//set image and build mesh
this.setTexture(x, y, startX, startY, width, height, texture);
//set default color #FFFFFF
this.getMesh().getMaterial().setColor(new Vector3f(1f, 1f, 1f));
//set position
this.setPosition(x, y, 0);
}
protected Mesh buildMesh (final float x, final float y, final float width, final float height, OpenGL2DTexture texture) {
List<Float> positions = new ArrayList();
List<Float> textCoords = new ArrayList();
//we dont need normals yet
float[] normals = new float[0];
//indices
List<Integer> indices = new ArrayList();
int i = 0;
ImageRegionInfo imageView = this.imageRegionInfo;
//left top vertex
positions.add(x); //x
positions.add(y); //y
positions.add(ZPOS); //z
textCoords.add(/*(float) charInfo.getStartX() / (float)fontTexture.getWidth()*/x / width);
textCoords.add(imageView.getStartY() / imageView.getHeight());
indices.add(i * VERTICES_PER_QUAD);
//left bottom vertex
positions.add(x); // x
positions.add(height); //y
positions.add(ZPOS); //z
textCoords.add(/*(float)charInfo.getStartX() / (float)fontTexture.getWidth()*/x / width);
textCoords.add((imageView.getStartY() + imageView.getHeight()) / imageView.getHeight());
indices.add(i * VERTICES_PER_QUAD + 1);
//right bottom vertex
positions.add(x + width); // x
positions.add(height); //y
positions.add(ZPOS); //z
textCoords.add((float)(x + width)/ width);
textCoords.add((imageView.getStartY() + imageView.getHeight()) / imageView.getHeight());
indices.add(i * VERTICES_PER_QUAD + 2);
//right top vertex
positions.add(x + width); // x
positions.add(0.0f); //y
positions.add(ZPOS); //z
textCoords.add((x + width) / width);
textCoords.add(imageView.getStartY() / imageView.getHeight());
indices.add(i * VERTICES_PER_QUAD + 3);
//add indices for left top and bottom right vertices
indices.add(i * VERTICES_PER_QUAD);
indices.add(i * VERTICES_PER_QUAD + 2);
float[] posArr = ArrayUtils.convertFloatListToArray(positions);
float[] textCoordsArr = ArrayUtils.convertFloatListToArray(textCoords);
int[] indicesArr = indices.stream().mapToInt((j) -> {
return j;
}).toArray();
//create new mesh with material
Mesh mesh = new Mesh(posArr, textCoordsArr, normals, indicesArr);
mesh.setMaterial(new Material(texture));
return mesh;
}
protected void updatePositions (final float x, final float y, final float width, final float height) {
//create list with new positions
List<Float> positions = new ArrayList();
//left top vertex
positions.add(x); //x
positions.add(y); //y
positions.add(ZPOS); //z
//left bottom vertex
positions.add(x); // x
positions.add(height); //y
positions.add(ZPOS); //z
//right bottom vertex
positions.add(x + width); // x
positions.add(height); //y
positions.add(ZPOS); //z
//right top vertex
positions.add(x + width); // x
positions.add(0.0f); //y
positions.add(ZPOS); //z
//convert list to array
float[] posArray = ArrayUtils.convertFloatListToArray(positions);
//upload positions array to gpu
this.getMesh().setPositionVBO(posArray);
}
protected void updateTexCoords (ReadonlyImageRegionInfo imageView) {
//create new list for texture coordinates
List<Float> textCoords = this.cachedTextCoords;
textCoords.clear();
//GameLogger.debug("OpenGL2DTextureRegion", "updateTexCoords, startX: " + imageView.getStartX() + ", startY: " + imageView.getStartY() + ", width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
/**
* (0, 0) is left bottom and (1, 1) right top of texture
*/
final float textureWidth = this.texture.getWidth();
final float textureHeight = this.texture.getHeight();
//left top vertex
textCoords.add(imageView.getStartX() / textureWidth);
textCoords.add(imageView.getStartY() / textureHeight);
//left bottom vertex
textCoords.add(imageView.getStartX() / textureWidth);
textCoords.add((imageView.getStartY() + imageView.getHeight()) / textureHeight);
//right bottom vertex
textCoords.add((imageView.getStartX() + imageView.getWidth() ) / textureWidth);
textCoords.add((imageView.getStartY() + imageView.getHeight()) / textureHeight);
//right top vertex
textCoords.add((imageView.getStartX() + imageView.getWidth() ) / textureWidth);
textCoords.add(imageView.getStartY() / textureHeight);
float[] textCoordsArr = ArrayUtils.convertFloatListToArray(textCoords);
this.getMesh().setTextureCoordinatesVBO(textCoordsArr);
}
public void setTexture (final float x, final float y, final float startX, final float startY, final float width, final float height, OpenGL2DTexture texture) {
//get old texture
OpenGL2DTexture oldTexture = this.texture;
//create and upload new texture
this.texture = texture;
//create new mesh
Mesh mesh = buildMesh(0, 0, width, height, texture);
//get old mesh
Mesh oldMesh = getMesh();
//set new mesh
setMesh(mesh);
//set default color #FFFFFF
this.getMesh().getMaterial().setColor(new Vector3f(1f, 1f, 1f));
if (oldMesh != null) {
//delete old mesh from gpu
oldMesh.cleanUp();
}
if (oldTexture != null) {
//delete old texture from gpu
oldTexture.delete();
}
//set region (viewport of image)
this.imageRegionInfo.set(startX, startY, width, height);
//update texture coordinates
this.updateTexCoords(this.imageRegionInfo);
//set position
this.setPosition(x, y, 0);
}
public void setRegion (final float startX, final float startY, final float width, final float height) {
float oldWidth = 0;
float oldHeight = 0;
if (this.imageRegionInfo == null) {
this.imageRegionInfo = new ImageRegionInfo(startX, startY, width, height);
} else {
oldWidth = this.imageRegionInfo.getWidth();
oldHeight = this.imageRegionInfo.getHeight();
this.imageRegionInfo.set(startX, startY, width, height);
}
//check, if width or height was changed
if (this.imageRegionInfo.getWidth() != oldWidth || this.imageRegionInfo.getHeight() != oldHeight) {
//we have to update position VBO
this.updatePositions(0, 0, width, height);
}
//update texture coordinates VBO
this.updateTexCoords(this.imageRegionInfo);
}
@Override
public boolean redrawWithSameParams() {
return true;
}
public ReadonlyImageRegionInfo getCurrentView () {
return this.imageRegionInfo;
}
}