Simplification work

This commit is contained in:
Caspian Rychlik-Prince 2004-03-26 11:26:04 +00:00
parent fca57cefd5
commit 3df3d2d401
9 changed files with 184 additions and 230 deletions

View File

@ -32,6 +32,7 @@
package org.lwjgl; package org.lwjgl;
import java.nio.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
@ -88,4 +89,20 @@ public final class BufferUtils {
return createByteBuffer(size << 2).asFloatBuffer(); return createByteBuffer(size << 2).asFloatBuffer();
} }
/**
* A helper function which is used to get the byte offset in an arbitrary buffer
* based on its position
* @return the position of the buffer, in BYTES
*/
public static int getOffset(Buffer buffer) {
if (buffer instanceof FloatBuffer || buffer instanceof IntBuffer)
return buffer.position() << 2;
else if (buffer instanceof ShortBuffer || buffer instanceof CharBuffer)
return buffer.position() << 1;
else if (buffer instanceof DoubleBuffer || buffer instanceof LongBuffer)
return buffer.position() << 3;
else
return buffer.position();
}
} }

View File

@ -476,8 +476,6 @@ public class Keyboard {
*/ */
public static int getNumKeyboardEvents() { public static int getNumKeyboardEvents() {
assert created : "The keyboard has not been created."; assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
return numEvents; return numEvents;
} }

View File

@ -39,6 +39,8 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.nio.ShortBuffer; import java.nio.ShortBuffer;
import org.lwjgl.BufferUtils;
/** /**
* $Id$ * $Id$
* *
@ -321,12 +323,12 @@ public final class ARBImaging {
private static native void nglGetConvolutionParameteriv(int target, int pname, IntBuffer params, int params_offset); private static native void nglGetConvolutionParameteriv(int target, int pname, IntBuffer params, int params_offset);
public static void glSeparableFilter2D(int target, int internalformat, int width, int height, int format, int type, Buffer row, Buffer column) { public static void glSeparableFilter2D(int target, int internalformat, int width, int height, int format, int type, Buffer row, Buffer column) {
// TODO: check buffer size valid // TODO: check buffer size valid
nglSeparableFilter2D(target, internalformat, width, height, format, type, row, Util.getOffset(row), column, Util.getOffset(column)); nglSeparableFilter2D(target, internalformat, width, height, format, type, row, BufferUtils.getOffset(row), column, BufferUtils.getOffset(column));
} }
private static native void nglSeparableFilter2D(int target, int internalformat, int width, int height, int format, int type, Buffer row, int row_offset, Buffer column, int column_offset); private static native void nglSeparableFilter2D(int target, int internalformat, int width, int height, int format, int type, Buffer row, int row_offset, Buffer column, int column_offset);
public static void glGetSeparableFilter(int target, int format, int type, Buffer row, Buffer column, Buffer span) { public static void glGetSeparableFilter(int target, int format, int type, Buffer row, Buffer column, Buffer span) {
// TODO: check buffer size valid // TODO: check buffer size valid
nglGetSeparableFilter(target, format, type, row, Util.getOffset(row), column, Util.getOffset(column), span, Util.getOffset(span)); nglGetSeparableFilter(target, format, type, row, BufferUtils.getOffset(row), column, BufferUtils.getOffset(column), span, BufferUtils.getOffset(span));
} }
private static native void nglGetSeparableFilter(int target, int format, int type, Buffer row, int row_offset, Buffer column, int column_offset, Buffer span, int span_offset); private static native void nglGetSeparableFilter(int target, int format, int type, Buffer row, int row_offset, Buffer column, int column_offset, Buffer span, int span_offset);
} }

View File

@ -33,6 +33,8 @@ package org.lwjgl.opengl;
import java.nio.*; import java.nio.*;
import org.lwjgl.BufferUtils;
/** /**
* Simple utility class. * Simple utility class.
* *
@ -40,34 +42,28 @@ import java.nio.*;
* @version $Revision$ * @version $Revision$
*/ */
abstract class Util { public final class Util {
static final IntBuffer int_buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asIntBuffer(); private static final IntBuffer int_buffer = BufferUtils.createIntBuffer(16);
/** /**
* A helper function which is used to get the byte offset in an arbitrary buffer * No c'tor
* based on its position
* @return the position of the buffer, in BYTES
*/ */
static int getOffset(Buffer buffer) { private Util() {}
if (buffer instanceof FloatBuffer || buffer instanceof IntBuffer)
return buffer.position() << 2; public static void checkGLError() {
else if (buffer instanceof ShortBuffer || buffer instanceof CharBuffer)
return buffer.position() << 1;
else if (buffer instanceof DoubleBuffer || buffer instanceof LongBuffer)
return buffer.position() << 3;
else
return buffer.position();
}
static void checkGLError() {
int err = GL11.glGetError(); int err = GL11.glGetError();
if (err != GL11.GL_NO_ERROR) { if (err != GL11.GL_NO_ERROR) {
throw new OpenGLException(err); throw new OpenGLException(err);
} }
} }
static int getGLInteger(int gl_enum) { /**
* Obtain a GL integer value from the driver
* @param gl_enum The GL value you want
* @return the integer value
*/
public static int glGetInteger(int gl_enum) {
GL11.glGetInteger(gl_enum, int_buffer); GL11.glGetInteger(gl_enum, int_buffer);
return int_buffer.get(0); return int_buffer.get(0);
} }

View File

@ -48,7 +48,7 @@ class VBOTracker {
private final StateStack attrib_stack; private final StateStack attrib_stack;
private VBOTracker() { private VBOTracker() {
int stack_size = Util.getGLInteger(GL11.GL_MAX_CLIENT_ATTRIB_STACK_DEPTH); int stack_size = Util.glGetInteger(GL11.GL_MAX_CLIENT_ATTRIB_STACK_DEPTH);
vbo_array_stack = new StateStack(stack_size, 0); vbo_array_stack = new StateStack(stack_size, 0);
vbo_element_stack = new StateStack(stack_size, 0); vbo_element_stack = new StateStack(stack_size, 0);
attrib_stack = new StateStack(stack_size, 0); attrib_stack = new StateStack(stack_size, 0);

View File

@ -242,7 +242,6 @@ public class PositionTest extends BasicTest {
// render and paint if !minimized and not dirty // render and paint if !minimized and not dirty
if(Window.isFocused() || Window.isDirty()) { if(Window.isFocused() || Window.isDirty()) {
render(); render();
Window.paint();
} else { } else {
// sleeeeeep // sleeeeeep
pause(100); pause(100);

View File

@ -29,21 +29,16 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/** /**
* $Id$ * $Id$
* *
* Simple java test program. * Simple java test program.
* *
* @author elias_naur <elias_naur@users.sourceforge.net> * @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision$ * @version $Revision$
*/ */
package org.lwjgl.test.opengl; package org.lwjgl.test.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.lwjgl.Display; import org.lwjgl.Display;
import org.lwjgl.DisplayMode; import org.lwjgl.DisplayMode;
import org.lwjgl.Sys; import org.lwjgl.Sys;
@ -51,26 +46,24 @@ import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse; import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.Util;
import org.lwjgl.opengl.Window; import org.lwjgl.opengl.Window;
import org.lwjgl.opengl.glu.GLU;
public final class Game { public final class Game {
static { static {
try { try {
//find first display mode that allows us 640*480*16 //find first display mode that allows us 640*480*16
int mode = -1; int mode = -1;
DisplayMode[] modes = Display.getAvailableDisplayModes(); DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++) { for (int i = 0; i < modes.length; i++) {
if (modes[i].width == 640 if (modes[i].width == 640 && modes[i].height == 480 && modes[i].bpp >= 16) {
&& modes[i].height == 480
&& modes[i].bpp >= 16) {
mode = i; mode = i;
break; break;
} }
} }
if (mode != -1) { if (mode != -1) {
//select above found displaymode //select above found displaymode
System.out.println("Setting display mode to "+modes[mode]); System.out.println("Setting display mode to " + modes[mode]);
Display.setDisplayMode(modes[mode]); Display.setDisplayMode(modes[mode]);
System.out.println("Created display."); System.out.println("Created display.");
} }
@ -78,132 +71,93 @@ public final class Game {
System.err.println("Failed to create display due to " + e); System.err.println("Failed to create display due to " + e);
} }
} }
static {
static { try {
try { Window.create("LWJGL Game Example");
Window.create("LWJGL Game Example", 16, 0, 0,0, 0); System.out.println("Created OpenGL.");
System.out.println("Created OpenGL."); } catch (Exception e) {
} catch (Exception e) { System.err.println("Failed to create OpenGL due to " + e);
System.err.println("Failed to create OpenGL due to "+e); System.exit(1);
System.exit(1); }
} }
} /** Is the game finished? */
private static boolean finished;
/** Is the game finished? */ /** A rotating square! */
private static boolean finished; private static float angle;
/** A rotating square! */ /**
private static float angle; * No construction allowed
*/
/** private Game() {
* No construction allowed }
*/ public static void main(String[] arguments) {
private Game() { try {
} init();
while (!finished) {
public static void main(String[] arguments) { if (Window.isMinimized()) {
try { Thread.sleep(200);
init(); } else if (Window.isCloseRequested()) {
while (!finished) { finished = true;
Window.update(); } else {
mainLoop();
if (Window.isMinimized()) render();
Thread.sleep(200); }
else if (Window.isCloseRequested()) Window.update();
System.exit(0); }
} catch (Throwable t) {
Keyboard.poll(); t.printStackTrace();
mainLoop(); } finally {
render(); cleanup();
Window.paint(); }
} }
} catch (Throwable t) { /**
t.printStackTrace(); * All calculations are done in here
} finally { */
cleanup(); private static void mainLoop() {
} angle += 1f;
} if (angle > 360.0f) angle = 0.0f;
if (Mouse.getDX() != 0 || Mouse.getDY() != 0 || Mouse.getDWheel() != 0)
/** System.out.println("Mouse moved " + Mouse.getDX() + " " + Mouse.getDY() + " " + Mouse.getDWheel());
* All calculations are done in here for (int i = 0; i < Mouse.getButtonCount(); i++)
*/ if (Mouse.isButtonDown(i)) System.out.println("Button " + i + " down");
private static void mainLoop() { for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) {
angle += 1f; Keyboard.next();
if (angle > 360.0f) if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState()) finished = true;
angle = 0.0f; if (Keyboard.getEventKey() == Keyboard.KEY_T && Keyboard.getEventKeyState())
System.out.println("Current time: " + Sys.getTime());
Mouse.poll(); }
if (Mouse.getDX() != 0 || Mouse.getDY() != 0 || Mouse.getDWheel() != 0) }
System.out.println("Mouse moved " + Mouse.getDX() + " " + Mouse.getDY() + " " + Mouse.getDWheel()); /**
for (int i = 0; i < Mouse.getButtonCount(); i++) * All rendering is done in here
if (Mouse.isButtonDown(i)) */
System.out.println("Button " + i + " down"); private static void render() {
/* Keyboard.poll(); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) GL11.glPushMatrix();
finished = true;*/ GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);
Keyboard.read(); GL11.glRotatef(angle, 0, 0, 1.0f);
for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { GL11.glBegin(GL11.GL_QUADS);
Keyboard.next(); GL11.glVertex2i(-50, -50);
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState()) GL11.glVertex2i(50, -50);
finished = true; GL11.glVertex2i(50, 50);
if (Keyboard.getEventKey() == Keyboard.KEY_T && Keyboard.getEventKeyState()) GL11.glVertex2i(-50, 50);
System.out.println("Current time: " + Sys.getTime()); GL11.glEnd();
} GL11.glPopMatrix();
} }
/**
/** * Initialize
* All rendering is done in here */
*/ private static void init()
private static void render() { throws Exception {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); Sys.setTime(0);
GL11.glPushMatrix(); Sys.setProcessPriority(Sys.HIGH_PRIORITY);
GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f); System.out.println("Timer resolution: " + Sys.getTimerResolution());
GL11.glRotatef(angle, 0, 0, 1.0f); System.out.println("Number of texture units: " + Util.glGetInteger(GL13.GL_MAX_TEXTURE_UNITS));
GL11.glBegin(GL11.GL_QUADS); }
GL11.glVertex2i(-50, -50); /**
GL11.glVertex2i(50, -50); * Cleanup
GL11.glVertex2i(50, 50); */
GL11.glVertex2i(-50, 50); private static void cleanup() {
GL11.glEnd(); Window.destroy();
GL11.glPopMatrix(); Display.resetDisplayMode();
} }
}
/**
* 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.
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(0, Display.getWidth(), 0, Display.getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
ByteBuffer num_tex_units_buf = ByteBuffer.allocateDirect(64);
num_tex_units_buf.order(ByteOrder.nativeOrder());
GL11.glGetInteger(GL13.GL_MAX_TEXTURE_UNITS, 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.
// GL11.wglSwapIntervalEXT(1);
}
/**
* Cleanup
*/
private static void cleanup() {
Keyboard.destroy();
Mouse.destroy();
Window.destroy();
try {
Display.resetDisplayMode();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -49,6 +49,7 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.Random; import java.util.Random;
import org.lwjgl.BufferUtils;
import org.lwjgl.Display; import org.lwjgl.Display;
import org.lwjgl.DisplayMode; import org.lwjgl.DisplayMode;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
@ -137,21 +138,18 @@ public class Grass {
} }
public static void main(String[] args) { public static void main(String[] args) {
ByteBuffer byte_buf = ByteBuffer.allocateDirect(4);
byte_buf.order(ByteOrder.nativeOrder());
System.out.println("Vertex program supported: " + GLContext.GL_NV_vertex_program); System.out.println("Vertex program supported: " + GLContext.GL_NV_vertex_program);
NVVertexProgram.glGenProgramsNV(byte_buf.asIntBuffer()); IntBuffer int_buf = BufferUtils.createIntBuffer(1);
IntBuffer int_buf = byte_buf.asIntBuffer(); NVVertexProgram.glGenProgramsNV(int_buf);
if (int_buf.get(0) == 0) if (int_buf.get(0) == 0)
throw new RuntimeException("Could not allocate new vertex program id!"); throw new RuntimeException("Could not allocate new vertex program id!");
program_handle = int_buf.get(0); program_handle = int_buf.get(0);
byte[] program = loadFile("cg_grass2.vp"); byte[] program = loadFile("cg_grass2.vp");
ByteBuffer program_buf = ByteBuffer.allocateDirect(program.length); ByteBuffer program_buf = BufferUtils.createByteBuffer(program.length);
program_buf.order(ByteOrder.nativeOrder()); program_buf.order(ByteOrder.nativeOrder());
program_buf.rewind();
program_buf.put(program); program_buf.put(program);
program_buf.rewind(); program_buf.flip();
NVVertexProgram.glLoadProgramNV( NVVertexProgram.glLoadProgramNV(
NVVertexProgram.GL_VERTEX_PROGRAM_NV, NVVertexProgram.GL_VERTEX_PROGRAM_NV,
program_handle, program_handle,
@ -163,18 +161,17 @@ public class Grass {
float[] LightDiffuse = { 1.0f, 0.0f, 0.0f, 1.0f }; float[] LightDiffuse = { 1.0f, 0.0f, 0.0f, 1.0f };
float[] LightPosition = { 1.0f, 1.0f, 1.0f, 0.0f }; float[] LightPosition = { 1.0f, 1.0f, 1.0f, 0.0f };
ByteBuffer light_buf = ByteBuffer.allocateDirect(4 * 4); FloatBuffer light_buf_f = BufferUtils.createFloatBuffer(4);
light_buf.order(ByteOrder.nativeOrder());
FloatBuffer light_buf_f = light_buf.asFloatBuffer();
light_buf_f.rewind();
light_buf_f.put(LightDiffuse); light_buf_f.put(LightDiffuse);
light_buf_f.flip();
GL11.glLightfv( GL11.glLightfv(
GL11.GL_LIGHT0, GL11.GL_LIGHT0,
GL11.GL_DIFFUSE, GL11.GL_DIFFUSE,
light_buf_f); light_buf_f);
light_buf_f.rewind(); light_buf_f.clear();
light_buf_f.put(LightPosition); light_buf_f.put(LightPosition);
light_buf_f.flip();
GL11.glLightfv( GL11.glLightfv(
GL11.GL_LIGHT0, GL11.GL_LIGHT0,
GL11.GL_POSITION, GL11.GL_POSITION,
@ -196,23 +193,23 @@ public class Grass {
aslod.count = 0.0f; aslod.count = 0.0f;
while (!finished) { while (!finished) {
if (Window.isCloseRequested()) {
finished = true;
} else if (!Window.isMinimized()) {
keyPoll();
float degree = (1.0f + (aslod.value * 20.0f)) * 0.01745329f;
degree *= (0.5 + myrand());
ptrAnimate(degree);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
//ptrDraw();
grsDraw();
}
Window.update(); Window.update();
keyPoll();
float degree = (1.0f + (aslod.value * 20.0f)) * 0.01745329f;
degree *= (0.5 + myrand());
ptrAnimate(degree);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
//ptrDraw();
grsDraw();
Window.paint();
} }
Mouse.destroy();
Keyboard.destroy();
Window.destroy(); Window.destroy();
} }
@ -448,7 +445,6 @@ public class Grass {
} }
private static void keyPoll() { private static void keyPoll() {
Keyboard.read();
for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) {
Keyboard.next(); Keyboard.next();
if (!Keyboard.getEventKeyState()) if (!Keyboard.getEventKeyState())

View File

@ -118,41 +118,33 @@ public class PbufferTest {
} }
} }
/** /**
* Runs the main loop of the "test" * Runs the main loop of the "test"
*/ */
private void mainLoop() { private void mainLoop() {
while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !Window.isCloseRequested()) {
&& !Window.isCloseRequested()) { if (!Window.isMinimized()) {
// allow subsystem to get a chance to run too // check keyboard input
Window.update(); processKeyboard();
// do "game" logic, and render it
if (!Window.isMinimized()) { logic();
// check keyboard input render();
processKeyboard(); } else {
// no need to render/paint if nothing has changed (ie. window
// do "game" logic, and render it // dragged over)
logic(); if (Window.isDirty()) {
render(); render();
}
// paint window // don't waste cpu time, sleep more
Window.paint(); try {
} else { Thread.sleep(100);
} catch (InterruptedException inte) {
// no need to render/paint if nothing has changed (ie. window dragged over) }
if (Window.isDirty()) { }
render(); // Update window
Window.paint(); Window.update();
} }
}
// don't waste cpu time, sleep more
try {
Thread.sleep(100);
} catch (InterruptedException inte) {
}
}
}
}
/** /**
* Performs the logic * Performs the logic