Adds a few missing features into the shader-based renderer relying on JOGL 2.0

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9889 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
jul..om 12 years ago
parent 87e6ea3230
commit 7fdc6bbe46
  1. 305
      engine/src/jogl/com/jme3/renderer/jogl/JoglRenderer.java

@ -65,6 +65,7 @@ import com.jme3.util.IntMap;
import com.jme3.util.IntMap.Entry; import com.jme3.util.IntMap.Entry;
import com.jme3.util.ListMap; import com.jme3.util.ListMap;
import com.jme3.util.NativeObjectManager; import com.jme3.util.NativeObjectManager;
import com.jme3.util.SafeArrayList;
import java.nio.*; import java.nio.*;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
@ -1898,18 +1899,6 @@ public class JoglRenderer implements Renderer {
img.clearUpdateNeeded(); img.clearUpdateNeeded();
} }
//FIXME remove it
private void checkTexturingUsed() {
IDList textureList = context.textureIndexList;
GL gl = GLContext.getCurrentGL();
// old mesh used texturing, new mesh doesn't use it
// should actually go through entire oldLen and
// disable texturing for each unit.. but that's for later.
if (textureList.oldLen > 0 && textureList.newLen == 0) {
gl.glDisable(GL.GL_TEXTURE_2D);
}
}
public void setTexture(int unit, Texture tex) { public void setTexture(int unit, Texture tex) {
GL gl = GLContext.getCurrentGL(); GL gl = GLContext.getCurrentGL();
Image image = tex.getImage(); Image image = tex.getImage();
@ -2175,48 +2164,6 @@ public class JoglRenderer implements Renderer {
} }
} }
//FIXME remove
private int convertArrayType(VertexBuffer.Type type) {
switch (type) {
case Position:
return GLPointerFunc.GL_VERTEX_ARRAY;
case Normal:
return GLPointerFunc.GL_NORMAL_ARRAY;
case TexCoord:
return GLPointerFunc.GL_TEXTURE_COORD_ARRAY;
case Color:
return GLPointerFunc.GL_COLOR_ARRAY;
default:
return -1; // unsupported
}
}
//FIXME remove
private int convertVertexFormat(VertexBuffer.Format fmt) {
switch (fmt) {
case Byte:
return GL.GL_BYTE;
case Double:
return GL2GL3.GL_DOUBLE;
case Float:
return GL.GL_FLOAT;
case Half:
return GL.GL_HALF_FLOAT;
case Int:
return GL2ES2.GL_INT;
case Short:
return GL.GL_SHORT;
case UnsignedByte:
return GL.GL_UNSIGNED_BYTE;
case UnsignedInt:
return GL2ES2.GL_UNSIGNED_INT;
case UnsignedShort:
return GL.GL_UNSIGNED_SHORT;
default:
throw new UnsupportedOperationException("Unrecognized vertex format: " + fmt);
}
}
public void drawTriangleList(VertexBuffer indexBuf, Mesh mesh, int count) { public void drawTriangleList(VertexBuffer indexBuf, Mesh mesh, int count) {
if (indexBuf.getBufferType() != VertexBuffer.Type.Index) { if (indexBuf.getBufferType() != VertexBuffer.Type.Index) {
throw new IllegalArgumentException("Only index buffers are allowed as triangle lists."); throw new IllegalArgumentException("Only index buffers are allowed as triangle lists.");
@ -2325,214 +2272,135 @@ public class JoglRenderer implements Renderer {
} }
} }
private void setVertexAttribVBO(VertexBuffer vb, VertexBuffer idb) { public void updateVertexArray(Mesh mesh) {
int id = mesh.getId();
GL gl = GLContext.getCurrentGL(); GL gl = GLContext.getCurrentGL();
int arrayType = convertArrayType(vb.getBufferType()); if (id == -1) {
if (arrayType == -1) { IntBuffer temp = intBuf1;
return; // unsupported gl.getGL2GL3().glGenVertexArrays(1, temp);
id = temp.get(0);
mesh.setId(id);
} }
if (vb.isUpdateNeeded() && idb == null) { if (context.boundVertexArray != id) {
updateBufferData(vb); gl.getGL2GL3().glBindVertexArray(id);
context.boundVertexArray = id;
} }
int bufId = idb != null ? idb.getId() : vb.getId(); VertexBuffer interleavedData = mesh.getBuffer(Type.InterleavedData);
if (context.boundArrayVBO != bufId) { if (interleavedData != null && interleavedData.isUpdateNeeded()) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufId); updateBufferData(interleavedData);
context.boundArrayVBO = bufId;
} }
gl.getGL2().glEnableClientState(arrayType); for (VertexBuffer vb : mesh.getBufferList().getArray()) {
context.boundAttribs[vb.getBufferType().ordinal()] = vb; if (vb.getBufferType() == Type.InterleavedData
|| vb.getUsage() == Usage.CpuOnly // ignore cpu-only buffers
if (vb.getBufferType() == Type.Normal) { || vb.getBufferType() == Type.Index) {
// normalize if requested continue;
if (vb.isNormalized() && !context.normalizeEnabled) {
gl.glEnable(GLLightingFunc.GL_NORMALIZE);
context.normalizeEnabled = true;
} else if (!vb.isNormalized() && context.normalizeEnabled) {
gl.glDisable(GLLightingFunc.GL_NORMALIZE);
context.normalizeEnabled = false;
} }
}
int comps = vb.getNumComponents();
int type = convertVertexFormat(vb.getFormat());
switch (vb.getBufferType()) { if (vb.getStride() == 0) {
case Position: // not interleaved
gl.getGL2().glVertexPointer(comps, type, vb.getStride(), vb.getOffset()); setVertexAttrib(vb);
break; } else {
case Normal: // interleaved
gl.getGL2().glNormalPointer(type, vb.getStride(), vb.getOffset()); setVertexAttrib(vb, interleavedData);
break; }
case Color:
gl.getGL2().glColorPointer(comps, type, vb.getStride(), vb.getOffset());
break;
case TexCoord:
gl.getGL2().glTexCoordPointer(comps, type, vb.getStride(), vb.getOffset());
break;
} }
} }
private void drawTriangleListVBO(VertexBuffer indexBuf, Mesh mesh, int count) { private void renderMeshVertexArray(Mesh mesh, int lod, int count) {
GL gl = GLContext.getCurrentGL(); if (mesh.getId() == -1) {
if (indexBuf.getBufferType() != VertexBuffer.Type.Index) { updateVertexArray(mesh);
throw new IllegalArgumentException("Only index buffers are allowed as triangle lists."); } else {
} // TODO: Check if it was updated
if (indexBuf.isUpdateNeeded()) {
updateBufferData(indexBuf);
}
int bufId = indexBuf.getId();
assert bufId != -1;
if (context.boundElementArrayVBO != bufId) {
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, bufId);
context.boundElementArrayVBO = bufId;
} }
if (mesh.getMode() == Mode.Hybrid) { if (context.boundVertexArray != mesh.getId()) {
int[] modeStart = mesh.getModeStart(); GL gl = GLContext.getCurrentGL();
int[] elementLengths = mesh.getElementLengths(); gl.getGL2GL3().glBindVertexArray(mesh.getId());
context.boundVertexArray = mesh.getId();
int elMode = convertElementMode(Mode.Triangles);
int fmt = convertVertexFormat(indexBuf.getFormat());
int elSize = indexBuf.getFormat().getComponentSize();
// int listStart = modeStart[0];
int stripStart = modeStart[1];
int fanStart = modeStart[2];
int curOffset = 0;
for (int i = 0; i < elementLengths.length; i++) {
if (i == stripStart) {
elMode = convertElementMode(Mode.TriangleStrip);
} else if (i == fanStart) {
//TriangleStrip?
elMode = convertElementMode(Mode.TriangleFan);
}
int elementLength = elementLengths[i];
gl.glDrawElements(elMode, elementLength, fmt, curOffset);
curOffset += elementLength * elSize;
}
} else {
gl.glDrawElements(convertElementMode(mesh.getMode()), indexBuf.getData().capacity(),
convertVertexFormat(indexBuf.getFormat()), 0);
} }
}
private void renderMeshDefault(Mesh mesh, int lod, int count) { // IntMap<VertexBuffer> buffers = mesh.getBuffers();
VertexBuffer indices/* = null*/; VertexBuffer indices;
VertexBuffer interleavedData = mesh.getBuffer(Type.InterleavedData);
IntMap<VertexBuffer> buffers = mesh.getBuffers();
if (mesh.getNumLodLevels() > 0) { if (mesh.getNumLodLevels() > 0) {
indices = mesh.getLodLevel(lod); indices = mesh.getLodLevel(lod);
} else { } else {
indices = buffers.get(Type.Index.ordinal()); indices = mesh.getBuffer(Type.Index);
} }
for (Entry<VertexBuffer> entry : buffers) {
VertexBuffer vb = entry.getValue();
if (vb.getBufferType() == Type.InterleavedData || vb.getUsage() == Usage.CpuOnly) {
continue;
}
if (vb.getBufferType() == Type.Index) {
indices = vb;
} else {
if (vb.getStride() == 0) {
// not interleaved
setVertexAttrib(vb);
} else {
// interleaved
setVertexAttrib(vb, interleavedData);
}
}
}
if (indices != null) { if (indices != null) {
drawTriangleList(indices, mesh, count); drawTriangleList(indices, mesh, count);
} else { } else {
GL gl = GLContext.getCurrentGL(); drawTriangleArray(mesh.getMode(), count, mesh.getVertexCount());
gl.glDrawArrays(convertElementMode(mesh.getMode()), 0, mesh.getVertexCount());
} }
clearVertexAttribs(); clearVertexAttribs();
clearTextureUnits(); clearTextureUnits();
} }
private void renderMeshVBO(Mesh mesh, int lod, int count) { private void renderMeshDefault(Mesh mesh, int lod, int count) {
GL gl = GLContext.getCurrentGL(); VertexBuffer indices;
VertexBuffer indices/* = null*/;
VertexBuffer interleavedData = mesh.getBuffer(Type.InterleavedData); VertexBuffer interleavedData = mesh.getBuffer(Type.InterleavedData);
if (interleavedData != null && interleavedData.isUpdateNeeded()) { if (interleavedData != null && interleavedData.isUpdateNeeded()) {
updateBufferData(interleavedData); updateBufferData(interleavedData);
} }
IntMap<VertexBuffer> buffers = mesh.getBuffers();
// IntMap<VertexBuffer> buffers = mesh.getBuffers();
SafeArrayList<VertexBuffer> buffersList = mesh.getBufferList();
if (mesh.getNumLodLevels() > 0) { if (mesh.getNumLodLevels() > 0) {
indices = mesh.getLodLevel(lod); indices = mesh.getLodLevel(lod);
} else { } else {
indices = buffers.get(Type.Index.ordinal()); indices = mesh.getBuffer(Type.Index);
} }
for (Entry<VertexBuffer> entry : buffers) {
VertexBuffer vb = entry.getValue();
if (vb.getBufferType() == Type.InterleavedData || vb.getUsage() == Usage.CpuOnly // ignore // for (Entry<VertexBuffer> entry : buffers) {
// cpu-only // VertexBuffer vb = entry.getValue();
// buffers for (VertexBuffer vb : mesh.getBufferList().getArray()) {
if (vb.getBufferType() == Type.InterleavedData
|| vb.getUsage() == Usage.CpuOnly // ignore cpu-only buffers
|| vb.getBufferType() == Type.Index) { || vb.getBufferType() == Type.Index) {
continue; continue;
} }
if (vb.getStride() == 0) { if (vb.getStride() == 0) {
// not interleaved // not interleaved
setVertexAttribVBO(vb, null); setVertexAttrib(vb);
} else { } else {
// interleaved // interleaved
setVertexAttribVBO(vb, interleavedData); setVertexAttrib(vb, interleavedData);
} }
} }
if (indices != null) { if (indices != null) {
drawTriangleListVBO(indices, mesh, count); drawTriangleList(indices, mesh, count);
} else { } else {
gl.glDrawArrays(convertElementMode(mesh.getMode()), 0, mesh.getVertexCount()); drawTriangleArray(mesh.getMode(), count, mesh.getVertexCount());
} }
clearVertexAttribs(); clearVertexAttribs();
clearTextureUnits(); clearTextureUnits();
} }
private void updateDisplayList(Mesh mesh) { public void renderMesh(Mesh mesh, int lod, int count) {
GL gl = GLContext.getCurrentGL(); if (mesh.getVertexCount() == 0) {
if (mesh.getId() != -1) { return;
// delete list first
gl.getGL2().glDeleteLists(mesh.getId(), mesh.getId());
mesh.setId(-1);
} }
// create new display list
// first set state to NULL
applyRenderState(RenderState.NULL);
// disable lighting
setLighting(null);
int id = gl.getGL2().glGenLists(1);
mesh.setId(id);
gl.getGL2().glNewList(id, GL2.GL_COMPILE);
renderMeshDefault(mesh, 0, 1);
gl.getGL2().glEndList();
}
private void renderMeshDisplayList(Mesh mesh) {
GL gl = GLContext.getCurrentGL(); GL gl = GLContext.getCurrentGL();
if (mesh.getId() == -1) { if (context.pointSprite && mesh.getMode() != Mode.Points) {
updateDisplayList(mesh); // XXX: Hack, disable point sprite mode if mesh not in point mode
if (context.boundTextures[0] != null) {
if (context.boundTextureUnit != 0) {
gl.glActiveTexture(GL.GL_TEXTURE0);
context.boundTextureUnit = 0;
}
gl.glDisable(GL2.GL_POINT_SPRITE);
gl.glDisable(GL2.GL_VERTEX_PROGRAM_POINT_SIZE);
context.pointSprite = false;
}
} }
gl.getGL2().glCallList(mesh.getId());
}
public void renderMesh(Mesh mesh, int lod, int count) {
GL gl = GLContext.getCurrentGL();
if (context.pointSize != mesh.getPointSize()) { if (context.pointSize != mesh.getPointSize()) {
gl.getGL2().glPointSize(mesh.getPointSize()); gl.getGL2().glPointSize(mesh.getPointSize());
context.pointSize = mesh.getPointSize(); context.pointSize = mesh.getPointSize();
@ -2542,32 +2410,11 @@ public class JoglRenderer implements Renderer {
context.lineWidth = mesh.getLineWidth(); context.lineWidth = mesh.getLineWidth();
} }
checkTexturingUsed(); statistics.onMeshDrawn(mesh, lod);
// if (GLContext.getCapabilities().GL_ARB_vertex_array_object){
/*if (vbo) {*/ // renderMeshVertexArray(mesh, lod, count);
renderMeshVBO(mesh, lod, count); // }else{
/*} else { renderMeshDefault(mesh, lod, count);
boolean dynamic = false; // }
if (mesh.getNumLodLevels() == 0) {
IntMap<VertexBuffer> bufs = mesh.getBuffers();
for (Entry<VertexBuffer> entry : bufs) {
if (entry.getValue().getUsage() != VertexBuffer.Usage.Static) {
dynamic = true;
break;
}
}
} else {
dynamic = true;
}
if (!dynamic) {
// dealing with a static object, generate display list
renderMeshDisplayList(mesh);
} else {
renderMeshDefault(mesh, lod, count);
}
}*/
} }
} }

Loading…
Cancel
Save