This repository was archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathSampleLwjglProject.java
More file actions
184 lines (156 loc) · 5.75 KB
/
SampleLwjglProject.java
File metadata and controls
184 lines (156 loc) · 5.75 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
172
173
174
175
176
177
178
179
180
181
182
183
184
package sample.java.project;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.Project;
/**
* The main class.
*
* This is the main class of the application. It contains the main()
* method, the first method called.
*/
public class SampleLwjglProject implements Runnable {
/** The delay between frames. */
private static final int FPS = 60;
/** Width of the display. */
private static final int WIDTH = 600;
/** Height of the display. */
private static final int HEIGHT = 600;
/** The length of one second in milliseconds. */
private static final double SECOND = 1000d;
/** Rate of rotation. */
private static final float ROTS_RATE = 100f;
/** Rate of rotation oscillation. */
private static final float ROTR_RATE = 4f;
/** Range of rotation oscillation. */
private static final float ROT_RANGE = 36;
/** Red diffuse light. */
private static final float[] DIFFUSE = {1f, 0f, 0f, 1f};
/** Infinite light location. */
private static final float[] POSITION = {1f, 1f, 1f, 0f};
/** Normals for the 6 faces of a cube. */
private static final float[][] NORMALS = {
{-1f, 0f, 0f}, {0f, 1f, 0f}, {1f, 0f, 0f},
{0f, -1f, 0f}, {0f, 0f, 1f}, {0f, 0f, -1f}
};
/** Vertex indices for the 6 faces of a cube. */
private static final int[][] FACES = {
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
};
/** Will be filled in with X, Y, Z vertexes. */
private static final float[][] V = new float[8][3];
/** Colors of the sides of the cube. */
private static final float[][] COLORS = {
{1f, 0f, 0f}, {0f, 1f, 0f}, {0f, 0f, 1f},
{1f, 1f, 0f}, {0f, 1f, 1f}, {1f, 0f, 1f}
};
/**
* The main class.
*
* Print the "Hello, world!" string.
*
* @param args application input arguments
*/
public static void main(final String[] args) {
try {
com.nullprogram.lwjgl.Lwjgl.setup();
} catch (java.io.IOException e) {
System.out.println("error: could not prepare libraries: " + e);
System.exit(0);
}
new SampleLwjglProject().run();
}
@Override
public final void run() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("LWJGL Cube Demo");
Display.create();
init();
} catch (LWJGLException e) {
System.out.println("error: could not prepare display: " + e);
return;
}
while (!Display.isCloseRequested()) {
repaint();
Display.update();
Display.sync(FPS);
}
Display.destroy();
}
/**
* Wrap a float array with a direct FloatBuffer.
* @param in the float array to be wrapped
* @return a direct FloatBuffer
*/
private FloatBuffer wrap(final float[] in) {
FloatBuffer buffer;
buffer = BufferUtils.createFloatBuffer(in.length * 4);
return buffer.put(in);
}
/**
* Initial display configuration.
*/
private void init() {
/* Setup cube vertex data. */
V[0][0] = V[1][0] = V[2][0] = V[3][0] = -1;
V[4][0] = V[5][0] = V[6][0] = V[7][0] = 1;
V[0][1] = V[1][1] = V[4][1] = V[5][1] = -1;
V[2][1] = V[3][1] = V[6][1] = V[7][1] = 1;
V[0][2] = V[3][2] = V[4][2] = V[7][2] = 1;
V[1][2] = V[2][2] = V[5][2] = V[6][2] = -1;
/* Enable a single OpenGL light. */
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, wrap(DIFFUSE));
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, wrap(POSITION));
GL11.glEnable(GL11.GL_LIGHT0);
//GL11.glEnable(GL11.GL_LIGHTING);
/* Use depth buffering for hidden surface elimination. */
GL11.glEnable(GL11.GL_DEPTH_TEST);
/* Setup the view of the cube. */
GL11.glMatrixMode(GL11.GL_PROJECTION);
Project.gluPerspective(40f, 1f, 1f, 10f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
Project.gluLookAt(0f, 0f, 5f,
0f, 0f, 0f,
0f, 1f, 0f);
/* Adjust cube position to be asthetic angle. */
GL11.glTranslatef(0f, 0f, -1f);
GL11.glRotatef(60f, 1f, 0f, 0f);
GL11.glRotatef(-20f, 0f, 0f, 1f);
}
/**
* Draw the OpenGL display.
*/
private void repaint() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
double time = System.currentTimeMillis() / SECOND;
GL11.glPushMatrix();
float r = (float) (Math.sin(time * ROTR_RATE) * ROT_RANGE + ROT_RANGE);
GL11.glRotatef(r, 0f, 0f, 1f);
float s = (float) (time * ROTS_RATE % 360);
GL11.glRotatef(s, 1f, 0f, 0f);
for (int i = 0; i < 6; i++) {
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(COLORS[i][0], COLORS[i][1], COLORS[i][2]);
GL11.glNormal3f(NORMALS[i][0], NORMALS[i][1], NORMALS[i][2]);
GL11.glVertex3f(V[FACES[i][0]][0],
V[FACES[i][0]][1],
V[FACES[i][0]][2]);
GL11.glVertex3f(V[FACES[i][1]][0],
V[FACES[i][1]][1],
V[FACES[i][1]][2]);
GL11.glVertex3f(V[FACES[i][2]][0],
V[FACES[i][2]][1],
V[FACES[i][2]][2]);
GL11.glVertex3f(V[FACES[i][3]][0],
V[FACES[i][3]][1],
V[FACES[i][3]][2]);
GL11.glEnd();
}
GL11.glPopMatrix();
}
}