@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER); GLES20.glShaderSource(vertexShaderHandle, loadShader(R.raw.vertex_shader)); GLES20.glCompileShader(vertexShaderHandle); int fragmentShaderHandle = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER); GLES20.glShaderSource(fragmentShaderHandle, loadShader(R.raw.fragment_shader)); GLES20.glCompileShader(fragmentShaderHandle); int programHandle = GLES20.glCreateProgram(); GLES20.glAttachShader(programHandle, vertexShaderHandle); GLES20.glAttachShader(programHandle, fragmentShaderHandle); GLES20.glLinkProgram(programHandle); GLES20.glUseProgram(programHandle); Object3D.init(programHandle); mLightPosHandle = GLES20.glGetUniformLocation(programHandle, "uLightPos"); GLES20.glEnable(GLES20.GL_DEPTH_TEST); GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glCullFace(GLES20.GL_BACK); // GLES20.glEnable(GLES20.GL_BLEND); // GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); mPrism.init(); mCylinder.init(); mSphere.init(); }
/** * Applies the Rajawali default GL state to the driver. Developers who wish to change this default * behavior can override this method. */ public void resetGLState() { GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glCullFace(GLES20.GL_BACK); GLES20.glFrontFace(GLES20.GL_CCW); GLES20.glDisable(GLES20.GL_BLEND); GLES20.glEnable(GLES20.GL_DEPTH_TEST); }
@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { // Set rendering options GLES20.glEnable(GLES20.GL_BLEND); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); GLES20.glDisable(GLES20.GL_DITHER); // Face culling GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glFrontFace(GL10.GL_CCW); GLES20.glCullFace(GLES20.GL_BACK); // Depth GLES20.glEnable(GLES20.GL_DEPTH_TEST); GLES20.glDepthFunc(GLES20.GL_LEQUAL); GLES20.glDepthMask(true); }
@Override public void glCullFace(int mode) { GLES20.glCullFace(mode); }
/* (non-Javadoc) * @see fr.kesk.gl.shader.GlShader#render(fr.kesk.gl.GlAssets.Node) */ @Override public void render(Node nodeInstance) { // android.util.Log.d(TAG,"render("+nodeInstance.id+")"); if (this.lightType == Light.DIRECTIONAL) { this.lightModel[4] = this.lightModel[5] = this.lightModel[6] = 0f; this.lightModel[7] = 1f; MatrixUtils.multiplyMV(this.lightModel, 4, nodeInstance.model, 0, this.lightModel, 4); this.lightModel[0] = this.lightModel[4] - this.lightModel[8]; this.lightModel[1] = this.lightModel[5] - this.lightModel[9]; this.lightModel[2] = this.lightModel[6] - this.lightModel[10]; this.lightModel[3] = 1f; Matrix.setLookAtM( this.lightMvpCache, 0, this.lightModel[0], this.lightModel[1], this.lightModel[2], this.lightModel[4], this.lightModel[5], this.lightModel[6], 0f, 1f, 0f); MatrixUtils.multiplyMM(this.lightMvpCache, 32, this.lightMvpCache, 16, this.lightMvpCache, 0); MatrixUtils.multiplyMM( this.currentShadowMap.shadowMatrix, 0, GlFastShadowMapShader.BIAS_MATRIX, 0, this.lightMvpCache, 32); } else if (this.lightType == Light.POINT) { this.lightModel[4] = this.lightModel[5] = this.lightModel[6] = 0f; this.lightModel[7] = 1f; MatrixUtils.multiplyMV(this.lightModel, 4, nodeInstance.model, 0, this.lightModel, 4); Matrix.setLookAtM( this.lightMvpCache, 0, this.lightModel[0], this.lightModel[1], this.lightModel[2], this.lightModel[4], this.lightModel[5], this.lightModel[6], 0f, 1f, 0f); MatrixUtils.multiplyMM(this.lightMvpCache, 32, this.lightMvpCache, 16, this.lightMvpCache, 0); MatrixUtils.multiplyMM( this.currentShadowMap.shadowMatrix, 0, GlFastShadowMapShader.BIAS_MATRIX, 0, this.lightMvpCache, 32); } // Render the scene in the FBO this.fbo.bind(); this.program.start(); this.cullfaceMode = GlContext.glGetState(GLES20.GL_CULL_FACE_MODE); this.viewPort = GlContext.glGetState(GLES20.GL_VIEWPORT); GLES20.glCullFace(GLES20.GL_FRONT); GLES20.glViewport(0, 0, this.quality, this.quality); GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); GLES20.glEnableVertexAttribArray(this.a_PositionVec4Handle); this.drawNode(nodeInstance); GLES20.glDisableVertexAttribArray(this.a_PositionVec4Handle); this.fbo.unbind(); GLES20.glCullFace((int) this.cullfaceMode[0]); GLES20.glViewport( (int) this.viewPort[0], (int) this.viewPort[1], (int) this.viewPort[2], (int) this.viewPort[3]); }
// The render function. private void renderFrame() { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); State state = mRenderer.begin(); mRenderer.drawVideoBackground(); GLES20.glEnable(GLES20.GL_DEPTH_TEST); // handle face culling, we need to detect if we are using reflection // to determine the direction of the culling GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glCullFace(GLES20.GL_BACK); if (Renderer.getInstance().getVideoBackgroundConfig().getReflection() == VIDEO_BACKGROUND_REFLECTION.VIDEO_BACKGROUND_REFLECTION_ON) GLES20.glFrontFace(GLES20.GL_CW); // Front camera else GLES20.glFrontFace(GLES20.GL_CCW); // Back camera // did we find any trackables this frame? for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) { TrackableResult result = state.getTrackableResult(tIdx); Trackable trackable = result.getTrackable(); printUserData(trackable); Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose()); float[] modelViewMatrix = modelViewMatrix_Vuforia.getData(); int textureIndex = trackable.getName().equalsIgnoreCase("cat") ? 0 : 1; textureIndex = trackable.getName().equals("city") ? 1 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("tree") ? 2 : textureIndex; // Part B Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_WEEK); if (day != Calendar.SATURDAY && day != Calendar.SUNDAY) { textureIndex = trackable.getName().equalsIgnoreCase("b11_013") ? ImageTargets.orderImages.indexOf("b11_013") + 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("b11_014") ? ImageTargets.orderImages.indexOf("b11_014") + 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("b11_015") ? ImageTargets.orderImages.indexOf("b11_015") + 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("berkaer_003") ? ImageTargets.orderImages.indexOf("berkaer_003") + 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("p_013") ? ImageTargets.orderImages.indexOf("p_013") + 3 : textureIndex; } else { textureIndex = trackable.getName().equalsIgnoreCase("b11_013") ? 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("b11_014") ? 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("b11_015") ? 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("berkaer_003") ? 3 : textureIndex; textureIndex = trackable.getName().equalsIgnoreCase("p_013") ? 3 : textureIndex; } // deal with the modelview and projection matrices float[] modelViewProjection = new float[16]; boolean partA = trackable.getName().equalsIgnoreCase("cat") || trackable.getName().equalsIgnoreCase("city") || trackable.getName().equalsIgnoreCase("tree"); if (!mActivity.isExtendedTrackingActive()) { if (partA) { Matrix.translateM(modelViewMatrix, 0, 0.0f, 0.0f, OBJECT_SCALE_FLOAT); Matrix.scaleM( modelViewMatrix, 0, OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT); } else { Matrix.translateM( modelViewMatrix, 0, OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT); Matrix.scaleM( modelViewMatrix, 0, OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT); } } else { Matrix.rotateM(modelViewMatrix, 0, 90.0f, 1.0f, 0, 0); Matrix.scaleM(modelViewMatrix, 0, kBuildingScale, kBuildingScale, kBuildingScale); } Matrix.multiplyMM( modelViewProjection, 0, vuforiaAppSession.getProjectionMatrix().getData(), 0, modelViewMatrix, 0); // activate the shader program and bind the vertex/normal/tex coords GLES20.glUseProgram(shaderProgramID); if (!mActivity.isExtendedTrackingActive()) { if (partA) { GLES20.glVertexAttribPointer( vertexHandle, 3, GLES20.GL_FLOAT, false, 0, mTeapot.getVertices()); GLES20.glVertexAttribPointer( normalHandle, 3, GLES20.GL_FLOAT, false, 0, mTeapot.getNormals()); GLES20.glVertexAttribPointer( textureCoordHandle, 2, GLES20.GL_FLOAT, false, 0, mTeapot.getTexCoords()); } else { Log.d("Object", "Name:" + trackable.getName()); // To replace the Teapot for a plane Image GLES20.glVertexAttribPointer( vertexHandle, 3, GLES20.GL_FLOAT, false, 0, mTextPlane.getVertices()); GLES20.glVertexAttribPointer( normalHandle, 3, GLES20.GL_FLOAT, false, 0, mTextPlane.getNormals()); GLES20.glVertexAttribPointer( textureCoordHandle, 2, GLES20.GL_FLOAT, false, 0, mTextPlane.getTexCoords()); } GLES20.glEnableVertexAttribArray(vertexHandle); GLES20.glEnableVertexAttribArray(normalHandle); GLES20.glEnableVertexAttribArray(textureCoordHandle); // activate texture 0, bind it, and pass to shader GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures.get(textureIndex).mTextureID[0]); GLES20.glUniform1i(texSampler2DHandle, 0); // pass the model view matrix to the shader GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, modelViewProjection, 0); if (partA) { // Finally draw the teapot GLES20.glDrawElements( GLES20.GL_TRIANGLES, mTeapot.getNumObjectIndex(), GLES20.GL_UNSIGNED_SHORT, mTeapot.getIndices()); } else { // Draw the plane text GLES20.glDrawElements( GLES20.GL_TRIANGLES, mTextPlane.getNumObjectIndex(), GLES20.GL_UNSIGNED_SHORT, mTextPlane.getIndices()); } // disable the enabled arrays GLES20.glDisableVertexAttribArray(vertexHandle); GLES20.glDisableVertexAttribArray(normalHandle); GLES20.glDisableVertexAttribArray(textureCoordHandle); } else { GLES20.glDisable(GLES20.GL_CULL_FACE); GLES20.glVertexAttribPointer( vertexHandle, 3, GLES20.GL_FLOAT, false, 0, mBuildingsModel.getVertices()); GLES20.glVertexAttribPointer( normalHandle, 3, GLES20.GL_FLOAT, false, 0, mBuildingsModel.getNormals()); GLES20.glVertexAttribPointer( textureCoordHandle, 2, GLES20.GL_FLOAT, false, 0, mBuildingsModel.getTexCoords()); GLES20.glEnableVertexAttribArray(vertexHandle); GLES20.glEnableVertexAttribArray(normalHandle); GLES20.glEnableVertexAttribArray(textureCoordHandle); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures.get(3).mTextureID[0]); GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, modelViewProjection, 0); GLES20.glUniform1i(texSampler2DHandle, 0); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, mBuildingsModel.getNumObjectVertex()); SampleUtils.checkGLError("Renderer DrawBuildings"); } SampleUtils.checkGLError("Render Frame"); } GLES20.glDisable(GLES20.GL_DEPTH_TEST); mRenderer.end(); }
/** Initialization function */ public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { // initialize shaders try { _shaders[GOURAUD_SHADER] = new Shader( vShaders[GOURAUD_SHADER], fShaders[GOURAUD_SHADER], mContext, false, 0); // gouraud _shaders[PHONG_SHADER] = new Shader(vShaders[PHONG_SHADER], fShaders[PHONG_SHADER], mContext, false, 0); // phong _shaders[NORMALMAP_SHADER] = new Shader( vShaders[NORMALMAP_SHADER], fShaders[NORMALMAP_SHADER], mContext, false, 0); // normal map } catch (Exception e) { Log.d("SHADER 0 SETUP", e.getLocalizedMessage()); } GLES20.glEnable(GLES20.GL_DEPTH_TEST); GLES20.glClearDepthf(1.0f); GLES20.glDepthFunc(GLES20.GL_LEQUAL); GLES20.glDepthMask(true); // cull backface GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glCullFace(GLES20.GL_BACK); // light variables float[] lightP = {30.0f, 0.0f, 10.0f, 1}; this.lightPos = lightP; float[] lightC = {0.5f, 0.5f, 0.5f}; this.lightColor = lightC; // material properties float[] mA = {0.45f, 0.45f, 0.45f, 1.0f}; matAmbient = mA; float[] mD = {0.6f, 0.6f, 0.6f, 1.0f}; matDiffuse = mD; float[] mS = {0.9f, 0.9f, 0.9f, 1.0f}; matSpecular = mS; matShininess = 5.0f; // setup textures for all objects for (int i = 0; i < _objects.length; i++) setupTextures(_objects[i]); // set the view matrix Matrix.setLookAtM( mVMatrix, 0, eyePos[0], eyePos[1], eyePos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); /*Matrix.setLookAtM( rm, rmOffset, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)*/ }
@Override public void cullFace(final int c) { checkOpenGLThread(); GLES20.glCullFace(c); }