📄 pickintersection.java
字号:
/** Get the normals of the intersected primitive. This will return null if the primitive does not contain normals. @return an array of Point3d's for the primitive that was intersected */ public Vector3f[] getPrimitiveNormals () { if (hasNormals && (primitiveNormals == null)) { primitiveNormals = new Vector3f[primitiveVertexIndices.length]; int[] indices = getPrimitiveNormalIndices(); int vformat = geom.getVertexFormat(); int val; if ((vformat & GeometryArray.BY_REFERENCE) == 0) { for (int i = 0; i < indices.length; i++) { primitiveNormals[i] = new Vector3f(); geom.getNormal(indices[i], primitiveNormals[i]); } } else { if ((vformat & GeometryArray.INTERLEAVED) == 0) { float[] floatNormals = geom.getNormalRefFloat(); if (floatNormals != null) { for (int i = 0; i < indices.length; i++) { val = indices[i] * 3; primitiveNormals[i] = new Vector3f(floatNormals[val],floatNormals[val+1],floatNormals[val+2]); } } else { Vector3f[] normal3f = geom.getNormalRef3f(); for (int i = 0; i < indices.length; i++) { primitiveNormals[i] = new Vector3f(normal3f[indices[i]].x,normal3f[indices[i]].y,normal3f[indices[i]].z); } } } else { float[] floatData = geom.getInterleavedVertices(); int offset = getInterleavedColorOffset(geom); int stride = getInterleavedStride(geom); for (int i = 0; i < indices.length; i++) { val = stride * indices[i]+offset; primitiveNormals[i] = new Vector3f(floatData[val],floatData[val+1],floatData[val+2]); } } } } return primitiveNormals; } /** Get the color indices for the intersected primitive. For a non-indexed primitive, this will be the same as the primitive vertex indices If the geometry array does not contain colors this will return null. @return an array indices */ public int[] getPrimitiveColorIndices () { if (hasColors && (primitiveColorIndices == null)) { if (geometryIsIndexed()) { primitiveColorIndices = new int[primitiveVertexIndices.length]; for (int i = 0; i < primitiveVertexIndices.length; i++) { primitiveColorIndices[i] = iGeom.getColorIndex(primitiveVertexIndices[i]); } } else { primitiveColorIndices = primitiveVertexIndices; } } return primitiveColorIndices; } /** Get the colors of the intersected primitive. This will return null if the primitive does not contain colors. If the geometry was defined using GeometryArray.COLOR_3, the 'w' value of the color will be set to 1.0. @return an array of Point3d's for the primitive that was intersected */ public Color4f[] getPrimitiveColors () { if (hasColors && (primitiveColors == null)) { primitiveColors = new Color4f[primitiveVertexIndices.length]; int[] indices = getPrimitiveColorIndices(); int vformat = geom.getVertexFormat(); if ((vformat & GeometryArray.BY_REFERENCE) == 0) { if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) { for (int i = 0; i < indices.length; i++) { primitiveColors[i] = new Color4f(); geom.getColor(indices[i], primitiveColors[i]); } } else { Color3f color = new Color3f(); for (int i = 0; i < indices.length; i++) { primitiveColors[i] = new Color4f(); geom.getColor(indices[i], color); primitiveColors[i].x = color.x; primitiveColors[i].y = color.y; primitiveColors[i].z = color.z; primitiveColors[i].w = 1.0f; } } } else { if ((vformat & GeometryArray.INTERLEAVED) == 0) { float[] floatData = geom.getColorRefFloat(); // If data was set as float then .. if (floatData == null) { byte[] byteData = geom.getColorRefByte(); if (byteData == null) { Color3f[] c3fData = geom.getColorRef3f(); if (c3fData == null) { Color4f[] c4fData = geom.getColorRef4f(); if (c4fData == null) { Color3b[] c3bData = geom.getColorRef3b(); if (c3bData == null) { Color4b[] c4bData = geom.getColorRef4b(); for (int i = 0; i < indices.length; i++) { primitiveColors[i] = new Color4f(c4bData[indices[i]].x,c4bData[indices[i]].y,c4bData[indices[i]].z,c4bData[indices[i]].w); } } else { for (int i = 0; i < indices.length; i++) { primitiveColors[i] = new Color4f(c3bData[indices[i]].x,c3bData[indices[i]].y,c3bData[indices[i]].z,1.0f); } } } else { for (int i = 0; i < indices.length; i++) { primitiveColors[i] = new Color4f(c4fData[indices[i]].x,c4fData[indices[i]].y,c4fData[indices[i]].z,c4fData[indices[i]].w); } } } else { for (int i = 0; i < indices.length; i++) { primitiveColors[i] = new Color4f(c3fData[indices[i]].x,c3fData[indices[i]].y,c3fData[indices[i]].z,1.0f); } } } else { // Could be color3 or color4 int val; if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) { for (int i = 0; i < indices.length; i++) { val = indices[i] << 2; // for color4f primitiveColors[i] = new Color4f(byteData[val],byteData[val+1],byteData[val+2],byteData[val+3]); } } else { for (int i = 0; i < indices.length; i++) { val = indices[i] * 3; // for color3f primitiveColors[i] = new Color4f(byteData[val],byteData[val+1],byteData[val+2], 1.0f); } } } } else { // Could be color3 or color4 int val; if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) { for (int i = 0; i < indices.length; i++) { val = indices[i] << 2; // for color4f primitiveColors[i] = new Color4f(floatData[val],floatData[val+1],floatData[val+2],floatData[val+3]); } } else { for (int i = 0; i < indices.length; i++) { val = indices[i] * 3; // for color3f primitiveColors[i] = new Color4f(floatData[val],floatData[val+1],floatData[val+2],1.0f); } } } } else { float[] floatData = geom.getInterleavedVertices(); int offset = getInterleavedColorOffset(geom); int stride = getInterleavedStride(geom); for (int i = 0; i < indices.length; i++) { int val = stride * indices[i]+offset; if ((vformat & GeometryArray.COLOR_4) == GeometryArray.COLOR_4) { primitiveColors[i] = new Color4f(floatData[val],floatData[val+1],floatData[val+2],floatData[val+3]); } else { primitiveColors[i] = new Color4f(floatData[val],floatData[val+1],floatData[val+2],1.0f); } } } } } return primitiveColors; } /** Get the texture coordinate indices for the intersected primitive at the specifed index in the specified texture coordinate set. For a non-indexed primitive, this will be the same as the primitive vertex indices If the geometry array does not contain texture coordinates, this will return null. @return an array indices */ public int[] getPrimitiveTexCoordIndices (int index) { if (hasTexCoords && (primitiveTexCoordIndices == null)) { if (geometryIsIndexed()) { primitiveTexCoordIndices = new int[primitiveVertexIndices.length]; for (int i = 0; i < primitiveVertexIndices.length; i++) { primitiveTexCoordIndices[i] = iGeom.getTextureCoordinateIndex(index, primitiveVertexIndices[i]); } } else { primitiveTexCoordIndices = primitiveVertexIndices; } } return primitiveTexCoordIndices; } /** Get the texture coordinates of the intersected primitive at the specifed index in the specified texture coordinate set. null if the primitive does not contain texture coordinates. If the geometry was defined using GeometryArray.TEXTURE_COORDINATE_2, the 'z' value of the texture coordinate will be set to 0.0. @return an array of TexCoord3f's for the primitive that was intersected */ public TexCoord3f[] getPrimitiveTexCoords (int index) { if (primitiveTexCoords == null) { primitiveTexCoords = new TexCoord3f[primitiveVertexIndices.length]; TexCoord2f primitiveTexCoords2DTmp = new TexCoord2f(); int[] indices = getPrimitiveTexCoordIndices(index); int vformat = geom.getVertexFormat(); if ((vformat & GeometryArray.BY_REFERENCE) == 0) { for (int i = 0; i < indices.length; i++) { primitiveTexCoords[i] = new TexCoord3f(); geom.getTextureCoordinate(index, indices[i], primitiveTexCoords2DTmp); primitiveTexCoords[i].set( primitiveTexCoords2DTmp.x, primitiveTexCoords2DTmp.y, 0.0f); } } else { if ((vformat & GeometryArray.INTERLEAVED) == 0) { int val; float[] floatTexCoords = geom.getTexCoordRefFloat(index); if (floatTexCoords != null) { if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { for (int i = 0; i < indices.length; i++) { val = indices[i] << 1; // t2f primitiveTexCoords[i] = new TexCoord3f(floatTexCoords[val], floatTexCoords[val+1], 0.0f); } } else { for (int i = 0; i < indices.length; i++) { val = indices[i] * 3; // t3f primitiveTexCoords[i] = new TexCoord3f(floatTexCoords[val], floatTexCoords[val+1], floatTexCoords[val+2]); } } } else { TexCoord2f[] texCoord2f = geom.getTexCoordRef2f(index); if (texCoord2f != null) { for (int i = 0; i < indices.length; i++) { primitiveTexCoords[i] = new TexCoord3f(texCoord2f[indices[i]].x, texCoord2f[indices[i]].y, 0.0f); } } else { TexCoord3f[] texCoord3f = geom.getTexCoordRef3f(index); for (int i = 0; i < indices.length; i++) { primitiveTexCoords[i] = new TexCoord3f(texCoord3f[indices[i]].x, texCoord3f[indices[i]].y, texCoord3f[indices[i]].z); } } } } else { float[] floatData = geom.getInterleavedVertices(); int stride = getInterleavedStride(geom); int offset; // Get the correct tex coord set if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { offset = index << 1; } else { offset = index * 3; } for (int i = 0; i < indices.length; i++) { int val = stride * indices[i]; if ((vformat & GeometryArray.TEXTURE_COORDINATE_2) == GeometryArray.TEXTURE_COORDINATE_2) { primitiveTexCoords[i] = new TexCoord3f(floatData[val +offset ],floatData[val+1+offset],0.0f); } else { primitiveTexCoords[i] = new TexCoord3f(floatData[val+offset],floatData[val+1+offset],floatData[val+2+offset]); } } } } } return primitiveTexCoords; } /* ================================================================== */ /* Derived Data: Intersection Point */ /* ================================================================== */ /** Returns the coordinates of the intersection point (local coordinates), if available. @return coordinates of the intersection point */ public Point3d getPointCoordinates() { if (pointCoordinates == null) { double[] weights = getInterpWeights(); Point3d[] coords = getPrimitiveCoordinates(); pointCoordinates = new Point3d(); for (int i = 0; i < weights.length; i++) { pointCoordinates.x += weights[i] * coords[i].x; pointCoordinates.y += weights[i] * coords[i].y; pointCoordinates.z += weights[i] * coords[i].z; } } return pointCoordinates; } /** Returns the normal of the intersection point. Returns null if the geometry does not contain normals. @return normal at the intersection point. */ public Vector3f getPointNormal() { if (hasNormals && (pointNormal == null)) { double[] weights = getInterpWeights(); Vector3f[] normals = getPrimitiveNormals(); pointNormal = new Vector3f(); for (int i = 0; i < weights.length; i++) { pointNormal.x += (float) weights[i] * normals[i].x; pointNormal.y += (float) weights[i] * normals[i].y; pointNormal.z += (float) weights[i] * normals[i].z; } } return pointNormal; } /** Returns the color of the intersection point. Returns null if the geometry does not contain colors. If the geometry was defined with GeometryArray.COLOR_3, the 'w' component of the color will initialized to 1.0 @return color at the intersection point. */ public Color4f getPointColor() { if (hasColors && (pointColor == null)) { double[] weights = getInterpWeights(); Color4f[] colors = getPrimitiveColors(); pointColor = new Color4f(); for (int i = 0; i < weights.length; i++) { pointColor.x += (float) weights[i] * colors[i].x; pointColor.y += (float) weights[i] * colors[i].y; pointColor.z += (float) weights[i] * colors[i].z; pointColor.w += (float) weights[i] * colors[i].w; } } return pointColor; } /** Returns the texture coordinate of the intersection point at the specifed index in the specified texture coordinate set. Returns null if the geometry does not contain texture coordinates. If the geometry was defined with GeometryArray.TEXTURE_COORDINATE_3, the 'z' component of the texture coordinate will initialized to 0.0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -