Implemented safe VBO indices (phew)

This commit is contained in:
Elias Naur 2003-08-04 23:00:49 +00:00
parent e6cca3fda8
commit 35a1538f82
24 changed files with 821 additions and 100 deletions

View file

@ -128,25 +128,6 @@ public final class Sys {
setTime(0);
}
/**
* Create a buffer representing an integer index. Use it with functions that in C can take
* both a pointer and an integer argument, like the ARB_vertex_buffer_object extension specifies
* gl*Pointer to do (among others).
*
* Example:
*
* ByteBuffer b = Sys.createIndexBuffer(0);
* gl.glVertexPointer(3, GL.GL_INT, 0, b);
*
* is equivalent to the C call:
*
* glVertexPointer(3, GL.GL_INT, 0, 0);
*
* @param index The index to represent
* @return a ByteBuffer representing the index
*/
public static native ByteBuffer createIndexBuffer(int index);
/**
* Obtains the number of ticks that the hires timer does in a second.
*

View file

@ -87,12 +87,19 @@ public abstract class CoreGL11 implements CoreGL11Constants {
public static native void glCopyTexImage1D(int target, int level, int internalFormat, int x, int y, int width, int border);
public static native void glCopyPixels(int x, int y, int width, int height, int type);
public static void glColorPointer(int size, boolean unsigned, int stride, ByteBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglColorPointer(size, unsigned ? GL_UNSIGNED_BYTE : GL_BYTE, stride, pointer, pointer.position());
}
public static void glColorPointer(int size, int stride, FloatBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglColorPointer(size, GL_FLOAT, stride, pointer, pointer.position() << 2);
}
private static native void nglColorPointer(int size, int type, int stride, Buffer pointer, int pointer_offset);
public static void glColorPointer(int size, int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglColorPointerVBO(size, type, stride, buffer_offset);
}
private static native void nglColorPointerVBO(int size, int type, int stride, int buffer_offset);
public static native void glColorMaterial(int face, int mode);
public static native void glColorMask(boolean red, boolean green, boolean blue, boolean alpha);
public static native void glColor3b(byte red, byte green, byte blue);
@ -118,9 +125,15 @@ public abstract class CoreGL11 implements CoreGL11Constants {
public static native void glEnable(int cap);
public static native void glDisable(int cap);
public static void glEdgeFlagPointer(int stride, ByteBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglEdgeFlagPointer(stride, pointer, pointer.position());
}
private static native void nglEdgeFlagPointer(int stride, Buffer pointer, int pointer_offset);
public static void glEdgeFlagPointer(int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglEdgeFlagPointerVBO(stride, buffer_offset);
}
private static native void nglEdgeFlagPointerVBO(int stride, int buffer_offset);
public static native void glEdgeFlag(boolean flag);
public static void glDrawPixels(int width, int height, int format, int type, ByteBuffer pixels) {
nglDrawPixels(width, height, format, type, pixels, pixels.position());
@ -133,15 +146,23 @@ public abstract class CoreGL11 implements CoreGL11Constants {
}
private static native void nglDrawPixels(int width, int height, int format, int type, Buffer pixels, int pixels_offset);
public static void glDrawElements(int mode, ByteBuffer indices) {
assert VBOTracker.getVBOElementStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglDrawElements(mode, indices.remaining(), GL_UNSIGNED_BYTE, indices, indices.position());
}
public static void glDrawElements(int mode, ShortBuffer indices) {
assert VBOTracker.getVBOElementStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglDrawElements(mode, indices.remaining(), GL_UNSIGNED_SHORT, indices, indices.position() << 1);
}
public static void glDrawElements(int mode, IntBuffer indices) {
assert VBOTracker.getVBOElementStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglDrawElements(mode, indices.remaining(), GL_UNSIGNED_INT, indices, indices.position() << 2);
}
private static native void nglDrawElements(int mode, int count, int type, Buffer indices, int indices_offset);
public static void glDrawElements(int mode, int count, int type, int buffer_offset) {
assert VBOTracker.getVBOElementStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglDrawElementsVBO(mode, count, type, buffer_offset);
}
private static native void nglDrawElementsVBO(int mode, int count, int type, int buffer_offset);
public static native void glDrawBuffer(int mode);
public static native void glDrawArrays(int mode, int first, int count);
public static native void glDepthRange(double zNear, double zFar);
@ -237,18 +258,27 @@ public abstract class CoreGL11 implements CoreGL11Constants {
public static native ByteBuffer glGetPointerv(int pname, int size);
public static native boolean glIsEnabled(int cap);
public static void glInterleavedArrays(int format, int stride, ByteBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglInterleavedArrays(format, stride, pointer, pointer.position());
}
public static void glInterleavedArrays(int format, int stride, ShortBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglInterleavedArrays(format, stride, pointer, pointer.position() << 1);
}
public static void glInterleavedArrays(int format, int stride, IntBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglInterleavedArrays(format, stride, pointer, pointer.position() << 2);
}
public static void glInterleavedArrays(int format, int stride, FloatBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglInterleavedArrays(format, stride, pointer, pointer.position() << 2);
}
private static native void nglInterleavedArrays(int format, int stride, Buffer pointer, int pointer_offset);
public static void glInterleavedArrays(int format, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglInterleavedArraysVBO(format, stride, buffer_offset);
}
private static native void nglInterleavedArraysVBO(int format, int stride, int buffer_offset);
public static native void glInitNames();
public static native void glHint(int target, int mode);
public static void glGetTexParameter(int target, int pname, FloatBuffer params) {
@ -377,15 +407,23 @@ public abstract class CoreGL11 implements CoreGL11Constants {
public static native void glPassThrough(float token);
public static native void glOrtho(double left, double right, double bottom, double top, double zNear, double zFar);
public static void glNormalPointer(int stride, ByteBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglNormalPointer(GL_BYTE, stride, pointer, pointer.position());
}
public static void glNormalPointer(int stride, IntBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglNormalPointer(GL_INT, stride, pointer, pointer.position() << 2);
}
public static void glNormalPointer(int stride, FloatBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglNormalPointer(GL_FLOAT, stride, pointer, pointer.position() << 2);
}
private static native void nglNormalPointer(int type, int stride, Buffer pointer, int pointer_offset);
public static void glNormalPointer(int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglNormalPointerVBO(type, stride, buffer_offset);
}
private static native void nglNormalPointerVBO(int type, int stride, int buffer_offset);
public static native void glNormal3b(byte nx, byte ny, byte nz);
public static native void glNormal3f(float nx, float ny, float nz);
public static native void glNormal3i(int nx, int ny, int nz);
@ -427,18 +465,41 @@ public abstract class CoreGL11 implements CoreGL11Constants {
public static native void glPopName();
public static native void glPushMatrix();
public static native void glPopMatrix();
public static native void glPushClientAttrib(int mask);
public static native void glPopClientAttrib();
public static void glPushClientAttrib(int mask) {
VBOTracker.getClientAttribStack().pushState();
VBOTracker.getClientAttribStack().setState(mask);
if ((mask & GL_CLIENT_VERTEX_ARRAY_BIT) != 0) {
VBOTracker.getVBOArrayStack().pushState();
VBOTracker.getVBOElementStack().pushState();
}
nglPushClientAttrib(mask);
}
private static native void nglPushClientAttrib(int mask);
public static void glPopClientAttrib() {
if ((VBOTracker.getClientAttribStack().popState() & GL_CLIENT_VERTEX_ARRAY_BIT) != 0) {
VBOTracker.getVBOArrayStack().popState();
VBOTracker.getVBOElementStack().popState();
}
nglPopClientAttrib();
}
private static native void nglPopClientAttrib();
public static native void glPushAttrib(int mask);
public static native void glPopAttrib();
public static native void glStencilFunc(int func, int ref, int mask);
public static void glVertexPointer(int size, int stride, FloatBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglVertexPointer(size, GL_FLOAT, stride, pointer, pointer.position() << 2);
}
public static void glVertexPointer(int size, int stride, IntBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglVertexPointer(size, GL_INT, stride, pointer, pointer.position() << 2);
}
private static native void nglVertexPointer(int size, int type, int stride, Buffer pointer, int pointer_offset);
public static void glVertexPointer(int size, int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglVertexPointerVBO(size, type, stride, buffer_offset);
}
private static native void nglVertexPointerVBO(int size, int type, int stride, int buffer_offset);
public static native void glVertex2f(float x, float y);
public static native void glVertex2i(int x, int y);
public static native void glVertex3f(float x, float y, float z);
@ -509,9 +570,15 @@ public abstract class CoreGL11 implements CoreGL11Constants {
}
private static native void nglTexEnviv(int target, int pname, IntBuffer params, int params_offset);
public static void glTexCoordPointer(int size, int stride, FloatBuffer pointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglTexCoordPointer(size, GL_FLOAT, stride, pointer, pointer.position() << 2);
}
private static native void nglTexCoordPointer(int size, int type, int stride, Buffer pointer, int pointer_offset);
public static void glTexCoordPointer(int size, int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglTexCoordPointerVBO(size, type, stride, buffer_offset);
}
private static native void nglTexCoordPointerVBO(int size, int type, int stride, int buffer_offset);
public static native void glTexCoord1f(float s);
public static native void glTexCoord2f(float s, float t);
public static native void glTexCoord3f(float s, float t, float r);

View file

@ -191,15 +191,23 @@ public abstract class CoreGL12 extends CoreGL11 implements CoreGL12Constants {
}
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);
public static void glDrawRangeElements(int mode, int start, int end, ByteBuffer indices) {
assert VBOTracker.getVBOElementStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglDrawRangeElements(mode, start, end, indices.remaining(), GL_UNSIGNED_BYTE, indices, indices.position());
}
public static void glDrawRangeElements(int mode, int start, int end, ShortBuffer indices) {
assert VBOTracker.getVBOElementStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglDrawRangeElements(mode, start, end, indices.remaining(), GL_UNSIGNED_SHORT, indices, indices.position() << 1);
}
public static void glDrawRangeElements(int mode, int start, int end, IntBuffer indices) {
assert VBOTracker.getVBOElementStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglDrawRangeElements(mode, start, end, indices.remaining(), GL_UNSIGNED_INT, indices, indices.position() << 2);
}
private static native void nglDrawRangeElements(int mode, int start, int end, int count, int type, Buffer indices, int indices_offset);
public static void glDrawRangeElements(int mode, int start, int end, int count, int type, int buffer_offset) {
assert VBOTracker.getVBOElementStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglDrawRangeElementsVBO(mode, start, end, count, type, buffer_offset);
}
private static native void nglDrawRangeElementsVBO(int mode, int start, int end, int count, int type, int buffer_offset);
public static void glTexImage3D(int target, int level, int internalFormat, int width, int height, int depth, int border, int format, int type, ByteBuffer pixels) {
nglTexImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixels, pixels.position());
}

View file

@ -46,11 +46,17 @@ import java.nio.Buffer;
* @version $Revision: 1.23 $
*/
public abstract class CoreGL14 extends CoreGL13 implements CoreGL14Constants {
public static native void glFogCoordf (float coord);
public static void glFogCoordPointer (int stride, FloatBuffer data) {
public static native void glFogCoordf(float coord);
public static void glFogCoordPointer(int stride, FloatBuffer data) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglFogCoordPointer(GL_FLOAT, stride, data, data.position() << 2);
}
private static native void nglFogCoordPointer (int type, int stride, Buffer data, int data_offset);
private static native void nglFogCoordPointer(int type, int stride, Buffer data, int data_offset);
public static void glFogCoordPointer(int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglFogCoordPointerVBO(type, stride, buffer_offset);
}
private static native void nglFogCoordPointerVBO(int type, int stride, int buffer_offset);
public static void glMultiDrawArrays(int mode, IntBuffer piFirst, IntBuffer piCount) {
assert piFirst.remaining() == piCount.remaining(): "piFirst.remaining() != piCount.remaining()";
nglMultiDrawArrays(mode, piFirst, piFirst.position(), piCount, piCount.position(), piFirst.remaining());
@ -65,13 +71,20 @@ public abstract class CoreGL14 extends CoreGL13 implements CoreGL14Constants {
public static native void glSecondaryColor3b (byte red, byte green, byte blue);
public static native void glSecondaryColor3f (float red, float green, float blue);
public static native void glSecondaryColor3ub (byte red, byte green, byte blue);
public static void glSecondaryColorPointer (int size, boolean unsigned, int stride, ByteBuffer data) {
public static void glSecondaryColorPointer(int size, boolean unsigned, int stride, ByteBuffer data) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglSecondaryColorPointer(size, unsigned ? GL_UNSIGNED_BYTE : GL_BYTE, stride, data, data.position());
}
public static void glSecondaryColorPointer (int size, int stride, FloatBuffer data) {
public static void glSecondaryColorPointer(int size, int stride, FloatBuffer data) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglSecondaryColorPointer(size, GL_FLOAT, stride, data, data.position() << 2);
}
private static native void nglSecondaryColorPointer (int size, int type, int stride, Buffer data, int data_offset);
public static void glSecondaryColorPointer(int size, int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglSecondaryColorPointerVBO(size, type, stride, buffer_offset);
}
private static native void nglSecondaryColorPointerVBO(int size, int type, int stride, int buffer_offset);
public static native void glBlendFuncSeparate (int sfactorRGB, int dfactorRGB, int sfactorAlpha, int dfactorAlpha);
public static native void glWindowPos2f (float x, float y);
public static native void glWindowPos2i (int x, int y);

View file

@ -1149,19 +1149,27 @@ public abstract class GL extends CoreGL14 implements GLConstants {
private static native void nglVertexAttrib4usvARB(int index, ShortBuffer psV, int psV_offset);
public static void glVertexAttribPointerARB(int index, int size, boolean unsigned, boolean normalized, int stride, ByteBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglVertexAttribPointerARB(index, size, unsigned ? GL_UNSIGNED_BYTE : GL_BYTE, normalized, stride, pPointer, pPointer.position());
}
public static void glVertexAttribPointerARB(int index, int size, boolean unsigned, boolean normalized, int stride, ShortBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglVertexAttribPointerARB(index, size, unsigned ? GL_UNSIGNED_SHORT : GL_SHORT, normalized, stride, pPointer, pPointer.position()<<1);
}
public static void glVertexAttribPointerARB(int index, int size, boolean normalized, int stride, FloatBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglVertexAttribPointerARB(index, size, GL_FLOAT, normalized, stride, pPointer, pPointer.position()<<2);
}
public static void glVertexAttribPointerARB(int index, int size, boolean unsigned, boolean normalized, int stride, IntBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglVertexAttribPointerARB(index, size, unsigned ? GL_UNSIGNED_INT : GL_INT, normalized, stride, pPointer, pPointer.position()<<2);
}
private static native void nglVertexAttribPointerARB(int index, int size, int type, boolean normalized, int stride, Buffer pPointer, int pPointer_offset);
public static void glVertexAttribPointerARB(int index, int size, int type, boolean normalized, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglVertexAttribPointerARBVBO(index, size, type, normalized, stride, buffer_offset);
}
private static native void nglVertexAttribPointerARBVBO(int index, int size, int type, boolean normalized, int stride, int buffer_offset);
public static void glVertexAttribPointerNV(int index, int size, boolean unsigned, int stride, ByteBuffer pPointer) {
nglVertexAttribPointerNV(index, size, unsigned ? GL_UNSIGNED_BYTE : GL_BYTE, stride, pPointer, pPointer.position());
}
@ -1283,18 +1291,27 @@ public abstract class GL extends CoreGL14 implements GLConstants {
private static native void nglWeightivARB(int size, IntBuffer piWeights, int piWeights_offset);
public static void glWeightPointerARB(int size, boolean unsigned, int stride, ByteBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglWeightPointerARB(size, unsigned ? GL_UNSIGNED_BYTE : GL_BYTE, stride, pPointer, pPointer.position());
}
public static void glWeightPointerARB(int size, boolean unsigned, int stride, ShortBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglWeightPointerARB(size, unsigned ? GL_UNSIGNED_SHORT : GL_SHORT, stride, pPointer, pPointer.position()<<1);
}
public static void glWeightPointerARB(int size, int stride, FloatBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglWeightPointerARB(size, GL_FLOAT, stride, pPointer, pPointer.position()<<2);
}
public static void glWeightPointerARB(int size, boolean unsigned, int stride, IntBuffer pPointer) {
assert VBOTracker.getVBOArrayStack().getState() == 0: "Cannot use Buffers when VBO is enabled";
nglWeightPointerARB(size, unsigned ? GL_UNSIGNED_INT : GL_INT, stride, pPointer, pPointer.position()<<2);
}
private static native void nglWeightPointerARB(int size, int type, int stride, Buffer pPointer, int pPointer_offset);
public static void glWeightPointerARB(int size, int type, int stride, int buffer_offset) {
assert VBOTracker.getVBOArrayStack().getState() != 0: "Cannot use int offsets when VBO is disabled";
nglWeightPointerARBVBO(size, type, stride, buffer_offset);
}
private static native void nglWeightPointerARBVBO(int size, int type, int stride, int buffer_offset);
public static void glWeightARB(ShortBuffer psWeights) {
nglWeightsvARB(psWeights.remaining(), psWeights, psWeights.position());
@ -1444,8 +1461,27 @@ public abstract class GL extends CoreGL14 implements GLConstants {
int outZ,
int outW);
public static native void glBindBufferARB(int target, int buffer);
public static void glBindBufferARB(int target, int buffer) {
switch (target) {
case GL_ELEMENT_ARRAY_BUFFER_ARB:
VBOTracker.getVBOElementStack().setState(buffer);
break;
case GL_ARRAY_BUFFER_ARB:
VBOTracker.getVBOArrayStack().setState(buffer);
break;
default: assert false: "Unsupported VBO target " + target;
}
nglBindBufferARB(target, buffer);
}
private static native void nglBindBufferARB(int target, int buffer);
public static void glDeleteBuffersARB(IntBuffer buffers) {
for (int i = buffers.position(); i < buffers.limit(); i++) {
int buffer_handle = buffers.get(i);
if (VBOTracker.getVBOElementStack().getState() == buffer_handle)
VBOTracker.getVBOElementStack().setState(0);
if (VBOTracker.getVBOArrayStack().getState() == buffer_handle)
VBOTracker.getVBOArrayStack().setState(0);
}
nglDeleteBuffersARB(buffers.remaining(), buffers, buffers.position());
}
private static native void nglDeleteBuffersARB(int n, IntBuffer buffers, int buffers_offset);

View file

@ -50,16 +50,19 @@ import org.lwjgl.*;
public class Pbuffer {
public final static int PBUFFER_SUPPORTED = 1;
/** Current Pbuffer */
private static Pbuffer currentBuffer = null;
/** Handle to the native GL rendering context */
private final int handle;
/** Tracks VBO state */
private final VBOTracker vbo_tracker;
static {
System.loadLibrary(Sys.getLibraryName());
}
/** Handle to the native GL rendering context */
protected final int handle;
/** Current Pbuffer */
private static Pbuffer currentBuffer = null;
/**
* Construct an instance of a Pbuffer. If this fails then an Exception will be thrown.
* The buffer is single-buffered.
@ -81,6 +84,7 @@ public class Pbuffer {
*/
public Pbuffer(int width, int height, int bpp, int alpha, int depth, int stencil) throws Exception {
handle = nCreate(width, height, bpp, alpha, depth, stencil);
vbo_tracker = new VBOTracker();
}
/**
@ -88,6 +92,7 @@ public class Pbuffer {
*/
public static void releaseContext() {
currentBuffer = null;
VBOTracker.releaseCurrent();
nReleaseContext();
}
@ -119,6 +124,7 @@ public class Pbuffer {
*/
public void makeCurrent() {
currentBuffer = this;
VBOTracker.setCurrent(vbo_tracker);
nMakeCurrent(handle);
}
@ -150,7 +156,8 @@ public class Pbuffer {
* Destroys the Pbuffer. The buffer must not be current.
*/
public void destroy() {
assert currentBuffer != this : "Pbuffers must not be current when releasing it";
if (currentBuffer == this)
releaseContext();
nDestroy(handle);
}

View file

@ -0,0 +1,64 @@
/*
* 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.opengl;
public class StateStack {
/** Only int state is tracked */
private final int[] state_stack;
private int stack_pos;
public int getState() {
return state_stack[stack_pos];
}
public void setState(int new_state) {
state_stack[stack_pos] = new_state;
}
public void pushState() {
stack_pos++;
state_stack[stack_pos] = state_stack[stack_pos - 1];
}
public int popState() {
int result = state_stack[stack_pos];
stack_pos--;
return result;
}
public StateStack(int stack_size, int initial_value) {
state_stack = new int[stack_size];
stack_pos = 0;
state_stack[stack_pos] = initial_value;
}
}

View file

@ -40,6 +40,7 @@ import java.nio.*;
* @version $Revision$
*/
abstract class Util {
private final static IntBuffer int_buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
/**
* A helper function which is used to get the byte offset in an arbitrary buffer
* based on its position
@ -55,4 +56,9 @@ abstract class Util {
else
return buffer.position();
}
static int getGLInteger(int enum) {
CoreGL11.glGetInteger(enum, int_buffer);
return int_buffer.get(0);
}
}

View file

@ -0,0 +1,69 @@
/*
* 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.opengl;
public class VBOTracker {
private static VBOTracker default_tracker = new VBOTracker();
private static VBOTracker current_tracker = default_tracker;
private final StateStack vbo_array_stack;
private final StateStack vbo_element_stack;
private final StateStack attrib_stack;
public static void setCurrent(VBOTracker tracker) {
current_tracker = tracker;
}
public static void releaseCurrent() {
current_tracker = default_tracker;
}
public VBOTracker() {
int stack_size = Util.getGLInteger(CoreGL11Constants.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);
}
public static StateStack getVBOArrayStack() {
return current_tracker.vbo_array_stack;
}
public static StateStack getVBOElementStack() {
return current_tracker.vbo_element_stack;
}
public static StateStack getClientAttribStack() {
return current_tracker.attrib_stack;
}
}

View file

@ -66,6 +66,9 @@ public final class Window {
/** Fullscreen */
private static boolean fullscreen;
/** Tracks VBO state for the window context */
private static VBOTracker vbo_tracker;
/**
* Construct a Window. Some OSs may not support non-fullscreen windows; in
* which case the window will be fullscreen regardless.
@ -232,8 +235,7 @@ public final class Window {
Window.title = title;
Window.width = Display.getWidth();
Window.height = Display.getHeight();
nCreate(title, x, y, width, height, fullscreen, color, alpha, depth, stencil);
created = true;
createWindow();
}
/**
@ -268,8 +270,7 @@ public final class Window {
Window.stencil = stencil;
Window.fullscreen = false;
Window.title = title;
nCreate(title, x, y, width, height, fullscreen, color, alpha, depth, stencil);
created = true;
createWindow();
}
/**
@ -289,6 +290,11 @@ public final class Window {
int stencil)
throws Exception;
private static void createWindow() throws Exception {
nCreate(title, x, y, width, height, fullscreen, color, alpha, depth, stencil);
created = true;
}
/**
* Destroy the window.
*/

View file

@ -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 <elias_naur@users.sourceforge.net>
* @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.tick();
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();
}
}
}

View file

@ -191,9 +191,8 @@ public final class VBOTest {
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);
ByteBuffer index_buffer = Sys.createIndexBuffer(0);
GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
GL.glVertexPointer(2, 0, index_buffer.asFloatBuffer());
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.