Skip to content

Commit 8eba3ac

Browse files
committed
Turn it into a cube demo.
1 parent 9b120dc commit 8eba3ac

3 files changed

Lines changed: 99 additions & 44 deletions

File tree

checkstyle.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@
161161
<module name="EqualsHashCode"/>
162162
<module name="HiddenField"/>
163163
<module name="IllegalInstantiation"/>
164-
<module name="InnerAssignment"/>
165-
<module name="MagicNumber"/>
166164
<module name="MissingSwitchDefault"/>
167165
<module name="RedundantThrows"/>
168166
<module name="SimplifyBooleanExpression"/>

ivy.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<artifact name="jinput-platform" e:classifier="natives-windows"/>
2222
<artifact name="jinput-platform" e:classifier="natives-osx"/>
2323
</dependency>
24+
<dependency org="org.lwjgl.lwjgl" name="lwjgl_util" rev="2.8.2"
25+
conf="default"/>
2426
<dependency org="com.nullprogram" name="native-guide" rev="0.2"
2527
conf="default"/>
2628
<dependency org="commons-cli" name="commons-cli" rev="1.2"

src/sample/java/project/SampleLwjglProject.java

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,68 @@
11
package sample.java.project;
22

3-
import lombok.extern.java.Log;
3+
import java.nio.FloatBuffer;
4+
import org.lwjgl.BufferUtils;
45
import org.lwjgl.LWJGLException;
56
import org.lwjgl.opengl.Display;
67
import org.lwjgl.opengl.DisplayMode;
78
import org.lwjgl.opengl.GL11;
9+
import org.lwjgl.util.glu.Project;
810

911
/**
1012
* The main class.
1113
*
1214
* This is the main class of the application. It contains the main()
1315
* method, the first method called.
1416
*/
15-
@Log
1617
public class SampleLwjglProject implements Runnable {
1718

1819
/** The delay between frames. */
19-
private static final int FPS = 30;
20+
private static final int FPS = 60;
2021

2122
/** Width of the display. */
22-
private static final int WIDTH = 800;
23+
private static final int WIDTH = 600;
2324

2425
/** Height of the display. */
2526
private static final int HEIGHT = 600;
2627

2728
/** The length of one second in milliseconds. */
2829
private static final double SECOND = 1000d;
2930

30-
/** The size of the quad. */
31-
private static final float QUAD_SIZE = 200;
31+
/** Rate of rotation. */
32+
private static final float ROTS_RATE = 100f;
3233

33-
/** Rate of red oscillation. */
34-
private static final float RED_RATE = 1f;
34+
/** Rate of rotation oscillation. */
35+
private static final float ROTR_RATE = 4f;
3536

36-
/** Rate of green oscillation. */
37-
private static final float GREEN_RATE = 2.2f;
37+
/** Range of rotation oscillation. */
38+
private static final float ROT_RANGE = 36;
3839

39-
/** Rate of blue oscillation. */
40-
private static final float BLUE_RATE = 0.125f;
40+
/** Red diffuse light. */
41+
private static final float[] DIFFUSE = {1f, 0f, 0f, 1f};
4142

42-
/** Rate of rotation oscillation. */
43-
private static final float ROT_RATE = 4f;
43+
/** Infinite light location. */
44+
private static final float[] POSITION = {1f, 1f, 1f, 0f};
4445

45-
/** Range of rotation oscillation. */
46-
private static final float ROT_RANGE = 12;
46+
/** Normals for the 6 faces of a cube. */
47+
private static final float[][] NORMALS = {
48+
{-1f, 0f, 0f}, {0f, 1f, 0f}, {1f, 0f, 0f},
49+
{0f, -1f, 0f}, {0f, 0f, 1f}, {0f, 0f, -1f}
50+
};
4751

48-
/** Height division for quad. */
49-
private static final float HDIV = 4;
52+
/** Vertex indices for the 6 faces of a cube. */
53+
private static final int[][] FACES = {
54+
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
55+
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
56+
};
5057

51-
/** Width division for quad. */
52-
private static final float WDIV = 2;
58+
/** Will be filled in with X, Y, Z vertexes. */
59+
private static final float[][] V = new float[8][3];
60+
61+
/** Colors of the sides of the cube. */
62+
private static final float[][] COLORS = {
63+
{1f, 0f, 0f}, {0f, 1f, 0f}, {0f, 0f, 1f},
64+
{1f, 1f, 0f}, {0f, 1f, 1f}, {1f, 0f, 1f}
65+
};
5366

5467
/**
5568
* The main class.
@@ -62,7 +75,7 @@ public static void main(final String[] args) {
6275
try {
6376
com.nullprogram.lwjgl.Lwjgl.setup();
6477
} catch (java.io.IOException e) {
65-
log.severe("could not prepare libraries: " + e);
78+
System.out.println("error: could not prepare libraries: " + e);
6679
System.exit(0);
6780
}
6881
new SampleLwjglProject().run();
@@ -72,10 +85,11 @@ public static void main(final String[] args) {
7285
public final void run() {
7386
try {
7487
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
88+
Display.setTitle("LWJGL Cube Demo");
7589
Display.create();
7690
init();
7791
} catch (LWJGLException e) {
78-
log.severe("could not prepare display: " + e);
92+
System.out.println("error: could not prepare display: " + e);
7993
return;
8094
}
8195

@@ -87,42 +101,83 @@ public final void run() {
87101
Display.destroy();
88102
}
89103

104+
/**
105+
* Wrap a float array with a direct FloatBuffer.
106+
* @param in the float array to be wrapped
107+
* @return a direct FloatBuffer
108+
*/
109+
private FloatBuffer wrap(final float[] in) {
110+
FloatBuffer buffer;
111+
buffer = BufferUtils.createFloatBuffer(in.length * 4);
112+
return buffer.put(in);
113+
}
114+
90115
/**
91116
* Initial display configuration.
92117
*/
93118
private void init() {
119+
/* Setup cube vertex data. */
120+
V[0][0] = V[1][0] = V[2][0] = V[3][0] = -1;
121+
V[4][0] = V[5][0] = V[6][0] = V[7][0] = 1;
122+
V[0][1] = V[1][1] = V[4][1] = V[5][1] = -1;
123+
V[2][1] = V[3][1] = V[6][1] = V[7][1] = 1;
124+
V[0][2] = V[3][2] = V[4][2] = V[7][2] = 1;
125+
V[1][2] = V[2][2] = V[5][2] = V[6][2] = -1;
126+
127+
/* Enable a single OpenGL light. */
128+
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, wrap(DIFFUSE));
129+
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, wrap(POSITION));
130+
GL11.glEnable(GL11.GL_LIGHT0);
131+
//GL11.glEnable(GL11.GL_LIGHTING);
132+
133+
/* Use depth buffering for hidden surface elimination. */
134+
GL11.glEnable(GL11.GL_DEPTH_TEST);
135+
136+
/* Setup the view of the cube. */
94137
GL11.glMatrixMode(GL11.GL_PROJECTION);
95-
GL11.glLoadIdentity();
96-
GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
138+
Project.gluPerspective(40f, 1f, 1f, 10f);
97139
GL11.glMatrixMode(GL11.GL_MODELVIEW);
140+
Project.gluLookAt(0f, 0f, 5f,
141+
0f, 0f, 0f,
142+
0f, 1f, 0f);
143+
144+
/* Adjust cube position to be asthetic angle. */
145+
GL11.glTranslatef(0f, 0f, -1f);
146+
GL11.glRotatef(60f, 1f, 0f, 0f);
147+
GL11.glRotatef(-20f, 0f, 0f, 1f);
98148
}
99149

