-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathTextureState.java
202 lines (173 loc) · 7.25 KB
/
TextureState.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
/*
* The MIT License (MIT)
*
* Copyright © 2015-2016, Heiko Brumme
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package silvertiger.tutorial.lwjgl.state;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.MemoryStack;
import silvertiger.tutorial.lwjgl.graphic.Shader;
import silvertiger.tutorial.lwjgl.graphic.ShaderProgram;
import silvertiger.tutorial.lwjgl.graphic.Texture;
import silvertiger.tutorial.lwjgl.graphic.VertexArrayObject;
import silvertiger.tutorial.lwjgl.graphic.VertexBufferObject;
import silvertiger.tutorial.lwjgl.math.Matrix4f;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glDrawElements;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
/**
* This state is for the texture tutorial.
*
* @author Heiko Brumme
*/
public class TextureState implements State {
private VertexArrayObject vao;
private VertexBufferObject vbo;
private VertexBufferObject ebo;
private Texture texture;
private Shader vertexShader;
private Shader fragmentShader;
private ShaderProgram program;
@Override
public void input() {
/* Nothing to do here */
}
@Override
public void update(float delta) {
/* Nothing to do here */
}
@Override
public void render(float alpha) {
glClear(GL_COLOR_BUFFER_BIT);
vao.bind();
texture.bind();
program.use();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
@Override
public void enter() {
/* Get width and height of framebuffer */
int width, height;
try (MemoryStack stack = MemoryStack.stackPush()) {
long window = GLFW.glfwGetCurrentContext();
IntBuffer widthBuffer = stack.mallocInt(1);
IntBuffer heightBuffer = stack.mallocInt(1);
GLFW.glfwGetFramebufferSize(window, widthBuffer, heightBuffer);
width = widthBuffer.get();
height = heightBuffer.get();
}
/* Create texture */
texture = Texture.loadTexture("resources/example.png");
texture.bind();
/* Generate Vertex Array Object */
vao = new VertexArrayObject();
vao.bind();
/* Get coordinates for centering the texture on screen */
float x1 = (width - texture.getWidth()) / 2f;
float y1 = (height - texture.getHeight()) / 2f;
float x2 = x1 + texture.getWidth();
float y2 = y1 + texture.getHeight();
try (MemoryStack stack = MemoryStack.stackPush()) {
/* Vertex data */
FloatBuffer vertices = stack.mallocFloat(4 * 7);
vertices.put(x1).put(y1).put(1f).put(1f).put(1f).put(0f).put(0f);
vertices.put(x2).put(y1).put(1f).put(1f).put(1f).put(1f).put(0f);
vertices.put(x2).put(y2).put(1f).put(1f).put(1f).put(1f).put(1f);
vertices.put(x1).put(y2).put(1f).put(1f).put(1f).put(0f).put(1f);
vertices.flip();
/* Generate Vertex Buffer Object */
vbo = new VertexBufferObject();
vbo.bind(GL_ARRAY_BUFFER);
vbo.uploadData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
/* Element data */
IntBuffer elements = stack.mallocInt(2 * 3);
elements.put(0).put(1).put(2);
elements.put(2).put(3).put(0);
elements.flip();
/* Generate Element Buffer Object */
ebo = new VertexBufferObject();
ebo.bind(GL_ELEMENT_ARRAY_BUFFER);
ebo.uploadData(GL_ELEMENT_ARRAY_BUFFER, elements, GL_STATIC_DRAW);
}
/* Load shaders */
vertexShader = Shader.loadShader(GL_VERTEX_SHADER, "resources/default.vert");
fragmentShader = Shader.loadShader(GL_FRAGMENT_SHADER, "resources/default.frag");
/* Create shader program */
program = new ShaderProgram();
program.attachShader(vertexShader);
program.attachShader(fragmentShader);
program.bindFragmentDataLocation(0, "fragColor");
program.link();
program.use();
specifyVertexAttributes();
/* Set texture uniform */
int uniTex = program.getUniformLocation("texImage");
program.setUniform(uniTex, 0);
/* Set model matrix to identity matrix */
Matrix4f model = new Matrix4f();
int uniModel = program.getUniformLocation("model");
program.setUniform(uniModel, model);
/* Set view matrix to identity matrix */
Matrix4f view = new Matrix4f();
int uniView = program.getUniformLocation("view");
program.setUniform(uniView, view);
/* Set projection matrix to an orthographic projection */
Matrix4f projection = Matrix4f.orthographic(0f, width, 0f, height, -1f, 1f);
int uniProjection = program.getUniformLocation("projection");
program.setUniform(uniProjection, projection);
}
@Override
public void exit() {
vao.delete();
vbo.delete();
ebo.delete();
texture.delete();
vertexShader.delete();
fragmentShader.delete();
program.delete();
}
/**
* Specifies the vertex attributes.
*/
private void specifyVertexAttributes() {
/* Specify Vertex Pointer */
int posAttrib = program.getAttributeLocation("position");
program.enableVertexAttribute(posAttrib);
program.pointVertexAttribute(posAttrib, 2, 7 * Float.BYTES, 0);
/* Specify Color Pointer */
int colAttrib = program.getAttributeLocation("color");
program.enableVertexAttribute(colAttrib);
program.pointVertexAttribute(colAttrib, 3, 7 * Float.BYTES, 2 * Float.BYTES);
/* Specify Texture Pointer */
int texAttrib = program.getAttributeLocation("texcoord");
program.enableVertexAttribute(texAttrib);
program.pointVertexAttribute(texAttrib, 2, 7 * Float.BYTES, 5 * Float.BYTES);
}
}