@Override public void onDrawEye(Eye eye) { if (resetCameraFlag) { float[] invertedEye = new float[16]; Matrix.invertM(invertedEye, 0, eye.getEyeView(), 0); setUpCamera(); Matrix.multiplyMM(mCameraMatrix, 0, invertedEye, 0, mCameraMatrix, 0); resetCameraFlag = false; } GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); if (lockCameraFlag) { mViewMatrix = mCameraMatrix.clone(); } else { Matrix.multiplyMM(mViewMatrix, 0, eye.getEyeView(), 0, mCameraMatrix, 0); } Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0); Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0); mProjectionMatrix = eye.getPerspective(Z_NEAR, Z_FAR); GLES20.glUseProgram(mProgramHandle); currentContent.draw(eye); if (drawRedPoint) { drawTargetingPoint(); } Util.checkGLError("Error Draw Eye"); }
/** * Rendering of a single frame. Here we update and render the detected trackable list. * * @param gl Unused context. */ @Override public void onDrawFrame(GL10 gl) { if (MainInterface.DEBUG_FRAME_LOGGING) log.pushTimer(this, "opengl frame"); // Clear: GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); // If new markerlist, get: if (mainInterface.getListUpdateStatus()) { this.toRender = mainInterface.getList(); if (toRender == null) { log.log(TAG, "Error getting list!"); toRender = new ArrayList<Trackable>(); } else log.debug(TAG, "Updated list – found " + this.toRender.size() + " " + "trackables."); } // ------------------------ RENDER ------------------------ if (!toRender.isEmpty()) { for (Trackable trackable : toRender) { // Reset model matrix to identity Matrix.setIdentityM(mModelMatrix, 0); Matrix.multiplyMM(mModelMatrix, 0, trackable.getTRANSLATION(), 0, mModelMatrix, 0); drawObject(trackable.getFloatbuffer()); } } if (MainInterface.DEBUG_FRAME_LOGGING) { log.debug(TAG, "OpenGL rendered frame in " + log.popTimer(this).time + "ms."); } }
// public int compareTo(Object objB) // { // glMultiLines b = (glMultiLines) objB; // if (fit > b.fit) return 1; // else if (fit < b.fit) return -1; // else return 0; // } public void update(glVector tr, glVector sc) { Matrix.setIdentityM(t, 0); Matrix.setIdentityM(s, 0); glBase.translate(t, tr.x, tr.y, tr.z); Matrix.scaleM(s, 0, sc.x, sc.y, sc.z); }
public void setSize(int width, int height) { Utils.assertTrue(width >= 0 && height >= 0); if (mTargetTexture == null) { mScreenWidth = width; mScreenHeight = height; } mAlpha = 1.0f; GL11 gl = mGL; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL11.GL_PROJECTION); gl.glLoadIdentity(); GLU.gluOrtho2D(gl, 0, width, 0, height); gl.glMatrixMode(GL11.GL_MODELVIEW); gl.glLoadIdentity(); float matrix[] = mMatrixValues; Matrix.setIdentityM(matrix, 0); // to match the graphic coordinate system in android, we flip it vertically. if (mTargetTexture == null) { Matrix.translateM(matrix, 0, 0, height, 0); Matrix.scaleM(matrix, 0, 1, -1, 1); } }
@Override public void draw(SchminceRenderer renderer, SBlock block) { float[] vpMatrix = renderer.getVPMatrix(); Matrix.translateM(vpMatrix, 0, block.X, block.Y, 0); Matrix.scaleM(vpMatrix, 0, 0.5f, 0.5f, 1f); renderer.getGlib().getDrawer(itemType).draw(vpMatrix); }
public void onDrawFrame(GL10 gl) { // Redraw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); Log.i("MyGLRenderer", "MyGLRenderer : onDrawFrame() - Starting to set camera position"); float[] mViewMatrix = new float[16]; // Set the camera position (View matrix) Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); float[] mMVPMatrix = new float[16]; // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); Log.i("MyGLRenderer", "MyGLRenderer : onDrawFrame() - Drawing ClientSide"); float[] scratch = new float[16]; // Create a rotation transformation for the triangle long time = SystemClock.uptimeMillis() % 4000L; float angle = 0.090f * ((int) time); Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f); // Combine the rotation matrix with the projection and camera view // Note that the mMVPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0); // Draw triangle mTriangle.draw_ClientSide(scratch); }
/** * Method for painting any given object. Data must be 7 floats per face – we render one triangle * consisting of 3 vertices with each vertice having an rgba color float value. * * @param data The vertice data containing coordinates and colors to draw. */ private void drawObject(final FloatBuffer data) { // Pass in the position information data.position(mPositionOffset); GLES20.glVertexAttribPointer( mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, mStrideBytes, data); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information data.position(mColorOffset); GLES20.glVertexAttribPointer( mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, data); GLES20.glEnableVertexAttribArray(mColorHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the // MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); // The (1+data.capacity() / 8) tells us how many vertices we need to // draw GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, (1 + data.capacity() / 8)); }
public void drawAll(Camera cam) { float[] rotationMatrix = new float[16]; boolean typeSwitchDirty = false; OrderUnit[] renderOrder = buildCompleteDrawOrder(cam.getPos()); Matrix.setIdentityM(rotationMatrix, 0); Matrix.rotateM(rotationMatrix, 0, -angle, 0.0f, 1.0f, 0.0f); for (int i = 0; i < renderOrder.length; i++) { if (renderOrder[i].getType() == OrderUnit.ORDERUNITTYPE.Node) { if (!typeSwitchDirty) { mConnectionsBatch.endBatch(); mDottedBatch.endBatch(); } drawOneNode(renderOrder[i].getId(), rotationMatrix, cam); typeSwitchDirty = true; } else { if (typeSwitchDirty) { mConnectionsBatch.beginBatch(cam, modelMatrix); mDottedBatch.beginBatch(cam, modelMatrix); typeSwitchDirty = false; } Line3D currentLine = connections[renderOrder[i].getId()].getLine(); if (currentLine.isDotted()) mDottedBatch.batchElement(currentLine); else mConnectionsBatch.batchElement(currentLine); } } }
public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); ratio = (float) width / height; // this projection matrix is applied to object coodinates // in the onDrawFrame() method Matrix.orthoM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); Matrix.setLookAtM(mVMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); engine.initGL(); GLES20.glUseProgram(mProgram); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glUniform1i(mSampleHandle, 0); GLES20.glUniform1f(mOpacityHandle, 1f); GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, quadCoordsBuf); GLES20.glEnableVertexAttribArray(maPositionHandle); GLES20.glVertexAttribPointer(maTexCoordsHandle, 2, GLES20.GL_FLOAT, false, 8, quadTexCoordsBuf); GLES20.glEnableVertexAttribArray(maTexCoordsHandle); initTextures(); }
@Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub // Draw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // Draw triangle mTriangle.draw(); // 设置相机的位置(视口矩阵) // Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // 计算投影和视口变换 // Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); // 绘制形状 // mTriangle.draw(mMVPMatrix); // Create a rotation for the triangle // long time = SystemClock.uptimeMillis() % 4000L; // float angle = 0.090f * ((int) time); Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f); // 把旋转矩阵合并到投影和相机矩阵 Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); // 画三角形 mTriangle.draw(mMVPMatrix); }
@Override public void onSurfaceChanged(GL10 glUnused, int width, int height) { GLES20.glViewport(0, 0, width, height); ratio = (float) width / height; Matrix.perspectiveM( mProjectionMatrix, 0, FOV / (float) Math.PI * 180.0f, ratio, len / LEN_RANGE, len * LEN_RANGE); Matrix.setLookAtM( mViewMatrix, 0, matCam[0] * len, matCam[1] * len, matCam[2] * len, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); float[] lightPos = {len, len, 0}; GLES20.glUniform3fv(mLightPosHandle, 1, lightPos, 0); }
public void drawSphere(final FloatBuffer aSphereBuffer) { aSphereBuffer.position(mPositionOffset); GLES20.glVertexAttribPointer( mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, aSphereBuffer); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information // aTriangleBuffer.position(mColorOffset); // GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, // mStrideBytes, aTriangleBuffer); // // GLES20.glEnableVertexAttribArray(mColorHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the // MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, sphereTriangles * 3); }
@Override public void onDrawFrame(GL10 unused) { GLES20.glClearColor(0f, 0f, 0f, 0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); if (mShaderCompilerSupport[0] == false) { return; } GLES20.glDisable(GLES20.GL_DEPTH_TEST); GLES20.glEnable(GLES20.GL_BLEND); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); float angle = (SystemClock.uptimeMillis() % 5000) / 5000f * 360; Matrix.setRotateM(mMatrixModel, 0, angle, 1, 2, 0); Matrix.multiplyMM(mMatrixModelViewProjection, 0, mMatrixView, 0, mMatrixModel, 0); Matrix.multiplyMM( mMatrixModelViewProjection, 0, mMatrixProjection, 0, mMatrixModelViewProjection, 0); mShaderSpline.useProgram(); GLES20.glUniformMatrix4fv( mShaderSpline.getHandle("uModelViewProjectionM"), 1, false, mMatrixModelViewProjection, 0); GLES20.glVertexAttribPointer( mShaderSpline.getHandle("aPosition"), 2, GLES20.GL_FLOAT, false, 0, mBufferSpline); GLES20.glEnableVertexAttribArray(mShaderSpline.getHandle("aPosition")); for (float[] ctrl : mSplines) { GLES20.glUniform3fv(mShaderSpline.getHandle("uCtrl"), 4, ctrl, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, COUNT_VERTICES); } }
@Override public void onDrawFrame(GL10 glUnused) { GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); // Do a complete rotation every 10 seconds. long time = SystemClock.uptimeMillis() % 10000L; float angleInDegrees = (360.0f / 10000.0f) * ((int) time); // Matrix.setIdentityM(mModelMatrix, 0); // Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, -50.0f); // Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f); // drawSphere(mSpherePositions); DrawFrame(glUnused); // tdmodel.draw(glUnused); // Draw the triangle facing straight on. Matrix.setIdentityM(mModelMatrix, 0); Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f); drawTriangle(mTriangle1Vertices); // Draw one translated a bit down and rotated to be flat on the ground. Matrix.setIdentityM(mModelMatrix, 0); Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, 0.0f); Matrix.rotateM(mModelMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f); Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f); drawTriangle(mTriangle2Vertices); // Draw one translated a bit to the right and rotated to be facing to the left. Matrix.setIdentityM(mModelMatrix, 0); Matrix.translateM(mModelMatrix, 0, 1.0f, 0.0f, 0.0f); Matrix.rotateM(mModelMatrix, 0, 90.0f, 0.0f, 1.0f, 0.0f); Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f); drawTriangle(mTriangle3Vertices); }
public void rotate(float angle, float x, float y, float z) { if (angle == 0) return; float[] temp = mTempMatrix; Matrix.setRotateM(temp, 0, angle, x, y, z); Matrix.multiplyMM(temp, 16, mMatrixValues, 0, temp, 0); System.arraycopy(temp, 16, mMatrixValues, 0, 16); }
public Vector3 getDirection() { Matrix.setIdentityM(matrix, 0); Matrix.rotateM(matrix, 0, yaw, 0, 1, 0); Matrix.rotateM(matrix, 0, pitch, 1, 0, 0); Matrix.multiplyMV(outVex, 0, matrix, 0, inVex, 0); direction.set(outVex[0], outVex[1], outVex[2]); return direction; }
@Override public void onSurfaceChanged(GL10 unused, int width, int height) { float aspect = (float) width / height; Matrix.perspectiveM(mMatrixProjection, 0, 60f, aspect, .1f, 10f); Matrix.setLookAtM(mMatrixView, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0); Matrix.multiplyMM(mMatrixModelViewProjection, 0, mMatrixProjection, 0, mMatrixView, 0); mLastRenderTime = -1; }
@Override public void draw( float[] mMVPMatrix, float[] mRotationMatrix, float[] mvMatrix, float[] mProjMatrix) { if (visible && canvasSurfaceCreated()) { if (!initialized) { if (!initObject()) { return; } } // Add program to OpenGL environment GLES20.glUseProgram(programHandle); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureDataHandle); GLES20.glUniform1i(textureUniformHandle, 0); // Enable a handle to the triangle vertices // Prepare the triangle coordinate data vBuffer.position(0); GLES20.glVertexAttribPointer( positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vBuffer); GLES20.glEnableVertexAttribArray(positionHandle); // Pass in the texture coordinate information textureBuffer.position(0); GLES20.glVertexAttribPointer( textureCoordinateHandle, COORDS_PER_TEXTURE, GLES20.GL_FLOAT, false, (COORDS_PER_TEXTURE * 4), textureBuffer); GLES20.glEnableVertexAttribArray(textureCoordinateHandle); Matrix.multiplyMM(mMVPMatrix, 0, mvMatrix, 0, mModelMatrix, 0); // Apply the projection and view transformation GLES20.glUniformMatrix4fv(mModelMatrixHandle, 1, false, mMVPMatrix, 0); GLES20Renderer.checkGLError("glUniformMatrix4fv"); Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); GLES20Renderer.checkGLError("glUniformMatrix4fv"); // Draw the square GLES20.glDrawElements( GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawBuffer); // Disable vertex array // GLES20.glDisableVertexAttribArray(positionHandle); } }
/** Draws a cube. */ private void drawCube() { // Pass in the position information mCubePositions.position(0); GLES20.glVertexAttribPointer( mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, mCubePositions); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information mCubeColors.position(0); GLES20.glVertexAttribPointer( mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, mCubeColors); GLES20.glEnableVertexAttribArray(mColorHandle); // Pass in the normal information mCubeNormals.position(0); GLES20.glVertexAttribPointer( mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false, 0, mCubeNormals); GLES20.glEnableVertexAttribArray(mNormalHandle); // Pass in the texture coordinate information mCubeTextureCoordinates.position(0); GLES20.glVertexAttribPointer( mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, mCubeTextureCoordinates); GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // Pass in the modelview matrix. GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the // MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); // Pass in the combined matrix. GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); // Pass in the light position in eye space. GLES20.glUniform3f( mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]); // Draw the cube. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36); }
public void draw(float[] vpMatrix, GLCubeShader shader) { float[] mvpMatrix = new float[16]; float[] moduleMatrix = new float[16]; Matrix.setIdentityM(moduleMatrix, 0); Matrix.translateM(moduleMatrix, 0, mCenter[0], mCenter[1], mCenter[2]); Matrix.multiplyMM(mvpMatrix, 0, vpMatrix, 0, moduleMatrix, 0); shader.draw(mvpMatrix, mColor); }
protected void applyTransforms() { Matrix.setIdentityM(mWorldMatrix, 0); Matrix.translateM(mWorldMatrix, 0, mTransDir.getX(), mTransDir.getY(), mTransDir.getZ()); if (mRotAxes != null) { Matrix.rotateM(mWorldMatrix, 0, mRotAngle, mRotAxes.getX(), mRotAxes.getY(), mRotAxes.getZ()); } Matrix.scaleM(mWorldMatrix, 0, mScaleFac.getX(), mScaleFac.getY(), mScaleFac.getZ()); mDirty = false; }
public void drawNodesBatched(Camera cam) { int cur; TextureRegion texRgn = new TextureRegion(); float[] color; float[] spriteMatrix = new float[16]; float[] rotationMatrix = new float[16]; float[] convMatrix = new float[16]; Matrix.multiplyMM( convMatrix, 0, cam.getViewM(), 0, modelMatrix, 0); // multiply view matrix by model to calc distances from cam NodeOrderUnit[] renderOrder = this.buildDrawOrder(convMatrix); Matrix.setIdentityM(rotationMatrix, 0); Matrix.rotateM(rotationMatrix, 0, -angle, 0.0f, 1.0f, 0.0f); mNodeBatch.beginBatch(cam, rotationMatrix); // batch.beginBatch(cam); for (int i = 0; i < renderOrder.length; i++) { cur = renderOrder[i].getId(); /*if (nodes[cur].isSelected()) color = new float[] {0.1f, 0.1f, 0.7f, 1.0f}; else color = new float[] {1.0f, 1.0f, 1.0f, 1.0f};*/ switch (nodes[cur].getState()) { case CORRECT: color = new float[] {0.0f, 0.8f, 0.0f, 1.0f}; break; case WRONG: color = new float[] {0.8f, 0.0f, 0.0f, 1.0f}; break; case OPEN: color = new float[] {1.0f, 1.0f, 1.0f, 1.0f}; break; default: // when IDLE color = new float[] {0.7f, 0.7f, 0.7f, 1.0f}; break; } Matrix.setIdentityM(spriteMatrix, 0); Matrix.translateM( spriteMatrix, 0, modelMatrix, 0, nodes[cur].posX, nodes[cur].posY, nodes[cur].posZ); float diam = nodes[cur].getRadius() * 2; mNodeBatch.batchElement(diam, diam, color, texRgn, spriteMatrix); } mNodeBatch.endBatch(); }
// XXX Find out whether or not using Matrix operations is better than a home-grown solution. // Perhaps run a test to see how long matrix ops really take. // TODO: Try to multiply multiple points in one M x V operation in Primitive. // This function belongs in Element, not Point. // XXX This rotates about the origin. public Float3 rotate(float a, float x, float y, float z) { float[] v = new float[] {this.x, this.y, this.z, 1}; float[] m = new float[16]; Matrix.setIdentityM(m, 0); Matrix.rotateM(m, 0, a, x, y, z); Matrix.multiplyMV(v, 0, m, 0, v, 0); return new Float3(v[0], v[1], v[2]); }
@Override public void onDrawFrame(GL10 gl) { super.onDrawFrame(gl); // GL Draw code onwards GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); // Do a complete rotation every 10 seconds. long time = SystemClock.uptimeMillis() % 10000L; float angleInDegrees = (360.0f / 10000.0f) * ((int) time); // Set our per-vertex lighting program. GLES20.glUseProgram(mProgramHandle); // Set program handles for cube drawing. mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix"); mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix"); mLightPosHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_LightPos"); mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture"); mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position"); mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color"); mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal"); mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate"); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, getGLSurfaceTexture()); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glUniform1i(mTextureUniformHandle, 0); // GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); // GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, glSurfaceTex); // GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // // Set the active texture unit to texture unit 0. // GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // // // Bind the texture to this unit. // GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, glSurfaceTex); // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit // 0. // GLES20.glUniform1i(mTextureUniformHandle, 0); // Calculate position of the light. Rotate and then push into the distance. Matrix.setIdentityM(mLightModelMatrix, 0); Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -4.0f); Matrix.rotateM(mLightModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f); Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 2.0f); Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0); Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0); Matrix.setIdentityM(mModelMatrix, 0); Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -3.3f); Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 1.0f, 0.0f); drawCube(); // Draw a point to indicate the light. GLES20.glUseProgram(mPointProgramHandle); drawLight(); }
@Override public void onDrawEye(Eye eyeTransform) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); checkGLError("colorParam"); GLES20.glUseProgram(mProgram); GLES20.glDrawElements( GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); checkGLError("befor activeTex"); GLES20.glActiveTexture(GLES20.GL_TEXTURE1); checkGLError("after activeTex"); GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture); checkGLError("bind Text"); checkGLError("mPostionHandle"); mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position"); GLES20.glEnableVertexAttribArray(mPositionHandle); GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); checkGLError("mTexture Handle"); mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate"); GLES20.glEnableVertexAttribArray(mTextureCoordHandle); GLES20.glVertexAttribPointer( mTextureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, textureVerticesBuffer); checkGLError("after mTexture Handle"); mColorHandle = GLES20.glGetAttribLocation(mProgram, "s_texture"); checkGLError("mColor Handel"); GLES20.glDrawElements( GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); checkGLError("Disable"); GLES20.glDisableVertexAttribArray(mPositionHandle); GLES20.glDisableVertexAttribArray(mTextureCoordHandle); checkGLError("before Cube 2"); Matrix.multiplyMM(mView, 0, eyeTransform.getEyeView(), 0, mCamera, 0); Matrix.multiplyMV(lightPosInEyeSpace, 0, mView, 0, LIGHT_POS_IN_WORLD_SPACE, 0); checkGLError("before Cube"); float[] perspective = eyeTransform.getPerspective(Z_NEAR, Z_FAR); Matrix.multiplyMM(modelView, 0, mView, 0, mModelCube, 0); Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0); drawCube(); }
/** * Set the absolute position of this camera * * @param x x coordinate * @param y y coordinate * @param z z coordinate */ public void setPosition(float x, float y, float z) { synchronized (mViewMatrix) { // revert last position Matrix.translateM(mViewMatrix, 0, mPosition[0], mPosition[1], mPosition[2]); // set new position mPosition[0] = x; mPosition[1] = y; mPosition[2] = z; Matrix.translateM(mViewMatrix, 0, -mPosition[0], -mPosition[1], -mPosition[2]); } }
@Override public void onDrawFrame(GL10 glUnused) { GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); Matrix.setIdentityM(mModelMatrix, 0); Matrix.rotateM(mModelMatrix, 0, 90, 1.0f, 0.0f, 0.0f); Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0); mPrism.render(mMVPMatrix, mMVMatrix); mCylinder.render(mMVPMatrix, mMVMatrix); mSphere.render(mMVPMatrix, mMVMatrix); }
@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { // Set the background clear color to sky blue GLES20.glClearColor(135.0f / 255.0f, 206.0f / 255.0f, 255.0f / 255f, 1.0f); GLES20.glEnable(GLES20.GL_DEPTH_TEST); // GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glEnable(GLES20.GL_TEXTURE_2D); // Position the eye behind the origin this.mCamera.eye = new Vector3d(0.0f, 0.0f, 0.0f); // We are looking toward the distance this.mCamera.look = new Vector3d(0f, 0f, 0f); // Set our up vector. This is where our head would be pointing were we holding the camera this.mCamera.up = new Vector3d(0.0f, 0.0f, 1.0f); // Set the view matrix. This matrix can be said to represent the camera position. // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose. Matrix.setLookAtM( this.mViewMatrix, 0, this.mCamera.eye.x, this.mCamera.eye.y, this.mCamera.eye.z, this.mCamera.look.x, this.mCamera.look.y, this.mCamera.look.z, this.mCamera.up.x, this.mCamera.up.y, this.mCamera.up.z); Matrix.setIdentityM(mModelMatrix, 0); final String vertexShader = getVertexShader(); final String fragmentShader = getFragmentShader(); final int vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER, vertexShader); final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader); mPerVertexProgramHandle = createAndLinkProgram( vertexShaderHandle, fragmentShaderHandle, new String[] {"a_Position", "a_Color", "a_Normal", "a_TexCoordinate"}); addTexture(R.drawable.grass_texture_1); addTexture(R.drawable.azure_waters); addTexture(R.drawable.wood_texture_1); addTexture(R.drawable.sky_texture_1); }
protected BaseRenderer(Context context) { this.context = context; /* *** set default ViewProjection Matrix*** */ Matrix.setIdentityM(modelMatrix, 0); Matrix.translateM(modelMatrix, 0, xP, yP, zP); float[] viewMatrix = new float[16]; float[] projectionMatrix = new float[16]; MatrixHelper.perspectiveM(projectionMatrix, 45, 1f, 1f, 10f); Matrix.setLookAtM(viewMatrix, 0, 0, 0, 3.0f, 0.0f, 0.0f, 0.0f, 0, 1.0f, 0); Matrix.multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0); setModelViewProjectionMatrix(); }
public void camZoom(float exp) { len = LEN_DEFAULT * (float) Math.pow(LEN_RANGE, exp); Matrix.perspectiveM( mProjectionMatrix, 0, FOV / (float) Math.PI * 180.0f, ratio, len / LEN_RANGE, len * LEN_RANGE); Matrix.setLookAtM( mViewMatrix, 0, matCam[0] * len, matCam[1] * len, matCam[2] * len, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); float[] lightPos = {len, len, 0}; GLES20.glUniform3fv(mLightPosHandle, 1, lightPos, 0); }