100150
/**
101151
* Draw the OpenGL display.
102152
*/
103153
private void repaint() {
104-
double time = System.currentTimeMillis() / SECOND;
105-
106-
/* Clear the screen and depth buffer */
107154
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
108155

109-
/* set the color of the quad (R,G,B,A) */
110-
float red = (float) Math.abs(Math.sin(time * RED_RATE));
111-
float green = (float) Math.abs(Math.cos(time * GREEN_RATE));
112-
float blue = (float) Math.abs(Math.tan(time * BLUE_RATE));
113-
GL11.glColor3f(red, green, blue);
114-
115-
/* draw quad */
156+
double time = System.currentTimeMillis() / SECOND;
116157
GL11.glPushMatrix();
117-
float r = (float) (Math.sin(time * ROT_RATE) * ROT_RANGE + ROT_RANGE);
158+
float r = (float) (Math.sin(time * ROTR_RATE) * ROT_RANGE + ROT_RANGE);
118159
GL11.glRotatef(r, 0f, 0f, 1f);
119-
120-
GL11.glBegin(GL11.GL_QUADS);
121-
GL11.glVertex2f(WIDTH / WDIV, HEIGHT / HDIV);
122-
GL11.glVertex2f(WIDTH / WDIV + QUAD_SIZE, HEIGHT / HDIV);
123-
GL11.glVertex2f(WIDTH / WDIV + QUAD_SIZE, HEIGHT / HDIV + QUAD_SIZE);
124-
GL11.glVertex2f(WIDTH / WDIV, HEIGHT / HDIV + QUAD_SIZE);
125-
GL11.glEnd();
160+
float s = (float) (time * ROTS_RATE % 360);
161+
GL11.glRotatef(s, 1f, 0f, 0f);
162+
163+
for (int i = 0; i < 6; i++) {
164+
GL11.glBegin(GL11.GL_QUADS);
165+
GL11.glColor3f(COLORS[i][0], COLORS[i][1], COLORS[i][2]);
166+
GL11.glNormal3f(NORMALS[i][0], NORMALS[i][1], NORMALS[i][2]);
167+
GL11.glVertex3f(V[FACES[i][0]][0],
168+
V[FACES[i][0]][1],
169+
V[FACES[i][0]][2]);
170+
GL11.glVertex3f(V[FACES[i][1]][0],
171+
V[FACES[i][1]][1],
172+
V[FACES[i][1]][2]);
173+
GL11.glVertex3f(V[FACES[i][2]][0],
174+
V[FACES[i][2]][1],
175+
V[FACES[i][2]][2]);
176+
GL11.glVertex3f(V[FACES[i][3]][0],
177+
V[FACES[i][3]][1],
178+
V[FACES[i][3]][2]);
179+
GL11.glEnd();
180+
}
126181

127182
GL11.glPopMatrix();
128183
}

0 commit comments

Comments
 (0)