Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Lwjgl cube #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<project name="SampleJavaProject" default="jar"
<project name="SampleLwjglProject" default="jar"
xmlns:ivy="antlib:org.apache.ivy.ant">

<!-- Project-specific configuration -->
Expand Down
2 changes: 0 additions & 2 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@
<module name="EqualsHashCode"/>
<module name="HiddenField"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<module name="MissingSwitchDefault"/>
<module name="RedundantThrows"/>
<module name="SimplifyBooleanExpression"/>
Expand Down
20 changes: 19 additions & 1 deletion ivy.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
<?xml version="1.0"?>
<ivy-module version="2.0">
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
<info organisation="com.nullprogram" module="sample-java-project"/>
<configurations>
<conf name="default"/>
<conf name="build" extends="default" visibility="private"/>
<conf name="test" extends="build" visibility="private"/>
</configurations>
<dependencies>
<dependency org="org.lwjgl.lwjgl" name="lwjgl" rev="2.8.2"
conf="default"/>
<dependency org="org.lwjgl.lwjgl" name="lwjgl-platform" rev="2.8.2"
conf="default">
<artifact name="lwjgl-platform" e:classifier="natives-linux"/>
<artifact name="lwjgl-platform" e:classifier="natives-windows"/>
<artifact name="lwjgl-platform" e:classifier="natives-osx"/>
</dependency>
<dependency org="net.java.jinput" name="jinput-platform" rev="2.0.5"
conf="default">
<artifact name="jinput-platform" e:classifier="natives-linux"/>
<artifact name="jinput-platform" e:classifier="natives-windows"/>
<artifact name="jinput-platform" e:classifier="natives-osx"/>
</dependency>
<dependency org="org.lwjgl.lwjgl" name="lwjgl_util" rev="2.8.2"
conf="default"/>
<dependency org="com.nullprogram" name="native-guide" rev="0.2"
conf="default"/>
<dependency org="commons-cli" name="commons-cli" rev="1.2"
conf="default"/>
<dependency org="junit" name="junit" rev="4.10"
Expand Down
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project information
artifactId = sample-java-project
artifactId = sample-lwjgl-project
description = An Ant-based sample Java project.
version = 1.0-SNAPSHOT
package.main = sample.java.project
Expand Down
38 changes: 38 additions & 0 deletions src/com/nullprogram/lwjgl/Lwjgl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nullprogram.lwjgl;

import com.nullprogram.guide.Arch;
import static com.nullprogram.guide.NativeGuide.prepare;

/**
* Prepares all of the LWJGL native binaries for loading.
*/
public final class Lwjgl {

/** Hidden constructor. */
private Lwjgl() {
}

/**
* Prepares all of the LWJGL native libraries for loading.
* @throws java.io.IOException when the libraries could not be loaded
*/
public static void setup() throws java.io.IOException {
prepare(Arch.LINUX_64, "/libjinput-linux64.so");
prepare(Arch.LINUX_32, "/libjinput-linux.so");
prepare(Arch.LINUX_64, "/liblwjgl64.so");
prepare(Arch.LINUX_32, "/liblwjgl.so");
prepare(Arch.LINUX_64, "/libopenal64.so");
prepare(Arch.LINUX_32, "/libopenal.so");
prepare(Arch.MAC_64, "/libjinput-osx.jnilib");
prepare(Arch.MAC_64, "/liblwjgl.jnilib");
prepare(Arch.MAC_64, "/openal.dylib");
prepare(Arch.WINDOWS_64, "/jinput-dx8_64.dll");
prepare(Arch.WINDOWS_32, "/jinput-dx8.dll");
prepare(Arch.WINDOWS_64, "/jinput-raw_64.dll");
prepare(Arch.WINDOWS_32, "/jinput-raw.dll");
prepare(Arch.WINDOWS_64, "/lwjgl64.dll");
prepare(Arch.WINDOWS_32, "/lwjgl.dll");
prepare(Arch.WINDOWS_32, "/OpenAL32.dll");
prepare(Arch.WINDOWS_64, "/OpenAL64.dll");
}
}
4 changes: 4 additions & 0 deletions src/com/nullprogram/lwjgl/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Just a LWJGL native library loader, using NativeGuide.
*/
package com.nullprogram.lwjgl;
72 changes: 0 additions & 72 deletions src/sample/java/project/SampleJavaProject.java

This file was deleted.

184 changes: 184 additions & 0 deletions src/sample/java/project/SampleLwjglProject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
42 changes: 0 additions & 42 deletions test/sample/java/project/SampleJavaProjectTest.java

This file was deleted.

Loading