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.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
250.
251.
252.
253.
254.
255.
256.
257.
258.
259.
260.
261.
262.
263.
264.
265.
266.
package lwjgl;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
/**
* NeHe Lesson 05 - 3D Shapes
*
* @author Tim Biedert
* @author kappaOne
*/
public class Lesson05 {
String windowTitle = "NeHe Lesson 05 - 3D Shapes";
public boolean closeRequested = false;
long lastFrameTime; // used to calculate delta
float triangleAngle; // Angle of rotation for the triangles
float quadAngle; // Angle of rotation for the quads
float quadAngle_X; // Angle of rotation for the quads
float quadAngle_Y; // Angle of rotation for the quads
float TranlAngle_X = 0.0f; // Angle of rotation for the quads
float TranlAngle_Y = 0.0f; // Angle of rotation for the quads
float quadAngle_Z = 0.0f; // Angle of rotation for the quads
float ang_X = 0.0f; // Angle of rotation for the quads
float angy_Z = 0.0f; // Angle of rotation for the quads
public void run() {
createWindow();
getDelta(); // Initialise delta timer
initGL();
while (!closeRequested) {
pollInput();
updateLogic(/*getDelta()*/0, quadAngle_X,quadAngle_Y, quadAngle_Z);
renderGL();
Display.update();
}
cleanup();
}
private void initGL() {
/* OpenGL */
int width = Display.getDisplayMode().getWidth();
int height = Display.getDisplayMode().getHeight();
GL11.glViewport(0, 0, width, height); // Reset The Current Viewport
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glLoadIdentity(); // Reset The Modelview Matrix
GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL11.glClearDepth(1.0f); // Depth Buffer Setup
GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
}
private void updateLogic(int delta, float Xx, float Yy, float Zz) {
triangleAngle += 0.1f * delta; // Increase The Rotation Variable For The Triangles
quadAngle -= 0.05f * delta; // Decrease The Rotation Variable For The Quads
quadAngle_X = Xx;
quadAngle_Y = Yy;
quadAngle_Z = Zz;
}
private void updateLogic_Y(int delta) {
//triangleAngle += 0.1f * delta; // Increase The Rotation Variable For The Triangles
ang_X -= 0.05f * delta; // Decrease The Rotation Variable For The Quads
}
private void renderGL() {
float w = (float) 1.0;
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
GL11.glLoadIdentity(); // Reset The View
GL11.glLoadIdentity(); // Reset The View
GL11.glTranslatef(0.0f, 0.0f, -7.0f); // Move Right And Into The Screen
GL11.glRotatef(ang_X, 1.0f, 0.0f, 0.0f); // Rotate The Cube On X, Y & Z
GL11.glRotatef(angy_Z, 0.0f, 0.0f, 1.0f); // Rotate The Cube On X, Y & Z
GL11.glRotatef(quadAngle, quadAngle_X, quadAngle_Y, quadAngle_Z ); // Rotate The Cube On X, Y & Z
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
//GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBegin(GL11.GL_QUADS); // Start Drawing The Cube
GL11.glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
GL11.glEnd(); // Done Drawing The Quad
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GL11.glBegin(GL11.GL_QUADS); // Start Drawing The Cube
GL11.glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom)
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GL11.glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front)
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GL11.glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GL11.glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left)
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
GL11.glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
GL11.glEnd(); // Done Drawing The Quad
//GL11.gl(GL11.GL_BLEND);
}
/**
* Poll Input
*/
public void pollInput() {
// scroll through key events
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
closeRequested = true;
// -----------------------
/*else if ((Keyboard.getEventKey() == Keyboard.KEY_UP) )
{
//quadAngle = quadAngle + 0.1f;
updateLogic(30);
}*/
else if (Keyboard.isKeyDown(Keyboard.KEY_UP))
{
updateLogic_Y(200);
// if (ang_X <= -720) ang_X=-720;
TranlAngle_X = quadAngle;
System.out.println(quadAngle);
Keyboard.enableRepeatEvents(true);
}
else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
{
updateLogic(200, 0.0f, 1.0f,0.0f);
TranlAngle_Y = quadAngle;
System.out.println(quadAngle);
Keyboard.enableRepeatEvents(true);
}
// ----------------------
}
}
if (Display.isCloseRequested()) {
closeRequested = true;
}
}
/**
* Calculate how many milliseconds have passed
* since last frame.
*
* @return milliseconds passed since last frame
*/
public int getDelta() {
long time = (Sys.getTime() * 1000) / Sys.getTimerResolution();
int delta = (int) (time - lastFrameTime);
lastFrameTime = time;
return delta;
}
private void createWindow() {
try {
// Display.setFullscreen(true);
//Display.setDisplayMode(new DisplayMode(1500, 1000));
Display.setDisplayModeAndFullscreen(new DisplayMode(1000, 600));
Display.setVSyncEnabled(true);
Display.setTitle(windowTitle);
Display.create();
} catch (LWJGLException e) {
Sys.alert("Error", "Initialization failed!\n\n" + e.getMessage());
System.exit(0);
}
}
/**
* Destroy and clean up resources
*/
private void cleanup() {
Display.destroy();
}
public static void main(String[] args) {
Lesson05 lesson = new Lesson05();
lesson.run();
}
}