From 2fb37c78fb3ef89482b20b0521e6f1c63bba36c3 Mon Sep 17 00:00:00 2001 From: Carsten Weisse Date: Sun, 9 Jan 2005 22:36:31 +0000 Subject: the Polygon implementation (FloatBuffer as backbuffer) and the changes to use the interface --- src/jake2/render/fastjogl/Model.java | 5 +- src/jake2/render/fastjogl/Polygon.java | 160 +++++++++++++++++++++++++++++++++ src/jake2/render/fastjogl/Surf.java | 133 +++++++-------------------- src/jake2/render/fastjogl/Warp.java | 63 ++++++------- 4 files changed, 229 insertions(+), 132 deletions(-) create mode 100644 src/jake2/render/fastjogl/Polygon.java (limited to 'src/jake2/render/fastjogl') diff --git a/src/jake2/render/fastjogl/Model.java b/src/jake2/render/fastjogl/Model.java index 080f6dd..a5bcd5b 100644 --- a/src/jake2/render/fastjogl/Model.java +++ b/src/jake2/render/fastjogl/Model.java @@ -2,7 +2,7 @@ * Model.java * Copyright (C) 2003 * - * $Id: Model.java,v 1.5 2004-09-22 19:22:10 salomo Exp $ + * $Id: Model.java,v 1.6 2005-01-09 22:36:31 cawe Exp $ */ /* Copyright (C) 1997-2001 Id Software, Inc. @@ -1089,7 +1089,8 @@ public abstract class Model extends Surf { */ protected void R_BeginRegistration(String model) { resetModelArrays(); - resetPolygonArrays(); +// resetPolygonArrays(); + Polygon.reset(); cvar_t flushmap; diff --git a/src/jake2/render/fastjogl/Polygon.java b/src/jake2/render/fastjogl/Polygon.java new file mode 100644 index 0000000..6ccdda0 --- /dev/null +++ b/src/jake2/render/fastjogl/Polygon.java @@ -0,0 +1,160 @@ +/* + * Polygon.java + * Copyright (C) 2003 + * + * $Id: Polygon.java,v 1.1 2005-01-09 22:36:31 cawe Exp $ + */ +/* +Copyright (C) 1997-2001 Id Software, Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ +package jake2.render.fastjogl; + +import java.nio.FloatBuffer; + +import jake2.Defines; +import jake2.render.glpoly_t; +import jake2.util.Lib; + +/** + * Polygon + * + * @author cwei + */ +public final class Polygon extends glpoly_t { + + private final static int MAX_POLYS = 20000; + private final static int MAX_BUFFER_VERTICES = 120000; + + // backup for s1 scrolling + private static float[] s1_old = new float[MAX_VERTICES]; + + private static FloatBuffer buffer = Lib.newFloatBuffer(MAX_BUFFER_VERTICES * STRIDE); + private static int bufferIndex = 0; + private static int polyCount = 0; + private static Polygon[] polyCache = new Polygon[MAX_POLYS]; + static { + for (int i = 0; i < polyCache.length; i++) { + polyCache[i] = new Polygon(); + } + } + + static glpoly_t create(int numverts) { + Polygon poly = polyCache[polyCount++]; + poly.clear(); + poly.numverts = numverts; + poly.pos = bufferIndex; + bufferIndex += numverts; + return poly; + } + + static void reset() { + polyCount = 0; + bufferIndex = 0; + } + + static FloatBuffer getInterleavedBuffer() { + return (FloatBuffer)buffer.rewind(); + } + + private Polygon() { + } + + private final void clear() { + next = null; + chain = null; + numverts = 0; + flags = 0; + } + + // the interleaved buffer has the format: + // textureCoord0 (index 0, 1) + // vertex (index 2, 3, 4) + // textureCoord1 (index 5, 6) + + public final float x(int index) { + return buffer.get((index + pos) * 7 + 2); + } + + public final void x(int index, float value) { + buffer.put((index + pos) * 7 + 2, value); + } + + public final float y(int index) { + return buffer.get((index + pos) * 7 + 3); + } + + public final void y(int index, float value) { + buffer.put((index + pos) * 7 + 3, value); + } + + public final float z(int index) { + return buffer.get((index + pos) * 7 + 4); + } + + public final void z(int index, float value) { + buffer.put((index + pos) * 7 + 4, value); + } + + public final float s1(int index) { + return buffer.get((index + pos) * 7 + 0); + } + + public final void s1(int index, float value) { + buffer.put((index + pos) * 7 + 0, value); + } + + public final float t1(int index) { + return buffer.get((index + pos) * 7 + 1); + } + + public final void t1(int index, float value) { + buffer.put((index + pos) * 7 + 1, value); + } + + public final float s2(int index) { + return buffer.get((index + pos) * 7 + 5); + } + + public final void s2(int index, float value) { + buffer.put((index + pos) * 7 + 5, value); + } + + public final float t2(int index) { + return buffer.get((index + pos) * 7 + 6); + } + + public final void t2(int index, float value) { + buffer.put((index + pos) * 7 + 6, value); + } + + public final void beginScrolling(float scroll) { + int index = pos * 7; + for (int i = 0; i < numverts; i++, index+=7) { + scroll += s1_old[i] = buffer.get(index); + buffer.put(index, scroll); + } + } + + public final void endScrolling() { + int index = pos * 7; + for (int i = 0; i < numverts; i++, index+=7) { + buffer.put(index, s1_old[i]); + } + } +} \ No newline at end of file diff --git a/src/jake2/render/fastjogl/Surf.java b/src/jake2/render/fastjogl/Surf.java index e15e1d8..752d83f 100644 --- a/src/jake2/render/fastjogl/Surf.java +++ b/src/jake2/render/fastjogl/Surf.java @@ -2,7 +2,7 @@ * Surf.java * Copyright (C) 2003 * - * $Id: Surf.java,v 1.5 2004-09-22 19:22:11 salomo Exp $ + * $Id: Surf.java,v 1.6 2005-01-09 22:36:31 cawe Exp $ */ /* Copyright (C) 1997-2001 Id Software, Inc. @@ -172,22 +172,14 @@ public abstract class Surf extends Draw { * scrolling texture ================ */ void DrawGLFlowingPoly(glpoly_t p) { - int i; - float scroll; - - scroll = -64 + float scroll = -64 * ((r_newrefdef.time / 40.0f) - (int) (r_newrefdef.time / 40.0f)); if (scroll == 0.0f) scroll = -64.0f; - FloatBuffer texCoord = globalPolygonInterleavedBuf; - float[][] v = p.verts; - int index = p.pos * POLYGON_STRIDE; - for (i = 0; i < p.numverts; i++) { - texCoord.put(index, v[i][3] + scroll); - index += POLYGON_STRIDE; - } + p.beginScrolling(scroll); gl.glDrawArrays(GL.GL_POLYGON, p.pos, p.numverts); + p.endScrolling(); } // PGM @@ -197,28 +189,22 @@ public abstract class Surf extends Draw { * * R_DrawTriangleOutlines */ void R_DrawTriangleOutlines() { - int i, j; - glpoly_t p; - - if (gl_showtris.value == 0) + if (gl_showtris.value == 0) return; gl.glDisable(GL.GL_TEXTURE_2D); gl.glDisable(GL.GL_DEPTH_TEST); gl.glColor4f(1, 1, 1, 1); - for (i = 0; i < MAX_LIGHTMAPS; i++) { - msurface_t surf; - - for (surf = gl_lms.lightmap_surfaces[i]; surf != null; surf = surf.lightmapchain) { - p = surf.polys; - for (; p != null; p = p.chain) { - for (j = 2; j < p.numverts; j++) { + for (int i = 0; i < MAX_LIGHTMAPS; i++) { + for (msurface_t surf = gl_lms.lightmap_surfaces[i]; surf != null; surf = surf.lightmapchain) { + for (glpoly_t p = surf.polys; p != null; p = p.chain) { + for (int j = 2; j < p.numverts; j++) { gl.glBegin(GL.GL_LINE_STRIP); - gl.glVertex3fv(p.verts[0]); - gl.glVertex3fv(p.verts[j - 1]); - gl.glVertex3fv(p.verts[j]); - gl.glVertex3fv(p.verts[0]); + gl.glVertex3f(p.x(0), p.y(0), p.z(0)); + gl.glVertex3f(p.x(j-1), p.y(j-1), p.z(j-1)); + gl.glVertex3f(p.x(j), p.y(j), p.z(j)); + gl.glVertex3f(p.x(0), p.y(0), p.z(0)); gl.glEnd(); } } @@ -351,7 +337,7 @@ public abstract class Surf extends Draw { // so scale it back down intens = gl_state.inverse_intensity; - gl.glInterleavedArrays(GL.GL_T2F_V3F, POLYGON_BYTE_STRIDE, + gl.glInterleavedArrays(GL.GL_T2F_V3F, Polygon.BYTE_STRIDE, globalPolygonInterleavedBuf); for (s = r_alpha_surfaces; s != null; s = s.texturechain) { @@ -519,13 +505,9 @@ public abstract class Surf extends Draw { scroll = -64.0f; for (p = surf.polys; p != null; p = p.chain) { - v = p.verts; - index = p.pos * POLYGON_STRIDE; - for (i = 0; i < p.numverts; i++) { - texCoord.put(index, v[i][3] + scroll); - index += POLYGON_STRIDE; - } + p.beginScrolling(scroll); gl.glDrawArrays(GL.GL_POLYGON, p.pos, p.numverts); + p.endScrolling(); } } else { for (p = surf.polys; p != null; p = p.chain) { @@ -551,13 +533,9 @@ public abstract class Surf extends Draw { scroll = -64.0f; for (p = surf.polys; p != null; p = p.chain) { - v = p.verts; - index = p.pos * POLYGON_STRIDE; - for (i = 0; i < p.numverts; i++) { - texCoord.put(index, v[i][3] + scroll); - index += POLYGON_STRIDE; - } + p.beginScrolling(scroll); gl.glDrawArrays(GL.GL_POLYGON, p.pos, p.numverts); + p.endScrolling(); } } else { // PGM @@ -702,11 +680,11 @@ public abstract class Surf extends Draw { GL_EnableMultitexture(true); GL_SelectTexture(GL_TEXTURE0); GL_TexEnv(GL.GL_REPLACE); - gl.glInterleavedArrays(GL.GL_T2F_V3F, POLYGON_BYTE_STRIDE, + gl.glInterleavedArrays(GL.GL_T2F_V3F, Polygon.BYTE_STRIDE, globalPolygonInterleavedBuf); GL_SelectTexture(GL_TEXTURE1); GL_TexEnv(GL.GL_MODULATE); - gl.glTexCoordPointer(2, GL.GL_FLOAT, POLYGON_BYTE_STRIDE, + gl.glTexCoordPointer(2, GL.GL_FLOAT, Polygon.BYTE_STRIDE, globalPolygonTexCoord1Buf); gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY); @@ -875,10 +853,10 @@ public abstract class Surf extends Draw { GL_SelectTexture(GL_TEXTURE0); GL_TexEnv(GL.GL_REPLACE); - gl.glInterleavedArrays(GL.GL_T2F_V3F, POLYGON_BYTE_STRIDE, + gl.glInterleavedArrays(GL.GL_T2F_V3F, Polygon.BYTE_STRIDE, globalPolygonInterleavedBuf); GL_SelectTexture(GL_TEXTURE1); - gl.glTexCoordPointer(2, GL.GL_FLOAT, POLYGON_BYTE_STRIDE, + gl.glTexCoordPointer(2, GL.GL_FLOAT, Polygon.BYTE_STRIDE, globalPolygonTexCoord1Buf); gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY); @@ -1074,7 +1052,6 @@ public abstract class Surf extends Draw { int vertpage; float[] vec; float s, t; - glpoly_t poly; float[] total = { 0, 0, 0 }; // reconstruct the polygon @@ -1086,14 +1063,11 @@ public abstract class Surf extends Draw { // // draw texture // - // poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * - // VERTEXSIZE*sizeof(float)); - poly = new glpoly_t(lnumverts); + glpoly_t poly = Polygon.create(lnumverts); poly.next = fa.polys; poly.flags = fa.flags; fa.polys = poly; - poly.numverts = lnumverts; for (i = 0; i < lnumverts; i++) { lindex = currentmodel.surfedges[fa.firstedge + i]; @@ -1114,9 +1088,11 @@ public abstract class Surf extends Draw { t /= fa.texinfo.image.height; Math3D.VectorAdd(total, vec, total); - Math3D.VectorCopy(vec, poly.verts[i]); - poly.verts[i][3] = s; - poly.verts[i][4] = t; + poly.x(i, vec[0]); + poly.y(i, vec[1]); + poly.z(i, vec[2]); + poly.s1(i, s); + poly.t1(i, t); // // lightmap texture coordinates @@ -1135,14 +1111,9 @@ public abstract class Surf extends Draw { t += 8; t /= BLOCK_HEIGHT * 16; //fa.texinfo.texture.height; - poly.verts[i][5] = s; - poly.verts[i][6] = t; + poly.s2(i, s); + poly.t2(i, t); } - - poly.numverts = lnumverts; - - precompilePolygon(poly); - } /* @@ -1284,55 +1255,19 @@ public abstract class Surf extends Draw { } /* - * new functions for vertex array handling + * new buffers for vertex array handling */ - static final int POLYGON_BUFFER_SIZE = 120000; - - static final int POLYGON_STRIDE = 7; - - static final int POLYGON_BYTE_STRIDE = POLYGON_STRIDE - * BufferUtils.SIZEOF_FLOAT; - - static FloatBuffer globalPolygonInterleavedBuf = BufferUtils - .newFloatBuffer(POLYGON_BUFFER_SIZE * 7); + static FloatBuffer globalPolygonInterleavedBuf = Polygon.getInterleavedBuffer(); static FloatBuffer globalPolygonTexCoord1Buf = null; static { - globalPolygonInterleavedBuf.position(POLYGON_STRIDE - 2); + globalPolygonInterleavedBuf.position(Polygon.STRIDE - 2); globalPolygonTexCoord1Buf = globalPolygonInterleavedBuf.slice(); globalPolygonInterleavedBuf.position(0); }; - void precompilePolygon(glpoly_t p) { - - p.pos = globalPolygonInterleavedBuf.position() / POLYGON_STRIDE; - - float[] v; - FloatBuffer buffer = globalPolygonInterleavedBuf; - - for (int i = 0; i < p.verts.length; i++) { - v = p.verts[i]; - // textureCoord0 - buffer.put(v[3]); - buffer.put(v[4]); - - // vertex - buffer.put(v[0]); - buffer.put(v[1]); - buffer.put(v[2]); - - // textureCoord1 - buffer.put(v[5]); - buffer.put(v[6]); - } - } - - public static void resetPolygonArrays() { - globalPolygonInterleavedBuf.rewind(); - } - - //ImageFrame frame; + //ImageFrame frame; // void debugLightmap(byte[] buf, int w, int h, float scale) { // IntBuffer pix = diff --git a/src/jake2/render/fastjogl/Warp.java b/src/jake2/render/fastjogl/Warp.java index ca4e42e..bfaf6b4 100644 --- a/src/jake2/render/fastjogl/Warp.java +++ b/src/jake2/render/fastjogl/Warp.java @@ -2,7 +2,7 @@ * Warp.java * Copyright (C) 2003 * - * $Id: Warp.java,v 1.4 2004-09-22 19:22:11 salomo Exp $ + * $Id: Warp.java,v 1.5 2005-01-09 22:36:30 cawe Exp $ */ /* Copyright (C) 1997-2001 Id Software, Inc. @@ -200,16 +200,17 @@ public abstract class Warp extends Model { // VERTEXSIZE*sizeof(float)); // init polys - glpoly_t poly = new glpoly_t(numverts + 2); + glpoly_t poly = Polygon.create(numverts + 2); poly.next = warpface.polys; warpface.polys = poly; - poly.numverts = numverts + 2; Math3D.VectorClear(total); total_s = 0; total_t = 0; for (i = 0; i < numverts; i++) { - Math3D.VectorCopy(verts[i], poly.verts[i + 1]); + poly.x(i + 1, verts[i][0]); + poly.y(i + 1, verts[i][1]); + poly.z(i + 1, verts[i][2]); s = Math3D.DotProduct(verts[i], warpface.texinfo.vecs[0]); t = Math3D.DotProduct(verts[i], warpface.texinfo.vecs[1]); @@ -217,19 +218,25 @@ public abstract class Warp extends Model { total_t += t; Math3D.VectorAdd(total, verts[i], total); - poly.verts[i + 1][3] = s; - poly.verts[i + 1][4] = t; + poly.s1(i + 1, s); + poly.t1(i + 1, t); } - - Math3D.VectorScale(total, (1.0f / numverts), poly.verts[0]); - poly.verts[0][3] = total_s / numverts; - poly.verts[0][4] = total_t / numverts; + + float scale = 1.0f / numverts; + poly.x(0, total[0] * scale); + poly.y(0, total[1] * scale); + poly.z(0, total[2] * scale); + poly.s1(0, total_s * scale); + poly.t1(0, total_t * scale); // memcpy (poly.verts[i+1], poly.verts[1], sizeof(poly.verts[0])); - System.arraycopy(poly.verts[1], 0, poly.verts[i + 1], 0, - poly.verts[1].length); // :-) - - precompilePolygon(poly); + poly.x(i + 1, poly.x(1)); + poly.y(i + 1, poly.y(1)); + poly.z(i + 1, poly.z(1)); + poly.s1(i + 1, poly.s1(1)); + poly.t1(i + 1, poly.t1(1)); + poly.s2(i + 1, poly.s2(1)); + poly.t2(i + 1, poly.t2(1)); } /* @@ -282,7 +289,6 @@ public abstract class Warp extends Model { */ void EmitWaterPolys(msurface_t fa) { glpoly_t p, bp; - float[] v; int i; float s = 0; float t = 0; @@ -296,16 +302,13 @@ public abstract class Warp extends Model { else scroll = 0; - int index; - FloatBuffer texCoord = globalPolygonInterleavedBuf; for (bp = fa.polys; bp != null; bp = bp.next) { p = bp; - index = p.pos * POLYGON_STRIDE; + gl.glBegin(GL.GL_TRIANGLE_FAN); for (i = 0; i < p.numverts; i++) { - v = p.verts[i]; - os = v[3]; - ot = v[4]; + os = p.s1(i); + ot = p.t1(i); s = os + Warp.SIN[(int) ((ot * 0.125f + r_newrefdef.time) * TURBSCALE) & 255]; @@ -316,11 +319,10 @@ public abstract class Warp extends Model { + Warp.SIN[(int) ((os * 0.125f + rdt) * TURBSCALE) & 255]; t *= (1.0f / 64); - texCoord.put(index, s); - texCoord.put(index + 1, t); - index += POLYGON_STRIDE; + gl.glTexCoord2f(s, t); + gl.glVertex3f(p.x(i), p.y(i), p.z(i)); } - gl.glDrawArrays(GL.GL_TRIANGLE_FAN, p.pos, p.numverts); + gl.glEnd(); } } @@ -521,13 +523,12 @@ public abstract class Warp extends Model { * ================= R_AddSkySurface ================= */ void R_AddSkySurface(msurface_t fa) { - int i; - glpoly_t p; - // calculate vertex values for sky box - for (p = fa.polys; p != null; p = p.next) { - for (i = 0; i < p.numverts; i++) { - Math3D.VectorSubtract(p.verts[i], r_origin, verts[i]); + for (glpoly_t p = fa.polys; p != null; p = p.next) { + for (int i = 0; i < p.numverts; i++) { + verts[i][0] = p.x(i) - r_origin[0]; + verts[i][1] = p.y(i) - r_origin[1]; + verts[i][2] = p.z(i) - r_origin[2]; } ClipSkyPolygon(p.numverts, verts, 0); } -- cgit v1.2.3