I want to have shadows in my Android App (Java, not NDK), but there is no simple example Source code (in the last 8 days… I found nothing).
Now I’ve found this code, but I didn’t understand the “shadowIndexBuffer”.
All Objects are Boxes, and one Box is flying inside another Box and the shadows are cast to the 6 Walls inside the box.
The “mIndexBuffer” is clear, these are the Index Numbers of the Vertex Coordinates, but what are the “shadowIndex” Buffers.
For me it would be easier if I can find a simple demo with one plate (floor) and one textured box + shadow. But it seems that doesn’t exist.
Here is the Code:
int sz = FACE_COUNT * VERTICES_PER_FACE * FLOATS_PER_VERTEX * FLOAT_SIZE_BYTES;
ByteBuffer buffer = ByteBuffer.allocateDirect(sz);
mVertexBuffer = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
buffer = ByteBuffer.allocateDirect(sz);
mNormalBuffer = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
buffer = ByteBuffer.allocateDirect(sz);
mColorBuffer = buffer.order(ByteOrder.nativeOrder()).asFloatBuffer();
mIndexBuffer = ByteBuffer.allocateDirect(6 * 6);
mIndexBuffer.position(0);
for (int i = 0; i < 6; ++i) {
mIndexBuffer.put((byte) (i * 8 + 0));
mIndexBuffer.put((byte) (i * 8 + 2));
mIndexBuffer.put((byte) (i * 8 + 4));
mIndexBuffer.put((byte) (i * 8 + 2));
mIndexBuffer.put((byte) (i * 8 + 6));
mIndexBuffer.put((byte) (i * 8 + 4));
}
mShadowIndexBuffer = ByteBuffer.allocateDirect(6 * 6 * 6);
mShadowIndexBuffer.position(0);
for (int i = 0; i < 6; ++i) {
mShadowIndexBuffer.put((byte) (i * 8 + 0));
mShadowIndexBuffer.put((byte) (i * 8 + 1));
mShadowIndexBuffer.put((byte) (i * 8 + 2));
mShadowIndexBuffer.put((byte) (i * 8 + 1));
mShadowIndexBuffer.put((byte) (i * 8 + 3));
mShadowIndexBuffer.put((byte) (i * 8 + 2));
mShadowIndexBuffer.put((byte) (i * 8 + 2));
mShadowIndexBuffer.put((byte) (i * 8 + 3));
mShadowIndexBuffer.put((byte) (i * 8 + 6));
mShadowIndexBuffer.put((byte) (i * 8 + 3));
mShadowIndexBuffer.put((byte) (i * 8 + 7));
mShadowIndexBuffer.put((byte) (i * 8 + 6));
mShadowIndexBuffer.put((byte) (i * 8 + 6));
mShadowIndexBuffer.put((byte) (i * 8 + 7));
mShadowIndexBuffer.put((byte) (i * 8 + 4));
mShadowIndexBuffer.put((byte) (i * 8 + 7));
mShadowIndexBuffer.put((byte) (i * 8 + 5));
mShadowIndexBuffer.put((byte) (i * 8 + 4));
mShadowIndexBuffer.put((byte) (i * 8 + 4));
mShadowIndexBuffer.put((byte) (i * 8 + 5));
mShadowIndexBuffer.put((byte) (i * 8 + 0));
mShadowIndexBuffer.put((byte) (i * 8 + 5));
mShadowIndexBuffer.put((byte) (i * 8 + 1));
mShadowIndexBuffer.put((byte) (i * 8 + 0));
}
setNormal(0, 0f, 0f, 1f);
setNormal(1, 0f, 0f, -1f);
setNormal(2, 0f, 1f, 0f);
setNormal(3, 0f, -1f, 0f);
setNormal(4, 1f, 0f, 0f);
setNormal(5, -1f, 0f, 0f);
setSize(1f, 1f, 1f);
setColor(.5f, .5f, .5f);
@Override
public void render(GlslShaderIds ids) {
super.render(ids);
int stride = FLOATS_PER_VERTEX * FLOAT_SIZE_BYTES;
mVertexBuffer.position(0);
GLES20.glVertexAttribPointer(ids.aPosition, 3, GLES20.GL_FLOAT, false, stride, mVertexBuffer);
GLES20.glEnableVertexAttribArray(ids.aPosition);
mNormalBuffer.position(0);
GLES20.glVertexAttribPointer(ids.aNormal, 3, GLES20.GL_FLOAT, false, stride, mNormalBuffer);
GLES20.glEnableVertexAttribArray(ids.aNormal);
mColorBuffer.position(0);
GLES20.glVertexAttribPointer(ids.aColor, 3, GLES20.GL_FLOAT, false, stride, mColorBuffer);
GLES20.glEnableVertexAttribArray(ids.aColor);
GLES20.glUniformMatrix4fv(ids.uModelViewM, 1, false, getModelViewM(), 0);
GLES20.glUniformMatrix4fv(ids.uModelViewProjM, 1, false, getModelViewProjM(), 0);
GLES20.glUniformMatrix4fv(ids.uNormalM, 1, false, getNormalM(), 0);
mIndexBuffer.position(0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6 * 6,GLES20.GL_UNSIGNED_BYTE, mIndexBuffer);
}
@Override
public void renderShadow(GlslShaderIds ids) {
super.renderShadow(ids);
int stride = FLOATS_PER_VERTEX * FLOAT_SIZE_BYTES;
mVertexBuffer.position(0);
GLES20.glVertexAttribPointer(ids.aPosition, 3, GLES20.GL_FLOAT, false,stride, mVertexBuffer);
GLES20.glEnableVertexAttribArray(ids.aPosition);
mNormalBuffer.position(0);
GLES20.glVertexAttribPointer(ids.aNormal, 3, GLES20.GL_FLOAT, false, stride, mNormalBuffer);
GLES20.glEnableVertexAttribArray(ids.aNormal);
GLES20.glUniformMatrix4fv(ids.uModelViewM, 1, false, getModelViewM(), 0);
GLES20.glUniformMatrix4fv(ids.uModelViewProjM, 1, false, getModelViewProjM(), 0);
GLES20.glUniformMatrix4fv(ids.uNormalM, 1, false, getNormalM(), 0);
mShadowIndexBuffer.position(0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6 * 4 * 6,GLES20.GL_UNSIGNED_BYTE, mShadowIndexBuffer);
}