From 92c70e6feb69b208c6c2fa2d19233050276c0372 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Tue, 26 Jan 2010 20:58:27 +0000 Subject: [PATCH] Finished new API of GLModel --- .../src/processing/android/core/GLModel.java | 876 +++++------------- .../android/core/GLModelParameters.java | 6 - 2 files changed, 252 insertions(+), 630 deletions(-) diff --git a/android/core/src/processing/android/core/GLModel.java b/android/core/src/processing/android/core/GLModel.java index da2c2e578..9c34f83ab 100644 --- a/android/core/src/processing/android/core/GLModel.java +++ b/android/core/src/processing/android/core/GLModel.java @@ -14,12 +14,7 @@ import javax.microedition.khronos.opengles.*; /** * This class holds a 3D model composed of vertices, normals, colors (per vertex) and * texture coordinates (also per vertex). All this data is stored in Vertex Buffer Objects - * (VBO) for fast access. - * This is class is still undergoing development, the API will probably change quickly - * in the following months as features are tested and refined. - * In particular, with the settings of the VBOs in this first implementation (GL.GL_DYNAMIC_DRAW_ARB) - * it is assumed that the coordinates will change often during the lifetime of the model. - * For static models a different VBO setting (GL.GL_STATIC_DRAW_ARB) should be used. + * (VBO) in GPU memory for very fast access. */ public class GLModel implements GLConstants, PConstants { protected PApplet parent; @@ -54,15 +49,12 @@ public class GLModel implements GLConstants, PConstants { protected ArrayList groupBreaks; protected ArrayList groups; - - protected static final int SIZEOF_FLOAT = 4; + protected VertexGroup[] vertGroup; + protected float MAX_POINTSIZE; + protected static final int SIZEOF_FLOAT = 4; - protected float[] specularColor = {1.0f, 1.0f, 1.0f, 1.0f}; - protected float[] emissiveColor = {0.0f, 0.0f, 0.0f, 1.0f}; - protected float[] shininess = {0}; - - + //////////////////////////////////////////////////////////// public GLModel(PApplet parent, int numVert) { @@ -80,8 +72,7 @@ public class GLModel implements GLConstants, PConstants { a3d = (PGraphicsAndroid3D)parent.g; if (a3d.gl instanceof GL11) { gl = (GL11)a3d.gl; - } - else { + } else { throw new RuntimeException("GLModel: OpenGL ES 1.1 required"); } @@ -107,25 +98,9 @@ public class GLModel implements GLConstants, PConstants { updateElement = -1; selectedTexture = 0; - - - - - imageMode = a3d.imageMode; - tintR = a3d.tintR; - - tintR = tintG = tintB = tintA = 1.0f; - shininess[0] = 0.0f; - - pointSize = 1.0f; - lineWidth = 1.0f; - usingPointSprites = false; - blend = false; - blendMode = ADD; - float[] tmp = { 0.0f }; gl.glGetFloatv(GL11.GL_POINT_SIZE_MAX, tmp, 0); - maxPointSize = tmp[0]; + MAX_POINTSIZE = tmp[0]; } @@ -138,6 +113,9 @@ public class GLModel implements GLConstants, PConstants { //////////////////////////////////////////////////////////// + // Textures + + public void selectTexture(int n) { if (updateElement != -1) { throw new RuntimeException("GLModel: cannot select texture between beginUpdate()/endUpdate()"); @@ -145,15 +123,17 @@ public class GLModel implements GLConstants, PConstants { selectedTexture = n; } + public void setTexture(GLTexture tex) { - VertexGroup group; for (int i = 0; i < groups.size(); i++) setGroupTexture(i, tex); } + public GLTexture getTexture() { return getGroupTexture(0); } + /** * Returns the number of textures. * @return int @@ -162,10 +142,12 @@ public class GLModel implements GLConstants, PConstants { return numTextures; } + //////////////////////////////////////////////////////////// // beginUpdate/endUpdate + public void beginUpdate(int element) { if (updateElement != -1) { throw new RuntimeException("GLModel: only one element can be updated at the time"); @@ -207,6 +189,11 @@ public class GLModel implements GLConstants, PConstants { normals.rewind(); } } else if (updateElement == TEXTURES) { + // Check that all the groups have texture assigned. + for (int i = 0; i < groups.size(); i++) + if (((VertexGroup)groups.get(i)).textures[selectedTexture] == null) + throw new RuntimeException("GLModel: texture must be set first in group " + i); + gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, glTexCoordBufferID[selectedTexture]); firstUpdateIdx = numVertices; @@ -224,6 +211,7 @@ public class GLModel implements GLConstants, PConstants { } } + public void endUpdate() { if (updateElement == -1) { throw new RuntimeException("GLModel: call beginUpdate()"); @@ -290,10 +278,12 @@ public class GLModel implements GLConstants, PConstants { updateElement = -1; } + //////////////////////////////////////////////////////////// // SET/GET VERTICES + public PVector getVertex(int idx) { if (updateElement != VERTICES) { throw new RuntimeException("GLModel: update mode is not set to VERTICES"); @@ -306,6 +296,7 @@ public class GLModel implements GLConstants, PConstants { PVector res = new PVector(x, y, z); return res; } + public float[] getVertexArray() { if (updateElement != VERTICES) { @@ -317,6 +308,7 @@ public class GLModel implements GLConstants, PConstants { return res; } + public ArrayList getVertexArrayList() { if (updateElement != VERTICES) { @@ -330,11 +322,13 @@ public class GLModel implements GLConstants, PConstants { return res; } + public void setVertex(int idx, float x, float y) { setVertex(idx, x, y, 0); } + public void setVertex(int idx, float x, float y, float z) { if (updateElement != VERTICES) { throw new RuntimeException("GLModel: update mode is not set to VERTICES"); @@ -348,6 +342,7 @@ public class GLModel implements GLConstants, PConstants { updateVertexArray[3 * idx + 2] = z; } + public void setVertex(float[] data) { if (updateElement != VERTICES) { throw new RuntimeException("GLModel: updadate mode is not set to VERTICES"); @@ -358,6 +353,7 @@ public class GLModel implements GLConstants, PConstants { PApplet.arrayCopy(data, updateVertexArray); } + public void setVertex(ArrayList data) { if (updateElement != VERTICES) { throw new RuntimeException("GLModel: updadate mode is not set to VERTICES"); @@ -374,11 +370,13 @@ public class GLModel implements GLConstants, PConstants { updateVertexArray[3 * i + 2] = vec.z; } } + //////////////////////////////////////////////////////////// // SET/GET COLORS + public int color(float[] rgba) { int r = (int)(rgba[0] * 255); int g = (int)(rgba[1] * 255); @@ -388,6 +386,7 @@ public class GLModel implements GLConstants, PConstants { return a << 24 | r << 16 | g << 8 | b; } + public float[] rgba(int c) { int a = (c >> 24) & 0xFF; int r = (c >> 16) & 0xFF; @@ -403,6 +402,7 @@ public class GLModel implements GLConstants, PConstants { return res; } + public float[] getColor(int idx) { if (updateElement != COLORS) { throw new RuntimeException("GLModel: update mode is not set to COLORS"); @@ -426,6 +426,7 @@ public class GLModel implements GLConstants, PConstants { return res; } + public ArrayList getColorArrayList() { if (updateElement != COLORS) { throw new RuntimeException("GLModel: update mode is not set to COLORS"); @@ -439,8 +440,8 @@ public class GLModel implements GLConstants, PConstants { return res; } - public void setColor(int idx, int c) - { + + public void setColor(int idx, int c) { int a = (c >> 24) & 0xFF; int r = (c >> 16) & 0xFF; int g = (c >> 8) & 0xFF; @@ -449,10 +450,12 @@ public class GLModel implements GLConstants, PConstants { setColor(idx, r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); } + public void setColor(int idx, float r, float g, float b) { setColor(idx, r, g, b, 1.0f); } + public void setColor(int idx, float r, float g, float b, float a) { if (updateElement != COLORS) { throw new RuntimeException("GLModel: update mode is not set to COLORS"); @@ -467,6 +470,7 @@ public class GLModel implements GLConstants, PConstants { updateColorArray[4 * idx + 3] = a; } + public void setColor(float[] data) { if (updateElement != COLORS) { throw new RuntimeException("GLModel: update mode is not set to COLORS"); @@ -477,6 +481,7 @@ public class GLModel implements GLConstants, PConstants { PApplet.arrayCopy(data, updateColorArray); } + public void setColor(ArrayList data) { if (updateElement != COLORS) { throw new RuntimeException("GLModel: update mode is not set to COLORS"); @@ -494,10 +499,12 @@ public class GLModel implements GLConstants, PConstants { updateColorArray[4 * i + 3] = rgba[3]; } } + //////////////////////////////////////////////////////////// - // SET/GET NORMALS + // SET/GET NORMALS + public PVector getNormal(int idx) { if (updateElement != NORMALS) { @@ -511,6 +518,7 @@ public class GLModel implements GLConstants, PConstants { PVector res = new PVector(x, y, z); return res; } + public float[] getNormalArray() { if (updateElement != NORMALS) { @@ -523,6 +531,7 @@ public class GLModel implements GLConstants, PConstants { return res; } + public ArrayList getNormalArrayList() { if (updateElement != NORMALS) { throw new RuntimeException("GLModel: update mode is not set to NORMALS"); @@ -534,12 +543,14 @@ public class GLModel implements GLConstants, PConstants { for (int i = 0; i < numVertices; i++) res.add(getNormal(i)); return res; - } + } + public void setNormal(int idx, float x, float y) { setNormal(idx, x, y, 0); } + public void setNormal(int idx, float x, float y, float z) { if (updateElement != NORMALS) { throw new RuntimeException("GLModel: update mode is not set to NORMALS"); @@ -553,6 +564,7 @@ public class GLModel implements GLConstants, PConstants { updateNormalArray[3 * idx + 2] = z; } + public void setNormal(float[] data) { if (updateElement != NORMALS) { throw new RuntimeException("GLModel: update mode is not set to NORMALS"); @@ -563,6 +575,7 @@ public class GLModel implements GLConstants, PConstants { PApplet.arrayCopy(data, updateNormalArray); } + public void setNormal(ArrayList data) { if (updateElement != NORMALS) { throw new RuntimeException("GLModel: update mode is not set to NORMALS"); @@ -580,6 +593,7 @@ public class GLModel implements GLConstants, PConstants { } } + //////////////////////////////////////////////////////////// // SET/GET TEXTURE COORDINATES @@ -593,6 +607,11 @@ public class GLModel implements GLConstants, PConstants { float s = updateTexCoordArray[2 * idx + 0]; float t = updateTexCoordArray[2 * idx + 1]; + if (a3d.imageMode == IMAGE) { + s *= vertGroup[idx].textures[selectedTexture].width; + t *= vertGroup[idx].textures[selectedTexture].height; + } + PVector res = new PVector(s, t, 0); return res; } @@ -606,6 +625,20 @@ public class GLModel implements GLConstants, PConstants { float[] res = new float[numVertices * 2]; PApplet.arrayCopy(updateTexCoordArray, res); + if (a3d.imageMode == IMAGE) { + float u, v; + for (int i = 0; i < numVertices; i++) { + u = res[2 * i + 0]; + v = res[2 * i + 1]; + + u *= vertGroup[i].textures[selectedTexture].width; + v *= vertGroup[i].textures[selectedTexture].height; + + res[2 * i + 0] = u; + res[2 * i + 1] = v; + } + } + return res; } @@ -631,6 +664,11 @@ public class GLModel implements GLConstants, PConstants { if (idx < firstUpdateIdx) firstUpdateIdx = idx; if (lastUpdateIdx < idx) lastUpdateIdx = idx; + + if (a3d.imageMode == IMAGE) { + u /= vertGroup[idx].textures[selectedTexture].width; + v /= vertGroup[idx].textures[selectedTexture].height; + } updateTexCoordArray[2 * idx + 0] = u; updateTexCoordArray[2 * idx + 1] = v; @@ -643,8 +681,23 @@ public class GLModel implements GLConstants, PConstants { } firstUpdateIdx = 0; - firstUpdateIdx = numVertices; - PApplet.arrayCopy(data, updateTexCoordArray); + firstUpdateIdx = numVertices; + + if (a3d.imageMode == IMAGE) { + float u, v; + for (int i = 0; i < numVertices; i++) { + u = data[2 * i + 0]; + v = data[2 * i + 1]; + + u /= vertGroup[i].textures[selectedTexture].width; + v /= vertGroup[i].textures[selectedTexture].height; + + updateTexCoordArray[2 * i + 0] = u; + updateTexCoordArray[2 * i + 1] = v; + } + } else { + PApplet.arrayCopy(data, updateTexCoordArray); + } } @@ -659,8 +712,14 @@ public class GLModel implements GLConstants, PConstants { PVector vec; for (int i = 0; i < numVertices; i++) { vec = (PVector)data.get(i); - updateTexCoordArray[3 * i + 0] = vec.x; - updateTexCoordArray[3 * i + 1] = vec.y; + + if (a3d.imageMode == IMAGE) { + updateTexCoordArray[2 * i + 0] = vec.x / vertGroup[i].textures[selectedTexture].width; + updateTexCoordArray[2 * i + 1] = vec.y / vertGroup[i].textures[selectedTexture].height; + } else { + updateTexCoordArray[2 * i + 0] = vec.x; + updateTexCoordArray[2 * i + 1] = vec.y; + } } } @@ -719,7 +778,7 @@ public class GLModel implements GLConstants, PConstants { beginUpdate(COLORS); firstUpdateIdx = group.first; lastUpdateIdx = group.last; - for (int i = group.first; i < group.last; i++) { + for (int i = group.first; i <= group.last; i++) { updateColorArray[4 * i + 0] = r; updateColorArray[4 * i + 1] = g; updateColorArray[4 * i + 2] = b; @@ -739,7 +798,7 @@ public class GLModel implements GLConstants, PConstants { beginUpdate(NORMALS); firstUpdateIdx = group.first; lastUpdateIdx = group.last; - for (int i = group.first; i < group.last; i++) { + for (int i = group.first; i <= group.last; i++) { updateNormalArray[3 * i + 0] = x; updateNormalArray[3 * i + 1] = y; updateNormalArray[3 * i + 2] = z; @@ -751,12 +810,18 @@ public class GLModel implements GLConstants, PConstants { protected void initGroups() { groupBreaks = new ArrayList(); groups = new ArrayList(); - groups.add(new VertexGroup(0, numVertices - 1, numTextures)); + VertexGroup group = new VertexGroup(0, numVertices - 1, numTextures); + groups.add(group); + vertGroup = new VertexGroup[numVertices]; + for (int i = 0; i < numVertices; i++) { + vertGroup[i] = group; + } } protected void createGroups() { // Constructing the intervals given the "break-point" vertices. + VertexGroup group; int idx0, idx1; idx0 = 0; Integer idx; @@ -767,18 +832,69 @@ public class GLModel implements GLConstants, PConstants { idx1 = idx.intValue(); if (idx0 <= idx1) { - groups.add(new VertexGroup(idx0, idx1, numTextures)); - idx0 = idx1 + 1; + group = new VertexGroup(idx0, idx1, numTextures); + groups.add(group); + idx0 = idx1 + 1; + for (int n = idx0; n <= idx1; n++) { + vertGroup[n] = group; + } } } idx1 = numVertices - 1; if (idx0 <= idx1) { - groups.add(new VertexGroup(idx0, idx1, numTextures)); + group = new VertexGroup(idx0, idx1, numTextures); + groups.add(group); + for (int n = idx0; n <= idx1; n++) { + vertGroup[n] = group; + } } } + //////////////////////////////////////////////////////////// + + // Parameters + + + public void setDrawMode(int mode) { + pointSprites = false; + if (mode == POINTS) glMode = GL11.GL_POINTS; + else if (mode == POINT_SPRITES) { + glMode = GL11.GL_POINTS; + pointSprites = true; + } + else if (mode == LINES) glMode = GL11.GL_LINES; + else if (mode == LINE_STRIP) glMode = GL11.GL_LINE_STRIP; + else if (mode == LINE_LOOP) glMode = GL11.GL_LINE_LOOP; + else if (mode == TRIANGLES) glMode = GL11.GL_TRIANGLES; + else if (mode == TRIANGLE_FAN) glMode = GL11.GL_TRIANGLE_FAN; + else if (mode == TRIANGLE_STRIP) glMode = GL11.GL_TRIANGLE_STRIP; + else { + throw new RuntimeException("GLModel: Unknown draw mode"); + } + } + + + public GLModelParameters getParameters() { + GLModelParameters res = new GLModelParameters(); + + if (glMode == GL11.GL_POINTS) { + if (pointSprites) res.drawMode = POINT_SPRITES; + else res.drawMode = POINTS; + } + else if (glMode == GL11.GL_LINES) res.drawMode = LINES; + else if (glMode == GL11.GL_LINE_STRIP) res.drawMode = LINE_STRIP; + else if (glMode == GL11.GL_LINE_LOOP) res.drawMode = LINE_LOOP; + else if (glMode == GL11.GL_TRIANGLES) res.drawMode = TRIANGLES; + else if (glMode == GL11.GL_TRIANGLE_FAN) res.drawMode = TRIANGLE_FAN; + else if (glMode == GL11.GL_TRIANGLE_STRIP) res.drawMode = TRIANGLE_STRIP; + + if (glUsage == GL11.GL_STATIC_DRAW) res.updateMode = STATIC; + else if (glUsage == GL11.GL_DYNAMIC_DRAW) res.updateMode = DYNAMIC; + + return res; + } protected void readParameters(GLModelParameters params) { @@ -787,15 +903,6 @@ public class GLModel implements GLConstants, PConstants { else if (params.drawMode == POINT_SPRITES) { glMode = GL11.GL_POINTS; pointSprites = true; - - - usingPointSprites = true; - float[] tmp = { 0.0f }; - gl.glGetFloatv(GL11.GL_POINT_SIZE_MAX, tmp, 0); - maxPointSize = tmp[0]; - pointSize = maxPointSize; - spriteFadeSize = 0.6f * pointSize; - } else if (params.drawMode == LINES) glMode = GL11.GL_LINES; else if (params.drawMode == LINE_STRIP) glMode = GL11.GL_LINE_STRIP; @@ -814,6 +921,12 @@ public class GLModel implements GLConstants, PConstants { } } + + //////////////////////////////////////////////////////////// + + // Buffer handling. + + void initBufferIDs() { glVertexBufferID = new int[1]; glColorBufferID = new int[1]; @@ -823,6 +936,7 @@ public class GLModel implements GLConstants, PConstants { for (int i = 0; i < numTextures; i++) glTexCoordBufferID[i] = 0; } + protected void createVertexBuffer() { // Creating the float buffer to hold vertices as a direct byte buffer. Each vertex has 3 coordinates // and each coordinate takes SIZEOF_FLOAT bytes (one float). @@ -841,6 +955,7 @@ public class GLModel implements GLConstants, PConstants { gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); } + protected void createColorBuffer() { ByteBuffer vbb = ByteBuffer.allocateDirect(numVertices * 4 * SIZEOF_FLOAT); vbb.order(ByteOrder.nativeOrder()); @@ -857,6 +972,7 @@ public class GLModel implements GLConstants, PConstants { gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); } + protected void createTexCoordBuffer() { float[] values = new float[numVertices * 2]; for (int i = 0; i < values.length; i++) values[i] = 0.0f; @@ -878,6 +994,7 @@ public class GLModel implements GLConstants, PConstants { gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); } + protected void createNormalBuffer() { ByteBuffer vbb = ByteBuffer.allocateDirect(numVertices * 3 * SIZEOF_FLOAT); vbb.order(ByteOrder.nativeOrder()); @@ -895,7 +1012,6 @@ public class GLModel implements GLConstants, PConstants { } - protected void deleteVertexBuffer() { if (glVertexBufferID[0] != 0) { gl.glDeleteBuffers(1, glVertexBufferID, 0); @@ -928,532 +1044,102 @@ public class GLModel implements GLConstants, PConstants { } + /////////////////////////////////////////////////////////// + // Rendering methods - - - - - - - - - - - - - - - - - - - - - - - - - - - public void setLineWidth(float w) - { - lineWidth = w; - } - - public void setPointSize(float s) - { - pointSize = s; - } - - public float getMaxPointSize() - { - return maxPointSize; - } - - public void setSpriteFadeSize(float s) - { - spriteFadeSize = s; - } - - /** - * Disables blending. - */ - public void noBlend() - { - blend = false; - } - - /** - * Enables blending and sets the mode. - * @param MODE int - */ - public void setBlendMode(int MODE) - { - blend = true; - blendMode = MODE; - } - - /** - * Set the tint color to the specified gray tone. - * @param gray float - */ - public void setTint(float gray) - { - int c = parent.color(gray); - setTintColor(c); - } - - /** - * Set the tint color to the specified gray tone and alpha value. - * @param gray int - * @param alpha int - */ - public void setTint(int gray, int alpha) - { - int c = parent.color(gray, alpha); - setTintColor(c); - } - - /** - * Set the tint color to the specified rgb color and alpha value. - * @param rgb int - * @param alpha float - */ - public void setTint(int rgb, float alpha) - { - int c = parent.color(rgb, alpha); - setTintColor(c); - } - - /** - * Set the tint color to the specified gray tone and alpha value. - * @param gray float - * @param alpha float - */ - public void setTint(float gray, float alpha) - { - int c = parent.color(gray, alpha); - setTintColor(c); - } - - /** - * Set the tint color to the specified color components. - * @param x int - * @param y int - * @param z int - */ - public void setTint(int x, int y, int z) - { - int c = parent.color(x, y, z); - setTintColor(c); - } - - /** - * Set the tint color to the specified color components. - * @param x float - * @param y float - * @param z float - */ - public void setTint(float x, float y, float z) - { - int c = parent.color(x, y, z); - setTintColor(c); - } - - /** - * Set the tint color to the specified color components and alpha component. - * @param x int - * @param y int - * @param z int - * @param a int - */ - public void setTint(int x, int y, int z, int a) - { - int c = parent.color(x, y, z, a); - setTintColor(c); - } - - /** - * Set the tint color to the specified color components and alpha component. - * @param x float - * @param y float - * @param z float - * @param a float - */ - public void setTint(float x, float y, float z, float a) - { - int c = parent.color(x, y, z, a); - setTintColor(c); - } - - protected void setTintColor(int color) - { - int ir, ig, ib, ia; - - ia = (color >> 24) & 0xff; - ir = (color >> 16) & 0xff; - ig = (color >> 8) & 0xff; - ib = color & 0xff; - - tintA = ia / 255.0f; - tintR = ir / 255.0f; - tintG = ig / 255.0f; - tintB = ib / 255.0f; - } - - /** - * Set the specular color to the specified gray tone. - * @param gray float - */ - public void setReflection(float gray) - { - int c = parent.color(gray); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified gray tone and alpha value. - * @param gray int - * @param alpha int - */ - public void setReflection(int gray, int alpha) - { - int c = parent.color(gray, alpha); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified rgb color and alpha value. - * @param rgb int - * @param alpha float - */ - public void setReflection(int rgb, float alpha) - { - int c = parent.color(rgb, alpha); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified gray tone and alpha value. - * @param gray float - * @param alpha float - */ - public void setReflection(float gray, float alpha) - { - int c = parent.color(gray, alpha); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified color components. - * @param x int - * @param y int - * @param z int - */ - public void setReflection(int x, int y, int z) - { - int c = parent.color(x, y, z); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified color components. - * @param x float - * @param y float - * @param z float - */ - public void setReflection(float x, float y, float z) - { - int c = parent.color(x, y, z); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified color components and alpha component. - * @param x int - * @param y int - * @param z int - * @param a int - */ - public void setReflection(int x, int y, int z, int a) - { - int c = parent.color(x, y, z, a); - setSpecularColor(c); - } - - /** - * Set the specular color to the specified color components and alpha component. - * @param x float - * @param y float - * @param z float - * @param a float - */ - public void setReflection(float x, float y, float z, float a) - { - int c = parent.color(x, y, z, a); - setSpecularColor(c); - } - - protected void setSpecularColor(int color) - { - int ir, ig, ib, ia; - - ia = (color >> 24) & 0xff; - ir = (color >> 16) & 0xff; - ig = (color >> 8) & 0xff; - ib = color & 0xff; - - specularColor[0] = ir / 255.0f; - specularColor[1] = ig / 255.0f; - specularColor[2] = ib / 255.0f; - specularColor[3] = ia / 255.0f; - } - - /** - * Set the emissive color to the specified gray tone. - * @param gray float - */ - public void setEmission(float gray) - { - int c = parent.color(gray); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified gray tone and alpha value. - * @param gray int - * @param alpha int - */ - public void setEmission(int gray, int alpha) - { - int c = parent.color(gray, alpha); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified rgb color and alpha value. - * @param rgb int - * @param alpha float - */ - public void setEmission(int rgb, float alpha) - { - int c = parent.color(rgb, alpha); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified gray tone and alpha value. - * @param gray float - * @param alpha float - */ - public void setEmission(float gray, float alpha) - { - int c = parent.color(gray, alpha); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified color components. - * @param x int - * @param y int - * @param z int - */ - public void setEmission(int x, int y, int z) - { - int c = parent.color(x, y, z); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified color components. - * @param x float - * @param y float - * @param z float - */ - public void setEmission(float x, float y, float z) - { - int c = parent.color(x, y, z); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified color components and alpha component. - * @param x int - * @param y int - * @param z int - * @param a int - */ - public void setEmission(int x, int y, int z, int a) - { - int c = parent.color(x, y, z, a); - setEmissiveColor(c); - } - - /** - * Set the emissive color to the specified color components and alpha component. - * @param x float - * @param y float - * @param z float - * @param a float - */ - public void setEmission(float x, float y, float z, float a) - { - int c = parent.color(x, y, z, a); - setEmissiveColor(c); - } - - protected void setEmissiveColor(int color) - { - int ir, ig, ib, ia; - - ia = (color >> 24) & 0xff; - ir = (color >> 16) & 0xff; - ig = (color >> 8) & 0xff; - ib = color & 0xff; - - emissiveColor[0] = ir / 255.0f; - emissiveColor[1] = ig / 255.0f; - emissiveColor[2] = ib / 255.0f; - emissiveColor[3] = ia / 255.0f; - } - - public void render() - { - render(0, size - 1); - } - - /* - public void render(GLModelEffect effect) - { - render(0, size - 1, effect); - } public void render(int first, int last) { - render(0, size - 1, null); - } - */ - - public void render(int first, int last) - { - gl.glColor4f(tintR, tintG, tintB, tintA); - - gl.glLineWidth(a3d.strokeWeight); - gl.glPointSize(PApplet.min(a3d.strokeWeight, maxPointSize)); - - //if (effect != null) effect.start(); + int texTarget = GL11.GL_TEXTURE_2D; + float pointSize; + + // Setting line width and point size from stroke value. + gl.glLineWidth(a3d.strokeWeight); + pointSize = PApplet.min(a3d.strokeWeight, MAX_POINTSIZE); + gl.glPointSize(pointSize); + gl.glEnableClientState(GL11.GL_NORMAL_ARRAY); + gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, glNormalBufferID[0]); + gl.glNormalPointer(GL11.GL_FLOAT, 0, 0); - - if (normCoordsVBO != null) - { - gl.glEnableClientState(GL11.GL_NORMAL_ARRAY); - gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, normCoordsVBO[0]); - gl.glNormalPointer(GL11.GL_FLOAT, 0, 0); - } - - if (colorsVBO != null) - { - gl.glEnableClientState(GL11.GL_COLOR_ARRAY); - gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorsVBO[0]); - gl.glColorPointer(4, GL11.GL_FLOAT, 0, 0); - } + gl.glEnableClientState(GL11.GL_COLOR_ARRAY); + gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, glColorBufferID[0]); + gl.glColorPointer(4, GL11.GL_FLOAT, 0, 0); + + VertexGroup group; + for (int i = 0; i < groups.size(); i++) { + group = (VertexGroup)groups.get(i); - if (texCoordsVBO != null) - { - gl.glEnable(textures[0].getTextureTarget()); + if (0 < numTextures) { + GLTexture[] textures = group.textures; + texTarget = textures[0].getTextureTarget(); + gl.glEnable(texTarget); + // Binding texture units. + for (int n = 0; n < numTextures; n++) { + gl.glActiveTexture(GL11.GL_TEXTURE0 + n); + gl.glBindTexture(GL11.GL_TEXTURE_2D, textures[n].getTextureID()); + } + if (pointSprites) { + // Texturing with point sprites. + + // This is how will our point sprite's size will be modified by + // distance from the viewer + float quadratic[] = {1.0f, 0.0f, 0.01f, 1}; + ByteBuffer temp = ByteBuffer.allocateDirect(16); + temp.order(ByteOrder.nativeOrder()); + gl.glPointParameterfv(GL11.GL_POINT_DISTANCE_ATTENUATION, (FloatBuffer)temp.asFloatBuffer().put(quadratic).flip()); + + // The alpha of a point is calculated to allow the fading of points + // instead of shrinking them past a defined threshold size. The threshold + // is defined by GL_POINT_FADE_THRESHOLD_SIZE_ARB and is not clamped to + // the minimum and maximum point sizes. + gl.glPointParameterf(GL11.GL_POINT_FADE_THRESHOLD_SIZE, 0.6f * pointSize); + gl.glPointParameterf(GL11.GL_POINT_SIZE_MIN, 1.0f); + gl.glPointParameterf(GL11.GL_POINT_SIZE_MAX, MAX_POINTSIZE); - // Binding texture units. - for (int n = 0; n < numTextures; n++) - { - gl.glActiveTexture(GL11.GL_TEXTURE0 + n); - gl.glBindTexture(GL11.GL_TEXTURE_2D, textures[n].getTextureID()); - } - - if (usingPointSprites) - { - // Texturing with point sprites. - - // This is how will our point sprite's size will be modified by - // distance from the viewer - float quadratic[] = {1.0f, 0.0f, 0.01f, 1}; - ByteBuffer temp = ByteBuffer.allocateDirect(16); - temp.order(ByteOrder.nativeOrder()); - gl.glPointParameterfv(GL11.GL_POINT_DISTANCE_ATTENUATION, (FloatBuffer) temp.asFloatBuffer().put(quadratic).flip()); - - // The alpha of a point is calculated to allow the fading of points - // instead of shrinking them past a defined threshold size. The threshold - // is defined by GL_POINT_FADE_THRESHOLD_SIZE_ARB and is not clamped to - // the minimum and maximum point sizes. - gl.glPointParameterf(GL11.GL_POINT_FADE_THRESHOLD_SIZE, spriteFadeSize); - gl.glPointParameterf(GL11.GL_POINT_SIZE_MIN, 1.0f); - gl.glPointParameterf(GL11.GL_POINT_SIZE_MAX, maxPointSize); + // Specify point sprite texture coordinate replacement mode for each + // texture unit + gl.glTexEnvf(GL11.GL_POINT_SPRITE_OES, GL11.GL_COORD_REPLACE_OES, GL11.GL_TRUE); - // Specify point sprite texture coordinate replacement mode for each - // texture unit - gl.glTexEnvf(GL11.GL_POINT_SPRITE_OES, GL11.GL_COORD_REPLACE_OES, GL11.GL_TRUE); + gl.glEnable(GL11.GL_POINT_SPRITE_OES); + } else { + // Regular texturing. + gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); + for (int n = 0; n < numTextures; n++) { + gl.glClientActiveTexture(GL11.GL_TEXTURE0 + n); + gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, glTexCoordBufferID[n]); + gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); + } + } + } + + gl.glEnableClientState(GL11.GL_VERTEX_ARRAY); + gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, glVertexBufferID[0]); + gl.glVertexPointer(3, GL11.GL_FLOAT, 0, 0); + + // Last transformation: inversion of coordinate to make compatible with Processing's inverted Y axis. + gl.glPushMatrix(); + gl.glScalef(1, -1, 1); + gl.glDrawArrays(glMode, group.first, group.last - group.first + 1); + gl.glPopMatrix(); + } - gl.glEnable(GL11.GL_POINT_SPRITE_OES); - } - else - { - // Regular texturing. - gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); - for (int n = 0; n < numTextures; n++) - { - gl.glClientActiveTexture(GL11.GL_TEXTURE0 + n); - gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, texCoordsVBO[n]); - gl.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0); - } - } - - //if (effect != null) effect.setTextures(textures); - } - - // Drawing the vertices: - gl.glEnableClientState(GL11.GL_VERTEX_ARRAY); - - gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertCoordsVBO[0]); - - gl.glVertexPointer(3, GL11.GL_FLOAT, 0, 0); - - // Last transformation: inversion of coordinate to make comaptible with Processing's inverted Y axis. - gl.glPushMatrix(); - gl.glScalef(1, -1, 1); - gl.glDrawArrays(vertexMode, first, last - first + 1); - gl.glPopMatrix(); - - //if (effect != null) effect.disableVertexAttribs(); - - gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); - gl.glDisableClientState(GL11.GL_VERTEX_ARRAY); - - if (texCoordsVBO != null) - { - if (usingPointSprites) - { + gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0); + gl.glDisableClientState(GL11.GL_VERTEX_ARRAY); + + if (0 < numTextures) { + if (pointSprites) { gl.glDisable(GL11.GL_POINT_SPRITE_OES); - } - else - { + } else { gl.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY); } - gl.glDisable(textures[0].getTextureTarget()); + gl.glDisable(texTarget); } - if (colorsVBO != null) gl.glDisableClientState(GL11.GL_COLOR_ARRAY); - if (normCoordsVBO != null) gl.glDisableClientState(GL11.GL_NORMAL_ARRAY); - // If there was noblending originally - if (!blend0 && blend) gl.glDisable(GL11.GL_BLEND); - // Default blending mode in PGraphicsAndroid3D. - gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); - - //if (effect != null) effect.stop(); + gl.glDisableClientState(GL11.GL_COLOR_ARRAY); + gl.glDisableClientState(GL11.GL_NORMAL_ARRAY); } - - - protected class VertexGroup { VertexGroup(int n0, int n1, int numTex) { @@ -1466,62 +1152,4 @@ public class GLModel implements GLConstants, PConstants { int last; GLTexture[] textures; } - - - - - /* - protected int vertexMode; - protected int vboUsage; - protected int[] vertCoordsVBO = { 0 }; - protected String description; -*/ - - - - protected int[] colorsVBO = null; - protected int[] normCoordsVBO = null; - protected int[] texCoordsVBO = null; - - protected float tintR, tintG, tintB, tintA; - - - protected float pointSize; - protected float maxPointSize; - protected float lineWidth; - protected boolean usingPointSprites; - protected boolean blend; - protected boolean blend0; - protected int blendMode; - protected float spriteFadeSize; - - /* - protected int numAttributes; - protected int[] attribVBO; - protected String[] attribName; - protected int[] attribSize; - protected int curtAttrSize; - */ - - protected int imageMode; - - protected int firstVertIdx, lastVertIdx; - - - public GLTexture[] textures; - - - - - - //public static final int STREAM = 2; - - - - - //protected VertexGroup[] groups; - - - - } diff --git a/android/core/src/processing/android/core/GLModelParameters.java b/android/core/src/processing/android/core/GLModelParameters.java index 104205a45..2cdcdd7ac 100644 --- a/android/core/src/processing/android/core/GLModelParameters.java +++ b/android/core/src/processing/android/core/GLModelParameters.java @@ -4,19 +4,13 @@ public class GLModelParameters implements GLConstants, PConstants { GLModelParameters() { updateMode = STATIC; drawMode= POINTS; - pointSize = 1; - lineWidth = 1; } GLModelParameters(GLModelParameters src) { updateMode = src.updateMode; drawMode= src.drawMode; - pointSize = src.pointSize; - lineWidth = src.lineWidth; } public int updateMode; public int drawMode; - public int pointSize; - public int lineWidth; }