public static void main(String[] args) { Point3D p1 = new Point3D(0.0, 0.0, 0.0); Point3D p2 = new Point3D(4.0, 4.0, 4.0); Line3D l1 = new Line3D(p1, p2); Point3D p3 = new Point3D(0.0, 4.0, 4.0); Point3D p4 = new Point3D(4.0, 0.0, 0.0); Line3D l2 = new Line3D(p3, p4); System.out.println("p1: " + p1); System.out.println("p2: " + p2); System.out.println("l1: " + l1); System.out.println("l1.intersection(p2): " + l1.intersection(p2)); System.out.println("p2.intersection(l1): " + p2.intersection(l1)); System.out.println("l2: " + l2); System.out.println("l2.intersection(l1): " + l2.intersection(l1)); System.out.println("l1.intersection(l2): " + l1.intersection(l2)); System.out.println("Object3D c1 = l1.union(l2)"); Object3D c1 = l1.union(l2); System.out.println("c1: \n" + c1.toString()); System.out.println("p1.union(p4): \n" + p1.union(p4)); System.out.println("c1.intersection(p1.union(p4))"); System.out.println(c1.intersection(p1.union(p4))); System.out.println("c1.union(p3):\n" + c1.union(p3)); }
public void load(DataInputStream dis) throws IOException { dis.readInt(); // name length this.name = dis.readUTF(); dis.readInt(); // map_kdLength this.map_kd = dis.readUTF(); if (parent.hasTexcoords() && map_kd.length() > 0) { parent.loadTexture(map_kd); } this.ka[0] = dis.readFloat(); this.ka[1] = dis.readFloat(); this.ka[2] = dis.readFloat(); this.ka[3] = dis.readFloat(); this.kd[0] = dis.readFloat(); this.kd[1] = dis.readFloat(); this.kd[2] = dis.readFloat(); this.kd[3] = dis.readFloat(); this.ks[0] = dis.readFloat(); this.ks[1] = dis.readFloat(); this.ks[2] = dis.readFloat(); this.ks[3] = dis.readFloat(); this.ns = dis.readFloat(); this.illum = dis.readInt(); this.d = dis.readFloat(); }
// rotate the camera public void rotateCam(Quaternion q) { for (int i = 0; i < solids.size(); i++) { Object3D obj = solids.get(i); obj.rotate(q, new Vector(100.0f, 0.0f, 1500.0f)); } rotateLights(q); }
// draw solids of a scene public void drawScene(String drawType) { PA4.clearPixelBuffer(); for (int i = 0; i < solids.size(); i++) { Object3D obj = solids.get(i); obj.draw(lights, view_vector, drawType); } }
public void process(long ticks, Level level) { super.process(ticks, level); if (shield.getVisibility()) { shield.rotateY((float) ticks * 0.3f); // shield.setScale(1+(float)Math.sin(shield.getZAxis().x)/6f); } }
public void setToLocalObject(LocalObject lo) { super.setToLocalObject(lo); if (lo.isInvincible() && !shield.getVisibility()) { shield.setVisibility(true); } else { if (!lo.isInvincible()) { shield.setVisibility(false); } } }
public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); camera1.look(); fog.draw(gl); switch ((setLights / 10) % 3) { case 0: light4.disable(); light0.enable(); light3.enable(); light0.setPosition(); light3.setPosition(); break; case 1: light0.disable(); light3.enable(); light4.enable(); light3.setPosition(); light4.setPosition(); break; case 2: light3.disable(); light0.enable(); light4.enable(); light0.setPosition(); light4.setPosition(); break; default: break; } setLights++; gl.glColor4f(0.0f, 1.0f, 0.0f, 0.0f); gl.glPushMatrix(); gl.glRotatef(-90.0f - angle, 0.0f, 1.0f, 0.0f); winter.draw(gl); light1.setPosition(); light2.setPosition(); gl.glTranslatef(-7.0f, 20.0f, 0.0f); sphere.draw(gl); gl.glPopMatrix(); gl.glPushMatrix(); particles.draw(gl); gl.glPopMatrix(); }
/** Show texture or not? */ public void flipTexturing() { enableTexture = !enableTexture; Object3D ob = _objects[this._currentObject]; if (enableTexture && !ob.hasTexture()) { // Create a toast notification signifying that there is no texture associated with this object CharSequence text = "Object does not have associated texture"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(mContext, text, duration); toast.show(); } // this.toggleTexturing(); }
public void init(Map<String, String> attributes) { super.init(attributes); mesh = new ArrayList<Triangle>(); for (String attribute : attributes.keySet()) { if (attribute.startsWith("tri")) { String[] triangleValues = attributes.get(attribute).split("\\s+"); if (triangleValues.length != 9) { throw new IllegalArgumentException( "Triangle must have 9 values representing 3 3d points!"); } Point3D[] tri = new Point3D[3]; tri[0] = new Point3D( Double.parseDouble(triangleValues[0]), Double.parseDouble(triangleValues[1]), Double.parseDouble(triangleValues[2])); tri[1] = new Point3D( Double.parseDouble(triangleValues[3]), Double.parseDouble(triangleValues[4]), Double.parseDouble(triangleValues[5])); tri[2] = new Point3D( Double.parseDouble(triangleValues[6]), Double.parseDouble(triangleValues[7]), Double.parseDouble(triangleValues[8])); mesh.add(new Triangle(tri, this.material)); } } }
public void init(GLAutoDrawable drawable) { drawable.setGL(new DebugGL2(drawable.getGL().getGL2())); final GL2 gl = drawable.getGL().getGL2(); // drawable.getGL().getGL2(); gl.glViewport(0, 0, SCREENW, SCREENH); // Clear color buffer with black // gl.glClearColor(1.0f, 0.5f, 1.0f, 1.0f); gl.glClearColor(.0f, .0f, .0f, 1.0f); gl.glClearDepth(1.0f); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glEnable(GL2.GL_DEPTH_TEST); gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); gl.glCreateShader(GL2GL3.GL_VERTEX_SHADER); shader.init(gl); int programName = shader.getID(); gl.glBindAttribLocation(programName, Object3D.VERTEXPOSITION, "inposition"); gl.glBindAttribLocation(programName, Object3D.VERTEXCOLOR, "incolor"); gl.glBindAttribLocation(programName, Object3D.VERTEXNORMAL, "innormal"); gl.glBindAttribLocation(programName, Object3D.VERTEXTEXCOORD0, "intexcoord0"); shader.link(gl); uniformMat = gl.glGetUniformLocation(programName, "mat"); uniformLight = gl.glGetUniformLocation(programName, "lightdir"); gl.glUseProgram(programName); gl.glUniform3f(uniformLight, 0f, 10f, -10f); obj.init(gl, mats, programName); gl.glUseProgram(0); }
/** Sets up texturing for the object */ private void setupTextures(Object3D ob) { // create new texture ids if object has them if (ob.hasTexture()) { // number of textures int[] texIDs = ob.get_texID(); int[] textures = new int[texIDs.length]; _texIDs = new int[texIDs.length]; // texture file ids int[] texFiles = ob.getTexFile(); Log.d("TEXFILES LENGTH: ", texFiles.length + ""); GLES20.glGenTextures(texIDs.length, textures, 0); for (int i = 0; i < texIDs.length; i++) { texIDs[i] = textures[i]; GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]); // parameters GLES20.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); GLES20.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT); InputStream is = mContext.getResources().openRawResource(texFiles[i]); Bitmap bitmap; try { bitmap = BitmapFactory.decodeStream(is); } finally { try { is.close(); } catch (IOException e) { // Ignore. } } // create it GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); bitmap.recycle(); Log.d("ATTACHING TEXTURES: ", "Attached " + i); } } }
static { // static initializer that creates a mesh blueprint and loads the // textures. try { SimpleStream ss = new SimpleStream("data/weapon.jpg"); TextureManager.getInstance().addTexture("weapon", new Texture(ss.getStream())); ss.close(); ss = new SimpleStream("data/snork.md2"); bluePrint = new ClientObject(Loader.loadMD2(ss.getStream(), 0.22f)); // 0.32f bluePrint.translate(0, 4.5f, 0); bluePrint.translateMesh(); bluePrint.getTranslationMatrix().setIdentity(); bluePrint.translate(100000, 100000, 100000); sphereBluePrint = Primitives.getSphere(9, 6); sphereBluePrint.build(); sphereBluePrint.setTexture("shield"); sphereBluePrint.calcTextureWrapSpherical(); ss.close(); } catch (Exception e) { e.printStackTrace(); } }
public void hitByExplosion( CollisionParticipant source, LocalObject obj, DecalManager decal, CollisionEvent ce) { if (shield.getVisibility()) { // If the shield is visible, we are invincible... return; } Event event = new Event(Event.EXPLOSION_HIT, -99, obj.getObjectID(), obj.getClientID()); source.getEventQueue().add(event); }
private void createShieldMesh() { shield = new Object3D(sphereBluePrint); shield.setVisibility(false); shield.setMesh(sphereBluePrint.getMesh()); shield.build(); shield.translate(0, 4.5f, 0); shield.setTransparency(7); shield.setTransparencyMode(Object3D.TRANSPARENCY_MODE_ADD); if (Globals.compiledObjects) { shield.compile(); } addChildToSuper(shield); }
private static void loadObject3D(Object3D object) throws IOException { object.setUserID(readInt()); int animationTracks = readInt(); for (int i = 0; i < animationTracks; ++i) readInt(); // object.addAnimationTrack((AnimationTrack)getObject(readInt())); int userParameterCount = readInt(); for (int i = 0; i < userParameterCount; ++i) { int parameterID = readInt(); int numBytes = readInt(); byte[] parameterBytes = new byte[numBytes]; dis.readFully(parameterBytes); } }
public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); mats.glMatrixMode(GL2.GL_MODELVIEW); mats.glLoadIdentity(); mats.glTranslatef(0f, 0f, -2.0f); // t = t+0.5f; t = 40f; mats.glRotatef(t, 0f, 1f, 0f); mats.glMatrixMode(GL2.GL_PROJECTION); mats.glLoadIdentity(); mats.glFrustumf(-1f, 1f, -1f, 1f, 1f, 100f); mats.update(); gl.glUseProgram(shader.getID()); gl.glUniformMatrix4fv(uniformMat, 3, false, mats.glGetPMvMviMatrixf()); obj.display(gl, mats); gl.glFlush(); gl.glUseProgram(0); }
/** @see Object3D#union(Object3D) */ public Object3D union(Object3D space) { if (this.isContained(space)) return this; if (space.isContained(this)) return space; if (space instanceof Line3D) { /* * Try to avoid construction of an Object3DCombined. * If grade and offset in xz and xy area are matching and both lines * have a common x-range just create a longer Line3D. */ Line3D other = (Line3D) space; if ((other.gradeY == this.gradeY) && (other.gradeZ == this.gradeZ) && (other.offsetY == this.offsetY) && (other.offsetZ == this.offsetZ)) { if ((this.start.x <= other.start.x) && (this.end.x >= other.start.x)) { return new Line3D(this.start, other.end); } if ((this.start.x <= other.end.x) && (this.end.x >= other.end.x)) { return new Line3D(other.start, this.end); } } } return new Object3DCombined(this).union(space); }
// rotate the selected objects public void rotateObject(Quaternion q) { Object3D obj = solids.get(selectedObjIndex); obj.rotate(q, obj.getCenter()); }
static { try { SimpleStream ss = new SimpleStream("data/crate.3ds"); bluePrint = new ClientObject(Loader.load3DS(ss.getStream(), 0.09f)[0]); ss.close(); bluePrint.translate(0, -4.3f, -5); bluePrint.translateMesh(); bluePrint.getTranslationMatrix().setIdentity(); TextureInfo ti = null; if (!Globals.normalMapping) { bluePrint.setTexture("crate"); } else { ti = new TextureInfo(TextureManager.getInstance().getTextureID("crate")); ti.add( TextureManager.getInstance().getTextureID("cratenormals"), TextureInfo.MODE_MODULATE); bluePrint.setTexture(ti); } bluePrint.getMesh().compress(); bluePrint.build(); Object3D child = null; Animation anim = new Animation(6); anim.setClampingMode(Animation.USE_CLAMPING); anim.createSubSequence("explosion"); Loader.setVertexOptimization(false); for (int i = 1; i < 6; i++) { String name = "data/crate" + i + ".3ds"; ss = new SimpleStream(name); Object3D obj = Loader.load3DS(ss.getStream(), 4.4050189f)[0]; ss.close(); obj.translate(0, -5f, -0.6f); obj.translateMesh(); obj.getTranslationMatrix().setIdentity(); if (!Globals.normalMapping) { obj.setTexture("crate"); } else { obj.setTexture(ti); } obj.build(); if (i == 1) { child = obj; child.setMesh(child.getMesh().cloneMesh(true)); } obj.getMesh().compress(); anim.addKeyFrame(obj.getMesh()); } child.setAnimationSequence(anim); Loader.setVertexOptimization(true); childBluePrint = child; /* * for (int i=0; i<child.getMesh().getBoundingBox().length; i++) { * System.out.print(child.getMesh().getBoundingBox()[i]); * System.out.println("="+bluePrint.getMesh().getBoundingBox()[i]); * } */ } catch (Exception e) { e.printStackTrace(); } }
// translate the select objects public void translateObject(int x, int y, int z) { Object3D obj = solids.get(selectedObjIndex); obj.translate(x, y, z); translateView(-x, -y, -z); }
/** * Places the car. The car has to follow the ground. This is done by casting 4 rays (one from each * wheel) to the ground and calculating the resulting rotation angles to let the car follow the * ground as close as possible. * * @param ground the ground * @return if it was possible to place the car or not */ public boolean place(Object3D ground) { SimpleVector dropDown = new SimpleVector(0, 1, 0); /** * To cast the rays, the car will be rotated in horizontal position first, rotated around the * y-axis according to the cars direction and moved 10 units up. */ Matrix rotMat = getRotationMatrix(); rotMat.setIdentity(); setRotationMatrix(rotMat); rotateY(yRot); translate(0, -10, 0); /** Cast the rays... */ float rightFrontHeight = ground.calcMinDistance(rightFront.getTransformedCenter(), dropDown, 4 * 30); float rightRearHeight = ground.calcMinDistance(rightRear.getTransformedCenter(), dropDown, 4 * 30); float leftFrontHeight = ground.calcMinDistance(leftFront.getTransformedCenter(), dropDown, 4 * 30); float leftRearHeight = ground.calcMinDistance(leftRear.getTransformedCenter(), dropDown, 4 * 30); /** Correct the movement we did above. */ translate(0, 10, 0); /** Reset the rotation matrix again. */ rotMat = getRotationMatrix(); rotMat.setIdentity(); setRotationMatrix(rotMat); /** The rays all hit the ground, the car can be placed */ if (rightFrontHeight != Object3D.COLLISION_NONE && rightRearHeight != Object3D.COLLISION_NONE && leftFrontHeight != Object3D.COLLISION_NONE && leftRearHeight != Object3D.COLLISION_NONE) { /** Correct the values (see translation above) */ rightFrontHeight -= 10; rightRearHeight -= 10; leftFrontHeight -= 10; leftRearHeight -= 10; /** * Calculate the angles between the wheels and the ground. This is done four times: for the * front and the rear as well as for left and right. Front/rear and left/right are averaged * afterwards. */ double angleFront = rightFrontHeight - leftFrontHeight; double as = (angleFront / (16d)); angleFront = Math.atan(as); double angleRear = rightRearHeight - leftRearHeight; as = (angleRear / (16d)); angleRear = Math.atan(as); float rot = (float) ((angleFront + angleRear) / 2d); rotateZ(rot); double angleLeft = leftFrontHeight - leftRearHeight; as = (angleLeft / (16d)); angleLeft = Math.atan(as); double angleRight = rightFrontHeight - rightRearHeight; as = (angleRight / (16d)); angleRight = Math.atan(as); rot = (float) ((angleLeft + angleRight) / 2d); rotateX(rot); /** * The car is correctly rotated now. But we still have to adjust the height. We are simply * taking the minimum distance from all wheels to the ground as the new height. */ float down = rightFrontHeight; if (leftFrontHeight < down) { down = leftFrontHeight; } if (rightRearHeight < down) { down = rightRearHeight; } if (leftRearHeight < down) { down = leftRearHeight; } dropDown.scalarMul(down - 4); translate(dropDown); } else { return false; } /** And finally, rotate the car around Y (that's the car's direction) */ rotateY(yRot); return true; }
public Car() { /** * The car's "body" is this instance itself. That may seem a bit strange at first glance, but * it's quite convenient in this case. */ super(Primitives.getBox(8, 0.25f)); rightFront = Primitives.getSphere(5, 4); leftFront = Primitives.getSphere(5, 4); rightRear = Primitives.getSphere(5, 4); leftRear = Primitives.getSphere(5, 4); /** The wheels are parts, i.e. children of the car */ addChild(rightFront); addChild(leftFront); addChild(rightRear); addChild(leftRear); /** Initialize the car and the wheels */ setTexture("car"); rightFront.setTexture("car"); leftFront.setTexture("car"); rightRear.setTexture("car"); leftRear.setTexture("car"); setEnvmapped(Object3D.ENVMAP_ENABLED); rightFront.setEnvmapped(Object3D.ENVMAP_ENABLED); leftFront.setEnvmapped(Object3D.ENVMAP_ENABLED); rightRear.setEnvmapped(Object3D.ENVMAP_ENABLED); leftRear.setEnvmapped(Object3D.ENVMAP_ENABLED); /** We need to offset the wheels a little... */ rightFront.translate(new SimpleVector(-8, 4, 8)); rightRear.translate(new SimpleVector(-8, 4, -8)); leftFront.translate(new SimpleVector(8, 4, 8)); leftRear.translate(new SimpleVector(8, 4, -8)); rightFront.translateMesh(); rightRear.translateMesh(); leftFront.translateMesh(); leftRear.translateMesh(); rightFront.setTranslationMatrix(new Matrix()); rightRear.setTranslationMatrix(new Matrix()); leftFront.setTranslationMatrix(new Matrix()); leftRear.setTranslationMatrix(new Matrix()); /** ...the wheels are now in place. We can now build the car. */ build(); rightRear.build(); rightFront.build(); leftRear.build(); leftFront.build(); }
private void drawCar(int program, float[] startPos) { Matrix.setIdentityM(mMMatrix, 0); Matrix.setIdentityM(mTransMatrix, 0); // Képernyõ Y tengely if (Math.abs(mDY) < 2) mDY = 0; if (Math.abs(mDY) <= 250) { accelY = -1 * mDY * 2; } else { if (mDY < 0) mDY = -250f; if (mDY > 0) mDY = 250f; } distanceY += accelY; Log.d("mDY:", String.valueOf(mDY)); // Képernyõ X tengely if (Math.abs(mDX) < 2) mDX = 0; float[] forward = {(float) Math.cos(mDX / 50), 0.0f, (float) Math.sin(mDX / 50)}; // lookAt lookAt[0] = eyePos[0] + 25 * forward[0]; lookAt[1] = eyePos[1] + 25 * forward[1]; lookAt[2] = eyePos[2] + 25 * forward[2]; // eyePos eyePos[0] = 0.0f + distanceY / 1000 * forward[0]; eyePos[2] = 25.0f + distanceY / 1000 * forward[2]; Matrix.setLookAtM( mVMatrix, 0, eyePos[0], eyePos[1], eyePos[2], lookAt[0], lookAt[1], lookAt[2], 0.0f, 1.0f, 0.0f); // Kormányzás startPos[0] = eyePos[0] + 5 * forward[0]; startPos[2] = eyePos[2] + 5 * forward[2]; Matrix.translateM(mTransMatrix, 0, startPos[0], startPos[1], startPos[2]); Matrix.multiplyMM(mMMatrix, 0, mMMatrix, 0, mTransMatrix, 0); // Translate Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0); // View Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); // Proj // send to the shader GLES20.glUniformMatrix4fv( GLES20.glGetUniformLocation(program, "uMVPMatrix"), 1, false, mMVPMatrix, 0); // Create the normal modelview matrix // Invert + transpose of mvpmatrix Matrix.invertM(normalMatrix, 0, mMVPMatrix, 0); Matrix.transposeM(normalMatrix, 0, normalMatrix, 0); // send to the shader GLES20.glUniformMatrix4fv( GLES20.glGetUniformLocation(program, "normalMatrix"), 1, false, mMVPMatrix, 0); /** * DRAWING OBJECT * */ // Get buffers from mesh Object3D ob = this._objects[this.CUBE]; Mesh mesh = ob.getMesh(); FloatBuffer _vb = mesh.get_vb(); ShortBuffer _ib = mesh.get_ib(); short[] _indices = mesh.get_indices(); // Vertex buffer // the vertex coordinates _vb.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); GLES20.glVertexAttribPointer( GLES20.glGetAttribLocation(program, "aPosition"), 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb); GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aPosition")); // the normal info _vb.position(TRIANGLE_VERTICES_DATA_NOR_OFFSET); GLES20.glVertexAttribPointer( GLES20.glGetAttribLocation(program, "aNormal"), 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb); GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aNormal")); // Texture info // bind textures if (ob.hasTexture()) { // && enableTexture) { // number of textures int[] texIDs = ob.get_texID(); for (int i = 0; i < _texIDs.length; i++) { GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); // Log.d("TEXTURE BIND: ", i + " " + texIDs[i]); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]); GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "texture" + (i + 1)), i); } } // enable texturing? [fix - sending float is waste] GLES20.glUniform1f( GLES20.glGetUniformLocation(program, "hasTexture") /*shader.hasTextureHandle*/, ob.hasTexture() && enableTexture ? 2.0f : 0.0f); // texture coordinates _vb.position(TRIANGLE_VERTICES_DATA_TEX_OFFSET); GLES20.glVertexAttribPointer( GLES20.glGetAttribLocation(program, "textureCoord") /*shader.maTextureHandle*/, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb); GLES20.glEnableVertexAttribArray( GLES20.glGetAttribLocation( program, "textureCoord")); // GLES20.glEnableVertexAttribArray(shader.maTextureHandle); // Draw with indices GLES20.glDrawElements(GLES20.GL_TRIANGLES, _indices.length, GLES20.GL_UNSIGNED_SHORT, _ib); checkGlError("glDrawElements"); /** END DRAWING OBJECT ** */ }
private void drawRoad(int program) { Matrix.setIdentityM(mMMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0); // View Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); // Proj // send to the shader GLES20.glUniformMatrix4fv( GLES20.glGetUniformLocation(program, "uMVPMatrix"), 1, false, mMVPMatrix, 0); // Create the normal modelview matrix // Invert + transpose of mvpmatrix Matrix.invertM(normalMatrix, 0, mMVPMatrix, 0); Matrix.transposeM(normalMatrix, 0, normalMatrix, 0); // send to the shader GLES20.glUniformMatrix4fv( GLES20.glGetUniformLocation(program, "normalMatrix"), 1, false, mMVPMatrix, 0); /** * DRAWING OBJECT * */ // Get buffers from mesh Object3D ob = this._objects[this.ROAD]; Mesh mesh = ob.getMesh(); FloatBuffer _vb = mesh.get_vb(); ShortBuffer _ib = mesh.get_ib(); short[] _indices = mesh.get_indices(); /*float[] myVertices = { -20, 0, -20, -20f, 0, 20, 20, 0, -20, 20, 0, 20 }; _vb = ByteBuffer.allocateDirect(myVertices.length * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer(); _vb.put(myVertices);*/ // Vertex buffer // the vertex coordinates _vb.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); GLES20.glVertexAttribPointer( GLES20.glGetAttribLocation(program, "aPosition"), 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb); GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aPosition")); // the normal info _vb.position(TRIANGLE_VERTICES_DATA_NOR_OFFSET); GLES20.glVertexAttribPointer( GLES20.glGetAttribLocation(program, "aNormal"), 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb); GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aNormal")); // Texture info // bind textures if (ob.hasTexture()) { // && enableTexture) { // number of textures int[] texIDs = ob.get_texID(); for (int i = 0; i < _texIDs.length; i++) { GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); // Log.d("TEXTURE BIND: ", i + " " + texIDs[i]); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIDs[i]); GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "texture" + (i + 1)), i); } } // enable texturing? [fix - sending float is waste] GLES20.glUniform1f( GLES20.glGetUniformLocation(program, "hasTexture") /*shader.hasTextureHandle*/, ob.hasTexture() && enableTexture ? 2.0f : 0.0f); // texture coordinates _vb.position(TRIANGLE_VERTICES_DATA_TEX_OFFSET); GLES20.glVertexAttribPointer( GLES20.glGetAttribLocation(program, "textureCoord") /*shader.maTextureHandle*/, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _vb); GLES20.glEnableVertexAttribArray( GLES20.glGetAttribLocation( program, "textureCoord")); // GLES20.glEnableVertexAttribArray(shader.maTextureHandle); // Draw with indices GLES20.glDrawElements(GLES20.GL_TRIANGLES, _indices.length, GLES20.GL_UNSIGNED_SHORT, _ib); checkGlError("glDrawElements"); /** END DRAWING OBJECT ** */ }
/** @see Object3D#intersection(Object3D) */ public Object3D intersection(Object3D space) { double cutxyX = 0.0, cutxzX = 0.0; if (space instanceof Point3D) { Point3D other = (Point3D) space; if ((this.gradeY * other.x + this.offsetY == other.y) && (this.gradeZ * other.x + this.offsetZ == other.z)) return (Object3D) other.clone(); else return Object3DVoid; } if (space instanceof Line3D) { Line3D test = (Line3D) space; // Exclusion of parallel lines which never will intersect each others. if (test.gradeY == this.gradeY) { if (this.offsetY != test.offsetY) return Object3DVoid; // parallel lines in x-y area (they will never touch in this // dimension) } if (test.gradeZ == this.gradeZ) { if (this.offsetZ != test.offsetZ) return Object3DVoid; // parallel lines in x-z area (they will never touch in this // dimension) } // mathematical search for the intersection point: // has to cut in x-y dimension: cutxyX = (this.offsetY - test.offsetY) / (test.gradeY - this.gradeY); cutxzX = (this.offsetZ - test.offsetZ) / (test.gradeZ - this.gradeZ); if (cutxyX == 0.0 / 0.0) { // Parallel lines are caught above. // In xy-dimension the lines are matching. (see calculation of cutxyX) // Now there are two possibilities: // - The lines are also matching in dimension xz (cutxzX is NaN too): If the x-ranges of // both lines are overlapping "intersection" is true. // - The lines have to cross: Check wether cutxzX is in x-range of both lines. if (cutxzX == 0.0 / 0.0) { if (!((this.end.x < test.start.x) // start.x is smaller end.x! (constructor) || (this.start.x > test.end.x))) { Point3D startx = (this.start.x > test.start.x) ? this.start : test.start; Point3D endx = (this.end.x < test.end.x) ? this.end : test.end; return new Line3D(startx, endx); } else return Object3DVoid; } else { // on zx-area it's not matching, so we have to check wether the intersection of both // lines of the zx-area is within the x-bounds of both lines. if ((cutxzX >= this.start.x) && (cutxzX <= this.end.x) && (cutxzX >= test.start.x) && (cutxzX <= test.end.x)) return new Point3D( cutxzX, this.gradeY * cutxzX + this.offsetY, this.gradeZ * cutxzX + this.offsetZ); else return Object3DVoid; } } // The lines have an intersection in xy-dimension else { // this is the other way round: // either in xz - dimension the lines are on the same way: if (cutxzX == 0.0 / 0.0) { // in zx - area the lines are matching in grade and offset. // Then we will only have to check, wether the crossing of the lines in // xy-dimension is within the x-bounds of both lines. if ((cutxyX >= this.start.x) && (cutxyX <= this.end.x) && (cutxyX >= test.start.x) && (cutxyX <= test.end.x)) return new Point3D( cutxyX, cutxyX * this.gradeY + this.offsetY, cutxyX * this.gradeZ + this.offsetZ); else return Object3DVoid; } // The lines are crossing in xy and xz dimension: // only if both x values cutxyX and cutxzX are equal and within the // x-ranges of both lines this is an intersection: else { if ((cutxyX == cutxzX) && ((cutxyX >= this.start.x) && (cutxyX <= this.end.x)) && ((cutxyX >= test.start.x) && (cutxyX <= test.end.x))) { return new Point3D( cutxyX, cutxyX * this.gradeY + this.offsetY, cutxyX * this.gradeZ + this.offsetZ); } else return Object3DVoid; } } } return space.intersection(this); }
// scale the selected objects public void scaleObject(float factor) { Object3D obj = solids.get(selectedObjIndex); obj.scale(factor); }