-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathIntroduction.java
More file actions
171 lines (143 loc) · 5.55 KB
/
Introduction.java
File metadata and controls
171 lines (143 loc) · 5.55 KB
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
/*
* The MIT License (MIT)
*
* Copyright © 2014-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;
import java.nio.IntBuffer;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryUtil;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
/**
* This class is a simple quick starting guide. This is mainly a java conversion
* of the
* <a href=http://www.glfw.org/docs/latest/quick.html>Getting started guide</a>
* from the official GLFW3 homepage.
*
* @author Heiko Brumme
*/
public class Introduction {
/**
* This error callback will simply print the error to
* <code>System.err</code>.
*/
private static GLFWErrorCallback errorCallback
= GLFWErrorCallback.createPrint(System.err);
/**
* This key callback will check if ESC is pressed and will close the window
* if it is pressed.
*/
private static GLFWKeyCallback keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
};
/**
* The main function will create a 640x480 window and renders a rotating
* triangle until the window gets closed.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
long window;
/* Set the error callback */
glfwSetErrorCallback(errorCallback);
/* Initialize GLFW */
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
/* Create window */
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (window == NULL) {
glfwTerminate();
throw new RuntimeException("Failed to create the GLFW window");
}
/* Center the window on screen */
GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window,
(vidMode.width() - 640) / 2,
(vidMode.height() - 480) / 2
);
/* Create OpenGL context */
glfwMakeContextCurrent(window);
GL.createCapabilities();
/* Enable vertical synchronization */
glfwSwapInterval(1);
/* Set the key callback */
glfwSetKeyCallback(window, keyCallback);
/* Declare buffers for using inside the loop */
IntBuffer width = MemoryUtil.memAllocInt(1);
IntBuffer height = MemoryUtil.memAllocInt(1);
/* Loop until window gets closed */
while (!glfwWindowShouldClose(window)) {
float ratio;
/* Get width and height to calcualte the ratio */
glfwGetFramebufferSize(window, width, height);
ratio = width.get() / (float) height.get();
/* Rewind buffers for next get */
width.rewind();
height.rewind();
/* Set viewport and clear screen */
glViewport(0, 0, width.get(), height.get());
glClear(GL_COLOR_BUFFER_BIT);
/* Set ortographic projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1f, 1f, 1f, -1f);
glMatrixMode(GL_MODELVIEW);
/* Rotate matrix */
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50f, 0f, 0f, 1f);
/* Render triangle */
glBegin(GL_TRIANGLES);
glColor3f(1f, 0f, 0f);
glVertex3f(-0.6f, -0.4f, 0f);
glColor3f(0f, 1f, 0f);
glVertex3f(0.6f, -0.4f, 0f);
glColor3f(0f, 0f, 1f);
glVertex3f(0f, 0.6f, 0f);
glEnd();
/* Swap buffers and poll Events */
glfwSwapBuffers(window);
glfwPollEvents();
/* Flip buffers for next loop */
width.flip();
height.flip();
}
/* Free buffers */
MemoryUtil.memFree(width);
MemoryUtil.memFree(height);
/* Release window and its callbacks */
glfwDestroyWindow(window);
keyCallback.free();
/* Terminate GLFW and release the error callback */
glfwTerminate();
errorCallback.free();
}
}