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;
import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
@ -88,4 +89,20 @@ public final class BufferUtils {
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() {
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
return numEvents;
}

View File

@ -39,6 +39,8 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import org.lwjgl.BufferUtils;
/**
* $Id$
*
@ -321,12 +323,12 @@ public final class ARBImaging {
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) {
// 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);
public static void glGetSeparableFilter(int target, int format, int type, Buffer row, Buffer column, Buffer span) {
// 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);
}

View File

@ -33,6 +33,8 @@ package org.lwjgl.opengl;
import java.nio.*;
import org.lwjgl.BufferUtils;
/**
* Simple utility class.
*
@ -40,34 +42,28 @@ import java.nio.*;
* @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
* based on its position
* @return the position of the buffer, in BYTES
* No c'tor
*/
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();
}
static void checkGLError() {
private Util() {}
public static void checkGLError() {
int err = GL11.glGetError();
if (err != GL11.GL_NO_ERROR) {
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);
return int_buffer.get(0);
}

View File

@ -48,7 +48,7 @@ class VBOTracker {
private final StateStack attrib_stack;
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_element_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
if(Window.isFocused() || Window.isDirty()) {
render();
Window.paint();
} else {
// sleeeeeep
pause(100);

View File

@ -29,21 +29,16 @@
* 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 <elias_naur@users.sourceforge.net>
* @version $Revision$
*/
package org.lwjgl.test.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.lwjgl.Display;
import org.lwjgl.DisplayMode;
import org.lwjgl.Sys;
@ -51,26 +46,24 @@ import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.Util;
import org.lwjgl.opengl.Window;
import org.lwjgl.opengl.glu.GLU;
public final class Game {
static {
try {
//find first display mode that allows us 640*480*16
//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) {
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]);
System.out.println("Setting display mode to " + modes[mode]);
Display.setDisplayMode(modes[mode]);
System.out.println("Created display.");
}
@ -78,132 +71,93 @@ public final class Game {
System.err.println("Failed to create display due to " + e);
}
}
static {
try {
Window.create("LWJGL Game Example", 16, 0, 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.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++)
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.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState())
finished = true;
if (Keyboard.getEventKey() == Keyboard.KEY_T && Keyboard.getEventKeyState())
System.out.println("Current time: " + Sys.getTime());
}
}
/**
* All rendering is done in here
*/
private static void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glPushMatrix();
GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);
GL11.glRotatef(angle, 0, 0, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(-50, -50);
GL11.glVertex2i(50, -50);
GL11.glVertex2i(50, 50);
GL11.glVertex2i(-50, 50);
GL11.glEnd();
GL11.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.
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();
}
}
}
static {
try {
Window.create("LWJGL Game Example");
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) {
if (Window.isMinimized()) {
Thread.sleep(200);
} else if (Window.isCloseRequested()) {
finished = true;
} else {
mainLoop();
render();
}
Window.update();
}
} 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;
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++)
if (Mouse.isButtonDown(i)) System.out.println("Button " + i + " down");
for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) {
Keyboard.next();
if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState()) finished = true;
if (Keyboard.getEventKey() == Keyboard.KEY_T && Keyboard.getEventKeyState())
System.out.println("Current time: " + Sys.getTime());
}
}
/**
* All rendering is done in here
*/
private static void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glPushMatrix();
GL11.glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);
GL11.glRotatef(angle, 0, 0, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(-50, -50);
GL11.glVertex2i(50, -50);
GL11.glVertex2i(50, 50);
GL11.glVertex2i(-50, 50);
GL11.glEnd();
GL11.glPopMatrix();
}
/**
* Initialize
*/
private static void init()
throws Exception {
Sys.setTime(0);
Sys.setProcessPriority(Sys.HIGH_PRIORITY);
System.out.println("Timer resolution: " + Sys.getTimerResolution());
System.out.println("Number of texture units: " + Util.glGetInteger(GL13.GL_MAX_TEXTURE_UNITS));
}
/**
* Cleanup
*/
private static void cleanup() {
Window.destroy();
Display.resetDisplayMode();
}
}

View File

@ -49,6 +49,7 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Random;
import org.lwjgl.BufferUtils;
import org.lwjgl.Display;
import org.lwjgl.DisplayMode;
import org.lwjgl.input.Keyboard;
@ -137,21 +138,18 @@ public class Grass {
}
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);
NVVertexProgram.glGenProgramsNV(byte_buf.asIntBuffer());
IntBuffer int_buf = byte_buf.asIntBuffer();
IntBuffer int_buf = BufferUtils.createIntBuffer(1);
NVVertexProgram.glGenProgramsNV(int_buf);
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);
ByteBuffer program_buf = BufferUtils.createByteBuffer(program.length);
program_buf.order(ByteOrder.nativeOrder());
program_buf.rewind();
program_buf.put(program);
program_buf.rewind();
program_buf.flip();
NVVertexProgram.glLoadProgramNV(
NVVertexProgram.GL_VERTEX_PROGRAM_NV,
program_handle,
@ -163,18 +161,17 @@ public class Grass {
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();
FloatBuffer light_buf_f = BufferUtils.createFloatBuffer(4);
light_buf_f.put(LightDiffuse);
light_buf_f.flip();
GL11.glLightfv(
GL11.GL_LIGHT0,
GL11.GL_DIFFUSE,
light_buf_f);
light_buf_f.rewind();
light_buf_f.clear();
light_buf_f.put(LightPosition);
light_buf_f.flip();
GL11.glLightfv(
GL11.GL_LIGHT0,
GL11.GL_POSITION,
@ -196,23 +193,23 @@ public class Grass {
aslod.count = 0.0f;
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();
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();
}
@ -448,7 +445,6 @@ public class Grass {
}
private static void keyPoll() {
Keyboard.read();
for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) {
Keyboard.next();
if (!Keyboard.getEventKeyState())

View File

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