-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuadMeshGenerator.java
139 lines (125 loc) · 3.12 KB
/
QuadMeshGenerator.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
/*
* Copyright 2020 damios
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.damios.guacamole.gdx.graphics;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
/**
* This class contains utility methods for generating {@link Mesh}es.
* <p>
* A simple example for rendering on a mesh:
*
* <pre>
* {@code
* shader.bind();
* shader.setUniformMatrix("u_projTrans", cam.combined);
*
* mesh.render(shader, GL20.GL_TRIANGLE_STRIP);
* }
* </pre>
*
* And if the shader uses a texture:
*
* <pre>
* {@code
* renderContext.begin();
* shader.bind();
*
* shader.setUniformMatrix("u_projTrans", cam.combined);
* shader.setUniformi("u_texture", renderContext.textureBinder.bind(texture));
*
* mesh.render(shader, GL20.GL_TRIANGLE_STRIP);
*
* renderContext.end();
* }
* </pre>
*
* @author damios
*/
public final class QuadMeshGenerator {
private QuadMeshGenerator() {
throw new UnsupportedOperationException();
}
/**
* @param screenWidth
* @param screenHeight
* @param flipY
* @return a screen filling quad
*/
public static Mesh createFullScreenQuad(int screenWidth, int screenHeight,
boolean flipY) {
return createQuad(0, 0, screenWidth, screenHeight, flipY);
}
/**
* @param x
* the bottom left x
* @param y
* the bottom left y
* @param width
* @param height
* @param flipY
* @return a quad with the given dimensions
*/
public static Mesh createQuad(float x, float y, float width, float height,
boolean flipY) {
return createQuadFromCoordinates(x, y, x + width, y + height, flipY);
}
/**
* Coordinate system: y-up.
*
* @param x1
* the left x
* @param y1
* the bottom y
* @param x2
* the right x
* @param y2
* the top y
* @param flipY
* @return a quad with the given coordinates
*/
public static Mesh createQuadFromCoordinates(float x1, float y1, float x2,
float y2, boolean flipY) {
float[] verts = new float[20];
int i = 0;
// Bottom left
verts[i++] = x1;
verts[i++] = y1;
verts[i++] = 0;
verts[i++] = 0;
verts[i++] = flipY ? 0 : 1;
// Top left
verts[i++] = x1;
verts[i++] = y2;
verts[i++] = 0;
verts[i++] = 0;
verts[i++] = flipY ? 1 : 0;
// Bottom right
verts[i++] = x2;
verts[i++] = y1;
verts[i++] = 0;
verts[i++] = 1;
verts[i++] = flipY ? 0 : 1;
// Top right
verts[i++] = x2;
verts[i++] = y2;
verts[i++] = 0;
verts[i++] = 1;
verts[i++] = flipY ? 1 : 0;
Mesh mesh = new Mesh(true, 4, 0, VertexAttribute.Position(),
VertexAttribute.TexCoords(0));
mesh.setVertices(verts);
return mesh;
}
}