[66] | 1 | package pl.vorg.mowa.core.graphics; |
---|
| 2 | |
---|
| 3 | import javax.media.opengl.GL; |
---|
| 4 | |
---|
| 5 | /** |
---|
| 6 | * An 3d object. Contains information about its vertex points, whether its faces |
---|
| 7 | * are triangles or quads, etc. Enables displaying the object. |
---|
| 8 | * |
---|
| 9 | * @author vorg |
---|
| 10 | */ |
---|
| 11 | public class Geometry { |
---|
| 12 | private VertexStream vertexStream; |
---|
| 13 | private String name; |
---|
| 14 | private PrimitiveType primitiveType = PrimitiveType.None; |
---|
| 15 | private BoundingBox boundingBox = new BoundingBox(); |
---|
| 16 | private static final int TANGENT_ATTRIB_LOCATION = 0; |
---|
| 17 | |
---|
| 18 | // vbo |
---|
| 19 | int[] ids = new int[3]; |
---|
| 20 | boolean vboInit = false; |
---|
| 21 | |
---|
| 22 | public VertexStream getVertexStream() { |
---|
| 23 | return vertexStream; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | public void setVertexStream(VertexStream vertexStream) { |
---|
| 27 | this.vertexStream = vertexStream; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | public String getName() { |
---|
| 31 | return name; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | public void setName(String name) { |
---|
| 35 | this.name = name; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | public PrimitiveType getPrimitiveType() { |
---|
| 39 | return primitiveType; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | public void setPrimitiveType(PrimitiveType type) { |
---|
| 43 | this.primitiveType = type; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public void display(GL gl) { |
---|
| 47 | displayImmediately(gl); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | private void displayImmediately(GL gl) { |
---|
| 51 | if (vertexStream == null) { |
---|
| 52 | return; |
---|
| 53 | } |
---|
| 54 | int[] indices = vertexStream.getIndexBuffer().getBuffer(); |
---|
| 55 | FloatVertexAttrib posAttrib = (FloatVertexAttrib) vertexStream |
---|
| 56 | .getAttribByName("pos"); |
---|
| 57 | float[] positions = (posAttrib != null) ? posAttrib.getBuffer() : null; |
---|
| 58 | FloatVertexAttrib normalAttrib = (FloatVertexAttrib) vertexStream |
---|
| 59 | .getAttribByName("normal"); |
---|
| 60 | float[] normals = (normalAttrib != null) ? normalAttrib.getBuffer() |
---|
| 61 | : null; |
---|
| 62 | FloatVertexAttrib texCoord0Attrib = (FloatVertexAttrib) vertexStream |
---|
| 63 | .getAttribByName("texCoord0"); |
---|
| 64 | float[] texCoords0 = (texCoord0Attrib != null) ? texCoord0Attrib |
---|
| 65 | .getBuffer() : null; |
---|
| 66 | FloatVertexAttrib tangentAttrib = (FloatVertexAttrib) vertexStream |
---|
| 67 | .getAttribByName("tangent"); |
---|
| 68 | float[] tangents = (tangentAttrib != null) ? tangentAttrib.getBuffer() |
---|
| 69 | : null; |
---|
| 70 | |
---|
| 71 | gl.glBegin(primitiveType.toGL()); |
---|
| 72 | for (int i = 0; i < indices.length; i++) { |
---|
| 73 | if (normals != null) { |
---|
| 74 | gl.glNormal3fv(normals, indices[i] * 3); |
---|
| 75 | } |
---|
| 76 | if (texCoords0 != null) { |
---|
| 77 | gl.glTexCoord2fv(texCoords0, indices[i] * 2); |
---|
| 78 | } |
---|
| 79 | if (tangents != null) { |
---|
| 80 | gl.glVertexAttrib3fv(TANGENT_ATTRIB_LOCATION, tangents, |
---|
| 81 | indices[i] * 3); |
---|
| 82 | } |
---|
| 83 | gl.glVertex3fv(positions, indices[i] * 3); |
---|
| 84 | } |
---|
| 85 | gl.glEnd(); |
---|
| 86 | |
---|
| 87 | /* |
---|
| 88 | * //to powinno byc zrobione na VBO albo chociaz VertexArrays ale na |
---|
| 89 | * razie nie sa one supportowane w JOGL dla OSX if (!vboInit) { vboInit |
---|
| 90 | * = true; gl.glGenBuffers( 3, ids, 0); // get names |
---|
| 91 | * |
---|
| 92 | * FloatBuffer posFB = BufferUtil.newFloatBuffer(positions.length); |
---|
| 93 | * FloatBuffer normalFB = BufferUtil.newFloatBuffer(normals.length); |
---|
| 94 | * FloatBuffer texCoord0FB = |
---|
| 95 | * BufferUtil.newFloatBuffer(texCoords0.length); |
---|
| 96 | * |
---|
| 97 | * posFB.put(positions); normalFB.put(normals); |
---|
| 98 | * texCoord0FB.put(texCoords0); |
---|
| 99 | * |
---|
| 100 | * gl.glBindBuffer( GL.GL_ARRAY_BUFFER, ids[0] ); gl.glBufferData( |
---|
| 101 | * GL.GL_ARRAY_BUFFER, positions.length*BufferUtil.SIZEOF_FLOAT, posFB, |
---|
| 102 | * GL.GL_STATIC_DRAW ); gl.glVertexPointer( 3, GL.GL_FLOAT, 0, (Buffer) |
---|
| 103 | * null ); |
---|
| 104 | * |
---|
| 105 | * //gl.glBindBuffer( GL.GL_ARRAY_BUFFER, ids[1] ); //gl.glBufferData( |
---|
| 106 | * GL.GL_ARRAY_BUFFER, normals.length*BufferUtil.SIZEOF_FLOAT, normalFB, |
---|
| 107 | * GL.GL_STATIC_DRAW ); //gl.glNormalPointer( 3, 0, (Buffer) null ); |
---|
| 108 | * |
---|
| 109 | * //gl.glBindBuffer( GL.GL_ARRAY_BUFFER, ids[2] ); //gl.glBufferData( |
---|
| 110 | * GL.GL_ARRAY_BUFFER, texCoords0.length*BufferUtil.SIZEOF_FLOAT, |
---|
| 111 | * texCoord0FB, GL.GL_STATIC_DRAW ); //gl.glTexCoordPointer( 3, |
---|
| 112 | * GL.GL_FLOAT, 0, (Buffer) null ); |
---|
| 113 | * |
---|
| 114 | * } else { gl.glEnableClientState (GL.GL_VERTEX_ARRAY); |
---|
| 115 | * gl.glEnableClientState (GL.GL_NORMAL_ARRAY); gl.glEnableClientState |
---|
| 116 | * (GL.GL_TEXTURE_COORD_ARRAY); |
---|
| 117 | * |
---|
| 118 | * //gl.glBindBufferARB( GL.GL_ARRAY_BUFFER_ARB, ids[0] ); |
---|
| 119 | * //gl.glBindBufferARB( GL.GL_ARRAY_BUFFER_ARB, ids[1] ); |
---|
| 120 | * //gl.glBindBufferARB( GL.GL_ARRAY_BUFFER_ARB, ids[2] ); |
---|
| 121 | * |
---|
| 122 | * gl.glDrawArrays(GL.GL_TRIANGLES, 0, positions.length); // draw |
---|
| 123 | * everything (six vertices) |
---|
| 124 | * |
---|
| 125 | * gl.glDisableClientState (GL.GL_VERTEX_ARRAY); gl.glDisableClientState |
---|
| 126 | * (GL.GL_NORMAL_ARRAY); gl.glDisableClientState |
---|
| 127 | * (GL.GL_TEXTURE_COORD_ARRAY); } |
---|
| 128 | */ |
---|
| 129 | |
---|
| 130 | // if (positions != null) { |
---|
| 131 | // gl.glEnableClientState(GL.GL_VERTEX_ARRAY); |
---|
| 132 | // FloatBuffer buf = |
---|
| 133 | // BufferUtil.newFloatBuffer(positions.length);//FloatBuffer.allocate(positions.length); |
---|
| 134 | // buf.put(positions); |
---|
| 135 | // gl.glVertexPointer(3, GL.GL_FLOAT, 0, buf); |
---|
| 136 | // } |
---|
| 137 | |
---|
| 138 | // if (normals != null) { |
---|
| 139 | // gl.glEnableClientState(GL.GL_NORMAL_ARRAY); |
---|
| 140 | // FloatBuffer buf = FloatBuffer.wrap(normals); |
---|
| 141 | // gl.glNormalPointer(GL.GL_FLOAT, 3, buf); |
---|
| 142 | // } |
---|
| 143 | |
---|
| 144 | // if (texCoords0 != null) { |
---|
| 145 | // gl.glClientActiveTexture(GL.GL_TEXTURE0); |
---|
| 146 | // gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY); |
---|
| 147 | // FloatBuffer buf = FloatBuffer.wrap(texCoords0); |
---|
| 148 | // gl.glTexCoordPointer(2, GL.GL_FLOAT, 2, buf); |
---|
| 149 | // } |
---|
| 150 | |
---|
| 151 | // gl.glDrawElements(primitiveType.toGL(), |
---|
| 152 | // vertexStream.getIndexBuffer().getBuffer().length, GL.GL_INT, |
---|
| 153 | // stream.getIndices()); |
---|
| 154 | |
---|
| 155 | // attrib = stream.getAttrib("TANGENT", TYPE_VEC3); |
---|
| 156 | // if (attrib) { |
---|
| 157 | // glEnableVertexAttribArrayARB(3); |
---|
| 158 | // glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, attrib->getStride(), |
---|
| 159 | // attrib->getBuffer()); |
---|
| 160 | // } |
---|
| 161 | |
---|
| 162 | // //normals |
---|
| 163 | // gl.glDisable(GL.GL_LIGHTING); |
---|
| 164 | // gl.glDisable(GL.GL_TEXTURE_2D); |
---|
| 165 | // gl.glColor3ub((byte)255, (byte)0, (byte)0); |
---|
| 166 | // gl.glBegin(GL.GL_LINES); |
---|
| 167 | // for(int i=0; i<indices.length; i++) { |
---|
| 168 | // gl.glVertex3fv(positions, indices[i]*3); |
---|
| 169 | // if (normals != null) { |
---|
| 170 | // gl.glVertex3f( |
---|
| 171 | // positions[indices[i]*3 ] + normals[indices[i]*3 ]*0.05f, |
---|
| 172 | // positions[indices[i]*3+1] + normals[indices[i]*3+1]*0.05f, |
---|
| 173 | // positions[indices[i]*3+2] + normals[indices[i]*3+2]*0.05f |
---|
| 174 | // ); |
---|
| 175 | // } |
---|
| 176 | // } |
---|
| 177 | // gl.glEnd(); |
---|
| 178 | // gl.glEnable(GL.GL_LIGHTING); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | public BoundingBox getBoundingBox() { |
---|
| 182 | return boundingBox; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | public void updateBoundingBox() { |
---|
| 186 | boundingBox.empty(); |
---|
| 187 | if (vertexStream == null) { |
---|
| 188 | return; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | FloatVertexAttrib posAttrib = (FloatVertexAttrib) vertexStream |
---|
| 192 | .getAttribByName("pos"); |
---|
| 193 | if (posAttrib == null) { |
---|
| 194 | return; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | float[] posBuf = posAttrib.getBuffer(); |
---|
| 198 | if (posBuf == null) { |
---|
| 199 | return; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | for (int i = 0; i < posBuf.length; i += 3) { |
---|
| 203 | boundingBox.add(posBuf[i], posBuf[i + 1], posBuf[i + 2]); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | } |
---|