From f872c3b2da48aa0f1a548600e8caf78a6ddba076 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 17 Aug 2003 16:58:19 +0000 Subject: [PATCH] Those examples are not going anywhere --- .../lwjgl/test/DisplayConfigurationTest.java | 43 ++ .../org/lwjgl/test/WindowCreationTest.java | 44 ++ .../test/input/ControllerCreationTest.java | 225 +++++++++ .../org/lwjgl/test/input/ControllerTest.java | 201 ++++++++ .../org/lwjgl/test/input/HWCursorTest.java | 357 ++++++++++++++ .../org/lwjgl/test/input/KeyboardTest.java | 217 ++++++++ .../lwjgl/test/input/MouseCreationTest.java | 234 +++++++++ src/java/org/lwjgl/test/input/MouseTest.java | 188 +++++++ src/java/org/lwjgl/test/openal/ALCTest.java | 107 ++++ src/java/org/lwjgl/test/openal/BasicTest.java | 108 ++++ src/java/org/lwjgl/test/openal/EAXTest.java | 80 +++ .../lwjgl/test/openal/MovingSoundTest.java | 238 +++++++++ .../lwjgl/test/openal/OpenALCreationTest.java | 223 +++++++++ src/java/org/lwjgl/test/openal/PlayTest.java | 154 ++++++ .../org/lwjgl/test/openal/PlayTestMemory.java | 200 ++++++++ .../lwjgl/test/openal/SourceLimitTest.java | 162 ++++++ .../org/lwjgl/test/openal/StressTest.java | 219 ++++++++ src/java/org/lwjgl/test/openal/WaveData.java | 183 +++++++ .../test/opengl/FullScreenWindowedTest.java | 335 +++++++++++++ src/java/org/lwjgl/test/opengl/Game.java | 202 ++++++++ src/java/org/lwjgl/test/opengl/Grass.java | 466 ++++++++++++++++++ .../org/lwjgl/test/opengl/PbufferTest.java | 417 ++++++++++++++++ .../org/lwjgl/test/opengl/VBOIndexTest.java | 242 +++++++++ src/java/org/lwjgl/test/opengl/VBOTest.java | 218 ++++++++ src/java/org/lwjgl/test/opengl/cg_grass2.cg | 72 +++ src/java/org/lwjgl/test/opengl/cg_grass2.vp | 116 +++++ 26 files changed, 5251 insertions(+) create mode 100644 src/java/org/lwjgl/test/DisplayConfigurationTest.java create mode 100644 src/java/org/lwjgl/test/WindowCreationTest.java create mode 100644 src/java/org/lwjgl/test/input/ControllerCreationTest.java create mode 100644 src/java/org/lwjgl/test/input/ControllerTest.java create mode 100644 src/java/org/lwjgl/test/input/HWCursorTest.java create mode 100644 src/java/org/lwjgl/test/input/KeyboardTest.java create mode 100644 src/java/org/lwjgl/test/input/MouseCreationTest.java create mode 100644 src/java/org/lwjgl/test/input/MouseTest.java create mode 100644 src/java/org/lwjgl/test/openal/ALCTest.java create mode 100644 src/java/org/lwjgl/test/openal/BasicTest.java create mode 100644 src/java/org/lwjgl/test/openal/EAXTest.java create mode 100644 src/java/org/lwjgl/test/openal/MovingSoundTest.java create mode 100644 src/java/org/lwjgl/test/openal/OpenALCreationTest.java create mode 100644 src/java/org/lwjgl/test/openal/PlayTest.java create mode 100644 src/java/org/lwjgl/test/openal/PlayTestMemory.java create mode 100644 src/java/org/lwjgl/test/openal/SourceLimitTest.java create mode 100644 src/java/org/lwjgl/test/openal/StressTest.java create mode 100644 src/java/org/lwjgl/test/openal/WaveData.java create mode 100644 src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java create mode 100644 src/java/org/lwjgl/test/opengl/Game.java create mode 100644 src/java/org/lwjgl/test/opengl/Grass.java create mode 100644 src/java/org/lwjgl/test/opengl/PbufferTest.java create mode 100644 src/java/org/lwjgl/test/opengl/VBOIndexTest.java create mode 100644 src/java/org/lwjgl/test/opengl/VBOTest.java create mode 100644 src/java/org/lwjgl/test/opengl/cg_grass2.cg create mode 100644 src/java/org/lwjgl/test/opengl/cg_grass2.vp diff --git a/src/java/org/lwjgl/test/DisplayConfigurationTest.java b/src/java/org/lwjgl/test/DisplayConfigurationTest.java new file mode 100644 index 00000000..ae94603f --- /dev/null +++ b/src/java/org/lwjgl/test/DisplayConfigurationTest.java @@ -0,0 +1,43 @@ +/* + * Created on 18-03-2003 + * + * To change this generated comment go to + * Window>Preferences>Java>Code Generation>Code Template + */ +package org.lwjgl.test; + +import org.lwjgl.*; + +/** + * @author Elias Naur + */ +public class DisplayConfigurationTest { + private static void changeConfig(float gamma, float brightness, float contrast) { + Display.setDisplayConfiguration(gamma, brightness, contrast); + System.out.println("Configuration changed, gamma = " + gamma + " brightness = " + brightness + " contrast = " + contrast); + try { + Thread.sleep(3000); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + System.out.println("Testing normal setting"); + changeConfig(1.0f, 0f, 1f); + System.out.println("Testing gamma settings"); + changeConfig(5.0f, 0f, 1f); + changeConfig(0.5f, 0f, 1f); + System.out.println("Testing brightness settings"); + changeConfig(1.0f, -1.0f, 1f); + changeConfig(1.0f, -0.5f, 1f); + changeConfig(1.0f, 0.5f, 1f); + changeConfig(1.0f, 1.0f, 1f); + System.out.println("Testing contrast settings"); + changeConfig(1.0f, 0f, 0f); + changeConfig(1.0f, 0f, 0.5f); + changeConfig(1.0f, 0f, 10000.0f); + System.out.println("Test done - Resetting configuration"); + Display.resetDisplayMode(); + } +} diff --git a/src/java/org/lwjgl/test/WindowCreationTest.java b/src/java/org/lwjgl/test/WindowCreationTest.java new file mode 100644 index 00000000..43308df7 --- /dev/null +++ b/src/java/org/lwjgl/test/WindowCreationTest.java @@ -0,0 +1,44 @@ +/* + * Created on 18-03-2003 + * + * To change this generated comment go to + * Window>Preferences>Java>Code Generation>Code Template + */ +package org.lwjgl.test; + +import org.lwjgl.*; +import org.lwjgl.opengl.Window; + +/** + * @author Brian + */ +public class WindowCreationTest { + + public static void main(String[] args) { + DisplayMode[] modes = Display.getAvailableDisplayModes(); + System.out.println("Found " + modes.length + " display modes"); + + + try { + Window.create("WindowCreationTest", 50, 50, 320, 240, 16, 0, 0, 0); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println(Window.getHeight() + ", " + Window.getWidth() + ", " + Window.getTitle()); + + + System.out.println("Display created"); + + while(!Window.isCloseRequested()) { + Window.update(); + try { + Thread.sleep(100); + } catch (Exception e) { + e.printStackTrace(); + } + } + + Window.destroy(); + } +} diff --git a/src/java/org/lwjgl/test/input/ControllerCreationTest.java b/src/java/org/lwjgl/test/input/ControllerCreationTest.java new file mode 100644 index 00000000..5b34201d --- /dev/null +++ b/src/java/org/lwjgl/test/input/ControllerCreationTest.java @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.input; + +import org.lwjgl.Sys; +import org.lwjgl.Display; +import org.lwjgl.DisplayMode; +import org.lwjgl.input.Controller; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.Window; +import org.lwjgl.opengl.GLU; +import org.lwjgl.vector.Vector2f; + +/** + * $Id$ + *
+ * Controller creation test + * + * @author Brian Matzon + * @version $Revision$ + */ +public class ControllerCreationTest { + /** position of quad to draw */ + private Vector2f position = new Vector2f(320.0f, 240.0f); + + /** Display mode selected */ + private DisplayMode displayMode; + + /** Creates a new instance of MouseTest */ + public ControllerCreationTest() { + } + + private void initialize(boolean fullscreen) { + // find first display mode that allows us 640*480*16 + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == 640 + && modes[i].height == 480 + && modes[i].bpp >= 16) { + displayMode = modes[i]; + break; + } + } + + try { + if(fullscreen) { + Display.setDisplayMode(displayMode); + Window.create("ControllerCreationTest", 16, 0, 0, 0); + } else { + Window.create("ControllerCreationTest", 50, 50, 640, 480, 16, 0, 0, 0); + } + + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + + initializeOpenGL(); + } + + private void initializeOpenGL() { + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + GLU.gluOrtho2D(0.0, 640, 0, 480); + } + + public void executeTest() { + initialize(false); + + System.out.println("Test ready:\n"); + + // windowed mode + System.out.println("=========== WINDOWED MODE =============="); + for(int i=0; i<2; i++) { + System.out.println("Test " + (i+1) + ":"); + createController(); + wiggleController(); + destroyController(); + System.out.println(""); + } + + // recreate display in fullscreen mode + System.out.print("Destroying display..."); + Window.destroy(); + System.out.println("success"); + + System.out.print("Entering fullscreen mode..."); + try { + Window.destroy(); + initialize(true); + Display.setDisplayMode(displayMode); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("success"); + + + // fullscreen mode + System.out.println("=========== FULLSCREEN MODE =============="); + for(int i=0; i<2; i++) { + System.out.println("Test " + (i+3) + ":"); + createController(); + wiggleController(); + destroyController(); + System.out.println(""); + } + + System.out.println("Test completed successfully!"); + System.out.print("Shutting down..."); + Display.resetDisplayMode(); + Controller.destroy(); + Window.destroy(); + System.out.println("shutdown complete"); + } + + private void createController() { + System.out.print("Creating controller..."); + try { + Controller.create(); + } catch (Exception e) { + System.out.println("failed"); + System.exit(-1); + } + System.out.println("success"); + } + + private void wiggleController() { + System.out.print("Please move the controller around"); + + long statustime = Sys.getTime(); + long endtime = Sys.getTime() + Sys.getTimerResolution() * 5; + + while (Sys.getTime() < endtime) { + + Window.update(); + + Controller.poll(); + + //controller is a bit fuzzy + if(Controller.x > 100) { + position.x += 1; + } else if (Controller.x < -100) { + position.x -= 1; + } + if(Controller.y > 100) { + position.y -= 1; + } else if (Controller.y < -100) { + position.y += 1; + } + + render(); + + Window.paint(); + + if (Sys.getTime() - statustime > Sys.getTimerResolution()) { + System.out.print("."); + statustime = Sys.getTime(); + } + } + System.out.println("thank you"); + } + + private void destroyController() { + System.out.print("Destroying controller..."); + Controller.destroy(); + System.out.print("success"); + } + + private void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + GL.glPushMatrix(); + GL.glBegin(GL.GL_POLYGON); + { + GL.glColor3f(0.0f, 1.0f, 1.0f); + GL.glVertex2f(position.x + 0.0f, position.y + 0.0f); + + GL.glColor3f(1.0f, 0.0f, 1.0f); + GL.glVertex2f(position.x + 0.0f, position.y + 30.0f); + GL.glVertex2f(position.x + 40.0f, position.y + 30.0f); + + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glVertex2f(position.x + 60.0f, position.y + 15.f); + GL.glVertex2f(position.x + 40.0f, position.y + 0.0f); + } + GL.glEnd(); + GL.glPopMatrix(); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + ControllerCreationTest cct = new ControllerCreationTest(); + cct.executeTest(); + } +} diff --git a/src/java/org/lwjgl/test/input/ControllerTest.java b/src/java/org/lwjgl/test/input/ControllerTest.java new file mode 100644 index 00000000..42758274 --- /dev/null +++ b/src/java/org/lwjgl/test/input/ControllerTest.java @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.input; + +import org.lwjgl.DisplayMode; +import org.lwjgl.input.Controller; +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.Window; +import org.lwjgl.opengl.GLU; +import org.lwjgl.vector.Vector2f; + +/** + * $Id$ + *
+ * Controller test + * + * @author Brian Matzon + * @version $Revision$ + */ +public class ControllerTest { + + /** GLU instance */ + private GLU glu; + + /** position of quad to draw */ + private Vector2f position = new Vector2f(320.0f, 240.0f); + + /** Display mode selected */ + private DisplayMode displayMode; + + /** Creates a new instance of ControllerTest */ + public ControllerTest() { + } + + private void initialize() { + // create display and opengl + setupDisplay(false); + + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void setupDisplay(boolean fullscreen) { + try { + Window.create("ControllerTest", 50, 50, 640, 480, 16, 0, 0, 0); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + + initializeOpenGL(); + } + + private void initializeOpenGL() { + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + GLU.gluOrtho2D(0.0, 640, 0, 480); + } + + public void executeTest() { + initialize(); + + createController(); + + wiggleController(); + + Controller.destroy(); + Keyboard.destroy(); + Window.destroy(); + } + + private void createController() { + try { + Controller.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void wiggleController() { + while (!Window.isCloseRequested()) { + Window.update(); + + if(Window.isMinimized()) { + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + inte.printStackTrace(); + } + continue; + } + + Controller.poll(); + Keyboard.poll(); + + if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { + return; + } + + if (Controller.x > 200) { + position.x += 1; + } + + if (Controller.x < -200) { + position.x -= 1; + } + + if (Controller.y < -200) { + position.y += 1; + } + + if (Controller.y > 200) { + position.y -= 1; + } + + if(position.x<0) { + position.x = 0; + } else if (position.x>640-60) { + position.x = 640-60; + } + + if(position.y < 0) { + position.y = 0; + } else if (position.y>480-30) { + position.y = 480-30; + } + + + render(); + + Window.paint(); + } + } + + private void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + GL.glBegin(GL.GL_POLYGON); + { + float color = 1.0f; + int buttonDown = 0; + + for(int i=0;i + * @version $Revision$ + */ +public class HWCursorTest { + + /** Intended deiplay mode */ + private DisplayMode mode; + + /** GLU instance */ + private GLU glu; + + /** The native cursor */ + private static Cursor cursor = null; + + /** The mouse cursor position */ + private static int mouse_x; + private static int mouse_y; + + /** + * Executes the test + */ + public void execute() { + initialize(); + + mainLoop(); + + cleanup(); + } + + /** + * Initializes the test + */ + private void initialize() { + try { + //find displaymode + mode = findDisplayMode(800, 600, 16); + + // start of in windowed mode + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + + glInit(); + + Keyboard.create(); + initNativeCursor(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void initNativeCursor() { + try { + Mouse.create(); + } catch (Exception e) { + e.printStackTrace(); + } + if ((Mouse.getNativeCursorCaps() & Mouse.CURSOR_ONE_BIT_TRANSPARANCY) == 0) { + System.out.println("No HW cursor support!"); + System.exit(0); + } + System.out.println("Maximum native cursor size: " + Mouse.getMaxCursorSize() + ", min size: " + Mouse.getMinCursorSize()); + mouse_x = mouse_y = 0; + int num_images = 3; + int image_size = Mouse.getMaxCursorSize()*Mouse.getMaxCursorSize(); + IntBuffer cursor_images = ByteBuffer.allocateDirect(num_images*image_size*4).order(ByteOrder.nativeOrder()).asIntBuffer(); + IntBuffer delays = ByteBuffer.allocateDirect(num_images*4).order(ByteOrder.nativeOrder()).asIntBuffer(); + delays.put(0, 500); + delays.put(1, 500); + delays.put(2, 500); + int color_scale = 255/Mouse.getMaxCursorSize(); + int bit_mask = 0x81000000; + for (int j = 0; j < image_size; j++) { + if (j % 4 == 0) + bit_mask = (~bit_mask) & 0x81000000; + int color = (j*color_scale/Mouse.getMaxCursorSize()) << 16; + cursor_images.put(0*image_size + j, 0x00000020 | color | bit_mask); + } + for (int j = 0; j < image_size; j++) { + if (j % 4 == 0) + bit_mask = (~bit_mask) & 0x81000000; + int color = (j*color_scale/Mouse.getMaxCursorSize()) << 8; + cursor_images.put(1*image_size + j, 0x00000020 | color | bit_mask); + } + for (int j = 0; j < image_size; j++) { + if (j % 4 == 0) + bit_mask = (~bit_mask) & 0x81000000; + int color = (j*color_scale/Mouse.getMaxCursorSize()); + cursor_images.put(2*image_size + j, 0x00000020 | color | bit_mask); + } + try { + if ((Mouse.getNativeCursorCaps() | Mouse.CURSOR_ANIMATION) == 0) + num_images = 1; + cursor = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, num_images, cursor_images, delays); + Mouse.setNativeCursor(cursor); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + + /** + * Runs the main loop of the "test" + */ + private void mainLoop() { + while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) + && !Window.isCloseRequested()) { + // allow subsystem to get a chance to run too + Window.update(); + + if (!Window.isMinimized()) { + // check keyboard input + processKeyboard(); + + render(); + + // paint window + Window.paint(); + } else { + + // no need to render/paint if nothing has changed (ie. window dragged over) + if (Window.isDirty()) { + render(); + Window.paint(); + } + + // don't waste cpu time, sleep more + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + } + } + } + } + + /** + * Performs the logic + */ + private void render() { + //clear background + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + // draw white quad + GL.glPushMatrix(); + { + GL.glTranslatef(mouse_x, 600 - mouse_y, 0); + GL.glColor3f(1.0f, 1.0f, 1.0f); + GL.glBegin(GL.GL_QUADS); + { + GL.glVertex2i(-50, -50); + GL.glVertex2i(50, -50); + GL.glVertex2i(50, 50); + GL.glVertex2i(-50, 50); + } + GL.glEnd(); + } + GL.glPopMatrix(); + } + + /** + * Processes keyboard input + */ + private void processKeyboard() { + Keyboard.poll(); + Mouse.poll(); + + if (Mouse.dx != 0 || Mouse.dy != 0) { + mouse_x += Mouse.dx; + mouse_y += Mouse.dy; + System.out.println("mouse_x " + mouse_x + " mouse_y " + mouse_y); + } + + //check for fullscreen key + if (Keyboard.isKeyDown(Keyboard.KEY_F)) { + + try { + Keyboard.destroy(); + try { + Mouse.setNativeCursor(null); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + cursor.destroy(); + Mouse.destroy(); + Window.destroy(); + + Display.setDisplayMode(mode); + Window.create("Test", mode.bpp, 0, 0, 0); + + glInit(); + + Keyboard.create(); + initNativeCursor(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + //check for window key + if (Keyboard.isKeyDown(Keyboard.KEY_W)) { + try { + Keyboard.destroy(); + try { + Mouse.setNativeCursor(null); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + cursor.destroy(); + Mouse.destroy(); + Window.destroy(); + + Display.resetDisplayMode(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + + glInit(); + + Keyboard.create(); + initNativeCursor(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + if (Keyboard.isKeyDown(Keyboard.KEY_M)) { + try { + Mouse.setNativeCursor(null); + } catch (Exception e) { + e.printStackTrace(); + } + } + + if (Keyboard.isKeyDown(Keyboard.KEY_N)) { + try { + Mouse.setNativeCursor(cursor); + mouse_x = mouse_y = 0; + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Cleans up the test + */ + private void cleanup() { + Keyboard.destroy(); + try { + Mouse.setNativeCursor(null); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + cursor.destroy(); + Mouse.destroy(); + Display.resetDisplayMode(); + Window.destroy(); + } + + /** + * Retrieves a displaymode, if one such is available + * + * @param width Required width + * @param height Required height + * @param bpp Minimum required bits per pixel + * @return + */ + private DisplayMode findDisplayMode(int width, int height, int bpp) { + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == width + && modes[i].height == height + && modes[i].bpp >= bpp) { + return modes[i]; + } + } + return null; + } + + /** + * Initializes OGL + */ + private void glInit() { + // Go into orthographic projection mode. + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + GLU.gluOrtho2D(0, mode.width, 0, mode.height); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + GL.glViewport(0, 0, mode.width, mode.height); + + //set clear color to black + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + //sync frame (only works on windows) + GLCaps.determineAvailableExtensions(); + if (GLCaps.WGL_EXT_swap_control) { + GL.wglSwapIntervalEXT(1); + } + } + + /** + * Test entry point + */ + public static void main(String[] args) { + System.out.println( + "Change between fullscreen and windowed mode, by pressing F and W respectively. Enable hw cursor with N and disable it with M."); + System.out.println( + "Move quad using arrowkeys, and change rotation using +/-"); + HWCursorTest cursorTest = new HWCursorTest(); + cursorTest.execute(); + } +} diff --git a/src/java/org/lwjgl/test/input/KeyboardTest.java b/src/java/org/lwjgl/test/input/KeyboardTest.java new file mode 100644 index 00000000..a085cfba --- /dev/null +++ b/src/java/org/lwjgl/test/input/KeyboardTest.java @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.input; + +import org.lwjgl.DisplayMode; +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.Window; +import org.lwjgl.opengl.GLU; +import org.lwjgl.vector.Vector2f; + +/** + * $Id$ + *
+ * Keyboard test + * + * @author Brian Matzon + * @version $Revision$ + */ +public class KeyboardTest { + + /** GLU instance */ + private GLU glu; + + /** position of quad to draw */ + private Vector2f position = new Vector2f(320.0f, 240.0f); + + /** Display mode selected */ + private DisplayMode displayMode; + + private boolean bufferedKeyboard; + private boolean translatedKeyboard; + private int bufferSize; + + /** Creates a new instance of MouseTest */ + public KeyboardTest() { + } + + private void initialize() { + // create display and opengl + setupDisplay(false); + + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void setupDisplay(boolean fullscreen) { + try { + Window.create("KeyboardTest", 50, 50, 640, 480, 16, 0, 0, 0); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + + initializeOpenGL(); + } + + private void initializeOpenGL() { + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + GLU.gluOrtho2D(0.0, 640, 0, 480); + } + + public void executeTest() { + initialize(); + + createKeyboard(); + + wiggleKeyboard(); + + Keyboard.destroy(); + Window.destroy(); + } + + private void createKeyboard() { + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void wiggleKeyboard() { + Keyboard.enableBuffer(); + Keyboard.enableTranslation(); + + while (!Window.isCloseRequested()) { + Window.update(); + + if(Window.isMinimized()) { + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + inte.printStackTrace(); + } + continue; + } + + //check keys, buffered + Keyboard.read(); + + int count = Keyboard.getNumKeyboardEvents(); + System.out.println("Read " + count + " events"); + while(Keyboard.next()) { + System.out.println("Checking key:" + Keyboard.getKeyName(Keyboard.key)); + if(Keyboard.key == Keyboard.KEY_ESCAPE) { + return; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { + position.x += 1; + } + + if (Keyboard.key == Keyboard.KEY_RIGHT) { + position.x += 1; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { + position.x -= 1; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { + position.y += 1; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { + position.y -= 1; + } + + } + if (count > 0) { + System.out.println(); + } + + if(position.x<0) { + position.x = 0; + } else if (position.x>640-60) { + position.x = 640-60; + } + + if(position.y < 0) { + position.y = 0; + } else if (position.y>480-30) { + position.y = 480-30; + } + + + render(); + + Window.paint(); + + try { + Thread.sleep(0); + } catch (Exception e) { + } + } + } + + private void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + GL.glBegin(GL.GL_POLYGON); + { + float color = 1.0f; + int buttonDown = 0; + GL.glColor3f(color, color, color); + + GL.glVertex2f(position.x + 0.0f, position.y + 0.0f); + GL.glVertex2f(position.x + 0.0f, position.y + 30.0f); + GL.glVertex2f(position.x + 40.0f, position.y + 30.0f); + GL.glVertex2f(position.x + 60.0f, position.y + 15.f); + GL.glVertex2f(position.x + 40.0f, position.y + 0.0f); + } + GL.glEnd(); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + KeyboardTest kt = new KeyboardTest(); + kt.executeTest(); + } +} diff --git a/src/java/org/lwjgl/test/input/MouseCreationTest.java b/src/java/org/lwjgl/test/input/MouseCreationTest.java new file mode 100644 index 00000000..04d304d2 --- /dev/null +++ b/src/java/org/lwjgl/test/input/MouseCreationTest.java @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.input; + +import org.lwjgl.Sys; +import org.lwjgl.Display; +import org.lwjgl.DisplayMode; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.Window; +import org.lwjgl.opengl.GLU; +import org.lwjgl.vector.Vector2f; + +/** + * $Id$ + *
+ * Mouse test + * + * @author Brian Matzon + * @version $Revision$ + */ +public class MouseCreationTest { + /** GLU instance */ + private GLU glu; + + /** position of quad to draw */ + private Vector2f position = new Vector2f(320.0f, 240.0f); + + /** Display mode selected */ + private DisplayMode displayMode; + + /** Creates a new instance of MouseTest */ + public MouseCreationTest() { + } + + private void initialize(boolean fullscreen) { + // find first display mode that allows us 640*480*16 + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == 640 + && modes[i].height == 480 + && modes[i].bpp >= 16) { + displayMode = modes[i]; + break; + } + } + + try { + if(fullscreen) { + Display.setDisplayMode(displayMode); + Window.create("MouseCreationTest", 16, 0, 0, 0); + } else { + Window.create("MouseCreationTest", 50, 50, 640, 480, 16, 0, 0, 0); + } + + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + + initializeOpenGL(); + } + + private void initializeOpenGL() { + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + GLU.gluOrtho2D(0.0, 640, 0, 480); + } + + public void executeTest() { + initialize(false); + + System.out.println("Test ready:\n"); + + // windowed mode + System.out.println("=========== WINDOWED MODE =============="); + for(int i=0; i<2; i++) { + System.out.println("Test " + (i+1) + ":"); + createMouse(); + wiggleMouse(); + destroyMouse(); + System.out.println(""); + } + + // recreate display in fullscreen mode + System.out.print("Destroying display..."); + + System.out.println("success"); + + System.out.print("Entering fullscreen mode..."); + try { + Window.destroy(); + initialize(true); + Display.setDisplayMode(displayMode); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("success"); + + + // fullscreen mode + System.out.println("=========== FULLSCREEN MODE =============="); + for(int i=0; i<2; i++) { + System.out.println("Test " + (i+3) + ":"); + createMouse(); + wiggleMouse(); + destroyMouse(); + System.out.println(""); + } + + System.out.println("Test completed successfully!"); + System.out.print("Shutting down..."); + Display.resetDisplayMode(); + Mouse.destroy(); + Window.destroy(); + System.out.println("shutdown complete"); + } + + private void createMouse() { + System.out.print("Creating mouse..."); + try { + Mouse.create(); + } catch (Exception e) { + System.out.println("failed"); + System.exit(-1); + } + System.out.println("success"); + } + + private void wiggleMouse() { + System.out.print("Please move the mouse around"); + + long statustime = Sys.getTime(); + long endtime = Sys.getTime() + Sys.getTimerResolution() * 5; + + while (Sys.getTime() < endtime) { + Window.update(); + + Mouse.poll(); + + position.x += Mouse.dx; + position.y -= Mouse.dy; + + if(position.x<0) { + position.x = 0; + } else if (position.x>640-60) { + position.x = 640-60; + } + + if(position.y < 0) { + position.y = 0; + } else if (position.y>480-30) { + position.y = 480-30; + } + + render(); + + Window.paint(); + + if (Sys.getTime() - statustime > Sys.getTimerResolution()) { + System.out.print("."); + statustime = Sys.getTime(); + } + } + System.out.println("thank you"); + } + + private void destroyMouse() { + System.out.print("Destroying mouse..."); + Mouse.destroy(); + System.out.print("success"); + } + + private void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + GL.glBegin(GL.GL_POLYGON); + { + float color = 1.0f; + int buttonDown = 0; + + for(int i=0;i + * Mouse test + * + * @author Brian Matzon + * @version $Revision$ + */ +public class MouseTest { + + /** GLU instance */ + private GLU glu; + + /** position of quad to draw */ + private Vector2f position = new Vector2f(320.0f, 240.0f); + + /** Display mode selected */ + private DisplayMode displayMode; + + /** Creates a new instance of MouseTest */ + public MouseTest() { + } + + private void initialize() { + // create display and opengl + setupDisplay(false); + + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void setupDisplay(boolean fullscreen) { + try { + Window.create("MouseTest", 50, 50, 640, 480, 16, 0, 0, 0); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + + initializeOpenGL(); + } + + private void initializeOpenGL() { + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + GLU.gluOrtho2D(0.0, 640, 0, 480); + } + + public void executeTest() { + initialize(); + + createMouse(); + + wiggleMouse(); + + Mouse.destroy(); + Keyboard.destroy(); + Window.destroy(); + } + + private void createMouse() { + try { + Mouse.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + private void wiggleMouse() { + while (!Window.isCloseRequested()) { + Window.update(); + + if(Window.isMinimized()) { + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + inte.printStackTrace(); + } + continue; + } + + Mouse.poll(); + Keyboard.poll(); + + if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { + return; + } + + position.x += Mouse.dx; + position.y -= Mouse.dy; + + if(position.x<0) { + position.x = 0; + } else if (position.x>640-60) { + position.x = 640-60; + } + + if(position.y < 0) { + position.y = 0; + } else if (position.y>480-30) { + position.y = 480-30; + } + + + render(); + + Window.paint(); + } + } + + private void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + GL.glBegin(GL.GL_POLYGON); + { + float color = 1.0f; + int buttonDown = 0; + + for(int i=0;i + * @version $Revision$ + */ +public class ALCTest extends BasicTest { + + /** + * Creates an instance of ALCTest + */ + public ALCTest() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + //error stuff + int lastError = ALC.ALC_NO_ERROR; + + //create attribute list for context creation + IntBuffer buffer = createIntBuffer(7); + + if ((lastError = ALC.alcGetError()) != ALC.ALC_NO_ERROR) { + System.out.println("ALC Error: " + ALC.alcGetString(lastError)); + System.exit(-1); + } + + //query + System.out.println( + "DEFAULT_DEVICE_SPECIFIER: " + + ALC.alcGetString(ALC.ALC_DEFAULT_DEVICE_SPECIFIER)); + System.out.println( + "DEVICE_SPECIFIER: " + ALC.alcGetString(ALC.ALC_DEVICE_SPECIFIER)); + System.out.println("EXTENSIONS: " + ALC.alcGetString(ALC.ALC_EXTENSIONS)); + + //mo query + buffer.rewind(); + buffer.limit(1); + ALC.alcGetInteger(ALC.ALC_MAJOR_VERSION, buffer); + ALC.alcGetInteger(ALC.ALC_MINOR_VERSION, (IntBuffer) buffer.position(1).limit(2)); + + System.out.println("ALC_MAJOR_VERSION: " + buffer.get(0)); + System.out.println("ALC_MINOR_VERSION: " + buffer.get(1)); + + //no check for ALC_ALL_ATTRIBUTES / ALC_ATTRIBUTES_SIZE since it + //is buggy on win32 - my dev platform + + //get an enumerstion value + System.out.println( + "Value of ALC_MAJOR_VERSION: " + + ALC.alcGetEnumValue("ALC_MAJOR_VERSION")); + + alExit(); + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + ALCTest alcTest = new ALCTest(); + alcTest.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/BasicTest.java b/src/java/org/lwjgl/test/openal/BasicTest.java new file mode 100644 index 00000000..99a55f98 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/BasicTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; +import java.nio.FloatBuffer; + +/** + * $Id$ + * + * This is a basic test, which contains the most used stuff + * + * @author Brian Matzon + * @version $Revision$ + */ +public abstract class BasicTest { + + /** + * Creates an instance of PlayTest + */ + public BasicTest() { + try { + AL.create(); + } catch (Exception e) { + e.printStackTrace(); + return; + } + } + + /** + * Shutdowns OpenAL + */ + protected void alExit() { + AL.destroy(); + } + + /** + * Creates an integer buffer to hold specified ints + * - strictly a utility method + * + * @param size how many int to contain + * @return created IntBuffer + */ + protected IntBuffer createIntBuffer(int size) { + ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); + temp.order(ByteOrder.nativeOrder()); + + return temp.asIntBuffer(); + } + + /** + * Creates a float buffer to hold specified floats + * - strictly a utility method + * + * @param size how many floats to contain + * @return created FloatBuffer + */ + protected FloatBuffer createFloatBuffer(int size) { + ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); + temp.order(ByteOrder.nativeOrder()); + + return temp.asFloatBuffer(); + } + + /** + * Exits the test NOW, printing errorcode to stdout + * + * @param error Error code causing exit + */ + protected void exit(int error) { + System.out.println("OpenAL Error: " + AL.alGetString(error)); + alExit(); + System.exit(-1); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/EAXTest.java b/src/java/org/lwjgl/test/openal/EAXTest.java new file mode 100644 index 00000000..2f0ecfd8 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/EAXTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.eax.EAX; + +/** + * $Id$ + * + * This test initializes EAX and tries to get and set some EAX values + * + * @author Brian Matzon + * @version $Revision$ + */ +public class EAXTest extends BasicTest { + + /** + * Creates an instance of EAXTest + */ + public EAXTest() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + try { + System.out.print("Testing EAX support..."); + EAX.create(); + System.out.println("supported!"); + } catch (Exception e) { + System.out.println("no supported!"); + } + + //shutdown + alExit(); + + System.out.println("test done."); + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + EAXTest eaxTest = new EAXTest(); + eaxTest.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/MovingSoundTest.java b/src/java/org/lwjgl/test/openal/MovingSoundTest.java new file mode 100644 index 00000000..e5d751b7 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/MovingSoundTest.java @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; +import org.lwjgl.openal.eax.*; +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.Window; +import org.lwjgl.vector.Vector3f; + +import java.nio.IntBuffer; + +/** + * $Id$ + * + * This test simulates a listener positioned in the center, and + * a source moving around the listener using the keyboard + * + * @author Brian Matzon + * @version $Revision$ + */ +public class MovingSoundTest extends BasicTest { + + public static float MOVEMENT = 50.00f; + + /** + * Creates an instance of MovingSoundTest + */ + public MovingSoundTest() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + if (args.length < 1) { + System.out.println("no argument supplied, assuming Footsteps.wav"); + args = new String[] {"Footsteps.wav"}; + } + + try { + Window.create("Moving Sound Test", 100, 100, 320, 240, 32, 0 ,0 ,0); + } catch (Exception e) { + e.printStackTrace(); + } + + + int lastError; + Vector3f sourcePosition = new Vector3f(); + Vector3f listenerPosition = new Vector3f(); + boolean eaxApplied = false; + EAXListenerProperties eaxListenerProp = null; + + //initialize keyboard + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + exit(-1); + } + + //create 1 buffer and 1 source + IntBuffer buffers = createIntBuffer(1); + IntBuffer sources = createIntBuffer(1); + + // al generate buffers and sources + buffers.position(0).limit(1); + AL.alGenBuffers(buffers); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + sources.position(0).limit(1); + AL.alGenSources(sources); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //load wave data + WaveData wavefile = WaveData.create(args[0]); + + //copy to buffers + AL.alBufferData( + buffers.get(0), + wavefile.format, + wavefile.data, + wavefile.data.capacity(), + wavefile.samplerate); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //unload file again + wavefile.dispose(); + + //set up source input + AL.alSourcei(sources.get(0), AL.AL_BUFFER, buffers.get(0)); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + AL.alSourcef(sources.get(0), AL.AL_REFERENCE_DISTANCE, 1024.0f); + AL.alSourcef(sources.get(0), AL.AL_ROLLOFF_FACTOR, 0.5f); + + //lets loop the sound + AL.alSourcei(sources.get(0), AL.AL_LOOPING, AL.AL_TRUE); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //play source 0 + AL.alSourcePlay(sources.get(0)); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //setup EAX if possible + if (AL.alIsExtensionPresent("EAX")) { + try { + EAX.create(); + eaxListenerProp = new EAXListenerProperties(); + } catch (Exception e) { + } + } + + System.out.println("Move source with arrow keys\nMove listener with right shift and arrowkeys\nEnable EAX effect by pressing e (if available)\nExit with ESC"); + + while(!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { + Window.update(); + + Keyboard.poll(); + if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { + if(Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) { + listenerPosition.x -= MOVEMENT; + AL.alListener3f(AL.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z); + System.out.println("listenerx: " + listenerPosition.x); + } else { + sourcePosition.x -= MOVEMENT; + AL.alSource3f(sources.get(0), AL.AL_POSITION, sourcePosition.x, sourcePosition.y, sourcePosition.z); + System.out.println("sourcex: " + sourcePosition.x); + } + } + if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { + if(Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) { + listenerPosition.x += MOVEMENT; + AL.alListener3f(AL.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z); + System.out.println("listenerx: " + listenerPosition.x); + } else { + sourcePosition.x += MOVEMENT; + AL.alSource3f(sources.get(0), AL.AL_POSITION, sourcePosition.x, sourcePosition.y, sourcePosition.z); + System.out.println("sourcex: " + sourcePosition.x); + } + } + + if(Keyboard.isKeyDown(Keyboard.KEY_E)) { + if(eaxApplied) { + eaxListenerProp.setEnvironment(EAX.EAX_ENVIRONMENT_GENERIC); + eaxListenerProp.eaxSet(EAXListenerProperties.EAXLISTENER_ENVIRONMENT, 0); + } else { + eaxListenerProp.setEnvironment(EAX.EAX_ENVIRONMENT_HANGAR); + eaxListenerProp.eaxSet(EAXListenerProperties.EAXLISTENER_ENVIRONMENT, 0); + } + eaxApplied = !eaxApplied; + } + + if(Window.isCloseRequested()) { + break; + } + + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + } + } + + //stop source 0 + AL.alSourceStop(sources.get(0)); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //delete buffers and sources + sources.position(0).limit(1); + AL.alDeleteSources(sources); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + buffers.position(0).limit(1); + AL.alDeleteBuffers(buffers); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //shutdown + alExit(); + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + MovingSoundTest movingSoundTest = new MovingSoundTest(); + movingSoundTest.execute(args); + } +} diff --git a/src/java/org/lwjgl/test/openal/OpenALCreationTest.java b/src/java/org/lwjgl/test/openal/OpenALCreationTest.java new file mode 100644 index 00000000..3db9ff64 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/OpenALCreationTest.java @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; + +/** + * $Id$ + *
+ * Performs a creation test, by creating and destroying OpenAL twice. + * We cannot inherit from BasicTest since it follows another structure. + * + * @author Brian Matzon + * @version $Revision$ + */ +public class OpenALCreationTest { + + /** + * Creates an instance of OpenALCreationTest + */ + public OpenALCreationTest() { + } + + public void alInitialize() { + try { + AL.create(); + } catch (Exception e) { + e.printStackTrace(); + return; + } + } + + public void alExit() { + AL.destroy(); + } + + /** + * Creates an integer buffer to hold specified ints + * - strictly a utility method + * + * @param size how many int to contain + * @return created IntBuffer + */ + protected IntBuffer createIntBuffer(int size) { + ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); + temp.order(ByteOrder.nativeOrder()); + + return temp.asIntBuffer(); + } + + /** + * Exits the test NOW, printing errorcode to stdout + * + * @param error Error code causing exit + */ + protected void exit(int error) { + System.out.println("OpenAL Error: " + AL.alGetString(error)); + alExit(); + System.exit(-1); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + int lastError; + + //initialize AL, using ALC + System.out.print("initialize..."); + alInitialize(); + System.out.println("success"); + + //do some audio + executeAudioTest(); + + //shutdown + System.out.print("shutdown..."); + alExit(); + System.out.println("success"); + + //initialize AL, using ALC + System.out.print("initialize..."); + alInitialize(); + System.out.println("success"); + + //do some audio + executeAudioTest(); + + //shutdown + System.out.print("shutdown..."); + alExit(); + System.out.println("success"); + } + + /** + * Executes the audio test, which just plays some sound + */ + private void executeAudioTest() { + int lastError; + + //create 1 buffer and 1 source + IntBuffer buffers = createIntBuffer(1); + IntBuffer sources = createIntBuffer(1); + + // al generate buffers and sources + buffers.position(0).limit(1); + AL.alGenBuffers(buffers); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + sources.position(0).limit(1); + AL.alGenSources(sources); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //load wave data + WaveData wavefile = WaveData.create("Footsteps.wav"); + + //copy to buffers + AL.alBufferData( + buffers.get(0), + wavefile.format, + wavefile.data, + wavefile.data.capacity(), + wavefile.samplerate); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //unload file again + wavefile.dispose(); + + //set up source input + AL.alSourcei(sources.get(0), AL.AL_BUFFER, buffers.get(0)); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //lets loop the sound + AL.alSourcei(sources.get(0), AL.AL_LOOPING, AL.AL_TRUE); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //play source 0 + AL.alSourcePlay(sources.get(0)); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //wait 5 secs + try { + System.out.print("Playing 'Footsteps.wav' for 2 seconds..."); + Thread.sleep(2000); + } catch (InterruptedException inte) { + } + System.out.println("done"); + + //stop source 0 + AL.alSourceStop(sources.get(0)); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //delete buffers and sources + sources.position(0).limit(1); + AL.alDeleteSources(sources); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + buffers.position(0).limit(1); + AL.alDeleteBuffers(buffers); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + OpenALCreationTest oalCreationTest = new OpenALCreationTest(); + oalCreationTest.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/PlayTest.java b/src/java/org/lwjgl/test/openal/PlayTest.java new file mode 100644 index 00000000..4fd42db7 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/PlayTest.java @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; + +import java.nio.IntBuffer; + +/** + * $Id$ + * + * This is a basic play test + * Yes, over zealous use of getError ;) + * + * @author Brian Matzon + * @version $Revision$ + */ +public class PlayTest extends BasicTest { + + /** + * Creates an instance of PlayTest + */ + public PlayTest() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + if(args.length < 1) { + System.out.println("no argument supplied, assuming Footsteps.wav"); + args = new String[] {"Footsteps.wav"}; + } + + int lastError; + + //create 1 buffer and 1 source + IntBuffer buffers = createIntBuffer(1); + IntBuffer sources = createIntBuffer(1); + + // al generate buffers and sources + buffers.position(0).limit(1); + AL.alGenBuffers(buffers); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + sources.position(0).limit(1); + AL.alGenSources(sources); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //load wave data + WaveData wavefile = WaveData.create(args[0]); + + //copy to buffers + AL.alBufferData(buffers.get(0), wavefile.format, wavefile.data, wavefile.data.capacity(), wavefile.samplerate); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //unload file again + wavefile.dispose(); + + //set up source input + AL.alSourcei(sources.get(0), AL.AL_BUFFER, buffers.get(0)); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //lets loop the sound + AL.alSourcei(sources.get(0), AL.AL_LOOPING, AL.AL_TRUE); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //play source 0 + AL.alSourcePlay(sources.get(0)); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //wait 5 secs + try { + System.out.println("Waiting 5 seconds for sound to complete"); + Thread.sleep(5000); + } catch (InterruptedException inte) { + } + + //stop source 0 + AL.alSourceStop(sources.get(0)); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //delete buffers and sources + sources.position(0).limit(1); + AL.alDeleteSources(sources); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + buffers.position(0).limit(1); + AL.alDeleteBuffers(buffers); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //no errorchecking from now on, since our context is gone. + //shutdown + alExit(); + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + PlayTest playTest = new PlayTest(); + playTest.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/PlayTestMemory.java b/src/java/org/lwjgl/test/openal/PlayTestMemory.java new file mode 100644 index 00000000..f20274a2 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/PlayTestMemory.java @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.IntBuffer; + +/** + * $Id$ + * + * This is a basic play test + * Yes, over zealous use of getError ;) + * + * @author Brian Matzon + * @version $Revision$ + */ +public class PlayTestMemory extends BasicTest { + + /** + * Creates an instance of PlayTestMemory + */ + public PlayTestMemory() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + if(args.length < 1) { + System.out.println("no argument supplied, assuming Footsteps.wav"); + args = new String[] {"Footsteps.wav"}; + } + + int lastError; + + //create 1 buffer and 1 source + IntBuffer buffers = createIntBuffer(1); + IntBuffer sources = createIntBuffer(1); + + // al generate buffers and sources + buffers.position(0).limit(1); + AL.alGenBuffers(buffers); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + sources.position(0).limit(1); + AL.alGenSources(sources); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //load wave data + ByteBuffer filebuffer = getData(args[0]); + if(filebuffer == null) { + System.out.println("Error loading file: " + args[0]); + System.exit(-1); + } + + //ALUTLoadWAVData file = alut.loadWAVMemory(Sys.getDirectBufferAddress(filebuffer)); + WaveData wavefile = WaveData.create(filebuffer.array()); + + + //copy to buffers + AL.alBufferData(buffers.get(0), wavefile.format, wavefile.data, wavefile.data.capacity(), wavefile.samplerate); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //unload file again + wavefile.dispose(); + + //set up source input + AL.alSourcei(sources.get(0), AL.AL_BUFFER, buffers.get(0)); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //lets loop the sound + AL.alSourcei(sources.get(0), AL.AL_LOOPING, AL.AL_TRUE); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //play source 0 + AL.alSourcePlay(sources.get(0)); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //wait 5 secs + try { + System.out.println("Waiting 5 seconds for sound to complete"); + Thread.sleep(5000); + } catch (InterruptedException inte) { + } + + //stop source 0 + AL.alSourceStop(sources.get(0)); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //delete buffers and sources + sources.position(0).limit(1); + AL.alDeleteSources(sources); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + buffers.position(0).limit(1); + AL.alDeleteBuffers(buffers); + if((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + exit(lastError); + } + + //no errorchecking from now on, since our context is gone. + alExit(); + } + + /** + * Reads the file into a ByteBuffer + * + * @param filename Name of file to load + * @return ByteBuffer containing file data + */ + protected ByteBuffer getData(String filename) { + ByteBuffer buffer = null; + + System.out.println("Attempting to load: " + filename); + + try { + BufferedInputStream bis = new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filename)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + int bufferLength = 4096; + byte[] readBuffer = new byte[bufferLength]; + int read = -1; + + while((read = bis.read(readBuffer, 0, bufferLength)) != -1) { + baos.write(readBuffer, 0, read); + } + + //done reading, close + bis.close(); + + buffer = ByteBuffer.allocate(baos.size()); + buffer.order(ByteOrder.nativeOrder()); + buffer.put(baos.toByteArray()); + } catch (Exception ioe) { + ioe.printStackTrace(); + } + return buffer; + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + PlayTestMemory playTestMemory = new PlayTestMemory(); + playTestMemory.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/SourceLimitTest.java b/src/java/org/lwjgl/test/openal/SourceLimitTest.java new file mode 100644 index 00000000..140059da --- /dev/null +++ b/src/java/org/lwjgl/test/openal/SourceLimitTest.java @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; +import org.lwjgl.openal.OpenALException; + +import java.nio.IntBuffer; + +/** + * $Id$ + * + * Simple test for testing the number of available sources + * + * @author Brian Matzon + * @version $Revision$ + */ +public class SourceLimitTest extends BasicTest { + + /** Sources to create */ + protected int sourcesToCreate = 64; + + /** + * Creates an instance of SourceLimitTest + */ + public SourceLimitTest() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + //parse 1st arg to sourcecount + if (args.length > 0) { + try { + sourcesToCreate = Integer.parseInt(args[0]); + } catch (NumberFormatException nfe) { + System.out.println( + "Unable to parse parameter to integer. Defaulting to 64 sources."); + } + } + + System.out.print("Creating " + sourcesToCreate + " in one go..."); + try { + CreateAllSources(); + } catch(OpenALException oale) { + } + + + System.out.print("Creating " + sourcesToCreate + " one at a time..."); + try { + CreateSourcesStep(); + } catch(Exception e) { + } + //shutdown + alExit(); + } + + /** + * Tests the creation of n sources in on go + */ + protected void CreateAllSources() { + int lastError; + + //make bytbuffer that can hold sourcesToCreate sources + IntBuffer sources = createIntBuffer(sourcesToCreate); + + //Create sourcesToCreate sources in one fell swoop + sources.position(0).limit(sourcesToCreate); + AL.alGenSources(sources); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + System.out.println("failed to create " + sourcesToCreate + " sources (" + AL.alGetString(lastError) + ")"); + return; + } + + //delete sources + sources.position(0).limit(sourcesToCreate); + AL.alDeleteSources(sources); + + System.out.println("created " + sourcesToCreate + " sources successfully!"); + } + + /** + * Tests if n sources can be created one at a time + */ + protected void CreateSourcesStep() { + int lastError; + int sourcesCreated = 0; + + //make bytbuffer that can hold sourcesToCreate sources + IntBuffer[] sources = new IntBuffer[sourcesToCreate]; + + //create the sources + for (int i = 0; i <= sourcesToCreate; i++) { + sources[i] = createIntBuffer(1); + sources[i].position(0).limit(1); + AL.alGenSources(sources[i]); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + System.out.println("failed to create source: " + (i + 1)); + break; + } + sourcesCreated++; + } + + //delete allocated sources + for (int i = 0; i < sourcesCreated; i++) { + //delete buffers and sources + sources[i].position(0).limit(1); + AL.alDeleteSources(sources[i]); + if ((lastError = AL.alGetError()) != AL.AL_NO_ERROR) { + System.out.println("failed to delete source: " + i + "(" + AL.alGetString(lastError) + ")"); + break; + } + } + + if(sourcesCreated != sourcesToCreate) { + System.out.println("created " + sourcesCreated + " sources before failing"); + } else { + System.out.println("created " + sourcesCreated + " sources successfully!"); + } + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + SourceLimitTest sourceLimitTest = new SourceLimitTest(); + sourceLimitTest.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/StressTest.java b/src/java/org/lwjgl/test/openal/StressTest.java new file mode 100644 index 00000000..6fe8ccc2 --- /dev/null +++ b/src/java/org/lwjgl/test/openal/StressTest.java @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; +import java.nio.IntBuffer; + +/** + * $Id$ + * + * Simple test for stresstesting OpenAL playing random samples ad nausea + * + * @author Brian Matzon + * @version $Revision$ + */ +public class StressTest extends BasicTest { + + /** Buffer containing sources */ + private IntBuffer sources; + + /** Buffer containing buffers */ + private IntBuffer buffers; + + /** + * Creates an instance of StressTest + */ + public StressTest() { + super(); + } + + /** + * Runs the actual test, using supplied arguments + */ + protected void execute(String[] args) { + + createSources(); + + createBuffers(); + + try { + loadSamples(); + runTest(); + } catch (Exception e) { + e.printStackTrace(); + } + + + alExit(); + } + + private void createSources() { + sources = createIntBuffer(4); + sources.position(0).limit(4); + AL.alGenSources(sources); + if (AL.alGetError() != AL.AL_NO_ERROR) { + System.out.println("Unable to create 4 sources"); + alExit(); + } + } + + private void createBuffers() { + buffers = createIntBuffer(10); + buffers.position(0).limit(10); + AL.alGenBuffers(buffers); + if (AL.alGetError() != AL.AL_NO_ERROR) { + System.out.println("Unable to create 10 buffers"); + sources.position(0).limit(4); + AL.alDeleteSources(sources); + alExit(); + } + } + + private void loadSamples() throws Exception { + AL.alGetError(); + WaveData data = WaveData.create("ding.wav"); + for (int i = 1; i <= 10; i++) { + AL.alBufferData( + buffers.get(i - 1), + data.format, + data.data, + data.data.capacity(), + data.samplerate); + + if (AL.alGetError() != AL.AL_NO_ERROR) { + System.out.println("Failed to load " + i + ".wav into buffer"); + sources.position(0).limit(4); + AL.alDeleteSources(sources); + buffers.position(0).limit(10); + AL.alDeleteBuffers(buffers); + + alExit(); + } + } + data.dispose(); + } + + public void runTest() { + int iterations = 0; + int randomBuffer; + int startSlot = 1; + int nextSlot = startSlot; + long startTime = System.currentTimeMillis(); + + //mark background source as looping + AL.alSourcei(sources.get(0), AL.AL_LOOPING, AL.AL_TRUE); + + //play background + AL.alSourcei(sources.get(0), AL.AL_BUFFER, buffers.get(0)); + AL.alSourcePlay(sources.get(0)); + + while (System.currentTimeMillis() - startTime < (2000)) { + + randomBuffer = getRandomBuffer(); + System.out.println("random:" + randomBuffer); + + //stop source at slot + AL.alSourceStop(sources.get(nextSlot)); + if (AL.alGetError() != AL.AL_NO_ERROR) { + System.out.println("Error stopping source."); + } + System.out.println("Stopped source: " + nextSlot); + + //link source<->buffer + AL.alSourcei(sources.get(nextSlot), AL.AL_BUFFER, buffers.get(randomBuffer)); + if (AL.alGetError() != AL.AL_NO_ERROR) { + System.out.println("Error linking buffer and source."); + } + System.out.println("linked source " + nextSlot + " with buffer " + randomBuffer); + + //start playing + System.out.println("playing source " + nextSlot); + AL.alSourcePlay(sources.get(nextSlot++)); + if (nextSlot == 4) { + nextSlot = startSlot; + } + + //pause + try { + Thread.sleep(500); + } catch (InterruptedException inte) { + } + + //debug info + if ((++iterations % 10) == 0) { + System.out.println("========================"); + System.out.println("MaxMemory: " + Runtime.getRuntime().maxMemory() / 1024); + System.out.println("FreeMemory: " + Runtime.getRuntime().freeMemory() / 1024); + System.out.println("TotalMemory: " + Runtime.getRuntime().totalMemory() / 1024); + System.out.println("========================"); + } + } + + //stop all sources + for (int i = 0; i < 4; i++) { + AL.alSourceStop(sources.get(i)); + System.out.println("Stopping source " + (i+1)); + } + + //test done - ask for user input + try { + System.out.println("Test completed"); + System.out.println("========================"); + System.out.println("MaxMemory: " + Runtime.getRuntime().maxMemory() / 1024); + System.out.println("FreeMemory: " + Runtime.getRuntime().freeMemory() / 1024); + System.out.println("TotalMemory: " + Runtime.getRuntime().totalMemory() / 1024); + System.out.println("========================"); + System.in.read(); + } catch (Exception e) { + } + + sources.position(0).limit(4); + AL.alDeleteSources(sources); + buffers.position(0).limit(10); + AL.alDeleteBuffers(buffers); + } + + private int getRandomBuffer() { + return (int) (Math.random() * 10.0); + } + + /** + * main entry point + * + * @param args String array containing arguments + */ + public static void main(String[] args) { + StressTest stressTest = new StressTest(); + stressTest.execute(args); + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/openal/WaveData.java b/src/java/org/lwjgl/test/openal/WaveData.java new file mode 100644 index 00000000..53b5e17f --- /dev/null +++ b/src/java/org/lwjgl/test/openal/WaveData.java @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.openal; + +import org.lwjgl.openal.AL; + +import java.nio.ByteBuffer; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.*; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; + +/** + * $Id$ + * + * Utitlity class for loading wavefiles. + * + * @author Brian Matzon + * @version $Revision$ + */ +public class WaveData { + /** actual wave data */ + public final ByteBuffer data; + + /** format type of data */ + public final int format; + + /** sample rate of data */ + public final int samplerate; + + /** + * Creates a new WaveData + * + * @param data actual wavedata + * @param format format of wave data + * @param samplerate sample rate of data + */ + private WaveData(ByteBuffer data, int format, int samplerate) { + this.data = data; + this.format = format; + this.samplerate = samplerate; + } + + /** + * Disposes the wavedata + */ + public void dispose() { + data.clear(); + } + + /** + * Creates a WaveData container from the specified filename + * + * @param filepath path to file (relative, and in classpath) + * @return WaveData containing data, or null if a failure occured + */ + public static WaveData create(String filepath) { + try { + return create( + AudioSystem.getAudioInputStream( + new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filepath)))); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Creates a WaveData container from the specified bytes + * + * @param buffer array of bytes containing the complete wave file + * @return WaveData containing data, or null if a failure occured + */ + public static WaveData create(byte[] buffer) { + try { + return create( + AudioSystem.getAudioInputStream( + new BufferedInputStream(new ByteArrayInputStream(buffer)))); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Creates a WaveData container from the specified stream + * + * @param ais AudioInputStream to read from + * @return WaveData containing data, or null if a failure occured + */ + public static WaveData create(AudioInputStream ais) { + //get format of data + AudioFormat audioformat = ais.getFormat(); + + // get channels + int channels = 0; + if (audioformat.getChannels() == 1) { + if (audioformat.getSampleSizeInBits() == 8) { + channels = AL.AL_FORMAT_MONO8; + } else if (audioformat.getSampleSizeInBits() == 16) { + channels = AL.AL_FORMAT_MONO16; + } else { + assert false : "Illegal sample size"; + } + } else if (audioformat.getChannels() == 2) { + if (audioformat.getSampleSizeInBits() == 8) { + channels = AL.AL_FORMAT_STEREO8; + } else if (audioformat.getSampleSizeInBits() == 16) { + channels = AL.AL_FORMAT_STEREO16; + } else { + assert false : "Illegal sample size"; + } + } else { + assert false : "Only mono or stereo is supported"; + } + + //read data into buffer + byte[] buf = + new byte[audioformat.getChannels() + * (int) ais.getFrameLength() + * audioformat.getSampleSizeInBits() + / 8]; + int read = 0, total = 0; + try { + while ((read = ais.read(buf, total, buf.length - total)) != -1 + && total < buf.length) { + total += read; + } + } catch (IOException ioe) { + return null; + } + + //insert data into bytebuffer + ByteBuffer buffer = ByteBuffer.allocateDirect(buf.length); + buffer.put(buf); + buffer.rewind(); + + //create our result + WaveData wavedata = + new WaveData(buffer, channels, (int) audioformat.getSampleRate()); + + //close stream + try { + ais.close(); + } catch (IOException ioe) { + } + + return wavedata; + } +} \ No newline at end of file diff --git a/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java b/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java new file mode 100644 index 00000000..2ba18607 --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/FullScreenWindowedTest.java @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2003 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.opengl; + +import org.lwjgl.*; +import org.lwjgl.input.*; +import org.lwjgl.opengl.*; +import org.lwjgl.vector.Vector2f; + +/** + * $Id$ + * + * Tests switching between windowed and fullscreen + * + * @author Brian Matzon + * @version $Revision$ + */ +public class FullScreenWindowedTest { + + /** Intended deiplay mode */ + private DisplayMode mode; + + /** GLU instance */ + private GLU glu; + + /** our quad moving around */ + private Vector2f quadPosition; + + /** our quadVelocity */ + private Vector2f quadVelocity; + + /** angle of quad */ + private float angle; + + /** degrees to rotate per frame */ + private float angleRotation = 1.0f; + + /** Max speed of all changable attributes */ + private static final float MAX_SPEED = 20.0f; + + /** + * Creates a FullScreenWindowedTest + */ + public FullScreenWindowedTest() { + } + + /** + * Executes the test + */ + public void execute() { + initialize(); + + mainLoop(); + + cleanup(); + } + + /** + * Initializes the test + */ + private void initialize() { + try { + //find displaymode + mode = findDisplayMode(800, 600, 16); + + // start of in windowed mode + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + + glInit(); + + Keyboard.create(); + + quadPosition = new Vector2f(100f, 100f); + quadVelocity = new Vector2f(1.0f, 1.0f); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Runs the main loop of the "test" + */ + private void mainLoop() { + while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) + && !Window.isCloseRequested()) { + // allow subsystem to get a chance to run too + Window.update(); + + if (!Window.isMinimized()) { + // check keyboard input + processKeyboard(); + + // do "game" logic, and render it + logic(); + render(); + + // paint window + Window.paint(); + } else { + + // no need to render/paint if nothing has changed (ie. window dragged over) + if (Window.isDirty()) { + render(); + Window.paint(); + } + + // don't waste cpu time, sleep more + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + } + } + } + } + + /** + * Performs the logic + */ + private void logic() { + angle += angleRotation; + if (angle > 90.0f) { + angle = 0.0f; + } + + quadPosition.x += quadVelocity.x; + quadPosition.y += quadVelocity.y; + + //check colision with vertical border border + if (quadPosition.x + 50 >= mode.width || quadPosition.x - 50 <= 0) { + quadVelocity.x *= -1; + } + + //check collision with horizontal border + if (quadPosition.y + 50 >= mode.height || quadPosition.y - 50 <= 0) { + quadVelocity.y *= -1; + } + } + + private void render() { + //clear background + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + // draw white quad + GL.glPushMatrix(); + { + GL.glTranslatef(quadPosition.x, quadPosition.y, 0); + GL.glRotatef(angle, 0.0f, 0.0f, 1.0f); + GL.glColor3f(1.0f, 1.0f, 1.0f); + GL.glBegin(GL.GL_QUADS); + { + GL.glVertex2i(-50, -50); + GL.glVertex2i(50, -50); + GL.glVertex2i(50, 50); + GL.glVertex2i(-50, 50); + } + GL.glEnd(); + } + GL.glPopMatrix(); + } + + /** + * Processes keyboard input + */ + private void processKeyboard() { + Keyboard.poll(); + + //check for fullscreen key + if (Keyboard.isKeyDown(Keyboard.KEY_F)) { + + try { + Keyboard.destroy(); + Window.destroy(); + + Display.setDisplayMode(mode); + Window.create("Test", mode.bpp, 0, 0, 0); + + glInit(); + + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + //check for window key + if (Keyboard.isKeyDown(Keyboard.KEY_W)) { + try { + Keyboard.destroy(); + Window.destroy(); + + Display.resetDisplayMode(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + + glInit(); + + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + //check for speed changes + if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { + quadVelocity.y += 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { + quadVelocity.y -= 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { + quadVelocity.x += 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { + quadVelocity.x -= 0.1f; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) { + angleRotation += 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) { + angleRotation -= 0.1f; + } + + //throttle + if (quadVelocity.x < -MAX_SPEED) { + quadVelocity.x = -MAX_SPEED; + } + if (quadVelocity.x > MAX_SPEED) { + quadVelocity.x = MAX_SPEED; + } + if (quadVelocity.y < -MAX_SPEED) { + quadVelocity.y = -MAX_SPEED; + } + if (quadVelocity.y > MAX_SPEED) { + quadVelocity.y = MAX_SPEED; + } + + if (angleRotation < 0.0f) { + angleRotation = 0.0f; + } + if (angleRotation > MAX_SPEED) { + angleRotation = MAX_SPEED; + } + } + + /** + * Cleans up the test + */ + private void cleanup() { + Keyboard.destroy(); + Window.destroy(); + } + + /** + * Retrieves a displaymode, if one such is available + * + * @param width Required width + * @param height Required height + * @param bpp Minimum required bits per pixel + * @return + */ + private DisplayMode findDisplayMode(int width, int height, int bpp) { + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == width + && modes[i].height == height + && modes[i].bpp >= bpp) { + return modes[i]; + } + } + return null; + } + + /** + * Initializes OGL + */ + private void glInit() { + // Go into orthographic projection mode. + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + GLU.gluOrtho2D(0, mode.width, 0, mode.height); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + GL.glViewport(0, 0, mode.width, mode.height); + + //set clear color to black + GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + //sync frame (only works on windows) + GLCaps.determineAvailableExtensions(); + if (GLCaps.WGL_EXT_swap_control) { + GL.wglSwapIntervalEXT(1); + } + } + + /** + * Test entry point + */ + public static void main(String[] args) { + System.out.println( + "Change between fullscreen and windowed mode, by pressing F and W respectively"); + System.out.println( + "Move quad using arrowkeys, and change rotation using +/-"); + FullScreenWindowedTest fswTest = new FullScreenWindowedTest(); + fswTest.execute(); + } +} diff --git a/src/java/org/lwjgl/test/opengl/Game.java b/src/java/org/lwjgl/test/opengl/Game.java new file mode 100644 index 00000000..20598f63 --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/Game.java @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * $Id$ + * + * Simple java test program. + * + * @author elias_naur + * @version $Revision$ + */ + +package org.lwjgl.test.opengl; + +import org.lwjgl.*; +import org.lwjgl.opengl.*; +import org.lwjgl.input.*; + +import java.nio.*; + +public final class Game { + static { + try { + //find first display mode that allows us 640*480*16 + int mode = -1; + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == 640 + && modes[i].height == 480 + && modes[i].bpp >= 16) { + mode = i; + break; + } + } + if (mode != -1) { + //select above found displaymode + System.out.println("Setting display mode to "+modes[mode]); + Display.setDisplayMode(modes[mode]); + System.out.println("Created display."); + } + } catch (Exception e) { + System.err.println("Failed to create display due to " + e); + } + } + + static { + try { + Window.create("LWJGL Game Example", 16, 0, 0,0); + System.out.println("Created OpenGL."); + } catch (Exception e) { + System.err.println("Failed to create OpenGL due to "+e); + System.exit(1); + } + } + + /** Is the game finished? */ + private static boolean finished; + + /** A rotating square! */ + private static float angle; + + /** + * No construction allowed + */ + private Game() { + } + + public static void main(String[] arguments) { + try { + init(); + while (!finished) { + Window.update(); + + if (Window.isMinimized()) + Thread.sleep(200); + else if (Window.isCloseRequested()) + System.exit(0); + + Keyboard.poll(); + mainLoop(); + render(); + Window.paint(); + } + } catch (Throwable t) { + t.printStackTrace(); + } finally { + cleanup(); + } + } + + /** + * All calculations are done in here + */ + private static void mainLoop() { + angle += 1f; + if (angle > 360.0f) + angle = 0.0f; + + Mouse.poll(); + if (Mouse.dx != 0 || Mouse.dy != 0 || Mouse.dwheel != 0) + System.out.println("Mouse moved " + Mouse.dx + " " + Mouse.dy + " " + Mouse.dwheel); + for (int i = 0; i < Mouse.buttonCount; i++) + if (Mouse.isButtonDown(i)) + System.out.println("Button " + i + " down"); +/* Keyboard.poll(); + if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) + finished = true;*/ + Keyboard.read(); + for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { + Keyboard.next(); + if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state) + finished = true; + if (Keyboard.key == Keyboard.KEY_T && Keyboard.state) + System.out.println("Current time: " + Sys.getTime()); + } + } + + /** + * All rendering is done in here + */ + private static void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + GL.glPushMatrix(); + GL.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f); + GL.glRotatef(angle, 0, 0, 1.0f); + GL.glBegin(GL.GL_QUADS); + GL.glVertex2i(-50, -50); + GL.glVertex2i(50, -50); + GL.glVertex2i(50, 50); + GL.glVertex2i(-50, 50); + GL.glEnd(); + GL.glPopMatrix(); + } + + /** + * Initialize + */ + private static void init() throws Exception { + Keyboard.create(); + Keyboard.enableBuffer(); + Mouse.create(); + Sys.setTime(0); + Sys.setProcessPriority(Sys.HIGH_PRIORITY); + System.out.println("Timer resolution: " + Sys.getTimerResolution()); + // Go into orthographic projection mode. + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + GLU.gluOrtho2D(0, Display.getWidth(), 0, Display.getHeight()); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + GL.glViewport(0, 0, Display.getWidth(), Display.getHeight()); + ByteBuffer num_tex_units_buf = ByteBuffer.allocateDirect(4); + num_tex_units_buf.order(ByteOrder.nativeOrder()); + GL.glGetInteger(GL.GL_MAX_TEXTURE_UNITS_ARB, num_tex_units_buf.asIntBuffer()); + System.out.println("Number of texture units: " + num_tex_units_buf.getInt()); + // Fix the refresh rate to the display frequency. +// GL.wglSwapIntervalEXT(1); + } + + /** + * Cleanup + */ + private static void cleanup() { + Keyboard.destroy(); + Mouse.destroy(); + Window.destroy(); + try { + Display.resetDisplayMode(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/src/java/org/lwjgl/test/opengl/Grass.java b/src/java/org/lwjgl/test/opengl/Grass.java new file mode 100644 index 00000000..77880a7f --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/Grass.java @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * $Id$ + * + * Simple java test program. + * + * @author elias_naur + * @version $Revision$ + */ + +package org.lwjgl.test.opengl; + +import org.lwjgl.input.*; +import org.lwjgl.opengl.*; +import org.lwjgl.*; + +import java.io.*; +import java.nio.*; +import java.util.*; + +public class Grass { + + static class Aslod { + float angle; + float value; + float ripple; + float count; + } + private static boolean finished = false; + private static Random rand = new Random(); + + static { + try { + int mode = -1; + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == 640 + && modes[i].height == 480 + && modes[i].bpp >= 16) { + mode = i; + break; + } + } + + if (mode == -1) { + System.out.println("did not find suitable mode"); + } else { + System.out.println("Display mode: " + modes[mode]); + } + // For now let's just pick a mode we're certain to have + + Display.setDisplayMode(modes[mode]); + System.out.println("Created display."); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + + static { + try { + Window.create("LWJGL Grass", 50, 50, 640, 480, 16, 0, 0,0); + Keyboard.create(); + Keyboard.enableBuffer(); + Mouse.create(); + System.out.println("Created OpenGL."); + } catch (Exception e) { + System.err.println("Failed to create OpenGL due to " + e); + System.exit(1); + } + + } + + private static Aslod aslod = new Aslod(); + + private static int mesh; + private static int program_handle; + + private static byte[] loadFile(String file) { + int next; + java.util.Vector bytes = new java.util.Vector(); + try { + ClassLoader loader = Grass.class.getClassLoader(); + InputStream stream = new BufferedInputStream(loader.getResourceAsStream(file)); + while ((next = (stream.read())) != -1) + bytes.add(new Byte((byte) next)); + stream.close(); + byte[] result = new byte[bytes.size()]; + for (int i = 0; i < result.length; i++) + result[i] = ((Byte) bytes.get(i)).byteValue(); + return result; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + ByteBuffer byte_buf = ByteBuffer.allocateDirect(4); + byte_buf.order(ByteOrder.nativeOrder()); + GLCaps.determineAvailableExtensions(); + System.out.println("Vertex program supported: " + GLCaps.GL_NV_vertex_program); + GL.glGenProgramsNV(byte_buf.asIntBuffer()); + IntBuffer int_buf = byte_buf.asIntBuffer(); + if (int_buf.get(0) == 0) + throw new RuntimeException("Could not allocate new vertex program id!"); + + program_handle = int_buf.get(0); + byte[] program = loadFile("cg_grass2.vp"); + ByteBuffer program_buf = ByteBuffer.allocateDirect(program.length); + program_buf.order(ByteOrder.nativeOrder()); + program_buf.rewind(); + program_buf.put(program); + program_buf.rewind(); + GL.glLoadProgramNV( + GL.GL_VERTEX_PROGRAM_NV, + program_handle, + program_buf); + /*GL.glGetInteger(GL.PROGRAM_ERROR_POSITION_NV, int_buf); + System.out.println("error position: " + int_buf.get(0));*/ + + genMesh(); + + float[] LightDiffuse = { 1.0f, 0.0f, 0.0f, 1.0f }; + float[] LightPosition = { 1.0f, 1.0f, 1.0f, 0.0f }; + ByteBuffer light_buf = ByteBuffer.allocateDirect(4 * 4); + light_buf.order(ByteOrder.nativeOrder()); + FloatBuffer light_buf_f = light_buf.asFloatBuffer(); + light_buf_f.rewind(); + light_buf_f.put(LightDiffuse); + + GL.glLightfv( + GL.GL_LIGHT0, + GL.GL_DIFFUSE, + light_buf_f); + light_buf_f.rewind(); + light_buf_f.put(LightPosition); + GL.glLightfv( + GL.GL_LIGHT0, + GL.GL_POSITION, + light_buf_f); + GL.glEnable(GL.GL_LIGHT0); + GL.glEnable(GL.GL_LIGHTING); + GL.glEnable(GL.GL_DEPTH_TEST); + GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); + GL.glEnable(GL.GL_BLEND); + GL.glMatrixMode(GL.GL_PROJECTION); + GLU.gluPerspective(40.0, 1.0, 1.0, 50.0); + GL.glMatrixMode(GL.GL_MODELVIEW); + + GLU.gluLookAt(14.0, 10.0, -16.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); + + aslod.angle = 2.6179935f; + aslod.value = 0.2f; + aslod.ripple = 0.0f; + aslod.count = 0.0f; + + while (!finished) { + keyPoll(); + float degree = (1.0f + (aslod.value * 20.0f)) * 0.01745329f; + + degree *= (0.5 + myrand()); + + ptrAnimate(degree); + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + + //ptrDraw(); + + grsDraw(); + + Window.paint(); + } + Mouse.destroy(); + Keyboard.destroy(); + Window.destroy(); + } + + private static float myrand() { + // returns a value between 0 and 1 + return rand.nextFloat(); + } + + private static void genGrass( + float fFaceHeight, + float fFaceWidth, + float fX, + float fZ) { + int cFaces; + int numFaces; + float cWidth; + float fDecWidth, frndWidth, frndHeight; + float fRotate; + float fRigid; + + numFaces = 5; + frndHeight = + fFaceHeight + + ((fFaceHeight / 1.5f) + * (float) java.lang.Math.cos( + java.lang.Math.abs(rand.nextInt()))); + frndWidth = + fFaceWidth + + ((fFaceWidth / 4.0f) + * (float) java.lang.Math.cos( + java.lang.Math.abs(rand.nextInt()))); + fDecWidth = frndWidth / 5.0f; + fRotate = myrand() * 3.1415f; + fRigid = ((fRigid = myrand()) < 0.2f) ? 0.2f : fRigid; + + if (myrand() < 0.3) + GL.glBegin(GL.GL_LINE_STRIP); + else + GL.glBegin(GL.GL_QUAD_STRIP); + + for (cFaces = 0; cFaces < numFaces; cFaces++) { + for (cWidth = frndWidth; + cWidth >= -frndWidth; + cWidth -= (frndWidth * 2.0f)) { + GL.glColor4f(fX, fRigid, fZ, (float) cFaces / (float) numFaces); + GL.glVertex3f( + (float) (((cFaces - 2) * 0.1f) + * java.lang.Math.cos(fRotate) + + (cWidth) * java.lang.Math.sin(fRotate)), + cFaces * frndHeight, + - (float) + (((cFaces - 2) * 0.1f) * java.lang.Math.sin(fRotate) + + (cWidth) * java.lang.Math.cos(fRotate))); + } + frndWidth -= fDecWidth; + } + GL.glEnd(); + + } + + private static void genMesh() { + float cI, cJ, fArea; + + fArea = 20.0f; + mesh = GL.glGenLists(1); + GL.glNewList(mesh, GL.GL_COMPILE); + for (cI = -fArea / 2; cI < fArea / 2; cI += 0.25f) { + for (cJ = -fArea / 2; cJ < fArea / 2; cJ += 0.25f) { + genGrass(0.5f, 0.1f, cI, cJ); + } + } + GL.glEndList(); + } + + private static void grsDraw() { + GL.glEnable(GL.GL_VERTEX_PROGRAM_NV); + GL.glBindProgramNV(GL.GL_VERTEX_PROGRAM_NV, program_handle); + GL.glTrackMatrixNV( + GL.GL_VERTEX_PROGRAM_NV, + 0, + GL.GL_MODELVIEW_PROJECTION_NV, + GL.GL_IDENTITY_NV); + + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 4, + 0.0f, + 0.0f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 5, + 0.0f, + 0.0f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 6, + 1.763609f, + 0.496495f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 7, + -0.943599f, + 3.203737f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 8, + 4.101107f, + 0.943413f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 9, + -1.218603f, + 6.259399f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 10, + 7.214299f, + 1.352961f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 11, + -1.540748f, + 10.080958f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 12, + 10.880035f, + 1.759046f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 13, + -1.852705f, + 14.468674f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 14, + 14.292879f, + 1.973329f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 15, + -1.973387f, + 18.506531f, + 0.0f, + 0.0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 16, + (float) (java.lang.Math.sin(aslod.angle) + * (aslod.value + aslod.ripple)), + 0.0f, + (float) (java.lang.Math.cos(aslod.angle) + * (aslod.value + aslod.ripple)), + 0.0f); + + GL.glProgramParameter4fNV(GL.GL_VERTEX_PROGRAM_NV, 17, 1.7f, 5f, 2f, 0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 18, + -0.0187293f, + 0.074261f, + 0.2121144f, + 1.570729f); + GL.glProgramParameter4fNV(GL.GL_VERTEX_PROGRAM_NV, 20, 0f, 0.5f, 1f, 0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 21, + 0.25f, + -9f, + 0.75f, + 0.1591549f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 22, + 24.9808f, + -24.9808f, + -60.14581f, + 60.14581f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 23, + 85.45379f, + -85.45379f, + -64.93935f, + 64.93935f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 24, + 19.73921f, + -19.73921f, + -1f, + 1f); + GL.glProgramParameter4fNV(GL.GL_VERTEX_PROGRAM_NV, 25, 0f, 4f, 0f, 0f); + GL.glProgramParameter4fNV( + GL.GL_VERTEX_PROGRAM_NV, + 19, + 1f, + 3.141593f, + 0.5f, + 1f); + GL.glProgramParameter4fNV(GL.GL_VERTEX_PROGRAM_NV, 26, 0.7f, 0.4f, 0f, 0f); + GL.glCallList(mesh); + GL.glDisable(GL.GL_VERTEX_PROGRAM_NV); + + } + + private static void ptrAnimate(float degree) { + aslod.count += degree; + aslod.ripple = (float) (java.lang.Math.cos(aslod.count) / 80.0); + + } + + private static void keyPoll() { + Keyboard.read(); + for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { + Keyboard.next(); + if (!Keyboard.state) + continue; + switch (Keyboard.key) { + case Keyboard.KEY_A : + aslod.angle += 0.1; + break; + case Keyboard.KEY_D : + aslod.angle -= 0.1; + break; + case Keyboard.KEY_W : + aslod.value += (aslod.value >= 0.15) ? 0.0 : 0.0025; + break; + case Keyboard.KEY_S : + aslod.value -= (aslod.value <= 0.005) ? 0.0 : 0.0025; + break; + case Keyboard.KEY_ESCAPE : + finished = true; + break; + } + } + } + +} diff --git a/src/java/org/lwjgl/test/opengl/PbufferTest.java b/src/java/org/lwjgl/test/opengl/PbufferTest.java new file mode 100644 index 00000000..63d26833 --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/PbufferTest.java @@ -0,0 +1,417 @@ +/* + * Copyright (c) 2003 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Lightweight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.opengl; + +import org.lwjgl.*; +import org.lwjgl.input.*; +import org.lwjgl.opengl.*; +import org.lwjgl.vector.Vector2f; + +import java.nio.*; + +/** + * $Id$ + * + * Tests Pbuffers + * + * @author elias_naur + * @version $Revision$ + */ +public class PbufferTest { + + /** Intended deiplay mode */ + private DisplayMode mode; + + /** GLU instance */ + private GLU glu; + + /** our quad moving around */ + private Vector2f quadPosition; + + /** our quadVelocity */ + private Vector2f quadVelocity; + + /** angle of quad */ + private float angle; + + /** degrees to rotate per frame */ + private float angleRotation = 1.0f; + + /** Max speed of all changable attributes */ + private static final float MAX_SPEED = 20.0f; + + /** Pbuffer instance */ + private static Pbuffer pbuffer; + + /** The shared texture */ + private static int tex_handle; + + /** + * Executes the test + */ + public void execute() { + initialize(); + + mainLoop(); + + cleanup(); + } + + /** + * Initializes the test + */ + private void initialize() { + try { + //find displaymode + mode = findDisplayMode(800, 600, 16); + + // start of in windowed mode + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); +// gl = new GLWindow("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + if ((Pbuffer.getPbufferCaps() & Pbuffer.PBUFFER_SUPPORTED) == 0) { + System.out.println("No Pbuffer support!"); + System.exit(1); + } + System.out.println("Pbuffer support detected"); + + glInit(); + initPbuffer(); + + Keyboard.create(); + + quadPosition = new Vector2f(100f, 100f); + quadVelocity = new Vector2f(1.0f, 1.0f); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Runs the main loop of the "test" + */ + private void mainLoop() { + while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) + && !Window.isCloseRequested()) { + // allow subsystem to get a chance to run too + Window.update(); + + if (!Window.isMinimized()) { + // check keyboard input + processKeyboard(); + + // do "game" logic, and render it + logic(); + render(); + + // paint window + Window.paint(); + } else { + + // no need to render/paint if nothing has changed (ie. window dragged over) + if (Window.isDirty()) { + render(); + Window.paint(); + } + + // don't waste cpu time, sleep more + try { + Thread.sleep(100); + } catch (InterruptedException inte) { + } + } + } + } + + /** + * Performs the logic + */ + private void logic() { + angle += angleRotation; + if (angle > 90.0f) { + angle = 0.0f; + } + + quadPosition.x += quadVelocity.x; + quadPosition.y += quadVelocity.y; + + //check colision with vertical border border + if (quadPosition.x + 50 >= mode.width || quadPosition.x - 50 <= 0) { + quadVelocity.x *= -1; + } + + //check collision with horizontal border + if (quadPosition.y + 50 >= mode.height || quadPosition.y - 50 <= 0) { + quadVelocity.y *= -1; + } + } + + private void render() { + if (pbuffer.isBufferLost()) { + System.out.println("Buffer contents lost - will recreate the buffer"); + Pbuffer.releaseContext(); + pbuffer.destroy(); + initPbuffer(); + } + pbuffer.makeCurrent(); + // Pbuffer rendering + //clear background + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + + // draw white quad + GL.glPushMatrix(); + { + GL.glTranslatef(quadPosition.x, quadPosition.y, 0); + GL.glRotatef(angle, 0.0f, 0.0f, 1.0f); + GL.glColor3f(1.0f, 1.0f, 1.0f); + GL.glBegin(GL.GL_QUADS); + { + GL.glVertex2i(-50, -50); + GL.glVertex2i(50, -50); + GL.glVertex2i(50, 50); + GL.glVertex2i(-50, 50); + } + GL.glEnd(); + } + GL.glPopMatrix(); + GL.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 0, 0, 256, 256, 0); + Pbuffer.releaseContext(); + + // OpenGL window rendering + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + // draw white quad + GL.glPushMatrix(); + { + GL.glTranslatef(quadPosition.x, quadPosition.y, 0); + GL.glRotatef(angle, 0.0f, 0.0f, 1.0f); + GL.glColor3f(1.0f, 1.0f, 0.0f); + GL.glBegin(GL.GL_QUADS); + { + GL.glTexCoord2f(0f, 0f); + GL.glVertex2i(-50, -50); + GL.glTexCoord2f(1f, 0f); + GL.glVertex2i(50, -50); + GL.glTexCoord2f(1f, 1f); + GL.glVertex2i(50, 50); + GL.glTexCoord2f(0f, 1f); + GL.glVertex2i(-50, 50); + } + GL.glEnd(); + } + GL.glPopMatrix(); + } + + private void initPbuffer() { + try { + pbuffer = new Pbuffer(256, 256, mode.bpp, 0, 0, 0); + pbuffer.makeCurrent(); + initGLState(256, 256, 0.5f); + GL.glBindTexture(GL.GL_TEXTURE_2D, tex_handle); + Pbuffer.releaseContext(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Processes keyboard input + */ + private void processKeyboard() { + Keyboard.poll(); + + //check for fullscreen key + if (Keyboard.isKeyDown(Keyboard.KEY_F)) { + + try { + destroyTexture(); + Keyboard.destroy(); + Pbuffer.releaseContext(); + pbuffer.destroy(); + Window.destroy(); + + Display.setDisplayMode(mode); + Window.create("Test", mode.bpp, 0, 0, 0); + glInit(); + initPbuffer(); + + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + //check for window key + if (Keyboard.isKeyDown(Keyboard.KEY_W)) { + try { + destroyTexture(); + Keyboard.destroy(); + Pbuffer.releaseContext(); + pbuffer.destroy(); + Window.destroy(); + + Display.resetDisplayMode(); + Window.create("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0); + glInit(); + initPbuffer(); + + + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + //check for speed changes + if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { + quadVelocity.y += 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { + quadVelocity.y -= 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { + quadVelocity.x += 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { + quadVelocity.x -= 0.1f; + } + + if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) { + angleRotation += 0.1f; + } + if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) { + angleRotation -= 0.1f; + } + + //throttle + if (quadVelocity.x < -MAX_SPEED) { + quadVelocity.x = -MAX_SPEED; + } + if (quadVelocity.x > MAX_SPEED) { + quadVelocity.x = MAX_SPEED; + } + if (quadVelocity.y < -MAX_SPEED) { + quadVelocity.y = -MAX_SPEED; + } + if (quadVelocity.y > MAX_SPEED) { + quadVelocity.y = MAX_SPEED; + } + + if (angleRotation < 0.0f) { + angleRotation = 0.0f; + } + if (angleRotation > MAX_SPEED) { + angleRotation = MAX_SPEED; + } + } + + private void destroyTexture() { + IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); + buffer.put(0, tex_handle); + GL.glDeleteTextures(buffer); + } + + /** + * Cleans up the test + */ + private void cleanup() { + destroyTexture(); + Keyboard.destroy(); + Pbuffer.releaseContext(); + pbuffer.destroy(); + Window.destroy(); + } + + /** + * Retrieves a displaymode, if one such is available + * + * @param width Required width + * @param height Required height + * @param bpp Minimum required bits per pixel + * @return + */ + private DisplayMode findDisplayMode(int width, int height, int bpp) { + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == width + && modes[i].height == height + && modes[i].bpp >= bpp) { + return modes[i]; + } + } + return null; + } + + private void initGLState(int width, int height, float color) { + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + GLU.gluOrtho2D(0, mode.width, 0, mode.height); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + GL.glViewport(0, 0, width, height); + + //set clear color to black + GL.glClearColor(color, color, color, 0.0f); + } + + /** + * Initializes OGL + */ + private void glInit() { + //sync frame (only works on windows) + GLCaps.determineAvailableExtensions(); + if (GLCaps.WGL_EXT_swap_control) { + GL.wglSwapIntervalEXT(1); + } + GL.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); + GL.glEnable(GL.GL_TEXTURE_2D); + // Create shared texture + IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); + GL.glGenTextures(buffer); + tex_handle = buffer.get(0); + GL.glBindTexture(GL.GL_TEXTURE_2D, tex_handle); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + initGLState(mode.width, mode.height, 0f); + } + + /** + * Test entry point + */ + public static void main(String[] args) { + System.out.println( + "Change between fullscreen and windowed mode, by pressing F and W respectively"); + System.out.println("Move quad using arrowkeys, and change rotation using +/-"); + PbufferTest fswTest = new PbufferTest(); + fswTest.execute(); + } +} diff --git a/src/java/org/lwjgl/test/opengl/VBOIndexTest.java b/src/java/org/lwjgl/test/opengl/VBOIndexTest.java new file mode 100644 index 00000000..de47453e --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/VBOIndexTest.java @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * $Id$ + * + * Simple java test program. + * + * @author elias_naur + * @version $Revision$ + */ + +package org.lwjgl.test.opengl; + +import org.lwjgl.*; +import org.lwjgl.opengl.*; +import org.lwjgl.input.*; + +import java.nio.*; + +public final class VBOIndexTest { + static { + try { + //find first display mode that allows us 640*480*16 + int mode = -1; + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == 640 + && modes[i].height == 480 + && modes[i].bpp >= 16) { + mode = i; + break; + } + } + if (mode != -1) { + //select above found displaymode + System.out.println("Setting display mode to "+modes[mode]); + Display.setDisplayMode(modes[mode]); + System.out.println("Created display."); + } + } catch (Exception e) { + System.err.println("Failed to create display due to " + e); + } + } + + static { + try { + Window.create("LWJGL Game Example", 16, 0, 0,0); + System.out.println("Created OpenGL."); + } catch (Exception e) { + System.err.println("Failed to create OpenGL due to "+e); + System.exit(1); + } + } + + /** Is the game finished? */ + private static boolean finished; + + /** A rotating square! */ + private static float angle; + private static int buffer_id; + private static int indices_buffer_id; + private static FloatBuffer vertices; + private static ByteBuffer mapped_buffer = null; + private static FloatBuffer mapped_float_buffer = null; + private static IntBuffer indices; + private static ByteBuffer mapped_indices_buffer = null; + private static IntBuffer mapped_indices_int_buffer = null; + + public static void main(String[] arguments) { + try { + init(); + while (!finished) { + Window.update(); + + if (Window.isMinimized()) + Thread.sleep(200); + else if (Window.isCloseRequested()) + System.exit(0); + + Keyboard.poll(); + mainLoop(); + render(); + Window.paint(); + } + } catch (Throwable t) { + t.printStackTrace(); + } finally { + cleanup(); + } + } + + /** + * All calculations are done in here + */ + private static void mainLoop() { + angle += 1f; + if (angle > 360.0f) + angle = 0.0f; + + Mouse.poll(); + if (Mouse.dx != 0 || Mouse.dy != 0 || Mouse.dwheel != 0) + System.out.println("Mouse moved " + Mouse.dx + " " + Mouse.dy + " " + Mouse.dwheel); + for (int i = 0; i < Mouse.buttonCount; i++) + if (Mouse.isButtonDown(i)) + System.out.println("Button " + i + " down"); +/* Keyboard.poll(); + if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) + finished = true;*/ + Keyboard.read(); + for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { + Keyboard.next(); + if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state) + finished = true; + if (Keyboard.key == Keyboard.KEY_T && Keyboard.state) + System.out.println("Current time: " + Sys.getTime()); + } + } + + /** + * All rendering is done in here + */ + private static void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + GL.glPushMatrix(); + GL.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f); + GL.glRotatef(angle, 0, 0, 1.0f); + + + ByteBuffer new_mapped_buffer = GL.glMapBufferARB(GL.GL_ARRAY_BUFFER_ARB, GL.GL_WRITE_ONLY_ARB, 2*4*4, mapped_buffer); + if (new_mapped_buffer != mapped_buffer) + mapped_float_buffer = new_mapped_buffer.order(ByteOrder.nativeOrder()).asFloatBuffer(); + mapped_buffer = new_mapped_buffer; + + new_mapped_buffer = GL.glMapBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, GL.GL_WRITE_ONLY_ARB, 4*4, mapped_indices_buffer); + if (new_mapped_buffer != mapped_indices_buffer) + mapped_indices_int_buffer = new_mapped_buffer.order(ByteOrder.nativeOrder()).asIntBuffer(); + + mapped_float_buffer.rewind(); + vertices.rewind(); + mapped_float_buffer.put(vertices); + + mapped_indices_int_buffer.rewind(); + indices.rewind(); + mapped_indices_int_buffer.put(indices); + if (GL.glUnmapBufferARB(GL.GL_ARRAY_BUFFER_ARB) && GL.glUnmapBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB)) { + GL.glDrawElements(GL.GL_QUADS, 4, GL.GL_UNSIGNED_INT, 0); + } + GL.glPopMatrix(); + } + + /** + * Initialize + */ + private static void init() throws Exception { + Keyboard.create(); + Keyboard.enableBuffer(); + Mouse.create(); + Sys.setTime(0); + Sys.setProcessPriority(Sys.HIGH_PRIORITY); + System.out.println("Timer resolution: " + Sys.getTimerResolution()); + // Go into orthographic projection mode. + GLCaps.determineAvailableExtensions(); + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + GLU.gluOrtho2D(0, Display.getWidth(), 0, Display.getHeight()); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + GL.glViewport(0, 0, Display.getWidth(), Display.getHeight()); + if (!GLCaps.GL_ARB_vertex_buffer_object) { + System.out.println("ARB VBO not supported!"); + System.exit(1); + } + IntBuffer int_buffer = ByteBuffer.allocateDirect(8).order(ByteOrder.nativeOrder()).asIntBuffer(); + GL.glGenBuffersARB(int_buffer); + buffer_id = int_buffer.get(0); + indices_buffer_id = int_buffer.get(1); + GL.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, buffer_id); + GL.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, indices_buffer_id); + vertices = ByteBuffer.allocateDirect(2*4*4).order(ByteOrder.nativeOrder()).asFloatBuffer(); + vertices.put(-50).put(-50).put(50).put(-50).put(50).put(50).put(-50).put(50); + vertices.rewind(); + indices = ByteBuffer.allocateDirect(4*4).order(ByteOrder.nativeOrder()).asIntBuffer(); + indices.put(0).put(1).put(2).put(3); + indices.rewind(); + GL.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, 2*4*4, (ByteBuffer)null, GL.GL_STREAM_DRAW_ARB); + GL.glBufferDataARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, 4*4, (ByteBuffer)null, GL.GL_STREAM_DRAW_ARB); + GL.glEnableClientState(GL.GL_VERTEX_ARRAY); + GL.glVertexPointer(2, GL.GL_FLOAT, 0, 0); + GL.glGetInteger(GL.GL_MAX_TEXTURE_UNITS_ARB, int_buffer); + System.out.println("Number of texture units: " + int_buffer.get(0)); + // Fix the refresh rate to the display frequency. +// gl.wglSwapIntervalEXT(1); + } + + /** + * Cleanup + */ + private static void cleanup() { + IntBuffer int_buffer = ByteBuffer.allocateDirect(8).order(ByteOrder.nativeOrder()).asIntBuffer(); + int_buffer.put(0, buffer_id); + int_buffer.put(1, indices_buffer_id); + GL.glDeleteBuffersARB(int_buffer); + Keyboard.destroy(); + Mouse.destroy(); + Window.destroy(); + try { + Display.resetDisplayMode(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/src/java/org/lwjgl/test/opengl/VBOTest.java b/src/java/org/lwjgl/test/opengl/VBOTest.java new file mode 100644 index 00000000..87ebe85d --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/VBOTest.java @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2002 Lightweight Java Game Library Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * $Id$ + * + * Simple java test program. + * + * @author elias_naur + * @version $Revision$ + */ + +package org.lwjgl.test.opengl; + +import org.lwjgl.*; +import org.lwjgl.opengl.*; +import org.lwjgl.input.*; + +import java.nio.*; + +public final class VBOTest { + static { + try { + //find first display mode that allows us 640*480*16 + int mode = -1; + DisplayMode[] modes = Display.getAvailableDisplayModes(); + for (int i = 0; i < modes.length; i++) { + if (modes[i].width == 640 + && modes[i].height == 480 + && modes[i].bpp >= 16) { + mode = i; + break; + } + } + if (mode != -1) { + //select above found displaymode + System.out.println("Setting display mode to "+modes[mode]); + Display.setDisplayMode(modes[mode]); + System.out.println("Created display."); + } + } catch (Exception e) { + System.err.println("Failed to create display due to " + e); + } + } + + static { + try { + Window.create("LWJGL Game Example", 16, 0, 0,0); + System.out.println("Created OpenGL."); + } catch (Exception e) { + System.err.println("Failed to create OpenGL due to "+e); + System.exit(1); + } + } + + /** Is the game finished? */ + private static boolean finished; + + /** A rotating square! */ + private static float angle; + private static int buffer_id; + private static FloatBuffer vertices; + private static ByteBuffer mapped_buffer = null; + private static FloatBuffer mapped_float_buffer = null; + + public static void main(String[] arguments) { + try { + init(); + while (!finished) { + Window.update(); + + if (Window.isMinimized()) + Thread.sleep(200); + else if (Window.isCloseRequested()) + System.exit(0); + + Keyboard.poll(); + mainLoop(); + render(); + Window.paint(); + } + } catch (Throwable t) { + t.printStackTrace(); + } finally { + cleanup(); + } + } + + /** + * All calculations are done in here + */ + private static void mainLoop() { + angle += 1f; + if (angle > 360.0f) + angle = 0.0f; + + Mouse.poll(); + if (Mouse.dx != 0 || Mouse.dy != 0 || Mouse.dwheel != 0) + System.out.println("Mouse moved " + Mouse.dx + " " + Mouse.dy + " " + Mouse.dwheel); + for (int i = 0; i < Mouse.buttonCount; i++) + if (Mouse.isButtonDown(i)) + System.out.println("Button " + i + " down"); +/* Keyboard.poll(); + if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) + finished = true;*/ + Keyboard.read(); + for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { + Keyboard.next(); + if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state) + finished = true; + if (Keyboard.key == Keyboard.KEY_T && Keyboard.state) + System.out.println("Current time: " + Sys.getTime()); + } + } + + /** + * All rendering is done in here + */ + private static void render() { + GL.glClear(GL.GL_COLOR_BUFFER_BIT); + GL.glPushMatrix(); + GL.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f); + GL.glRotatef(angle, 0, 0, 1.0f); + ByteBuffer new_mapped_buffer = GL.glMapBufferARB(GL.GL_ARRAY_BUFFER_ARB, GL.GL_WRITE_ONLY_ARB, 2*4*4, mapped_buffer); + if (new_mapped_buffer != mapped_buffer) + mapped_float_buffer = new_mapped_buffer.order(ByteOrder.nativeOrder()).asFloatBuffer(); + mapped_buffer = new_mapped_buffer; + mapped_float_buffer.rewind(); + vertices.rewind(); + mapped_float_buffer.put(vertices); + if (GL.glUnmapBufferARB(GL.GL_ARRAY_BUFFER_ARB)) + GL.glDrawArrays(GL.GL_QUADS, 0, 4); + GL.glPopMatrix(); + } + + /** + * Initialize + */ + private static void init() throws Exception { + Keyboard.create(); + Keyboard.enableBuffer(); + Mouse.create(); + Sys.setTime(0); + Sys.setProcessPriority(Sys.HIGH_PRIORITY); + System.out.println("Timer resolution: " + Sys.getTimerResolution()); + // Go into orthographic projection mode. + GLCaps.determineAvailableExtensions(); + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glLoadIdentity(); + GLU.gluOrtho2D(0, Display.getWidth(), 0, Display.getHeight()); + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + GL.glViewport(0, 0, Display.getWidth(), Display.getHeight()); + if (!GLCaps.GL_ARB_vertex_buffer_object) { + System.out.println("ARB VBO not supported!"); + System.exit(1); + } + IntBuffer int_buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); + GL.glGenBuffersARB(int_buffer); + buffer_id = int_buffer.get(0); + GL.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, buffer_id); + vertices = ByteBuffer.allocateDirect(2*4*4).order(ByteOrder.nativeOrder()).asFloatBuffer(); + vertices.put(-50).put(-50).put(50).put(-50).put(50).put(50).put(-50).put(50); + GL.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, 2*4*4, (ByteBuffer)null, GL.GL_STREAM_DRAW_ARB); + GL.glEnableClientState(GL.GL_VERTEX_ARRAY); + GL.glVertexPointer(2, GL.GL_FLOAT, 0, 0); + GL.glGetInteger(GL.GL_MAX_TEXTURE_UNITS_ARB, int_buffer); + System.out.println("Number of texture units: " + int_buffer.get(0)); + // Fix the refresh rate to the display frequency. +// gl.wglSwapIntervalEXT(1); + } + + /** + * Cleanup + */ + private static void cleanup() { + IntBuffer int_buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); + int_buffer.put(0, buffer_id); + GL.glDeleteBuffersARB(int_buffer); + Keyboard.destroy(); + Mouse.destroy(); + Window.destroy(); + try { + Display.resetDisplayMode(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/src/java/org/lwjgl/test/opengl/cg_grass2.cg b/src/java/org/lwjgl/test/opengl/cg_grass2.cg new file mode 100644 index 00000000..4a743e9e --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/cg_grass2.cg @@ -0,0 +1,72 @@ +#pragma bind app2vert.position = ATTR0 +#pragma bind app2vert.normal = ATTR2 +#pragma bind app2vert.color = ATTR3 + + +struct app2vert : application2vertex +{ + float4 position; //in object space + float3 normal; //in object space + float4 color; +}; + +#pragma bind vert2frag.HPOS = HPOS +#pragma bind vert2frag.COL0 = COL0 + +struct vert2frag : vertex2fragment +{ + float4 HPOS; + float4 COL0; +}; + +vert2frag main(app2vert IN, + uniform float4x4 ModelViewProj, + uniform float4 Stiff[12], + uniform float4 Load) +{ + vert2frag OUT; + float4 position; + float3 normal; + float3 disloc; + float4 dif; + float index; + float teta; + float alpha; + + alpha = IN.position.y / 1.7; + + // matrix line index + index = IN.color.a * 5 * 2; + + // straw stiffness (randomic) + Load = Load * IN.color.y; + + disloc.x = IN.position.x + dot(Stiff[index].xy, Load.xz); + disloc.y = IN.position.y; + disloc.z = IN.position.z + dot(Stiff[index + 1].xy, Load.xz); + + // find the normal + teta = acos(IN.position.y / length(disloc)); + + normal = IN.position.xyz - disloc.xyz; + normal.y = sin(teta) * length(disloc); + normal = normalize(normal); + + position.x = disloc.x + IN.color.x; + position.y = disloc.y; + position.z = disloc.z + IN.color.z; + position.w = 1; + + OUT.HPOS = mul(ModelViewProj, position); + + // calculate diffuse lighting off the normal that was just calculated + float4 lightPos = float4(0,4,0,0); + float4 lightVec = normalize(lightPos - position); + float diffuseInten = dot(lightVec.xyz, normal); + + diffuseInten = (diffuseInten < 0.5) ? 0.5 : diffuseInten; + + OUT.COL0 = float4(IN.color.y * 0.7, 0.7, 0.4, alpha) * diffuseInten; + + return OUT; +} diff --git a/src/java/org/lwjgl/test/opengl/cg_grass2.vp b/src/java/org/lwjgl/test/opengl/cg_grass2.vp new file mode 100644 index 00000000..892a6430 --- /dev/null +++ b/src/java/org/lwjgl/test/opengl/cg_grass2.vp @@ -0,0 +1,116 @@ +!!VP1.0 +# NV_vertex_program generated by NVIDIA Cg compiler +# cgc version 1.5.0001, build date Sep 12 2002 15:49:58 +# command line args: -profile vp20 cg_grass2.cg +# nv30vp backend compiling 'main' program +#vendor NVIDIA Corporation +#version 1.0.1 +#profile vp20 +#program main +#semantic main.ModelViewProj +#semantic main.Stiff +#semantic main.Load +#var float4 IN.position : $vin.ATTR0 : ATTR0 : 0 : 1 +#var float3 IN.normal : $vin.ATTR2 : ATTR2 : 0 : 1 +#var float4 IN.color : $vin.ATTR3 : ATTR3 : 0 : 1 +#var float4x4 ModelViewProj : : c[0], 4 : 1 : 1 +#var float4 Stiff[0] : : c[4] : 2 : 1 +#var float4 Stiff[1] : : c[5] : 2 : 1 +#var float4 Stiff[2] : : c[6] : 2 : 1 +#var float4 Stiff[3] : : c[7] : 2 : 1 +#var float4 Stiff[4] : : c[8] : 2 : 1 +#var float4 Stiff[5] : : c[9] : 2 : 1 +#var float4 Stiff[6] : : c[10] : 2 : 1 +#var float4 Stiff[7] : : c[11] : 2 : 1 +#var float4 Stiff[8] : : c[12] : 2 : 1 +#var float4 Stiff[9] : : c[13] : 2 : 1 +#var float4 Stiff[10] : : c[14] : 2 : 1 +#var float4 Stiff[11] : : c[15] : 2 : 1 +#var float4 Load : : c[16] : 3 : 1 +#var float4 HPOS : $vout.HPOS : HPOS : -1 : 1 +#var float4 COL0 : $vout.COL0 : COL0 : -1 : 1 +#const c[17] = 1.7 5 2 0 +#const c[18] = -0.0187293 0.074261 0.2121144 1.570729 +#const c[20] = 0 0.5 1 0 +#const c[21] = 0.25 -9 0.75 0.1591549 +#const c[22] = 24.9808 -24.9808 -60.14581 60.14581 +#const c[23] = 85.45379 -85.45379 -64.93935 64.93935 +#const c[24] = 19.73921 -19.73921 -1 1 +#const c[25] = 0 4 0 0 +#const c[19] = 1 3.141593 0.5 1 +#const c[26] = 0.7 0.4 0 0 + MUL R2, c[16], v[3].y; + MUL R0, v[3].w, c[17].y; + MUL R1, R0.x, c[17].z; + ARL A0.x, R1.x; + MUL R0, c[A0.x + 4].xyxx, R2.xzxx; + ADD R0, R0.x, R0.y; + ADD R3.x, v[0].x, R0.x; + MOV R3.y, v[0].y; + MUL R0, c[A0.x + 5].xyxx, R2.xzxx; + ADD R0, R0.x, R0.y; + ADD R3.z, v[0].z, R0.x; + ADD R2.x, R3.x, v[3].x; + MOV R2.y, R3.y; + ADD R2.z, R3.z, v[3].z; + MOV R2.w, c[19].x; + DP4 R0.x, c[0], R2; + DP4 R0.y, c[1], R2; + DP4 R0.z, c[2], R2; + DP4 R0.w, c[3], R2; + MOV o[HPOS], R0; + ADD R1.xz, v[0].xyzx, -R3.xyzx; + DP3 R0, R3.xyzx, R3.xyzx; + RSQ R0, R0.x; + RCP R0, R0.x; + RCP R3, R0.x; + MUL R6, v[0].y, R3.x; + MAX R5, R6.x, -R6.x; + MAD R3, c[18].x, R5.x, c[18].y; + MAD R3, R3.x, R5.x, -c[18].z; + MAD R4, R3.x, R5.x, c[18].w; + ADD R3, c[19].x, -R5.x; + RSQ R3, R3.x; + RCP R3, R3.x; + MUL R5, R4.x, R3.x; + SLT R4, R6.x, c[17].w; + MUL R3, c[17].z, R4.x; + MUL R3, R3.x, R5.x; + ADD R3, R5.x, -R3.x; + MAD R3, R4.x, c[19].y, R3.x; + MAD R6, c[21].w, R3.x, -c[21].x; + EXP R6.y, R6.x; + ADD R3, c[20].xyzx, -R6.yyyy; + MUL R5, R3.xyzx, R3.xyzx; + SLT R4.x, R6.y, c[21].x; + SGE R3, R6.yyyy, c[21].yzyy; + MOV R4.yz, R3.xxyw; + DP3 R4.y, R4.xyzx, c[24].zwzz; + MAD R3, c[22].xyxx, R5.xyzx, c[22].zwzz; + MAD R3, R3.xyzx, R5.xyzx, c[23].xyxx; + MAD R3, R3.xyzx, R5.xyzx, c[23].zwzz; + MAD R3, R3.xyzx, R5.xyzx, c[24].xyxx; + MAD R3, R3.xyzx, R5.xyzx, c[24].zwzz; + DP3 R5.x, R3.xyzx, -R4.xyzx; + MUL R1.y, R5.x, R0.x; + DP3 R0, R1.xyzx, R1.xyzx; + RSQ R0, R0.x; + MUL R3, R0.x, R1.xyzx; + ADD R1, c[25], -R2; + DP4 R0, R1, R1; + RSQ R0, R0.x; + MUL R0, R0.x, R1; + DP3 R2, R0.xyzx, R3.xyzx; + SLT R1, R2.x, c[19].z; + ADD R0, c[19].w, -R1.x; + MUL R0, R0.x, R2.x; + MAD R2, R1.x, c[19].z, R0.x; + MUL R1.x, v[3].y, c[26].x; + MOV R1.y, c[26].x; + MOV R1.z, c[26].y; + RCP R0, c[17].x; + MUL R1.w, v[0].y, R0.x; + MUL o[COL0], R1, R2.x; +END +# 72 instructions +# End of program